chado_linker__relationship.inc 36 KB

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