chado_linker__relationship.inc 48 KB

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