chado_linker__relationship.inc 31 KB

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