chado_linker__relationship.inc 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. <?php
  2. class chado_linker__relationship extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Relationships';
  5. // The default description for this field.
  6. public static $default_description = 'Relationships between records.';
  7. // Add any default settings elements. If you override the fieldSettingsForm()
  8. // or the instanceSettingsForm() functions then you need to be sure that
  9. // any settings you want those functions to manage are listed in this
  10. // array.
  11. public static $default_settings = array(
  12. 'chado_table' => '',
  13. 'chado_column' => '',
  14. 'base_table' => '',
  15. 'semantic_web' => '',
  16. );
  17. // Set this to the name of the storage backend that by default will support
  18. // this field.
  19. public static $default_storage = 'field_chado_storage';
  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. 'child_relationship_types' => '',
  101. 'relationship_types' => '',
  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. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  146. // Get the settings
  147. $settings = $display['settings'];
  148. $rows = array();
  149. $headers = array('Subject' ,'Type', 'Object');
  150. $headers = array('Relationship');
  151. foreach ($items as $delta => $item) {
  152. if (!$item['value']) {
  153. continue;
  154. }
  155. $subject_name = $item['value']['subject']['name'];
  156. $subject_type = $item['value']['subject']['type'];
  157. $object_name = $item['value']['object']['name'];
  158. $object_type = $item['value']['object']['type'];
  159. $phrase = $item['value']['phrase'];
  160. // Handle some special cases.
  161. // For mRNA objects we don't want to show the CDS, exons, 5' UTR, etc.
  162. // we want to show the parent gene and the protein.
  163. if ($object_type == 'mRNA' and ($subject_type != 'polypeptide')) {
  164. continue;
  165. }
  166. if ($subject_type == 'mRNA' and ($object_type != 'gene')) {
  167. continue;
  168. }
  169. $phrase = preg_replace("/$subject_type/", "<b>$subject_type</b>", $phrase);
  170. $phrase = preg_replace("/$object_type/", "<b>$object_type</b>", $phrase);
  171. if (array_key_exists('entity', $item['value']['object'])) {
  172. list($entity_type, $object_entity_id) = explode(':', $item['value']['object']['entity']);
  173. if ($object_entity_id != $entity->id) {
  174. $link = l($object_name, 'bio_data/' . $object_entity_id);
  175. $phrase = preg_replace("/$object_name/", $link, $phrase);
  176. }
  177. }
  178. if (array_key_exists('entity', $item['value']['subject'])) {
  179. list($entity_type, $subject_entity_id) = explode(':', $item['value']['subject']['entity']);
  180. if ($subject_entity_id != $entity->id) {
  181. $link = l($subject_name, 'bio_data/' . $subject_entity_id);
  182. $phrase = preg_replace("/$subject_name/", $link, $phrase);
  183. }
  184. }
  185. $rows[] = array($phrase);
  186. }
  187. // the $table array contains the headers and rows array as well as other
  188. // options for controlling the display of the table. Additional
  189. // documentation can be found here:
  190. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  191. $table = array(
  192. 'header' => $headers,
  193. 'rows' => $rows,
  194. 'attributes' => array(
  195. 'id' => 'chado-linker--relationship-table',
  196. 'class' => 'tripal-data-table'
  197. ),
  198. 'sticky' => FALSE,
  199. 'caption' => '',
  200. 'colgroups' => array(),
  201. 'empty' => 'There are no relationships',
  202. );
  203. // once we have our table array structure defined, we call Drupal's theme_table()
  204. // function to generate the table.
  205. if (count($items) > 0) {
  206. $element[0] = array(
  207. '#type' => 'markup',
  208. '#markup' => theme_table($table),
  209. );
  210. }
  211. }
  212. /**
  213. * @see TripalField::widgetForm()
  214. */
  215. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  216. $field_name = $this->field['field_name'];
  217. $field_type = $this->field['type'];
  218. $field_table = $this->field['settings']['chado_table'];
  219. $field_column = $this->field['settings']['chado_column'];
  220. // Get the FK column that links to the base table.
  221. $chado_table = $this->field['settings']['chado_table'];
  222. $base_table = $this->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 (isset($items[$delta][$field_table . '__value'])) {
  246. $value = $items[$delta][$field_table . '__value'];
  247. }
  248. if (isset($items[$delta][$field_table . '__rank'])) {
  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. $cv_id = isset($form_state['values'][$field_name]['und'][0]['vocabulary']) ? $form_state['values'][$field_name]['und'][0]['vocabulary'] : 0;
  319. // Try getting the cv_id from cvterm for existing records
  320. $default_term = '';
  321. if (!$cv_id && $type_id) {
  322. $cvterm = tripal_get_cvterm(array('cvterm_id' => $type_id));
  323. if (isset($cvterm->cv_id->cv_id)) {
  324. $cv_id = $cvterm->cv_id->cv_id;
  325. $default_term = $cvterm->name;
  326. }
  327. }
  328. $widget['vocabulary'] = array(
  329. '#type' => 'select',
  330. '#title' => t('Vocabulary'),
  331. '#options' => $vocs,
  332. '#required' => $element['#required'],
  333. '#default_value' => $cv_id,
  334. '#ajax' => array(
  335. 'callback' => "chado_linker__relationship_widget_form_ajax_callback",
  336. 'wrapper' => "$chado_table-$delta",
  337. 'effect' => 'fade',
  338. 'method' => 'replace'
  339. ),
  340. );
  341. if ($cv_id) {
  342. $options = array();
  343. $widget['type_name'] = array(
  344. '#type' => 'textfield',
  345. '#title' => t('Relationship Type'),
  346. '#size' => 15,
  347. '#default_value' => $default_term,
  348. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id"
  349. );
  350. }
  351. $widget['object_name'] = array(
  352. '#type' => 'textfield',
  353. '#title' => t('Object'),
  354. '#default_value' => $object_uniquename,
  355. '#required' => $element['#required'],
  356. '#maxlength' => array_key_exists('length', $schema['fields']['object_id']) ? $schema['fields']['object_id']['length'] : 255,
  357. '#size' => 35,
  358. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  359. );
  360. }
  361. /**
  362. * @see TripalField::validate()
  363. */
  364. function validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  365. $field_name = $this->field['field_name'];
  366. $field_type = $this->field['type'];
  367. $field_table = $this->field['settings']['chado_table'];
  368. $field_column = $this->field['settings']['chado_column'];
  369. $base_table = $this->field['settings']['base_table'];
  370. $chado_record_id = $entity->chado_record_id;
  371. $schema = chado_get_schema($field_table);
  372. $fkeys = $schema['foreign keys'];
  373. foreach ($items as $delta => $item) {
  374. $subject_id = $item[$field_table . '__subject_id'];
  375. $object_id = $item[ $field_table . '__object_id'];
  376. $type_id = $item[$field_table . '__type_id'];
  377. $type_name = $item['type_name'];
  378. $subject_name = $item['subject_name'];
  379. $object_name = $item['object_name'];
  380. // If the row is empty then just continue, there's nothing to validate.
  381. if (!$type_name and !$subject_name and !$object_name) {
  382. continue;
  383. }
  384. // Make sure we have values for all of the fields.
  385. $form_error = FALSE;
  386. if (!$type_name) {
  387. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  388. 'error' => 'chado_linker__relationship',
  389. 'message' => t("Please provide the type of relationship."),
  390. );
  391. }
  392. if (!$subject_name) {
  393. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  394. 'error' => 'chado_linker__relationship',
  395. 'message' => t("Please provide the subject of the relationship."),
  396. );
  397. }
  398. if (!$object_name) {
  399. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  400. 'error' => 'chado_linker__relationship',
  401. 'message' => t("Please provide the object of the relationship."),
  402. );
  403. }
  404. if ($form_error) {
  405. continue;
  406. }
  407. // Before submitting this form we need to make sure that our subject_id and
  408. // object_ids are real values. There are two ways to get the value, either
  409. // just with the text value or with an [id: \d+] string embedded. If the
  410. // later we will pull it out.
  411. $subject_id = '';
  412. $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
  413. $matches = array();
  414. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  415. $subject_id = $matches[1];
  416. $values = array($fkey_rcolumn => $subject_id);
  417. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  418. if (count($subject) == 0) {
  419. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  420. 'error' => 'chado_linker__relationship',
  421. 'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
  422. );
  423. }
  424. }
  425. else {
  426. $values = array('uniquename' => $subject_name);
  427. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  428. if (count($subject) == 0) {
  429. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  430. 'error' => 'chado_linker__relationship',
  431. 'message' => t("The subject record cannot be found. Please check spelling."),
  432. );
  433. }
  434. elseif (count($subject) > 1) {
  435. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  436. 'error' => 'chado_linker__relationship',
  437. 'message' => t("The subject is not unique and therefore the relationship cannot be made."),
  438. );
  439. }
  440. }
  441. // Now check for a matching object.
  442. $object_id = '';
  443. $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
  444. $matches = array();
  445. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  446. $object_id = $matches[1];
  447. $values = array($fkey_rcolumn => $object_id);
  448. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  449. if (count($subject) == 0) {
  450. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  451. 'error' => 'chado_linker__relationship',
  452. 'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
  453. );
  454. }
  455. }
  456. else {
  457. $values = array('uniquename' => $object_name);
  458. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  459. if (count($object) == 0) {
  460. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  461. 'error' => 'chado_linker__relationship',
  462. 'message' => t("The object record cannot be found. Please check spelling."),
  463. );;
  464. }
  465. elseif (count($object) > 1) {
  466. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  467. 'error' => 'chado_linker__relationship',
  468. 'message' => t("The object is not unique and therefore the relationship cannot be made."),
  469. );
  470. }
  471. }
  472. // Make sure that either our object or our subject refers to the base record.
  473. if ($object_id != $chado_record_id and $subject_id != $chado_record_id) {
  474. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  475. 'error' => 'chado_linker__relationship',
  476. 'message' => t("Either the subject or the object in the relationship must refer to this record."),
  477. );
  478. }
  479. // Make sure that the object and subject are not both the same thing.
  480. if ($object_id == $subject_id) {
  481. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  482. 'error' => 'chado_linker__relationship',
  483. 'message' => t("The subject and the object in the relationship cannot both refer to the same record."),
  484. );
  485. }
  486. }
  487. }
  488. /**
  489. * @see TripalField::submit()
  490. */
  491. public function submit($entity_type, $entity, $field, $instance, $langcode,
  492. &$items, $form, &$form_state) {
  493. $field_name = $this->field['field_name'];
  494. $field_type = $this->field['type'];
  495. $field_table = $this->field['settings']['chado_table'];
  496. $field_column = $this->field['settings']['chado_column'];
  497. $base_table = $this->field['settings']['base_table'];
  498. $chado_record_id = $entity->chado_record_id;
  499. $schema = chado_get_schema($field_table);
  500. $fkeys = $schema['foreign keys'];
  501. foreach ($items as $delta => $item) {
  502. $subject_id = $item[$field_table . '__subject_id'];
  503. $object_id = $item[ $field_table . '__object_id'];
  504. $type_id = $item[$field_table . '__type_id'];
  505. $type_name = $item['type_name'];
  506. $subject_name = $item['subject_name'];
  507. $object_name = $item['object_name'];
  508. // If the row is empty then skip this one, there's nothing to validate.
  509. if (!$type_name and !$subject_name and !$object_name) {
  510. continue;
  511. }
  512. // Get the subject ID.
  513. $subject_id = '';
  514. $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
  515. $matches = array();
  516. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  517. $subject_id = $matches[1];
  518. }
  519. else {
  520. $values = array('uniquename' => $subject_name);
  521. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  522. $subject_id = $subject[0]->$fkey_rcolumn;
  523. }
  524. // Get the object ID.
  525. $object_id = '';
  526. $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
  527. $matches = array();
  528. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  529. $object_id = $matches[1];
  530. }
  531. else {
  532. $values = array('uniquename' => $object_name);
  533. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  534. $object_id = $object[0]->$fkey_rcolumn;
  535. }
  536. // Set the IDs according to the values that were determined above.
  537. $items[$delta][$field_table . '__subject_id'] = $subject_id;
  538. $items[$delta][$field_table . '__object_id'] = $object_id;
  539. $items[$delta][$field_table . '__type_id'] = $type_name;
  540. $items[$delta][$field_table . '__rank'] = $item['_weight'];
  541. }
  542. }
  543. /**
  544. * @see TripalField::load()
  545. */
  546. public function load($entity, $details = array()) {
  547. $settings = $this->field['settings'];
  548. $record = $details['record'];
  549. $field_name = $this->field['field_name'];
  550. $field_type = $this->field['type'];
  551. $field_table = $this->field['settings']['chado_table'];
  552. $field_column = $this->field['settings']['chado_column'];
  553. $base_table = $this->field['settings']['base_table'];
  554. // Get the PKey for this table
  555. $schema = chado_get_schema($field_table);
  556. $pkey = $schema['primary key'][0];
  557. // Get the Pkeys for the subject and object tables
  558. $subject_fkey_table = '';
  559. $object_fkey_table = '';
  560. $fkeys = $schema['foreign keys'];
  561. foreach ($fkeys as $fktable => $details) {
  562. foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
  563. if ($fkey_lcolumn == 'subject_id') {
  564. $subject_fkey_table = $fktable;
  565. }
  566. if ($fkey_lcolumn == 'object_id') {
  567. $object_fkey_table = $fktable;
  568. }
  569. }
  570. }
  571. $subject_schema = chado_get_schema($subject_fkey_table);
  572. $object_schema = chado_get_schema($object_fkey_table);
  573. $subject_pkey = $subject_schema['primary key'][0];
  574. $object_pkey = $object_schema['primary key'][0];
  575. // Get the FK that links to the base record.
  576. $schema = chado_get_schema($field_table);
  577. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  578. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  579. // Set some defaults for the empty record.
  580. // TODO: don't hardcode the uniquename as all tables won't have that.
  581. $entity->{$field_name}['und'][0] = array(
  582. 'value' => array(),
  583. $field_table . '__' . $pkey => '',
  584. $field_table . '__subject_id' => '',
  585. $field_table . '__object_id' => '',
  586. $field_table . '__type_id' => '',
  587. // These elements don't need to follow the naming scheme above
  588. // becasue we don't need the chado_field_storage to try and
  589. // save these values.
  590. 'object_name' => '',
  591. 'subject_name' => '',
  592. 'type_name' => '',
  593. );
  594. // If the table has rank and value fields then add those to the default
  595. // value array.
  596. if (array_key_exists('value', $schema['fields'])) {
  597. $entity->{$field_name}['und'][0][$field_table . '__value'] = '';
  598. }
  599. if (array_key_exists('rank', $schema['fields'])) {
  600. $entity->{$field_name}['und'][0][$field_table . '__rank'] = '';
  601. }
  602. // If we have no record then just return.
  603. if (!$record) {
  604. return;
  605. }
  606. // Expand the object to include the relationships.
  607. $options = array(
  608. 'return_array' => 1,
  609. // we don't want to fully recurse we only need information about the
  610. // relationship type and the object and subject
  611. 'include_fk' => array(
  612. 'type_id' => 1,
  613. 'object_id' => array(
  614. 'type_id' => 1,
  615. ),
  616. 'subject_id' => array(
  617. 'type_id' => 1,
  618. ),
  619. ),
  620. );
  621. $rel_table = $base_table . '_relationship';
  622. $schema = chado_get_schema($rel_table);
  623. if (array_key_exists('rank', $schema['fields'])) {
  624. $options['order_by'] = array('rank' => 'ASC');
  625. }
  626. $record = chado_expand_var($record, 'table', $rel_table, $options);
  627. if (!$record->$rel_table) {
  628. return;
  629. }
  630. $srelationships = $record->$rel_table->subject_id;
  631. $orelationships = $record->$rel_table->object_id;
  632. $i = 0;
  633. if ($orelationships) {
  634. foreach ($orelationships as $relationship) {
  635. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  636. $rel_type = $relationship->type_id->name;
  637. $verb = self::get_rel_verb($rel_type);
  638. $subject_name = $relationship->subject_id->name;
  639. $subject_type = $relationship->subject_id->type_id->name;
  640. $object_name = $relationship->object_id->name;
  641. $object_type = $relationship->object_id->type_id->name;
  642. $entity->{$field_name}['und'][$i]['value'] = array(
  643. 'type' => $relationship->type_id->name,
  644. 'subject' => array(
  645. 'type' => $subject_type,
  646. 'name' => $subject_name,
  647. ),
  648. 'type' => $relationship->type_id->name,
  649. 'object' => array(
  650. 'type' => $object_type,
  651. 'name' => $object_name,
  652. 'entity' => 'TripalEntity:' . $entity->id,
  653. )
  654. );
  655. if (property_exists($relationship->subject_id, 'uniquename')) {
  656. $entity->{$field_name}['und'][$i]['value']['subject']['identifier'] = $relationship->subject_id->uniquename;;
  657. }
  658. if (property_exists($relationship->object_id, 'uniquename')) {
  659. $entity->{$field_name}['und'][$i]['value']['object']['identifier'] = $relationship->object_id->uniquename;
  660. }
  661. if (property_exists($relationship->subject_id, 'entity_id')) {
  662. $entity_id = $relationship->subject_id->entity_id;
  663. $entity->{$field_name}['und'][$i]['value']['subject']['entity'] = 'TripalEntity:' . $entity_id;
  664. }
  665. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  666. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
  667. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' this ' .
  668. $object_type . '.';
  669. $entity->{$field_name}['und'][$i]['semantic_web'] = array(
  670. 'type' => $rel_acc,
  671. 'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  672. 'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
  673. );
  674. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  675. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  676. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  677. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  678. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  679. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  680. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  681. if (array_key_exists('value', $schema['fields'])) {
  682. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  683. }
  684. if (array_key_exists('rank', $schema['fields'])) {
  685. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  686. }
  687. $i++;
  688. }
  689. }
  690. if ($srelationships) {
  691. foreach ($srelationships as $relationship) {
  692. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  693. $rel_type = $relationship->type_id->name;
  694. $verb = self::get_rel_verb($rel_type);
  695. $subject_name = $relationship->subject_id->name;
  696. $subject_type = $relationship->subject_id->type_id->name;
  697. $object_name = $relationship->object_id->name;
  698. $object_type = $relationship->object_id->type_id->name;
  699. $entity->{$field_name}['und'][$i]['value'] = array(
  700. '@type' => $relationship->type_id->name,
  701. 'subject' => array(
  702. 'type' => $subject_type,
  703. 'name' => $subject_name,
  704. 'entity' => 'TripalEntity:' . $entity->id,
  705. ),
  706. 'type' => $relationship->type_id->name,
  707. 'object' => array(
  708. 'type' => $object_type,
  709. 'name' => $object_name,
  710. )
  711. );
  712. if (property_exists($relationship->subject_id, 'uniquename')) {
  713. $entity->{$field_name}['und'][$i]['value']['subject']['identifier'] = $relationship->subject_id->uniquename;
  714. }
  715. if (property_exists($relationship->object_id, 'uniquename')) {
  716. $entity->{$field_name}['und'][$i]['value']['object']['identifier'] = $relationship->object_id->uniquename;
  717. }
  718. if (property_exists($relationship->object_id, 'entity_id')) {
  719. $entity_id = $relationship->object_id->entity_id;
  720. $entity->{$field_name}['und'][$i]['value']['object']['entity'] = 'TripalEntity:' . $entity_id;
  721. }
  722. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  723. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This ' .
  724. $subject_type . ' ' . $verb . ' ' . $rel_type_clean . ' the ' .
  725. $object_type . ', ' . $object_name . '.';
  726. $entity->{$field_name}['und'][$i]['semantic_web'] = array(
  727. 'type' => $rel_acc,
  728. 'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  729. 'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
  730. );
  731. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  732. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  733. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  734. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  735. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  736. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  737. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  738. if (array_key_exists('value', $schema['fields'])) {
  739. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  740. }
  741. if (array_key_exists('rank', $schema['fields'])) {
  742. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  743. }
  744. $i++;
  745. }
  746. }
  747. }
  748. /**
  749. * A helper function to define English verbs for relationship types.
  750. *
  751. * @param $rel_type
  752. * The vocabulary term name for the relationship.
  753. *
  754. * @return
  755. * The verb to use when creating a sentence of the relationship.
  756. */
  757. public static function get_rel_verb($rel_type) {
  758. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  759. $verb = $rel_type_clean;
  760. switch ($rel_type_clean) {
  761. case 'integral part of':
  762. case 'instance of':
  763. $verb = 'is an';
  764. break;
  765. case 'proper part of':
  766. case 'transformation of':
  767. case 'genome of':
  768. case 'part of':
  769. $verb = 'is a';
  770. case 'position of':
  771. case 'sequence of':
  772. case 'variant of':
  773. $verb = 'is a';
  774. break;
  775. case 'derives from':
  776. case 'connects on':
  777. case 'contains':
  778. case 'finishes':
  779. case 'guides':
  780. case 'has origin':
  781. case 'has part':
  782. case 'has quality':
  783. case 'is consecutive sequence of':
  784. case 'maximally overlaps':
  785. case 'overlaps':
  786. case 'starts':
  787. break;
  788. default:
  789. $verb = 'is';
  790. }
  791. return $verb;
  792. }
  793. /**
  794. * @see TripalField::instanceSettingsForm()
  795. */
  796. public function instanceSettingsForm() {
  797. $element = parent::instanceSettingsForm();
  798. $element['relationships'] = array(
  799. '#type' => 'fieldset',
  800. '#title' => 'Allowed Relationship Types',
  801. '#description' => t('There are three ways that relationship types
  802. can be limited for users who have permission to add new relationships.
  803. Please select the most appropriate for you use case. By default
  804. all vocabularies are provided to the user which allows use of any
  805. term for the relationship type.'),
  806. '#collapsed' => TRUE,
  807. '#collapsible' => TRUE,
  808. );
  809. // $element['instructions'] = array(
  810. // '#type' => 'item',
  811. // '#markup' => 'You may provide a list of terms that will be available in a select box
  812. // as the relationship types. This select box will replace the vocabulary select box if the
  813. // following value is set.'
  814. // );
  815. $vocs = tripal_get_cv_select_options();
  816. $element['relationships']['option1'] = array(
  817. '#type' => 'item',
  818. '#title' => 'Option #1',
  819. '#description' => t('Use this option to limit the vocabularies that a user .
  820. could use to specify relationship types. With this option any term in .
  821. the vocabulary can be used for the relationship type. You may select
  822. more than one vocabulary.'),
  823. );
  824. $element['relationships']['option1_vocabs'] = array(
  825. '#type' => 'select',
  826. '#multiple' => TRUE,
  827. '#options' => $vocs,
  828. '#size' => 6,
  829. '#default_value' => $this->instance['settings']['default_vocabs'],
  830. // TODO add ajax here so that the relationship autocomplete below works
  831. );
  832. $element['relationships']['option2'] = array(
  833. '#type' => 'item',
  834. '#title' => '<b>Option #2</b>',
  835. '#description' => 'Some vocabularies are heirarchichal (an ontology). Within this
  836. heirarchy groups of related terms typically fall under a common parent. If you
  837. wish to limit the list of terms that a user can use for the relationship type,
  838. you can provide the parent term here. Then, only that term\'s children will
  839. then be avilable for use as a relationship type.',
  840. );
  841. $element['relationships']['option2_vocab'] = array(
  842. '#type' => 'select',
  843. '#description' => 'Specify Default Vocabularies',
  844. '#multiple' => FALSE,
  845. '#options' => $vocs,
  846. '#default_value' => $this->instance['settings']['default_vocabs'],
  847. // TODO add ajax here so that the relationship autocomplete below works
  848. );
  849. $element['relationships']['option2_parent'] = array(
  850. '#type' => 'textfield',
  851. '#description' => 'Specify a Heirarchical Parent Term',
  852. '#default_value' => $this->instance['settings']['child_relationship_types'],
  853. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/"
  854. );
  855. $element['relationships']['option3'] = array(
  856. '#type' => 'item',
  857. '#title' => 'Option #3',
  858. '#description' => 'Provide terms separated by a new line. The term provided should be
  859. unique and distinguishable by the name.',
  860. );
  861. $element['relationships']['relationship_types'] = array(
  862. '#type' => 'textarea',
  863. '#default_value' => $this->instance['settings']['relationship_types'],
  864. );
  865. return $element;
  866. }
  867. /**
  868. * @see TripalField::instanceSettingsFormValidate()
  869. */
  870. public function instanceSettingsFormValidate($form, &$form_state) {
  871. //dpm($form);dpm($form_state);
  872. $settings = $form_state['values']['instance']['settings'];
  873. // Make sure the supplied types are valid cvterms
  874. if (isset($settings['relationship_types'])) {
  875. $rel_types = explode(PHP_EOL, $settings['relationship_types']);
  876. foreach($rel_types AS $type) {
  877. // Ignore empty lines
  878. if (trim($type) == '') {
  879. continue;
  880. }
  881. $term = tripal_get_cvterm(array('name' => trim($type)));
  882. // Don't save the form if a term can not be found or it matches more than one cvterm
  883. if (!isset($term->cvterm_id)) {
  884. form_set_error(
  885. "instance][settings][relationships][relationship_types",
  886. t("The term '@type' can not be found or matches more than one term.", array('@type' => $type))
  887. );
  888. }
  889. }
  890. }
  891. // Make sure child relationship types are using a valid cvterm
  892. if (isset($settings['child_relationship_types'])) {
  893. $supertype = $settings['child_relationship_types'];
  894. $term = tripal_get_cvterm(array('name' => trim($supertype)));
  895. if (!isset($term->cvterm_id)) {
  896. form_set_error(
  897. "instance][settings][relationships][child_relationship_types",
  898. t("The term '@type' can not be found or matches more than one term.", array('@type' => $supertype))
  899. );
  900. }
  901. }
  902. }
  903. }
  904. /**
  905. * Theme function for the chado_linker__relationship_widget.
  906. */
  907. function theme_chado_linker__relationship_widget($variables) {
  908. $element = $variables['element'];
  909. $field_name = $element['#field_name'];
  910. $field = field_info_field($field_name);
  911. $field_type = $field['type'];
  912. $field_table = $field['settings']['chado_table'];
  913. $field_column = $field['settings']['chado_column'];
  914. $layout = "
  915. <div class=\"chado-linker--relationship-widget\">
  916. <div class=\"chado-linker--relationship-widget-item\">" .
  917. drupal_render($element['subject_name']) . "
  918. </div>
  919. <div class=\"chado-linker--relationship-widget-item\">" .
  920. drupal_render($element['vocabulary']) . "
  921. </div>
  922. <div class=\"chado-linker--relationship-widget-item\">" .
  923. drupal_render($element['type_name']) . "
  924. </div>
  925. <div class=\"chado-linker--relationship-widget-item\">" .
  926. drupal_render($element['object_name']) . "
  927. </div>
  928. </div>
  929. ";
  930. return $layout;
  931. }
  932. /**
  933. * An Ajax callback for the dbxref widget.
  934. */
  935. function chado_linker__relationship_widget_form_ajax_callback(&$form, $form_state) {
  936. // Get the triggering element
  937. $form_element_name = $form_state['triggering_element']['#name'];
  938. preg_match('/(.+?)\[(.+?)\]\[(.+?)\]/', $form_element_name, $matches);
  939. $field = $matches[1];
  940. $lang = $matches[2];
  941. $delta = $matches[3];
  942. // Return the widget that triggered the AJAX call
  943. if (isset($form[$field][$lang][$delta])) {
  944. return $form[$field][$lang][$delta];
  945. }
  946. // Alternatively, return the default value widget for the widget setting form
  947. else {
  948. return $form['instance']['default_value_widget'][$field];
  949. }
  950. }