chado_linker__relationship.inc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. <?php
  2. class chado_linker__relationship extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Relationships'),
  9. 'description' => t('Relationships between records.'),
  10. 'default_widget' => 'chado_linker__relationship_widget',
  11. 'default_formatter' => 'chado_linker__relationship_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_chado',
  16. 'active' => TRUE
  17. ),
  18. );
  19. }
  20. /**
  21. * @see TripalField::can_attach()
  22. */
  23. protected function can_attach($entity_type, $bundle, $details) {
  24. $table_name = $details['chado_table'];
  25. $type_table = $details['chado_type_table'];
  26. $type_field = $details['chado_type_column'];
  27. $cv_id = $details['chado_cv_id'];
  28. $cvterm_id = $details['chado_cvterm_id'];
  29. // If the linker table does not exists then we don't want to add attach.
  30. $rel_table = $table_name . '_relationship';
  31. if (chado_table_exists($rel_table)) {
  32. return TRUE;
  33. }
  34. return FALSE;
  35. }
  36. /**
  37. * @see TripalField::create_info()
  38. */
  39. function create_info($entity_type, $bundle, $details) {
  40. if (!$this->can_attach($entity_type, $bundle, $details)) {
  41. return;
  42. }
  43. $table_name = $details['chado_table'];
  44. $type_table = $details['chado_type_table'];
  45. $type_field = $details['chado_type_column'];
  46. $cv_id = $details['chado_cv_id'];
  47. $cvterm_id = $details['chado_cvterm_id'];
  48. $rel_table = $table_name . '_relationship';
  49. $schema = chado_get_schema($rel_table);
  50. $pkey = $schema['primary key'][0];
  51. return array(
  52. 'field_name' => $table_name . '_relationship',
  53. 'type' => 'chado_linker__relationship',
  54. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  55. 'locked' => FALSE,
  56. 'storage' => array(
  57. 'type' => 'field_chado_storage',
  58. ),
  59. 'settings' => array(
  60. 'chado_table' => $rel_table,
  61. 'chado_column' => $pkey,
  62. 'base_table' => $table_name,
  63. 'semantic_web' => 'SBO:0000374',
  64. ),
  65. );
  66. }
  67. /**
  68. * @see TripalField::create_instance_info()
  69. */
  70. function create_instance_info($entity_type, $bundle, $details) {
  71. if (!$this->can_attach($entity_type, $bundle, $details)) {
  72. return;
  73. }
  74. $table_name = $details['chado_table'];
  75. $type_table = $details['chado_type_table'];
  76. $type_field = $details['chado_type_column'];
  77. $cv_id = $details['chado_cv_id'];
  78. $cvterm_id = $details['chado_cvterm_id'];
  79. return array(
  80. 'field_name' => $table_name . '_relationship',
  81. 'entity_type' => $entity_type,
  82. 'bundle' => $bundle->name,
  83. 'label' => 'Relationships',
  84. 'description' => 'Other records with relationships to this record.',
  85. 'required' => FALSE,
  86. 'settings' => array(),
  87. 'widget' => array(
  88. 'type' => 'chado_linker__relationship_widget',
  89. 'settings' => array(
  90. 'display_label' => 1,
  91. ),
  92. ),
  93. 'display' => array(
  94. 'deafult' => array(
  95. 'label' => 'above',
  96. 'type' => 'chado_linker__relationship_formatter',
  97. 'settings' => array(),
  98. ),
  99. ),
  100. );
  101. }
  102. /**
  103. * @see TripalField::widget_info()
  104. */
  105. function widget_info() {
  106. return array(
  107. 'label' => t('Relationship Settings'),
  108. 'field types' => array('chado_linker__relationship')
  109. );
  110. }
  111. /**
  112. * @see TripalField::formatter_info()
  113. */
  114. function formatter_info() {
  115. return array(
  116. 'label' => t('Relationships'),
  117. 'field types' => array('chado_linker__relationship'),
  118. 'settings' => array(
  119. ),
  120. );
  121. }
  122. /**
  123. * @see TripalField::formatter_view()
  124. */
  125. function formatter_view(&$element, $entity_type, $entity,
  126. $field, $instance, $langcode, $items, $display) {
  127. // Get the settings
  128. $settings = $display['settings'];
  129. $rows = array();
  130. $headers = array('Subject' ,'Type', 'Object');
  131. $headers = array('Relationship');
  132. foreach ($items as $delta => $item) {
  133. if (!$item['value']) {
  134. continue;
  135. }
  136. $subject_name = $item['value']['subject']['name'];
  137. $subject_type = $item['value']['subject']['type'];
  138. $object_name = $item['value']['object']['name'];
  139. $object_type = $item['value']['object']['type'];
  140. $phrase = $item['value']['phrase'];
  141. // Handle some special cases.
  142. // For mRNA objects we don't want to show the CDS, exons, 5' UTR, etc.
  143. // we want to show the parent gene and the protein.
  144. if ($object_type == 'mRNA' and ($subject_type != 'polypeptide')) {
  145. continue;
  146. }
  147. if ($subject_type == 'mRNA' and ($object_type != 'gene')) {
  148. continue;
  149. }
  150. $phrase = preg_replace("/$subject_type/", "<b>$subject_type</b>", $phrase);
  151. $phrase = preg_replace("/$object_type/", "<b>$object_type</b>", $phrase);
  152. if (array_key_exists('entity_id', $item['value']['object'])) {
  153. $object_entity_id = $item['value']['object']['entity_id'];
  154. if ($object_entity_id != $entity->id) {
  155. $link = l($object_name, 'bio_data/' . $object_entity_id);
  156. $phrase = preg_replace("/$object_name/", $link, $phrase);
  157. }
  158. }
  159. if (array_key_exists('entity_id', $item['value']['subject'])) {
  160. $subject_entity_id = $item['value']['subject']['entity_id'];
  161. if ($subject_entity_id != $entity->id) {
  162. $link = l($subject_name, 'bio_data/' . $subject_entity_id);
  163. $phrase = preg_replace("/$subject_name/", $link, $phrase);
  164. }
  165. }
  166. $rows[] = array($phrase);
  167. }
  168. // the $table array contains the headers and rows array as well as other
  169. // options for controlling the display of the table. Additional
  170. // documentation can be found here:
  171. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  172. $table = array(
  173. 'header' => $headers,
  174. 'rows' => $rows,
  175. 'attributes' => array(
  176. 'id' => 'chado-linker--relationship-table',
  177. 'class' => 'tripal-data-table'
  178. ),
  179. 'sticky' => FALSE,
  180. 'caption' => '',
  181. 'colgroups' => array(),
  182. 'empty' => 'There are no relationships',
  183. );
  184. // once we have our table array structure defined, we call Drupal's theme_table()
  185. // function to generate the table.
  186. $element[$delta] = array(
  187. '#type' => 'markup',
  188. '#markup' => theme_table($table),
  189. );
  190. }
  191. /**
  192. * @see TripalField::widget_form()
  193. */
  194. function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  195. $langcode, $items, $delta, $element) {
  196. $field_name = $field['field_name'];
  197. $field_type = $field['type'];
  198. $field_table = $field['settings']['chado_table'];
  199. $field_column = $field['settings']['chado_column'];
  200. // Get the FK column that links to the base table.
  201. $chado_table = $field['settings']['chado_table'];
  202. $base_table = $field['settings']['base_table'];
  203. $schema = chado_get_schema($chado_table);
  204. $pkey = $schema['primary key'][0];
  205. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  206. $fkey = $fkeys[0];
  207. // Get the field defaults.
  208. $record_id = '';
  209. $fkey_value = $element['#entity']->chado_record_id;
  210. $subject_id = '';
  211. $subject_uniquename = '';
  212. $type_id = '';
  213. $type = '';
  214. $object_id = '';
  215. $object_uniquename = '';
  216. $value = '';
  217. $rank = '';
  218. // If the field already has a value then it will come through the $items
  219. // array. This happens when editing an existing record.
  220. if (array_key_exists($delta, $items)) {
  221. $record_id = $items[$delta][$field_table . '__' . $pkey];
  222. $subject_id = $items[$delta][$field_table . '__subject_id'];
  223. $type_id = $items[$delta][$field_table . '__type_id'];
  224. $object_id = $items[$delta][$field_table . '__object_id'];
  225. if (array_key_exists('value', $schema['fields'])) {
  226. $value = $items[$delta][$field_table . '__value'];
  227. }
  228. if (array_key_exists('rank', $schema['fields'])) {
  229. $rank = $items[$delta][$field_table . '__rank'];
  230. }
  231. $object_uniquename = $items[$delta]['object_name'];
  232. $subject_uniquename = $items[$delta]['subject_name'];
  233. $type = $items[$delta]['type_name'];
  234. }
  235. // Check $form_state['values'] to see if an AJAX call set the values.
  236. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  237. $record_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__' . $pkey);
  238. $subject_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__subject_id');
  239. $subject_uniquename = tripal_chado_get_field_form_values($field_name, $form_state, $delta, 'subject_name');
  240. $type_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__type_id');
  241. $type = tripal_chado_get_field_form_values($field_name, $form_state, $delta, 'type_name');
  242. $object_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__object_id');
  243. $object_uniquename = tripal_chado_get_field_form_values($field_name, $form_state, $delta, 'object_name');
  244. $value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__value');
  245. $rank = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__rank');
  246. }
  247. $widget['#table_name'] = $chado_table;
  248. $widget['#fkeys'] = $schema['foreign keys'];
  249. $widget['#base_table'] = $base_table;
  250. $widget['#chado_record_id'] = $form['#entity']->chado_record_id;
  251. //$widget['#element_validate'] = array('chado_linker__relationship_validate');
  252. $widget['#theme'] = 'chado_linker__relationship_widget';
  253. $widget['#prefix'] = "<span id='$chado_table-$delta'>";
  254. $widget['#suffix'] = "</span>";
  255. $widget['value'] = array(
  256. '#type' => 'value',
  257. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  258. );
  259. $widget[$field_table . '__' . $pkey] = array(
  260. '#type' => 'value',
  261. '#default_value' => $record_id,
  262. );
  263. $widget[$field_table . '__subject_id'] = array(
  264. '#type' => 'value',
  265. '#default_value' => $subject_id,
  266. );
  267. $widget[$field_table . '__type_id'] = array(
  268. '#type' => 'value',
  269. '#default_value' => $type_id,
  270. );
  271. $widget[$field_table . '__object_id'] = array(
  272. '#type' => 'value',
  273. '#default_value' => $object_id,
  274. );
  275. if (array_key_exists('value', $schema['fields'])) {
  276. $widget[$field_table . '__value'] = array(
  277. '#type' => 'value',
  278. '#default_value' => $value,
  279. );
  280. }
  281. if (array_key_exists('rank', $schema['fields'])) {
  282. $widget[$field_table . '__rank'] = array(
  283. '#type' => 'value',
  284. '#default_value' => $rank,
  285. );
  286. }
  287. $widget['subject_name'] = array(
  288. '#type' => 'textfield',
  289. '#title' => t('Subject'),
  290. '#default_value' => $subject_uniquename,
  291. '#required' => $element['#required'],
  292. '#maxlength' => array_key_exists('length', $schema['fields']['subject_id']) ? $schema['fields']['subject_id']['length'] : 255,
  293. '#size' => 35,
  294. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  295. );
  296. $default_cv = tripal_get_default_cv($field_table, 'type_id');
  297. $options = array();
  298. if (!$default_cv) {
  299. drupal_set_message("There is no default vocabulary set for the relationships....");
  300. }
  301. else {
  302. $options = tripal_get_cvterm_select_options($default_cv->cv_id, TRUE);
  303. }
  304. $widget['type_name'] = array(
  305. '#type' => 'select',
  306. '#title' => t('Type'),
  307. '#options' => $options,
  308. '#required' => $element['#required'],
  309. '#default_value' => $type_id,
  310. );
  311. $widget['object_name'] = array(
  312. '#type' => 'textfield',
  313. '#title' => t('Object'),
  314. '#default_value' => $object_uniquename,
  315. '#required' => $element['#required'],
  316. '#maxlength' => array_key_exists('length', $schema['fields']['object_id']) ? $schema['fields']['object_id']['length'] : 255,
  317. '#size' => 35,
  318. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  319. );
  320. }
  321. /**
  322. * @see TripalField::validate()
  323. */
  324. function validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  325. $field_name = $field['field_name'];
  326. $field_type = $field['type'];
  327. $field_table = $field['settings']['chado_table'];
  328. $field_column = $field['settings']['chado_column'];
  329. $base_table = $field['settings']['base_table'];
  330. $chado_record_id = $entity->chado_record_id;
  331. $schema = chado_get_schema($field_table);
  332. $fkeys = $schema['foreign keys'];
  333. foreach ($items as $delta => $item) {
  334. $subject_id = $item[$field_table . '__subject_id'];
  335. $object_id = $item[ $field_table . '__object_id'];
  336. $type_id = $item[$field_table . '__type_id'];
  337. $type_name = $item['type_name'];
  338. $subject_name = $item['subject_name'];
  339. $object_name = $item['object_name'];
  340. // If the row is empty then just continue, there's nothing to validate.
  341. if (!$type_name and !$subject_name and !$object_name) {
  342. continue;
  343. }
  344. // Make sure we have values for all of the fields.
  345. $form_error = FALSE;
  346. if (!$type_name) {
  347. $errors[$field['field_name']][$langcode][$delta][] = array(
  348. 'error' => 'chado_linker__relationship',
  349. 'message' => t("Please provide the type of relationship."),
  350. );
  351. }
  352. if (!$subject_name) {
  353. $errors[$field['field_name']][$langcode][$delta][] = array(
  354. 'error' => 'chado_linker__relationship',
  355. 'message' => t("Please provide the subject of the relationship."),
  356. );
  357. }
  358. if (!$object_name) {
  359. $errors[$field['field_name']][$langcode][$delta][] = array(
  360. 'error' => 'chado_linker__relationship',
  361. 'message' => t("Please provide the object of the relationship."),
  362. );
  363. }
  364. if ($form_error) {
  365. continue;
  366. }
  367. // Before submitting this form we need to make sure that our subject_id and
  368. // object_ids are real values. There are two ways to get the value, either
  369. // just with the text value or with an [id: \d+] string embedded. If the
  370. // later we will pull it out.
  371. $subject_id = '';
  372. $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
  373. $matches = array();
  374. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  375. $subject_id = $matches[1];
  376. $values = array($fkey_rcolumn => $subject_id);
  377. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  378. if (count($subject) == 0) {
  379. $errors[$field['field_name']][$langcode][$delta][] = array(
  380. 'error' => 'chado_linker__relationship',
  381. 'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
  382. );
  383. }
  384. }
  385. else {
  386. $values = array('uniquename' => $subject_name);
  387. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  388. if (count($subject) == 0) {
  389. $errors[$field['field_name']][$langcode][$delta][] = array(
  390. 'error' => 'chado_linker__relationship',
  391. 'message' => t("The subject record cannot be found. Please check spelling."),
  392. );
  393. }
  394. elseif (count($subject) > 1) {
  395. $errors[$field['field_name']][$langcode][$delta][] = array(
  396. 'error' => 'chado_linker__relationship',
  397. 'message' => t("The subject is not unique and therefore the relationship cannot be made."),
  398. );
  399. }
  400. }
  401. // Now check for a matching object.
  402. $object_id = '';
  403. $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
  404. $matches = array();
  405. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  406. $object_id = $matches[1];
  407. $values = array($fkey_rcolumn => $object_id);
  408. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  409. if (count($subject) == 0) {
  410. $errors[$field['field_name']][$langcode][$delta][] = array(
  411. 'error' => 'chado_linker__relationship',
  412. 'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
  413. );
  414. }
  415. }
  416. else {
  417. $values = array('uniquename' => $object_name);
  418. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  419. if (count($object) == 0) {
  420. $errors[$field['field_name']][$langcode][$delta][] = array(
  421. 'error' => 'chado_linker__relationship',
  422. 'message' => t("The object record cannot be found. Please check spelling."),
  423. );;
  424. }
  425. elseif (count($object) > 1) {
  426. $errors[$field['field_name']][$langcode][$delta][] = array(
  427. 'error' => 'chado_linker__relationship',
  428. 'message' => t("The object is not unique and therefore the relationship cannot be made."),
  429. );
  430. }
  431. }
  432. // Make sure that either our object or our subject refers to the base record.
  433. if ($object_id != $chado_record_id and $subject_id != $chado_record_id) {
  434. $errors[$field['field_name']][$langcode][$delta][] = array(
  435. 'error' => 'chado_linker__relationship',
  436. 'message' => t("Either the subject or the object in the relationship must refer to this record."),
  437. );
  438. }
  439. // Make sure that the object and subject are not both the same thing.
  440. if ($object_id == $subject_id) {
  441. $errors[$field['field_name']][$langcode][$delta][] = array(
  442. 'error' => 'chado_linker__relationship',
  443. 'message' => t("The subject and the object in the relationship cannot both refer to the same record."),
  444. );
  445. }
  446. }
  447. }
  448. /**
  449. * @see TripalField::submit()
  450. */
  451. public function submit($entity_type, $entity, $field, $instance, $langcode,
  452. &$items, $form, &$form_state) {
  453. $field_name = $field['field_name'];
  454. $field_type = $field['type'];
  455. $field_table = $field['settings']['chado_table'];
  456. $field_column = $field['settings']['chado_column'];
  457. $base_table = $field['settings']['base_table'];
  458. $chado_record_id = $entity->chado_record_id;
  459. $schema = chado_get_schema($field_table);
  460. $fkeys = $schema['foreign keys'];
  461. foreach ($items as $delta => $item) {
  462. $subject_id = $item[$field_table . '__subject_id'];
  463. $object_id = $item[ $field_table . '__object_id'];
  464. $type_id = $item[$field_table . '__type_id'];
  465. $type_name = $item['type_name'];
  466. $subject_name = $item['subject_name'];
  467. $object_name = $item['object_name'];
  468. // If the row is empty then skip this one, there's nothing to validate.
  469. if (!$type_name and !$subject_name and !$object_name) {
  470. continue;
  471. }
  472. // Get the subject ID.
  473. $subject_id = '';
  474. $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
  475. $matches = array();
  476. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  477. $subject_id = $matches[1];
  478. }
  479. else {
  480. $values = array('uniquename' => $subject_name);
  481. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  482. $subject_id = $subject[0]->$fkey_rcolumn;
  483. }
  484. // Get the object ID.
  485. $object_id = '';
  486. $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
  487. $matches = array();
  488. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  489. $object_id = $matches[1];
  490. }
  491. else {
  492. $values = array('uniquename' => $object_name);
  493. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  494. $object_id = $object[0]->$fkey_rcolumn;
  495. }
  496. // Set the IDs according to the values that were determined above.
  497. $items[$delta][$field_table . '__subject_id'] = $subject_id;
  498. $items[$delta][$field_table . '__object_id'] = $object_id;
  499. $items[$delta][$field_table . '__type_id'] = $type_name;
  500. $items[$delta][$field_table . '__rank'] = $item['_weight'];
  501. }
  502. }
  503. /**
  504. * @see TripalField::load()
  505. */
  506. function load($field, $entity, $details) {
  507. $settings = $field['settings'];
  508. $record = $details['record'];
  509. $field_name = $field['field_name'];
  510. $field_type = $field['type'];
  511. $field_table = $field['settings']['chado_table'];
  512. $field_column = $field['settings']['chado_column'];
  513. $base_table = $field['settings']['base_table'];
  514. // Get the PKey for this table
  515. $schema = chado_get_schema($field_table);
  516. $pkey = $schema['primary key'][0];
  517. // Get the Pkeys for the subject and object tables
  518. $subject_fkey_table = '';
  519. $object_fkey_table = '';
  520. $fkeys = $schema['foreign keys'];
  521. foreach ($fkeys as $fktable => $details) {
  522. foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
  523. if ($fkey_lcolumn == 'subject_id') {
  524. $subject_fkey_table = $fktable;
  525. }
  526. if ($fkey_lcolumn == 'object_id') {
  527. $object_fkey_table = $fktable;
  528. }
  529. }
  530. }
  531. $subject_schema = chado_get_schema($subject_fkey_table);
  532. $object_schema = chado_get_schema($object_fkey_table);
  533. $subject_pkey = $subject_schema['primary key'][0];
  534. $object_pkey = $object_schema['primary key'][0];
  535. // Get the FK that links to the base record.
  536. $schema = chado_get_schema($field_table);
  537. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  538. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  539. // Set some defaults for the empty record.
  540. // TODO: don't hardcode the uniquename as all tables won't have that.
  541. $entity->{$field_name}['und'][0] = array(
  542. 'value' => array(),
  543. $field_table . '__' . $pkey => '',
  544. $field_table . '__subject_id' => '',
  545. $field_table . '__object_id' => '',
  546. $field_table . '__type_id' => '',
  547. // These elements don't need to follow the naming scheme above
  548. // becasue we don't need the chado_field_storage to try and
  549. // save these values.
  550. 'object_name' => '',
  551. 'subject_name' => '',
  552. 'type_name' => '',
  553. );
  554. if (array_key_exists('value', $schema['fields'])) {
  555. $entity->{$field_name}['und'][0][$field_table . '__value'] = '';
  556. }
  557. if (array_key_exists('rank', $schema['fields'])) {
  558. $entity->{$field_name}['und'][0][$field_table . '__rank'] = '';
  559. }
  560. if (!$record) {
  561. return;
  562. }
  563. // Expand the object to include the relationships.
  564. $options = array(
  565. 'return_array' => 1,
  566. // we don't want to fully recurse we only need information about the
  567. // relationship type and the object and subject
  568. 'include_fk' => array(
  569. 'type_id' => 1,
  570. 'object_id' => array(
  571. 'type_id' => 1,
  572. ),
  573. 'subject_id' => array(
  574. 'type_id' => 1,
  575. ),
  576. ),
  577. );
  578. $rel_table = $base_table . '_relationship';
  579. $schema = chado_get_schema($rel_table);
  580. if (array_key_exists('rank', $schema['fields'])) {
  581. $options['order_by'] = array('rank' => 'ASC');
  582. }
  583. $record = chado_expand_var($record, 'table', $rel_table, $options);
  584. if (!$record->$rel_table) {
  585. return;
  586. }
  587. $srelationships = $record->$rel_table->subject_id;
  588. $orelationships = $record->$rel_table->object_id;
  589. $i = 0;
  590. if ($orelationships) {
  591. foreach ($orelationships as $relationship) {
  592. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  593. $rel_type = $relationship->type_id->name;
  594. $verb = $this->get_rel_verb($rel_type);
  595. $subject_name = $relationship->subject_id->name;
  596. $subject_type = $relationship->subject_id->type_id->name;
  597. $object_name = $relationship->object_id->name;
  598. $object_type = $relationship->object_id->type_id->name;
  599. $entity->{$field_name}['und'][$i]['value'] = array(
  600. '@type' => $rel_acc,
  601. 'subject' => array(
  602. '@type' => $relationship->subject_id->type_id->dbxref_id->db_id->name .
  603. ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  604. 'type' => $subject_type,
  605. 'name' => $subject_name,
  606. ),
  607. 'type' => $relationship->type_id->name,
  608. 'object' => array(
  609. '@type' => $relationship->object_id->type_id->dbxref_id->db_id->name .
  610. ':' . $relationship->object_id->type_id->dbxref_id->accession,
  611. // TODO the entity_id should not be here.... wherre to put it.
  612. 'entity_id' => $entity->id,
  613. 'entity_type' => 'TripalEntity',
  614. 'type' => $object_type,
  615. 'name' => $object_name,
  616. )
  617. );
  618. if (property_exists($relationship->subject_id, 'uniquename')) {
  619. $subject_name = $relationship->subject_id->uniquename;
  620. $entity->{$field_name}['und'][$i]['value']['subject']['name'] = $subject_name;
  621. }
  622. if (property_exists($relationship->object_id, 'uniquename')) {
  623. $object_name = $relationship->object_id->uniquename;
  624. $entity->{$field_name}['und'][$i]['value']['object']['name'] = $object_name;
  625. }
  626. if (property_exists($relationship->subject_id, 'entity_id')) {
  627. $entity_id = $relationship->subject_id->entity_id;
  628. $entity->{$field_name}['und'][$i]['value']['subject']['entity_id'] = $entity_id;
  629. $entity->{$field_name}['und'][$i]['value']['subject']['entity_type'] = 'TripalEntity';
  630. }
  631. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  632. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
  633. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' this ' .
  634. $object_type . '.';
  635. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  636. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  637. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  638. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  639. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  640. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->uniquename . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  641. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->uniquename . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  642. if (array_key_exists('value', $schema['fields'])) {
  643. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  644. }
  645. if (array_key_exists('rank', $schema['fields'])) {
  646. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  647. }
  648. $i++;
  649. }
  650. }
  651. if ($srelationships) {
  652. foreach ($srelationships as $relationship) {
  653. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  654. $rel_type = $relationship->type_id->name;
  655. $verb = $this->get_rel_verb($rel_type);
  656. $subject_name = $relationship->subject_id->name;
  657. $subject_type = $relationship->subject_id->type_id->name;
  658. $object_name = $relationship->object_id->name;
  659. $object_type = $relationship->object_id->type_id->name;
  660. $entity->{$field_name}['und'][$i]['value'] = array(
  661. '@type' => $rel_acc,
  662. 'subject' => array(
  663. '@type' => $relationship->subject_id->type_id->dbxref_id->db_id->name .
  664. ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  665. 'type' => $subject_type,
  666. 'name' => $subject_name,
  667. 'entity_id' => $entity->id,
  668. 'entity_type' => 'TripalEntity',
  669. ),
  670. 'type' => $relationship->type_id->name,
  671. 'object' => array(
  672. '@type' => $relationship->object_id->type_id->dbxref_id->db_id->name .
  673. ':' . $relationship->object_id->type_id->dbxref_id->accession,
  674. 'type' => $object_type,
  675. 'name' => $object_name,
  676. )
  677. );
  678. if (property_exists($relationship->subject_id, 'uniquename')) {
  679. $subject_name = $relationship->subject_id->uniquename;
  680. $entity->{$field_name}['und'][$i]['value']['subject']['name'] = $subject_name;
  681. }
  682. if (property_exists($relationship->object_id, 'uniquename')) {
  683. $object_name = $relationship->object_id->uniquename;
  684. $entity->{$field_name}['und'][$i]['value']['object']['name'] = $object_name;
  685. }
  686. if (property_exists($relationship->object_id, 'entity_id')) {
  687. $entity_id = $relationship->object_id->entity_id;
  688. $entity->{$field_name}['und'][$i]['value']['object']['entity_id'] = $entity_id;
  689. $entity->{$field_name}['und'][$i]['value']['object']['entity_type'] = 'TripalEntity';
  690. }
  691. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  692. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This ' .
  693. $subject_type . ' ' . $verb . ' ' . $rel_type_clean . ' the ' .
  694. $object_type . ', ' . $object_name . '.';
  695. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  696. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  697. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  698. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  699. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  700. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->uniquename . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  701. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->uniquename . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  702. if (array_key_exists('value', $schema['fields'])) {
  703. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  704. }
  705. if (array_key_exists('rank', $schema['fields'])) {
  706. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  707. }
  708. $i++;
  709. }
  710. }
  711. }
  712. /**
  713. * A helper function to define English verbs for relationship types.
  714. *
  715. * @param $rel_type
  716. * The vocabulary term name for the relationship.
  717. *
  718. * @return
  719. * The verb to use when creating a sentence of the relationship.
  720. */
  721. private function get_rel_verb($rel_type) {
  722. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  723. $verb = $rel_type_clean;
  724. switch ($rel_type_clean) {
  725. case 'integral part of':
  726. case 'instance of':
  727. $verb = 'is an';
  728. break;
  729. case 'proper part of':
  730. case 'transformation of':
  731. case 'genome of':
  732. case 'part of':
  733. $verb = 'is a';
  734. case 'position of':
  735. case 'sequence of':
  736. case 'variant of':
  737. $verb = 'is a';
  738. break;
  739. case 'derives from':
  740. case 'connects on':
  741. case 'contains':
  742. case 'finishes':
  743. case 'guides':
  744. case 'has origin':
  745. case 'has part':
  746. case 'has quality':
  747. case 'is consecutive sequence of':
  748. case 'maximally overlaps':
  749. case 'overlaps':
  750. case 'starts':
  751. break;
  752. default:
  753. $verb = 'is';
  754. }
  755. return $verb;
  756. }
  757. }
  758. /**
  759. * Theme function for the chado_linker__relationship_widget.
  760. */
  761. function theme_chado_linker__relationship_widget($variables) {
  762. $element = $variables['element'];
  763. $field_name = $element['#field_name'];
  764. $field = field_info_field($field_name);
  765. $field_type = $field['type'];
  766. $field_table = $field['settings']['chado_table'];
  767. $field_column = $field['settings']['chado_column'];
  768. $layout = "
  769. <div class=\"chado-linker--relationship-widget\">
  770. <div class=\"chado-linker--relationship-widget-item\">" .
  771. drupal_render($element['subject_name']) . "
  772. </div>
  773. <div class=\"chado-linker--relationship-widget-item\">" .
  774. drupal_render($element['type_name']) . "
  775. </div>
  776. <div class=\"chado-linker--relationship-widget-item\">" .
  777. drupal_render($element['object_name']) . "
  778. </div>
  779. </div>
  780. ";
  781. return $layout;
  782. }