chado_linker__relationship.inc 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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 globalSettingsForm()
  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. // Provide a list of instance specific settings. These can be access within
  18. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  19. // then Drupal with automatically change these settings for the instnace.
  20. // It is recommended to put settings at the instance level whenever possible.
  21. public static $default_instance_settings = array(
  22. 'relationships' => array(
  23. 'option1_vocabs' => '',
  24. 'option2_vocab' => '',
  25. 'option2_parent' => '',
  26. 'relationship_types' => '',
  27. ),
  28. );
  29. // Set this to the name of the storage backend that by default will support
  30. // this field.
  31. public static $default_storage = 'field_chado_storage';
  32. /**
  33. * @see TripalField::formatterView()
  34. */
  35. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  36. // Get the settings
  37. $settings = $display['settings'];
  38. $rows = array();
  39. $headers = array('Subject' ,'Type', 'Object');
  40. $headers = array('Relationship');
  41. foreach ($items as $delta => $item) {
  42. if (!$item['value']) {
  43. continue;
  44. }
  45. $subject_name = $item['value']['subject']['name'];
  46. $subject_type = $item['value']['subject']['type'];
  47. $object_name = $item['value']['object']['name'];
  48. $object_type = $item['value']['object']['type'];
  49. $phrase = $item['value']['phrase'];
  50. // Handle some special cases.
  51. // For mRNA objects we don't want to show the CDS, exons, 5' UTR, etc.
  52. // we want to show the parent gene and the protein.
  53. if ($object_type == 'mRNA' and ($subject_type != 'polypeptide')) {
  54. continue;
  55. }
  56. if ($subject_type == 'mRNA' and ($object_type != 'gene')) {
  57. continue;
  58. }
  59. $phrase = preg_replace("/$subject_type/", "<b>$subject_type</b>", $phrase);
  60. $phrase = preg_replace("/$object_type/", "<b>$object_type</b>", $phrase);
  61. if (array_key_exists('entity', $item['value']['object'])) {
  62. list($entity_type, $object_entity_id) = explode(':', $item['value']['object']['entity']);
  63. if ($object_entity_id != $entity->id) {
  64. $link = l($object_name, 'bio_data/' . $object_entity_id);
  65. $phrase = preg_replace("/$object_name/", $link, $phrase);
  66. }
  67. }
  68. if (array_key_exists('entity', $item['value']['subject'])) {
  69. list($entity_type, $subject_entity_id) = explode(':', $item['value']['subject']['entity']);
  70. if ($subject_entity_id != $entity->id) {
  71. $link = l($subject_name, 'bio_data/' . $subject_entity_id);
  72. $phrase = preg_replace("/$subject_name/", $link, $phrase);
  73. }
  74. }
  75. $rows[] = array($phrase);
  76. }
  77. // the $table array contains the headers and rows array as well as other
  78. // options for controlling the display of the table. Additional
  79. // documentation can be found here:
  80. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  81. $table = array(
  82. 'header' => $headers,
  83. 'rows' => $rows,
  84. 'attributes' => array(
  85. 'id' => 'chado-linker--relationship-table',
  86. 'class' => 'tripal-data-table'
  87. ),
  88. 'sticky' => FALSE,
  89. 'caption' => '',
  90. 'colgroups' => array(),
  91. 'empty' => 'There are no relationships',
  92. );
  93. // once we have our table array structure defined, we call Drupal's theme_table()
  94. // function to generate the table.
  95. if (count($items) > 0) {
  96. $element[0] = array(
  97. '#type' => 'markup',
  98. '#markup' => theme_table($table),
  99. );
  100. }
  101. }
  102. /**
  103. * @see TripalField::widgetForm()
  104. */
  105. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  106. parent::widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
  107. $field_name = $this->field['field_name'];
  108. $field_type = $this->field['type'];
  109. $field_table = $this->field['settings']['chado_table'];
  110. $field_column = $this->field['settings']['chado_column'];
  111. // Get the instance settings
  112. $instance = $this->instance;
  113. $settings = $instance['settings']['relationships'];
  114. $option1_vocabs = $settings['option1_vocabs'];
  115. $option2_vocab = $settings['option2_vocab'];
  116. $option2_parent = $settings['option2_parent'];
  117. $option3_rtypes = $settings['relationship_types'];
  118. // For testing if there are selected vocabs for option1 we'll copy the
  119. // contents in a special variable for later.
  120. $option1_test = $option1_vocabs;
  121. // Get the FK column that links to the base table.
  122. $chado_table = $this->field['settings']['chado_table'];
  123. $base_table = $this->field['settings']['base_table'];
  124. $schema = chado_get_schema($chado_table);
  125. $pkey = $schema['primary key'][0];
  126. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  127. $fkey = $fkeys[0];
  128. // Get the field defaults.
  129. $record_id = '';
  130. $fkey_value = $element['#entity'] ? $element['#entity']->chado_record_id : '';
  131. $subject_id = '';
  132. $subject_uniquename = '';
  133. $type_id = '';
  134. $type = '';
  135. $object_id = '';
  136. $object_uniquename = '';
  137. $value = '';
  138. $rank = '';
  139. // Handle special cases
  140. $subject_id_key = 'subject_id';
  141. $object_id_key = 'object_id';
  142. if ($chado_table == 'organism_relationship') {
  143. $subject_id_key = 'subject_organism_id';
  144. $object_id_key = 'object_organism_id';
  145. }
  146. else if ($chado_table == 'nd_reagent_relationship') {
  147. $subject_id_key = 'subject_reagent_id';
  148. $object_id_key = 'object_reagent_id';
  149. }
  150. else if ($chado_table == 'project_relationship') {
  151. $subject_id_key = 'subject_project_id';
  152. $object_id_key = 'object_project_id';
  153. }
  154. // If the field already has a value then it will come through the $items
  155. // array. This happens when editing an existing record.
  156. if (array_key_exists($delta, $items)) {
  157. $record_id = isset($items[$delta][$field_table . '__' . $pkey]) ? $items[$delta][$field_table . '__' . $pkey] : '';
  158. $subject_id = isset($items[$delta][$field_table . '__' . $subject_id_key]) ? $items[$delta][$field_table . '__' . $subject_id_key] : '';
  159. $type_id = isset($items[$delta][$field_table . '__type_id']) ? $items[$delta][$field_table . '__type_id'] : '';
  160. $object_id = isset($items[$delta][$field_table . '__' . $object_id_key]) ? $items[$delta][$field_table . '__' . $object_id_key] : '';
  161. if (isset($items[$delta][$field_table . '__value'])) {
  162. $value = $items[$delta][$field_table . '__value'];
  163. }
  164. if (isset($items[$delta][$field_table . '__rank'])) {
  165. $rank = $items[$delta][$field_table . '__rank'];
  166. }
  167. $object_uniquename = isset($items[$delta]['object_name']) ? $items[$delta]['object_name'] : '';
  168. $subject_uniquename = isset($items[$delta]['subject_name']) ? $items[$delta]['subject_name'] : '';
  169. $type = isset($items[$delta]['type_name']) ? $items[$delta]['type_name'] : '';
  170. }
  171. // Check $form_state['values'] to see if an AJAX call set the values.
  172. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  173. // $record_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__' . $pkey);
  174. // $subject_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__subject_id');
  175. // $subject_uniquename = tripal_chado_get_field_form_values($field_name, $form_state, $delta, 'subject_name');
  176. // $type_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__type_id');
  177. // $type = tripal_chado_get_field_form_values($field_name, $form_state, $delta, 'type_name');
  178. // $object_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__object_id');
  179. // $object_uniquename = tripal_chado_get_field_form_values($field_name, $form_state, $delta, 'object_name');
  180. // $value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__value');
  181. // $rank = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__rank');
  182. }
  183. $widget['#table_name'] = $chado_table;
  184. $widget['#fkeys'] = $schema['foreign keys'];
  185. $widget['#base_table'] = $base_table;
  186. $widget['#chado_record_id'] = isset($form['#entity']) ? $form['#entity']->chado_record_id : '';
  187. //$widget['#element_validate'] = array('chado_linker__relationship_validate');
  188. $widget['#theme'] = 'chado_linker__relationship_widget';
  189. $widget['#prefix'] = "<span id='$chado_table-$delta'>";
  190. $widget['#suffix'] = "</span>";
  191. $widget['value'] = array(
  192. '#type' => 'value',
  193. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  194. );
  195. $widget[$field_table . '__' . $pkey] = array(
  196. '#type' => 'value',
  197. '#default_value' => $record_id,
  198. );
  199. $widget[$field_table . '__' . $subject_id_key] = array(
  200. '#type' => 'value',
  201. '#default_value' => $subject_id,
  202. );
  203. $widget[$field_table . '__type_id'] = array(
  204. '#type' => 'value',
  205. '#default_value' => $type_id,
  206. );
  207. $widget[$field_table . '__' . $object_id_key] = array(
  208. '#type' => 'value',
  209. '#default_value' => $object_id,
  210. );
  211. if (array_key_exists('value', $schema['fields'])) {
  212. $widget[$field_table . '__value'] = array(
  213. '#type' => 'value',
  214. '#default_value' => $value,
  215. );
  216. }
  217. if (array_key_exists('rank', $schema['fields'])) {
  218. $widget[$field_table . '__rank'] = array(
  219. '#type' => 'value',
  220. '#default_value' => $rank,
  221. );
  222. }
  223. $widget['subject_name'] = array(
  224. '#type' => 'textfield',
  225. '#title' => t('Subject'),
  226. '#default_value' => $subject_uniquename,
  227. '#required' => $element['#required'],
  228. '#maxlength' => array_key_exists('length', $schema['fields'][$subject_id_key]) ? $schema['fields'][$subject_id_key]['length'] : 255,
  229. '#size' => 35,
  230. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  231. );
  232. // Getting default values
  233. $default_voc = '';
  234. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
  235. $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
  236. }
  237. $default_term = '';
  238. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
  239. $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
  240. }
  241. $default_type_id = $type_id;
  242. if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
  243. $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
  244. }
  245. // Option 3: Custom list of Relationship Types
  246. $rtype_options = array();
  247. $rtype_options[] = 'Select a Type';
  248. if ($option3_rtypes) {
  249. $rtypes = explode(PHP_EOL, $option3_rtypes);
  250. foreach($rtypes AS $rtype) {
  251. // Ignore empty lines
  252. if (trim($rtype) == '') {
  253. continue;
  254. }
  255. $term = tripal_get_cvterm(array('name' => trim($rtype)));
  256. // Try to get term with vocabulary specified
  257. if (!$term) {
  258. $tmp = explode('|', trim($rtype), 2);
  259. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  260. $rtype = trim($tmp[1]);
  261. $term = tripal_get_cvterm(array('name' => $rtype, 'cv_id' => $cv->cv_id));
  262. }
  263. $rtype_options[$term->cvterm_id] = $term->name;
  264. }
  265. $widget['type_id'] = array(
  266. '#type' => 'select',
  267. '#title' => t('Relationship Type'),
  268. '#options' => $rtype_options,
  269. '#default_value' => $default_type_id,
  270. );
  271. if ($type_id && !key_exists($type_id, $rtype_options)) {
  272. form_set_error($this->field['field_name'] . '[' . $langcode . '][' . $delta . '][type_id]', 'Illegal option detected for Relationship Type. Please contact site administrator to fix the problem');
  273. }
  274. }
  275. // Option 2: Child terms of a selected cvterm
  276. else if ($option2_vocab) {
  277. $values = array(
  278. 'cv_id' => $option2_vocab,
  279. 'name' => $option2_parent
  280. );
  281. $parent_term = tripal_get_cvterm($values);
  282. // If the term wasn't found then see if it's a synonym.
  283. if(!$parent_term) {
  284. $values = array(
  285. 'synonym' => array(
  286. 'name' => trim($option2_parent),
  287. )
  288. );
  289. $synonym = tripal_get_cvterm($values);
  290. if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
  291. $parent_term = $synonym;
  292. }
  293. }
  294. // Get the child terms of the parent term found above.
  295. $sql =
  296. "SELECT
  297. subject_id,
  298. (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
  299. FROM {cvtermpath}
  300. WHERE
  301. object_id = :parent_cvterm_id
  302. AND
  303. cv_id = :parent_cv_id
  304. ORDER BY name
  305. ";
  306. $args = array(
  307. ':parent_cvterm_id' => $parent_term->cvterm_id,
  308. ':parent_cv_id' => $parent_term->cv_id->cv_id
  309. );
  310. $results = chado_query($sql, $args);
  311. while($child = $results->fetchObject()) {
  312. $rtype_options[$child->subject_id] = $child->name;
  313. }
  314. $widget['type_id'] = array(
  315. '#type' => 'select',
  316. '#title' => t('Relationship Type'),
  317. '#options' => $rtype_options,
  318. '#default_value' => $default_type_id,
  319. );
  320. if ($type_id && !key_exists($type_id, $rtype_options)) {
  321. form_set_error($this->field['field_name'] . '[' . $langcode . '][' . $delta . '][type_id]', 'Illegal option detected for Relationship Type. Please contact site administrator to fix the problem');
  322. }
  323. }
  324. // Option 1: All terms of selected vocabularies
  325. else if ($option1_test && array_pop($option1_test)) {
  326. $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
  327. $results = chado_query($sql, array(':cv_id' => $option1_vocabs));
  328. while ($obj = $results->fetchObject()) {
  329. $rtype_options[$obj->cvterm_id] = $obj->name;
  330. }
  331. $widget['type_id'] = array(
  332. '#type' => 'select',
  333. '#title' => t('Relationship Type'),
  334. '#options' => $rtype_options,
  335. '#default_value' => $default_type_id,
  336. );
  337. if ($type_id && !key_exists($type_id, $rtype_options)) {
  338. form_set_error($this->field['field_name'] . '[' . $langcode . '][' . $delta . '][type_id]', 'Illegal option detected for Relationship Type. Please contact site administrator to fix the problem');
  339. }
  340. }
  341. // Default option:
  342. else {
  343. // Set up available cvterms for selection
  344. $vocs = array(0 => 'Select a vocabulary');
  345. $vocs = tripal_get_cv_select_options();
  346. $cv_id = isset($form_state['values'][$field_name]['und'][0]['vocabulary']) ? $form_state['values'][$field_name]['und'][0]['vocabulary'] : 0;
  347. // Try getting the cv_id from cvterm for existing records
  348. if (!$cv_id && $type_id) {
  349. $cvterm = tripal_get_cvterm(array('cvterm_id' => $type_id));
  350. if (isset($cvterm->cv_id->cv_id)) {
  351. $cv_id = $cvterm->cv_id->cv_id;
  352. $default_term = $cvterm->name;
  353. }
  354. }
  355. if (!$cv_id) {
  356. $cv_id = $default_voc;
  357. }
  358. $widget['vocabulary'] = array(
  359. '#type' => 'select',
  360. '#title' => t('Vocabulary'),
  361. '#options' => $vocs,
  362. '#required' => $element['#required'],
  363. '#default_value' => $cv_id,
  364. '#ajax' => array(
  365. 'callback' => "chado_linker__relationship_widget_form_ajax_callback",
  366. 'wrapper' => "$chado_table-$delta",
  367. 'effect' => 'fade',
  368. 'method' => 'replace'
  369. ),
  370. );
  371. if ($cv_id) {
  372. $options = array();
  373. $widget['type_name'] = array(
  374. '#type' => 'textfield',
  375. '#title' => t('Relationship Type'),
  376. '#size' => 15,
  377. '#default_value' => $default_term,
  378. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id"
  379. );
  380. }
  381. }
  382. $widget['object_name'] = array(
  383. '#type' => 'textfield',
  384. '#title' => t('Object'),
  385. '#default_value' => $object_uniquename,
  386. '#required' => $element['#required'],
  387. '#maxlength' => array_key_exists('length', $schema['fields'][$object_id_key]) ? $schema['fields'][$object_id_key]['length'] : 255,
  388. '#size' => 35,
  389. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  390. );
  391. }
  392. /**
  393. * @see TripalField::validate()
  394. */
  395. function validate($entity_type, $entity, $langcode, $items, &$errors) {
  396. $field_name = $this->field['field_name'];
  397. $field_type = $this->field['type'];
  398. $field_table = $this->field['settings']['chado_table'];
  399. $field_column = $this->field['settings']['chado_column'];
  400. $base_table = $this->field['settings']['base_table'];
  401. $schema = chado_get_schema($field_table);
  402. $fkeys = $schema['foreign keys'];
  403. // Handle special cases
  404. $subject_id_key = 'subject_id';
  405. $object_id_key = 'object_id';
  406. if ($field_table == 'organism_relationship') {
  407. $subject_id_key = 'subject_organism_id';
  408. $object_id_key = 'object_organism_id';
  409. }
  410. else if ($field_table == 'nd_reagent_relationship') {
  411. $subject_id_key = 'subject_reagent_id';
  412. $object_id_key = 'object_reagent_id';
  413. }
  414. else if ($field_table == 'project_relationship') {
  415. $subject_id_key = 'subject_project_id';
  416. $object_id_key = 'object_project_id';
  417. }
  418. foreach ($items as $delta => $item) {
  419. $subject_id = $item[$field_table . '__' . $subject_id_key];
  420. $object_id = $item[ $field_table . '__' . $object_id_key];
  421. $type_id = $item[$field_table . '__type_id'];
  422. $type_id = isset($item['type_id']) ? $item['type_id'] : $type_id;
  423. $type_name = isset($item['type_name']) ? $item['type_name'] : '';
  424. $subject_name = $item['subject_name'];
  425. $object_name = $item['object_name'];
  426. // If the row is empty then just continue, there's nothing to validate.
  427. if (!$type_id and !$type_name and !$subject_name and !$object_name) {
  428. continue;
  429. }
  430. // Make sure we have values for all of the fields.
  431. $form_error = FALSE;
  432. if (!$type_name && !$type_id) {
  433. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  434. 'error' => 'chado_linker__relationship',
  435. 'message' => t("Please provide the type of relationship."),
  436. );
  437. }
  438. if ($entity and !$subject_name) {
  439. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  440. 'error' => 'chado_linker__relationship',
  441. 'message' => t("Please provide the subject of the relationship."),
  442. );
  443. }
  444. if ($entity and !$object_name) {
  445. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  446. 'error' => 'chado_linker__relationship',
  447. 'message' => t("Please provide the object of the relationship."),
  448. );
  449. }
  450. if ($form_error) {
  451. continue;
  452. }
  453. // Before submitting this form we need to make sure that our subject_id and
  454. // object_ids are real values. There are two ways to get the value, either
  455. // just with the text value or with an [id: \d+] string embedded. If the
  456. // later we will pull it out.
  457. $subject_id = '';
  458. $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
  459. $matches = array();
  460. if ($entity) {
  461. if(preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  462. $subject_id = $matches[1];
  463. $values = array($fkey_rcolumn => $subject_id);
  464. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  465. if (count($subject) == 0) {
  466. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  467. 'error' => 'chado_linker__relationship',
  468. 'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
  469. );
  470. }
  471. }
  472. else {
  473. $values = array('uniquename' => $subject_name);
  474. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  475. if (count($subject) == 0) {
  476. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  477. 'error' => 'chado_linker__relationship',
  478. 'message' => t("The subject record cannot be found. Please check spelling."),
  479. );
  480. }
  481. elseif (count($subject) > 1) {
  482. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  483. 'error' => 'chado_linker__relationship',
  484. 'message' => t("The subject is not unique and therefore the relationship cannot be made."),
  485. );
  486. }
  487. }
  488. }
  489. // Now check for a matching object.
  490. $object_id = '';
  491. $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
  492. $matches = array();
  493. if ($entity) {
  494. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  495. $object_id = $matches[1];
  496. $values = array($fkey_rcolumn => $object_id);
  497. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  498. if (count($subject) == 0) {
  499. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  500. 'error' => 'chado_linker__relationship',
  501. 'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
  502. );
  503. }
  504. }
  505. else {
  506. $values = array('uniquename' => $object_name);
  507. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  508. if (count($object) == 0) {
  509. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  510. 'error' => 'chado_linker__relationship',
  511. 'message' => t("The object record cannot be found. Please check spelling."),
  512. );;
  513. }
  514. elseif (count($object) > 1) {
  515. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  516. 'error' => 'chado_linker__relationship',
  517. 'message' => t("The object is not unique and therefore the relationship cannot be made."),
  518. );
  519. }
  520. }
  521. }
  522. // Make sure that either our object or our subject refers to the base record.
  523. if ($entity) {
  524. $chado_record_id = $entity->chado_record_id;
  525. if ($object_id != $chado_record_id and $subject_id != $chado_record_id) {
  526. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  527. 'error' => 'chado_linker__relationship',
  528. 'message' => t("Either the subject or the object in the relationship must refer to this record."),
  529. );
  530. }
  531. // Make sure that the object and subject are not both the same thing.
  532. if ($object_id == $subject_id) {
  533. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  534. 'error' => 'chado_linker__relationship',
  535. 'message' => t("The subject and the object in the relationship cannot both refer to the same record."),
  536. );
  537. }
  538. }
  539. }
  540. }
  541. /**
  542. * @see TripalField::submit()
  543. */
  544. public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  545. return;
  546. $field_name = $this->field['field_name'];
  547. $field_type = $this->field['type'];
  548. $field_table = $this->field['settings']['chado_table'];
  549. $field_column = $this->field['settings']['chado_column'];
  550. $base_table = $this->field['settings']['base_table'];
  551. $chado_record_id = $entity->chado_record_id;
  552. $schema = chado_get_schema($field_table);
  553. $fkeys = $schema['foreign keys'];
  554. $type_name = array_key_exists('type_name', $item) ? $item['type_name'] : '';
  555. $subject_id = $form_state['values'][$field_name][$langcode][$delta][$field_table . '__subject_id'];
  556. $object_id = $form_state['values'][$field_name][$langcode][$delta][ $field_table . '__object_id'];
  557. $type_id = $form_state['values'][$field_name][$langcode][$delta][$field_table . '__type_id'];
  558. $subject_name = $form_state['values'][$field_name][$langcode][$delta]['subject_name'];
  559. $object_name = $form_state['values'][$field_name][$langcode][$delta]['object_name'];
  560. // If the row is empty then skip this one, there's nothing to validate.
  561. if (!($type_id or !$type_name) and !$subject_name and !$object_name) {
  562. return;
  563. }
  564. // Get the subject ID.
  565. $subject_id = '';
  566. $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
  567. $matches = array();
  568. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  569. $subject_id = $matches[1];
  570. }
  571. else {
  572. $values = array('uniquename' => $subject_name);
  573. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  574. $subject_id = $subject[0]->$fkey_rcolumn;
  575. }
  576. // Get the object ID.
  577. $object_id = '';
  578. $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
  579. $matches = array();
  580. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  581. $object_id = $matches[1];
  582. }
  583. else {
  584. $values = array('uniquename' => $object_name);
  585. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  586. $object_id = $object[0]->$fkey_rcolumn;
  587. }
  588. // Set the IDs according to the values that were determined above.
  589. $form_state['values'][$field_name][$langcode][$delta][$field_table . '__subject_id'] = $subject_id;
  590. $form_state['values'][$field_name][$langcode][$delta][$field_table . '__object_id'] = $object_id;
  591. $form_state['values'][$field_name][$langcode][$delta][$field_table . '__type_id'] = $type_name;
  592. $form_state['values'][$field_name][$langcode][$delta][$field_table . '__rank'] = $item['_weight'];
  593. }
  594. /**
  595. * @see TripalField::load()
  596. */
  597. public function load($entity, $details = array()) {
  598. $settings = $this->field['settings'];
  599. $record = $details['record'];
  600. $field_name = $this->field['field_name'];
  601. $field_type = $this->field['type'];
  602. $field_table = $this->field['settings']['chado_table'];
  603. $field_column = $this->field['settings']['chado_column'];
  604. $base_table = $this->field['settings']['base_table'];
  605. // Get the PKey for this table
  606. $schema = chado_get_schema($field_table);
  607. $pkey = $schema['primary key'][0];
  608. // Get the Pkeys for the subject and object tables
  609. $subject_fkey_table = '';
  610. $object_fkey_table = '';
  611. $fkeys = $schema['foreign keys'];
  612. foreach ($fkeys as $fktable => $details) {
  613. foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
  614. if ($fkey_lcolumn == 'subject_id') {
  615. $subject_fkey_table = $fktable;
  616. }
  617. if ($fkey_lcolumn == 'object_id') {
  618. $object_fkey_table = $fktable;
  619. }
  620. }
  621. }
  622. $subject_schema = chado_get_schema($subject_fkey_table);
  623. $object_schema = chado_get_schema($object_fkey_table);
  624. $subject_pkey = $subject_schema['primary key'][0];
  625. $object_pkey = $object_schema['primary key'][0];
  626. // Get the FK that links to the base record.
  627. $schema = chado_get_schema($field_table);
  628. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  629. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  630. // Set some defaults for the empty record.
  631. $entity->{$field_name}['und'][0] = array(
  632. 'value' => array(),
  633. $field_table . '__' . $pkey => '',
  634. $field_table . '__subject_id' => '',
  635. $field_table . '__object_id' => '',
  636. $field_table . '__type_id' => '',
  637. // These elements don't need to follow the naming scheme above
  638. // becasue we don't need the chado_field_storage to try and
  639. // save these values.
  640. 'object_name' => '',
  641. 'subject_name' => '',
  642. 'type_name' => '',
  643. );
  644. // If the table has rank and value fields then add those to the default
  645. // value array.
  646. if (array_key_exists('value', $schema['fields'])) {
  647. $entity->{$field_name}['und'][0][$field_table . '__value'] = '';
  648. }
  649. if (array_key_exists('rank', $schema['fields'])) {
  650. $entity->{$field_name}['und'][0][$field_table . '__rank'] = '';
  651. }
  652. // If we have no record then just return.
  653. if (!$record) {
  654. return;
  655. }
  656. // Expand the object to include the relationships.
  657. $options = array(
  658. 'return_array' => 1,
  659. // we don't want to fully recurse we only need information about the
  660. // relationship type and the object and subject
  661. 'include_fk' => array(
  662. 'type_id' => 1,
  663. 'object_id' => array(
  664. 'type_id' => 1,
  665. ),
  666. 'subject_id' => array(
  667. 'type_id' => 1,
  668. ),
  669. ),
  670. );
  671. $rel_table = $base_table . '_relationship';
  672. $schema = chado_get_schema($rel_table);
  673. if (array_key_exists('rank', $schema['fields'])) {
  674. $options['order_by'] = array('rank' => 'ASC');
  675. }
  676. $record = chado_expand_var($record, 'table', $rel_table, $options);
  677. if (!$record->$rel_table) {
  678. return;
  679. }
  680. $srelationships = null;
  681. $orelationships = null;
  682. if ($rel_table == 'organism_relationship') {
  683. $srelationships = $record->$rel_table->subject_organism_id;
  684. $orelationships = $record->$rel_table->object_organism_id;
  685. }
  686. else if ($rel_table == 'nd_reagent_relationship') {
  687. $srelationships = $record->$rel_table->subject_reagent_id;
  688. $orelationships = $record->$rel_table->object_reagent_id;
  689. }
  690. else if ($rel_table == 'project_relationship') {
  691. $srelationships = $record->$rel_table->subject_project_id;
  692. $orelationships = $record->$rel_table->object_project_id;
  693. }
  694. else {
  695. $srelationships = $record->$rel_table->subject_id;
  696. $orelationships = $record->$rel_table->object_id;
  697. }
  698. $i = 0;
  699. if ($orelationships) {
  700. foreach ($orelationships as $relationship) {
  701. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  702. $rel_type = $relationship->type_id->name;
  703. $verb = self::get_rel_verb($rel_type);
  704. $subject_name = $relationship->subject_id->name;
  705. $subject_type = $relationship->subject_id->type_id->name;
  706. $object_name = $relationship->object_id->name;
  707. $object_type = $relationship->object_id->type_id->name;
  708. $entity->{$field_name}['und'][$i]['value'] = array(
  709. 'type' => $relationship->type_id->name,
  710. 'subject' => array(
  711. 'type' => $subject_type,
  712. 'name' => $subject_name,
  713. ),
  714. 'type' => $relationship->type_id->name,
  715. 'object' => array(
  716. 'type' => $object_type,
  717. 'name' => $object_name,
  718. 'entity' => 'TripalEntity:' . $entity->id,
  719. )
  720. );
  721. if (property_exists($relationship->subject_id, 'uniquename')) {
  722. $entity->{$field_name}['und'][$i]['value']['subject']['identifier'] = $relationship->subject_id->uniquename;;
  723. }
  724. if (property_exists($relationship->object_id, 'uniquename')) {
  725. $entity->{$field_name}['und'][$i]['value']['object']['identifier'] = $relationship->object_id->uniquename;
  726. }
  727. if (property_exists($relationship->subject_id, 'entity_id')) {
  728. $entity_id = $relationship->subject_id->entity_id;
  729. $entity->{$field_name}['und'][$i]['value']['subject']['entity'] = 'TripalEntity:' . $entity_id;
  730. }
  731. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  732. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
  733. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' this ' .
  734. $object_type . '.';
  735. $entity->{$field_name}['und'][$i]['semantic_web'] = array(
  736. 'type' => $rel_acc,
  737. 'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  738. 'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
  739. );
  740. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  741. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  742. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  743. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  744. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  745. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  746. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  747. if (array_key_exists('value', $schema['fields'])) {
  748. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  749. }
  750. if (array_key_exists('rank', $schema['fields'])) {
  751. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  752. }
  753. $i++;
  754. }
  755. }
  756. if ($srelationships) {
  757. foreach ($srelationships as $relationship) {
  758. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  759. $rel_type = $relationship->type_id->name;
  760. $verb = self::get_rel_verb($rel_type);
  761. $subject_name = $relationship->subject_id->name;
  762. $subject_type = $relationship->subject_id->type_id->name;
  763. $object_name = $relationship->object_id->name;
  764. $object_type = $relationship->object_id->type_id->name;
  765. $entity->{$field_name}['und'][$i]['value'] = array(
  766. '@type' => $relationship->type_id->name,
  767. 'subject' => array(
  768. 'type' => $subject_type,
  769. 'name' => $subject_name,
  770. 'entity' => 'TripalEntity:' . $entity->id,
  771. ),
  772. 'type' => $relationship->type_id->name,
  773. 'object' => array(
  774. 'type' => $object_type,
  775. 'name' => $object_name,
  776. )
  777. );
  778. if (property_exists($relationship->subject_id, 'uniquename')) {
  779. $entity->{$field_name}['und'][$i]['value']['subject']['identifier'] = $relationship->subject_id->uniquename;
  780. }
  781. if (property_exists($relationship->object_id, 'uniquename')) {
  782. $entity->{$field_name}['und'][$i]['value']['object']['identifier'] = $relationship->object_id->uniquename;
  783. }
  784. if (property_exists($relationship->object_id, 'entity_id')) {
  785. $entity_id = $relationship->object_id->entity_id;
  786. $entity->{$field_name}['und'][$i]['value']['object']['entity'] = 'TripalEntity:' . $entity_id;
  787. }
  788. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  789. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This ' .
  790. $subject_type . ' ' . $verb . ' ' . $rel_type_clean . ' the ' .
  791. $object_type . ', ' . $object_name . '.';
  792. $entity->{$field_name}['und'][$i]['semantic_web'] = array(
  793. 'type' => $rel_acc,
  794. 'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  795. 'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
  796. );
  797. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  798. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  799. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  800. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  801. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  802. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  803. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  804. if (array_key_exists('value', $schema['fields'])) {
  805. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  806. }
  807. if (array_key_exists('rank', $schema['fields'])) {
  808. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  809. }
  810. $i++;
  811. }
  812. }
  813. }
  814. /**
  815. * A helper function to define English verbs for relationship types.
  816. *
  817. * @param $rel_type
  818. * The vocabulary term name for the relationship.
  819. *
  820. * @return
  821. * The verb to use when creating a sentence of the relationship.
  822. */
  823. public static function get_rel_verb($rel_type) {
  824. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  825. $verb = $rel_type_clean;
  826. switch ($rel_type_clean) {
  827. case 'integral part of':
  828. case 'instance of':
  829. $verb = 'is an';
  830. break;
  831. case 'proper part of':
  832. case 'transformation of':
  833. case 'genome of':
  834. case 'part of':
  835. $verb = 'is a';
  836. case 'position of':
  837. case 'sequence of':
  838. case 'variant of':
  839. $verb = 'is a';
  840. break;
  841. case 'derives from':
  842. case 'connects on':
  843. case 'contains':
  844. case 'finishes':
  845. case 'guides':
  846. case 'has origin':
  847. case 'has part':
  848. case 'has quality':
  849. case 'is consecutive sequence of':
  850. case 'maximally overlaps':
  851. case 'overlaps':
  852. case 'starts':
  853. break;
  854. default:
  855. $verb = 'is';
  856. }
  857. return $verb;
  858. }
  859. /**
  860. * @see TripalField::instanceSettingsForm()
  861. */
  862. public function instanceSettingsForm() {
  863. $element = parent::instanceSettingsForm();
  864. //$element = parent::instanceSettingsForm();
  865. $element['relationships'] = array(
  866. '#type' => 'fieldset',
  867. '#title' => 'Allowed Relationship Types',
  868. '#description' => t('There are three ways that relationship types
  869. can be limited for users who have permission to add new relationships.
  870. Please select the most appropriate for you use case. By default
  871. all vocabularies are provided to the user which allows use of any
  872. term for the relationship type.'),
  873. '#collapsed' => TRUE,
  874. '#collapsible' => TRUE,
  875. '#theme' => 'chado_linker__relationship_instance_settings'
  876. );
  877. // $element['instructions'] = array(
  878. // '#type' => 'item',
  879. // '#markup' => 'You may provide a list of terms that will be available in a select box
  880. // as the relationship types. This select box will replace the vocabulary select box if the
  881. // following value is set.'
  882. // );
  883. $vocs = tripal_get_cv_select_options();
  884. $element['relationships']['option1'] = array(
  885. '#type' => 'item',
  886. '#title' => 'Option #1',
  887. '#description' => t('Use this option to limit the vocabularies that a user .
  888. could use to specify relationship types. With this option any term in .
  889. the vocabulary can be used for the relationship type. You may select
  890. more than one vocabulary.'),
  891. );
  892. $element['relationships']['option1_vocabs'] = array(
  893. '#type' => 'select',
  894. '#multiple' => TRUE,
  895. '#options' => $vocs,
  896. '#size' => 6,
  897. '#default_value' => $this->instance['settings']['relationships']['option1_vocabs'],
  898. // TODO add ajax here so that the relationship autocomplete below works
  899. );
  900. $element['relationships']['option2'] = array(
  901. '#type' => 'item',
  902. '#title' => '<b>Option #2</b>',
  903. '#description' => 'Some vocabularies are heirarchichal (an ontology). Within this
  904. heirarchy groups of related terms typically fall under a common parent. If you
  905. wish to limit the list of terms that a user can use for the relationship type,
  906. you can provide the parent term here. Then, only that term\'s children will
  907. be avilable for use as a relationship type.',
  908. );
  909. $element['relationships']['option2_vocab'] = array(
  910. '#type' => 'select',
  911. '#description' => 'Specify Default Vocabulary',
  912. '#multiple' => FALSE,
  913. '#options' => $vocs,
  914. '#default_value' => $this->instance['settings']['relationships']['option2_vocab'],
  915. '#ajax' => array(
  916. 'callback' => "chado_linker__relationship_instance_settings_form_ajax_callback",
  917. 'wrapper' => 'relationships-option2-parent',
  918. 'effect' => 'fade',
  919. 'method' => 'replace'
  920. ),
  921. );
  922. $element['relationships']['option2_parent'] = array(
  923. '#type' => 'textfield',
  924. '#description' => 'Specify a Heirarchical Parent Term',
  925. '#default_value' => $this->instance['settings']['relationships']['option2_parent'],
  926. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  927. '#prefix' => '<div id=relationships-option2-parent>',
  928. '#suffix' => '</div>'
  929. );
  930. $element['relationships']['option3'] = array(
  931. '#type' => 'item',
  932. '#title' => 'Option #3',
  933. '#description' => 'Provide terms separated by a new line. The term provided should be
  934. unique and distinguishable by the name. You can use a bar | to separate a vocabulary
  935. and a term to allow more specific assignment.',
  936. );
  937. $element['relationships']['relationship_types'] = array(
  938. '#type' => 'textarea',
  939. '#default_value' => $this->instance['settings']['relationships']['relationship_types'],
  940. );
  941. return $element;
  942. }
  943. /**
  944. * @see TripalField::instanceSettingsFormValidate()
  945. */
  946. public function instanceSettingsFormValidate($form, &$form_state) {
  947. // Get relationships settings
  948. $settings = $form_state['values']['instance']['settings']['relationships'];
  949. $form_state['values']['instance']['settings']['relationships']['relationship_types']= trim($settings['relationship_types']);
  950. // Make sure only one option is selected
  951. $option1test = $settings['option1_vocabs'];
  952. $option1 = isset($settings['option1_vocabs']) && array_pop($option1test);
  953. $option2 = (isset($settings['option2_vocab']) && $settings['option2_vocab']) || $settings['option2_parent'];
  954. $option3 = isset($settings['relationship_types']) && trim($settings['relationship_types']);
  955. if ($option1 && ($option2 || $option3) == 1 ||
  956. $option2 && ($option1 || $option3) == 1 ||
  957. $option3 && ($option1 || $option2) == 1
  958. ) {
  959. form_set_error(
  960. "instance][settings][relationships",
  961. t("Only one option is allowed to limit the relationship types.")
  962. );
  963. return;
  964. }
  965. // For option3, make sure the supplied types are valid cvterms
  966. if ($option3) {
  967. $rel_types = explode(PHP_EOL, $settings['relationship_types']);
  968. foreach($rel_types AS $type) {
  969. $type = trim($type);
  970. // Ignore empty lines
  971. if ($type == '') {
  972. continue;
  973. }
  974. // Find the matching cvterm
  975. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name";
  976. $results = chado_query($sql, array(':name' => $type));
  977. $terms = array();
  978. while ($obj = $results->fetchObject()) {
  979. $terms[] = $obj;
  980. }
  981. // Don't save the form if a term can not be found or it matches more than one cvterm
  982. $cv = '';
  983. if (count($terms) == 0) {
  984. // If a term can not be found, maybe the type contains '|', parse it as 'vocabulary|cvterm'
  985. if (strpos($type, '|')) {
  986. $tmp = explode('|', $type, 2);
  987. $type = trim($tmp[1]);
  988. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  989. if($cv) {
  990. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name AND cv_id = :cv_id";
  991. $results = chado_query($sql, array(':name' => $type, ':cv_id' => $cv->cv_id));
  992. while ($obj = $results->fetchObject()) {
  993. $terms[] = $obj;
  994. }
  995. }
  996. else {
  997. $cv = $tmp[0];
  998. }
  999. }
  1000. if (count($terms) != 1) {
  1001. $message = "The term '@type' can not be found.";
  1002. $token = array('@type' => $type);
  1003. if ($cv) {
  1004. $message = "The term '@type' can not be found within the vocabulary '@vocab'.";
  1005. $token['@vocab'] = $cv;
  1006. }
  1007. form_set_error(
  1008. "instance][settings][relationships][relationship_types",
  1009. t($message, $token)
  1010. );
  1011. }
  1012. }
  1013. else if (count($terms) > 1) {
  1014. // If a type matches more than one term, parse it as 'vocabulary|cvterm' and try again
  1015. if (strpos($type, '|')) {
  1016. $tmp = explode('|', $type, 2);
  1017. $type = trim($tmp[1]);
  1018. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  1019. if ($cv) {
  1020. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name AND cv_id = :cv_id";
  1021. $results = chado_query($sql, array(':name' => $type, ':cv_id' => $cv->cv_id));
  1022. while ($obj = $results->fetchObject()) {
  1023. $terms[] = $obj;
  1024. }
  1025. }
  1026. }
  1027. if(count($terms) != 1) {
  1028. form_set_error(
  1029. "instance][settings][relationships][relationship_types",
  1030. t("The term '@type' matches more than one term. Please specify its vocabulary in
  1031. the format of 'vocabulary|@type'.", array('@type' => $type))
  1032. );
  1033. }
  1034. }
  1035. }
  1036. }
  1037. // For option2: Make sure the parent term is a valid cvterm
  1038. if ($option2) {
  1039. $cv_id = $settings['option2_vocab'];
  1040. $supertype = $settings['option2_parent'];
  1041. $term = tripal_get_cvterm(array(
  1042. 'name' => trim($supertype),
  1043. 'cv_id' => $cv_id,
  1044. ));
  1045. // Tripal cv autocomplete also allow cvterm synonyms, if the parent term doesn't match
  1046. // a cvterm, try cvtermsynonym
  1047. if (!$term) {
  1048. $synonym = tripal_get_cvterm(
  1049. array(
  1050. 'synonym' => array(
  1051. 'name' => trim($supertype),
  1052. )
  1053. )
  1054. );
  1055. if ($synonym && $synonym->cv_id->cv_id == $cv_id) {
  1056. $term = $synonym;
  1057. }
  1058. }
  1059. if (!isset($term->cvterm_id)) {
  1060. form_set_error(
  1061. "instance][settings][relationships][option2_parent",
  1062. t("The term '@type' is not a valid term for the vocabulary selected.", array('@type' => $supertype))
  1063. );
  1064. }
  1065. }
  1066. }
  1067. }
  1068. /**
  1069. * Theme function for the chado_linker__relationship_widget.
  1070. */
  1071. function theme_chado_linker__relationship_widget($variables) {
  1072. $element = $variables['element'];
  1073. $field_name = $element['#field_name'];
  1074. $field = field_info_field($field_name);
  1075. $field_type = $field['type'];
  1076. $field_table = $field['settings']['chado_table'];
  1077. $field_column = $field['settings']['chado_column'];
  1078. $layout = "
  1079. <div class=\"chado-linker--relationship-widget\">
  1080. <div class=\"chado-linker--relationship-widget-item\">" .
  1081. drupal_render($element['subject_name']) . "
  1082. </div>
  1083. <div class=\"chado-linker--relationship-widget-item\">" .
  1084. drupal_render($element['vocabulary']) . "
  1085. </div>
  1086. <div class=\"chado-linker--relationship-widget-item\">" .
  1087. drupal_render($element['type_name']) . "
  1088. </div>
  1089. <div class=\"chado-linker--relationship-widget-item\">" .
  1090. drupal_render($element['type_id']) . "
  1091. </div>
  1092. <div class=\"chado-linker--relationship-widget-item\">" .
  1093. drupal_render($element['object_name']) . "
  1094. </div>
  1095. </div>
  1096. ";
  1097. return $layout;
  1098. }
  1099. function theme_chado_linker__relationship_instance_settings ($variables) {
  1100. $element = $variables['element'];
  1101. $option1 = $element['option1'];
  1102. $option1_vocabs = $element['option1_vocabs'];
  1103. $option2 = $element['option2'];
  1104. $option2_vocab = $element['option2_vocab'];
  1105. $option2_parent = $element['option2_parent'];
  1106. $option3 = $element['option3'];
  1107. $rtype = $element['relationship_types'];
  1108. $layout = drupal_render($option1);
  1109. $layout .= drupal_render($option1_vocabs);
  1110. $layout .=
  1111. drupal_render($option2) .
  1112. "<div class=\"chado-linker--relationship-instance-settings-option2\">" .
  1113. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  1114. drupal_render($option2_vocab) .
  1115. "</div>" .
  1116. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  1117. drupal_render($option2_parent) .
  1118. "</div>" .
  1119. "</div>";
  1120. $layout .= drupal_render($option3);
  1121. $layout .= drupal_render($rtype);
  1122. return $layout;
  1123. }
  1124. /**
  1125. * An Ajax callback for the relationshp widget.
  1126. */
  1127. function chado_linker__relationship_widget_form_ajax_callback(&$form, $form_state) {
  1128. // Get the triggering element
  1129. $form_element_name = $form_state['triggering_element']['#name'];
  1130. preg_match('/(.+?)\[(.+?)\]\[(.+?)\]/', $form_element_name, $matches);
  1131. $field = $matches[1];
  1132. $lang = $matches[2];
  1133. $delta = $matches[3];
  1134. // Return the widget that triggered the AJAX call
  1135. if (isset($form[$field][$lang][$delta])) {
  1136. return $form[$field][$lang][$delta];
  1137. }
  1138. // Alternatively, return the default value widget for the widget setting form
  1139. else {
  1140. return $form['instance']['default_value_widget'][$field];
  1141. }
  1142. }
  1143. /**
  1144. * An Ajax callback for the relationshp instance setting form.
  1145. */
  1146. function chado_linker__relationship_instance_settings_form_ajax_callback(&$form, &$form_state) {
  1147. $acpath = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'];
  1148. $acpath .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'] . '/';
  1149. $urlval = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'];
  1150. $urlval .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'];
  1151. // Reset value if a different vocabulary is selected
  1152. $form['instance']['settings']['relationships']['option2_parent']['#value'] = NULL;
  1153. $form_state['values']['instance']['settings']['relationships']['option2_parent'] = NULL;
  1154. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'] = $acpath;
  1155. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'] = $urlval;
  1156. return $form['instance']['settings']['relationships']['option2_parent'];
  1157. }