chado_linker__relationship.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. $msg = t('There is no default vocabulary set for the relationship types.');
  320. if (module_exists('tripal_chado')) {
  321. $msg .= t(' Please set the default vocabulary at <a href="@url" target="_blank">Tripal > Data Storage > Chado > Vocabulary Defaults.</a>', array('@url' => url('/admin/tripal/storage/chado/vocab')));
  322. }
  323. drupal_set_message($msg, 'warning');
  324. }
  325. else {
  326. $options = tripal_get_cvterm_select_options($default_cv->cv_id, TRUE);
  327. }
  328. $widget['type_name'] = array(
  329. '#type' => 'select',
  330. '#title' => t('Type'),
  331. '#options' => $options,
  332. '#required' => $element['#required'],
  333. '#default_value' => $type_id,
  334. );
  335. $widget['object_name'] = array(
  336. '#type' => 'textfield',
  337. '#title' => t('Object'),
  338. '#default_value' => $object_uniquename,
  339. '#required' => $element['#required'],
  340. '#maxlength' => array_key_exists('length', $schema['fields']['object_id']) ? $schema['fields']['object_id']['length'] : 255,
  341. '#size' => 35,
  342. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  343. );
  344. }
  345. /**
  346. * @see TripalField::validate()
  347. */
  348. function validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  349. $field_name = $field['field_name'];
  350. $field_type = $field['type'];
  351. $field_table = $field['settings']['chado_table'];
  352. $field_column = $field['settings']['chado_column'];
  353. $base_table = $field['settings']['base_table'];
  354. $chado_record_id = $entity->chado_record_id;
  355. $schema = chado_get_schema($field_table);
  356. $fkeys = $schema['foreign keys'];
  357. foreach ($items as $delta => $item) {
  358. $subject_id = $item[$field_table . '__subject_id'];
  359. $object_id = $item[ $field_table . '__object_id'];
  360. $type_id = $item[$field_table . '__type_id'];
  361. $type_name = $item['type_name'];
  362. $subject_name = $item['subject_name'];
  363. $object_name = $item['object_name'];
  364. // If the row is empty then just continue, there's nothing to validate.
  365. if (!$type_name and !$subject_name and !$object_name) {
  366. continue;
  367. }
  368. // Make sure we have values for all of the fields.
  369. $form_error = FALSE;
  370. if (!$type_name) {
  371. $errors[$field['field_name']][$langcode][$delta][] = array(
  372. 'error' => 'chado_linker__relationship',
  373. 'message' => t("Please provide the type of relationship."),
  374. );
  375. }
  376. if (!$subject_name) {
  377. $errors[$field['field_name']][$langcode][$delta][] = array(
  378. 'error' => 'chado_linker__relationship',
  379. 'message' => t("Please provide the subject of the relationship."),
  380. );
  381. }
  382. if (!$object_name) {
  383. $errors[$field['field_name']][$langcode][$delta][] = array(
  384. 'error' => 'chado_linker__relationship',
  385. 'message' => t("Please provide the object of the relationship."),
  386. );
  387. }
  388. if ($form_error) {
  389. continue;
  390. }
  391. // Before submitting this form we need to make sure that our subject_id and
  392. // object_ids are real values. There are two ways to get the value, either
  393. // just with the text value or with an [id: \d+] string embedded. If the
  394. // later we will pull it out.
  395. $subject_id = '';
  396. $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
  397. $matches = array();
  398. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  399. $subject_id = $matches[1];
  400. $values = array($fkey_rcolumn => $subject_id);
  401. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  402. if (count($subject) == 0) {
  403. $errors[$field['field_name']][$langcode][$delta][] = array(
  404. 'error' => 'chado_linker__relationship',
  405. 'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
  406. );
  407. }
  408. }
  409. else {
  410. $values = array('uniquename' => $subject_name);
  411. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  412. if (count($subject) == 0) {
  413. $errors[$field['field_name']][$langcode][$delta][] = array(
  414. 'error' => 'chado_linker__relationship',
  415. 'message' => t("The subject record cannot be found. Please check spelling."),
  416. );
  417. }
  418. elseif (count($subject) > 1) {
  419. $errors[$field['field_name']][$langcode][$delta][] = array(
  420. 'error' => 'chado_linker__relationship',
  421. 'message' => t("The subject is not unique and therefore the relationship cannot be made."),
  422. );
  423. }
  424. }
  425. // Now check for a matching object.
  426. $object_id = '';
  427. $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
  428. $matches = array();
  429. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  430. $object_id = $matches[1];
  431. $values = array($fkey_rcolumn => $object_id);
  432. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  433. if (count($subject) == 0) {
  434. $errors[$field['field_name']][$langcode][$delta][] = array(
  435. 'error' => 'chado_linker__relationship',
  436. 'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
  437. );
  438. }
  439. }
  440. else {
  441. $values = array('uniquename' => $object_name);
  442. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  443. if (count($object) == 0) {
  444. $errors[$field['field_name']][$langcode][$delta][] = array(
  445. 'error' => 'chado_linker__relationship',
  446. 'message' => t("The object record cannot be found. Please check spelling."),
  447. );;
  448. }
  449. elseif (count($object) > 1) {
  450. $errors[$field['field_name']][$langcode][$delta][] = array(
  451. 'error' => 'chado_linker__relationship',
  452. 'message' => t("The object is not unique and therefore the relationship cannot be made."),
  453. );
  454. }
  455. }
  456. // Make sure that either our object or our subject refers to the base record.
  457. if ($object_id != $chado_record_id and $subject_id != $chado_record_id) {
  458. $errors[$field['field_name']][$langcode][$delta][] = array(
  459. 'error' => 'chado_linker__relationship',
  460. 'message' => t("Either the subject or the object in the relationship must refer to this record."),
  461. );
  462. }
  463. // Make sure that the object and subject are not both the same thing.
  464. if ($object_id == $subject_id) {
  465. $errors[$field['field_name']][$langcode][$delta][] = array(
  466. 'error' => 'chado_linker__relationship',
  467. 'message' => t("The subject and the object in the relationship cannot both refer to the same record."),
  468. );
  469. }
  470. }
  471. }
  472. /**
  473. * @see TripalField::submit()
  474. */
  475. public function submit($entity_type, $entity, $field, $instance, $langcode,
  476. &$items, $form, &$form_state) {
  477. $field_name = $field['field_name'];
  478. $field_type = $field['type'];
  479. $field_table = $field['settings']['chado_table'];
  480. $field_column = $field['settings']['chado_column'];
  481. $base_table = $field['settings']['base_table'];
  482. $chado_record_id = $entity->chado_record_id;
  483. $schema = chado_get_schema($field_table);
  484. $fkeys = $schema['foreign keys'];
  485. foreach ($items as $delta => $item) {
  486. $subject_id = $item[$field_table . '__subject_id'];
  487. $object_id = $item[ $field_table . '__object_id'];
  488. $type_id = $item[$field_table . '__type_id'];
  489. $type_name = $item['type_name'];
  490. $subject_name = $item['subject_name'];
  491. $object_name = $item['object_name'];
  492. // If the row is empty then skip this one, there's nothing to validate.
  493. if (!$type_name and !$subject_name and !$object_name) {
  494. continue;
  495. }
  496. // Get the subject ID.
  497. $subject_id = '';
  498. $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
  499. $matches = array();
  500. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  501. $subject_id = $matches[1];
  502. }
  503. else {
  504. $values = array('uniquename' => $subject_name);
  505. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  506. $subject_id = $subject[0]->$fkey_rcolumn;
  507. }
  508. // Get the object ID.
  509. $object_id = '';
  510. $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
  511. $matches = array();
  512. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  513. $object_id = $matches[1];
  514. }
  515. else {
  516. $values = array('uniquename' => $object_name);
  517. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  518. $object_id = $object[0]->$fkey_rcolumn;
  519. }
  520. // Set the IDs according to the values that were determined above.
  521. $items[$delta][$field_table . '__subject_id'] = $subject_id;
  522. $items[$delta][$field_table . '__object_id'] = $object_id;
  523. $items[$delta][$field_table . '__type_id'] = $type_name;
  524. $items[$delta][$field_table . '__rank'] = $item['_weight'];
  525. }
  526. }
  527. /**
  528. * @see TripalField::load()
  529. */
  530. static function load($field, $entity, $details = array()) {
  531. $settings = $field['settings'];
  532. $record = $details['record'];
  533. $field_name = $field['field_name'];
  534. $field_type = $field['type'];
  535. $field_table = $field['settings']['chado_table'];
  536. $field_column = $field['settings']['chado_column'];
  537. $base_table = $field['settings']['base_table'];
  538. // Get the PKey for this table
  539. $schema = chado_get_schema($field_table);
  540. $pkey = $schema['primary key'][0];
  541. // Get the Pkeys for the subject and object tables
  542. $subject_fkey_table = '';
  543. $object_fkey_table = '';
  544. $fkeys = $schema['foreign keys'];
  545. foreach ($fkeys as $fktable => $details) {
  546. foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
  547. if ($fkey_lcolumn == 'subject_id') {
  548. $subject_fkey_table = $fktable;
  549. }
  550. if ($fkey_lcolumn == 'object_id') {
  551. $object_fkey_table = $fktable;
  552. }
  553. }
  554. }
  555. $subject_schema = chado_get_schema($subject_fkey_table);
  556. $object_schema = chado_get_schema($object_fkey_table);
  557. $subject_pkey = $subject_schema['primary key'][0];
  558. $object_pkey = $object_schema['primary key'][0];
  559. // Get the FK that links to the base record.
  560. $schema = chado_get_schema($field_table);
  561. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  562. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  563. // Set some defaults for the empty record.
  564. // TODO: don't hardcode the uniquename as all tables won't have that.
  565. $entity->{$field_name}['und'][0] = array(
  566. 'value' => array(),
  567. $field_table . '__' . $pkey => '',
  568. $field_table . '__subject_id' => '',
  569. $field_table . '__object_id' => '',
  570. $field_table . '__type_id' => '',
  571. // These elements don't need to follow the naming scheme above
  572. // becasue we don't need the chado_field_storage to try and
  573. // save these values.
  574. 'object_name' => '',
  575. 'subject_name' => '',
  576. 'type_name' => '',
  577. );
  578. // If the table has rank and value fields then add those to the default
  579. // value array.
  580. if (array_key_exists('value', $schema['fields'])) {
  581. $entity->{$field_name}['und'][0][$field_table . '__value'] = '';
  582. }
  583. if (array_key_exists('rank', $schema['fields'])) {
  584. $entity->{$field_name}['und'][0][$field_table . '__rank'] = '';
  585. }
  586. // If we have no record then just return.
  587. if (!$record) {
  588. return;
  589. }
  590. // Expand the object to include the relationships.
  591. $options = array(
  592. 'return_array' => 1,
  593. // we don't want to fully recurse we only need information about the
  594. // relationship type and the object and subject
  595. 'include_fk' => array(
  596. 'type_id' => 1,
  597. 'object_id' => array(
  598. 'type_id' => 1,
  599. ),
  600. 'subject_id' => array(
  601. 'type_id' => 1,
  602. ),
  603. ),
  604. );
  605. $rel_table = $base_table . '_relationship';
  606. $schema = chado_get_schema($rel_table);
  607. if (array_key_exists('rank', $schema['fields'])) {
  608. $options['order_by'] = array('rank' => 'ASC');
  609. }
  610. $record = chado_expand_var($record, 'table', $rel_table, $options);
  611. if (!$record->$rel_table) {
  612. return;
  613. }
  614. $srelationships = $record->$rel_table->subject_id;
  615. $orelationships = $record->$rel_table->object_id;
  616. $i = 0;
  617. if ($orelationships) {
  618. foreach ($orelationships as $relationship) {
  619. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  620. $rel_type = $relationship->type_id->name;
  621. $verb = self::get_rel_verb($rel_type);
  622. $subject_name = $relationship->subject_id->name;
  623. $subject_type = $relationship->subject_id->type_id->name;
  624. $object_name = $relationship->object_id->name;
  625. $object_type = $relationship->object_id->type_id->name;
  626. $entity->{$field_name}['und'][$i]['value'] = array(
  627. 'type' => $relationship->type_id->name,
  628. 'subject' => array(
  629. 'type' => $subject_type,
  630. 'name' => $subject_name,
  631. ),
  632. 'type' => $relationship->type_id->name,
  633. 'object' => array(
  634. 'type' => $object_type,
  635. 'name' => $object_name,
  636. 'entity' => 'TripalEntity:' . $entity->id,
  637. )
  638. );
  639. if (property_exists($relationship->subject_id, 'uniquename')) {
  640. $entity->{$field_name}['und'][$i]['value']['subject']['identifier'] = $relationship->subject_id->uniquename;;
  641. }
  642. if (property_exists($relationship->object_id, 'uniquename')) {
  643. $entity->{$field_name}['und'][$i]['value']['object']['identifier'] = $relationship->object_id->uniquename;
  644. }
  645. if (property_exists($relationship->subject_id, 'entity_id')) {
  646. $entity_id = $relationship->subject_id->entity_id;
  647. $entity->{$field_name}['und'][$i]['value']['subject']['entity'] = 'TripalEntity:' . $entity_id;
  648. }
  649. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  650. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
  651. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' this ' .
  652. $object_type . '.';
  653. $entity->{$field_name}['und'][$i]['semantic_web'] = array(
  654. 'type' => $rel_acc,
  655. 'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  656. 'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
  657. );
  658. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  659. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  660. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  661. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  662. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  663. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  664. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  665. if (array_key_exists('value', $schema['fields'])) {
  666. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  667. }
  668. if (array_key_exists('rank', $schema['fields'])) {
  669. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  670. }
  671. $i++;
  672. }
  673. }
  674. if ($srelationships) {
  675. foreach ($srelationships as $relationship) {
  676. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  677. $rel_type = $relationship->type_id->name;
  678. $verb = self::get_rel_verb($rel_type);
  679. $subject_name = $relationship->subject_id->name;
  680. $subject_type = $relationship->subject_id->type_id->name;
  681. $object_name = $relationship->object_id->name;
  682. $object_type = $relationship->object_id->type_id->name;
  683. $entity->{$field_name}['und'][$i]['value'] = array(
  684. '@type' => $relationship->type_id->name,
  685. 'subject' => array(
  686. 'type' => $subject_type,
  687. 'name' => $subject_name,
  688. 'entity' => 'TripalEntity:' . $entity->id,
  689. ),
  690. 'type' => $relationship->type_id->name,
  691. 'object' => array(
  692. 'type' => $object_type,
  693. 'name' => $object_name,
  694. )
  695. );
  696. if (property_exists($relationship->subject_id, 'uniquename')) {
  697. $entity->{$field_name}['und'][$i]['value']['subject']['identifier'] = $relationship->subject_id->uniquename;
  698. }
  699. if (property_exists($relationship->object_id, 'uniquename')) {
  700. $entity->{$field_name}['und'][$i]['value']['object']['identifier'] = $relationship->object_id->uniquename;
  701. }
  702. if (property_exists($relationship->object_id, 'entity_id')) {
  703. $entity_id = $relationship->object_id->entity_id;
  704. $entity->{$field_name}['und'][$i]['value']['object']['entity'] = 'TripalEntity:' . $entity_id;
  705. }
  706. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  707. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This ' .
  708. $subject_type . ' ' . $verb . ' ' . $rel_type_clean . ' the ' .
  709. $object_type . ', ' . $object_name . '.';
  710. $entity->{$field_name}['und'][$i]['semantic_web'] = array(
  711. 'type' => $rel_acc,
  712. 'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  713. 'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
  714. );
  715. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  716. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  717. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  718. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  719. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  720. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  721. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  722. if (array_key_exists('value', $schema['fields'])) {
  723. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  724. }
  725. if (array_key_exists('rank', $schema['fields'])) {
  726. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  727. }
  728. $i++;
  729. }
  730. }
  731. }
  732. /**
  733. * A helper function to define English verbs for relationship types.
  734. *
  735. * @param $rel_type
  736. * The vocabulary term name for the relationship.
  737. *
  738. * @return
  739. * The verb to use when creating a sentence of the relationship.
  740. */
  741. public static function get_rel_verb($rel_type) {
  742. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  743. $verb = $rel_type_clean;
  744. switch ($rel_type_clean) {
  745. case 'integral part of':
  746. case 'instance of':
  747. $verb = 'is an';
  748. break;
  749. case 'proper part of':
  750. case 'transformation of':
  751. case 'genome of':
  752. case 'part of':
  753. $verb = 'is a';
  754. case 'position of':
  755. case 'sequence of':
  756. case 'variant of':
  757. $verb = 'is a';
  758. break;
  759. case 'derives from':
  760. case 'connects on':
  761. case 'contains':
  762. case 'finishes':
  763. case 'guides':
  764. case 'has origin':
  765. case 'has part':
  766. case 'has quality':
  767. case 'is consecutive sequence of':
  768. case 'maximally overlaps':
  769. case 'overlaps':
  770. case 'starts':
  771. break;
  772. default:
  773. $verb = 'is';
  774. }
  775. return $verb;
  776. }
  777. }
  778. /**
  779. * Theme function for the chado_linker__relationship_widget.
  780. */
  781. function theme_chado_linker__relationship_widget($variables) {
  782. $element = $variables['element'];
  783. $field_name = $element['#field_name'];
  784. $field = field_info_field($field_name);
  785. $field_type = $field['type'];
  786. $field_table = $field['settings']['chado_table'];
  787. $field_column = $field['settings']['chado_column'];
  788. $layout = "
  789. <div class=\"chado-linker--relationship-widget\">
  790. <div class=\"chado-linker--relationship-widget-item\">" .
  791. drupal_render($element['subject_name']) . "
  792. </div>
  793. <div class=\"chado-linker--relationship-widget-item\">" .
  794. drupal_render($element['type_name']) . "
  795. </div>
  796. <div class=\"chado-linker--relationship-widget-item\">" .
  797. drupal_render($element['object_name']) . "
  798. </div>
  799. </div>
  800. ";
  801. return $layout;
  802. }