chado_linker__relationship.inc 36 KB

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