chado_linker__relationship.inc 51 KB

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