chado_linker__relationship.inc 32 KB

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