chado_linker__relationship.inc 33 KB

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