chado_linker__relationship_widget.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. class chado_linker__relationship_widget extends TripalFieldWidget {
  3. // The default lable for this field.
  4. public static $label = 'Relationships';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('chado_linker__relationship');
  7. /**
  8. *
  9. * @see TripalFieldWidget::form()
  10. */
  11. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  12. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  13. // Get the field settings.
  14. $entity = $form['#entity'];
  15. $field_name = $this->field['field_name'];
  16. $field_type = $this->field['type'];
  17. $field_table = $this->field['settings']['chado_table'];
  18. $field_column = $this->field['settings']['chado_column'];
  19. $base_table = $this->field['settings']['base_table'];
  20. // Get the FK column that links to the base table.
  21. $base_table = $this->field['settings']['base_table'];
  22. $schema = chado_get_schema($field_table);
  23. $pkey = $schema['primary key'][0];
  24. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  25. $fkey = $fkeys[0];
  26. // Get the instance settings. There are three options for how this widget
  27. // will be displayed. Those are controlled in the instance settings
  28. // of the field.
  29. // Option 1: relationship types are limited to a specific vocabulary.
  30. // Option 2: relationship types are limited to a subset of one vocabulary.
  31. // Option 3: relationship types are limited to a predefined set.
  32. $instance = $this->instance;
  33. $settings = '';
  34. $option1_vocabs = '';
  35. $option2_parent = '';
  36. $option2_vocab = '';
  37. $option3_rtypes = '';
  38. if (array_key_exists('relationships', $instance)) {
  39. $settings = $instance['settings']['relationships'];
  40. $option1_vocabs = $settings['option1_vocabs'];
  41. $option2_vocab = $settings['option2_vocab'];
  42. $option2_parent = $settings['option2_parent'];
  43. $option3_rtypes = $settings['relationship_types'];
  44. }
  45. // For testing if there are selected vocabs for option1 we'll copy the
  46. // contents in a special variable for later.
  47. $option1_test = $option1_vocabs;
  48. // Get the field defaults.
  49. $record_id = '';
  50. $subject_id = '';
  51. $object_id = '';
  52. $type_id = '';
  53. $value = '';
  54. $rank = '';
  55. $subject_uniquename = '';
  56. $object_uniquename = '';
  57. $type = '';
  58. // Handle special cases
  59. $subject_id_key = 'subject_id';
  60. $object_id_key = 'object_id';
  61. if ($field_table == 'nd_reagent_relationship') {
  62. $subject_id_key = 'subject_reagent_id';
  63. $object_id_key = 'object_reagent_id';
  64. }
  65. else if ($field_table == 'project_relationship') {
  66. $subject_id_key = 'subject_project_id';
  67. $object_id_key = 'object_project_id';
  68. }
  69. // If the field already has a value then it will come through the $items
  70. // array. This happens when editing an existing record.
  71. if (count($items) > 0 and array_key_exists($delta, $items)) {
  72. // Check for element values that correspond to fields in the Chado table.
  73. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  74. $subject_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $subject_id_key, $subject_id);
  75. $object_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $object_id_key, $object_id);
  76. $type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__type_id', $type_id);
  77. // Not all Chado tables have a value and rank. So we'll only get
  78. // those if applicable.
  79. if (array_key_exists('value', $schema['fields'])) {
  80. $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__value', $value);
  81. }
  82. if (array_key_exists('rank', $schema['fields'])) {
  83. $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
  84. }
  85. // Get element values added to help support insert/updates.
  86. $object_uniquename = tripal_get_field_item_keyval($items, $delta, 'object_name', $object_uniquename);
  87. $subject_uniquename = tripal_get_field_item_keyval($items, $delta, 'subject_name', $subject_uniquename);
  88. $type = tripal_get_field_item_keyval($items, $delta, 'type_name', $type);
  89. }
  90. // Check $form_state['values'] to see if an AJAX call set the values.
  91. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  92. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  93. $subject_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key];
  94. $object_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $object_id_key];
  95. $type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
  96. if (array_key_exists('value', $schema['fields'])) {
  97. $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
  98. }
  99. if (array_key_exists('rank', $schema['fields'])) {
  100. $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
  101. }
  102. $object_uniquename = $form_state['values'][$field_name]['und'][$delta]['object_name'];
  103. $subject_uniquename = $form_state['values'][$field_name]['und'][$delta]['subject_name'];
  104. $type = $form_state['values'][$field_name]['und'][$delta]['type_name'];
  105. }
  106. //$widget['#element_validate'] = array('chado_linker__relationship_validate');
  107. $widget['#theme'] = 'chado_linker__relationship_widget';
  108. $widget['#prefix'] = "<span id='$field_table-$delta'>";
  109. $widget['#suffix'] = "</span>";
  110. $widget['value'] = array(
  111. '#type' => 'value',
  112. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  113. );
  114. $widget['chado-' . $field_table . '__' . $pkey] = array(
  115. '#type' => 'value',
  116. '#default_value' => $record_id,
  117. );
  118. $widget['chado-' . $field_table . '__' . $subject_id_key] = array(
  119. '#type' => 'value',
  120. '#default_value' => $subject_id,
  121. );
  122. $widget['chado-' . $field_table . '__type_id'] = array(
  123. '#type' => 'value',
  124. '#default_value' => $type_id,
  125. );
  126. $widget['chado-' . $field_table . '__' . $object_id_key] = array(
  127. '#type' => 'value',
  128. '#default_value' => $object_id,
  129. );
  130. if (array_key_exists('value', $schema['fields'])) {
  131. $widget['chado-' . $field_table . '__value'] = array(
  132. '#type' => 'value',
  133. '#default_value' => $value,
  134. );
  135. }
  136. if (array_key_exists('rank', $schema['fields'])) {
  137. $widget['chado-' . $field_table . '__rank'] = array(
  138. '#type' => 'value',
  139. '#default_value' => $rank,
  140. );
  141. }
  142. $widget['subject_name'] = array(
  143. '#type' => 'textfield',
  144. '#title' => t('Subject'),
  145. '#default_value' => $subject_uniquename,
  146. '#required' => $element['#required'],
  147. '#maxlength' => array_key_exists('length', $schema['fields'][$subject_id_key]) ? $schema['fields'][$subject_id_key]['length'] : 255,
  148. '#size' => 35,
  149. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  150. );
  151. // Getting default values for the relationship type element.
  152. $default_voc = '';
  153. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
  154. $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
  155. }
  156. $default_term = '';
  157. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
  158. $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
  159. }
  160. $default_type_id = $type_id;
  161. if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
  162. $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
  163. }
  164. // Option 3: Custom list of Relationship Types
  165. $rtype_options = array();
  166. $rtype_options[] = 'Select a Type';
  167. if ($option3_rtypes) {
  168. $rtypes = explode(PHP_EOL, $option3_rtypes);
  169. foreach($rtypes AS $rtype) {
  170. // Ignore empty lines
  171. if (trim($rtype) == '') {
  172. continue;
  173. }
  174. $term = tripal_get_cvterm(array('name' => trim($rtype)));
  175. // Try to get term with vocabulary specified
  176. if (!$term) {
  177. $tmp = explode('|', trim($rtype), 2);
  178. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  179. $rtype = trim($tmp[1]);
  180. $term = tripal_get_cvterm(array('name' => $rtype, 'cv_id' => $cv->cv_id));
  181. }
  182. $rtype_options[$term->cvterm_id] = $term->name;
  183. }
  184. $widget['type_id'] = array(
  185. '#type' => 'select',
  186. '#title' => t('Relationship Type'),
  187. '#options' => $rtype_options,
  188. '#default_value' => $default_type_id,
  189. );
  190. if ($type_id && !key_exists($type_id, $rtype_options)) {
  191. 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');
  192. }
  193. }
  194. // Option 2: Child terms of a selected cvterm
  195. else if ($option2_vocab) {
  196. $values = array(
  197. 'cv_id' => $option2_vocab,
  198. 'name' => $option2_parent
  199. );
  200. $parent_term = tripal_get_cvterm($values);
  201. // If the term wasn't found then see if it's a synonym.
  202. if(!$parent_term) {
  203. $values = array(
  204. 'synonym' => array(
  205. 'name' => trim($option2_parent),
  206. )
  207. );
  208. $synonym = tripal_get_cvterm($values);
  209. if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
  210. $parent_term = $synonym;
  211. }
  212. }
  213. // Get the child terms of the parent term found above.
  214. $sql = "
  215. SELECT subject_id,
  216. (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
  217. FROM {cvtermpath}
  218. WHERE
  219. object_id = :parent_cvterm_id AND
  220. cv_id = :parent_cv_id
  221. ORDER BY name
  222. ";
  223. $args = array(
  224. ':parent_cvterm_id' => $parent_term->cvterm_id,
  225. ':parent_cv_id' => $parent_term->cv_id->cv_id
  226. );
  227. $results = chado_query($sql, $args);
  228. while($child = $results->fetchObject()) {
  229. $rtype_options[$child->subject_id] = $child->name;
  230. }
  231. $widget['type_id'] = array(
  232. '#type' => 'select',
  233. '#title' => t('Relationship Type'),
  234. '#options' => $rtype_options,
  235. '#default_value' => $default_type_id,
  236. );
  237. if ($type_id && !key_exists($type_id, $rtype_options)) {
  238. 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');
  239. }
  240. }
  241. // Option 1: All terms of selected vocabularies
  242. else if ($option1_test && array_pop($option1_test)) {
  243. $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
  244. $results = chado_query($sql, array(':cv_id' => $option1_vocabs));
  245. while ($obj = $results->fetchObject()) {
  246. $rtype_options[$obj->cvterm_id] = $obj->name;
  247. }
  248. $widget['type_id'] = array(
  249. '#type' => 'select',
  250. '#title' => t('Relationship Type'),
  251. '#options' => $rtype_options,
  252. '#default_value' => $default_type_id,
  253. );
  254. if ($type_id && !key_exists($type_id, $rtype_options)) {
  255. 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');
  256. }
  257. }
  258. // Default option:
  259. else {
  260. // Set up available cvterms for selection
  261. $vocs = array(0 => 'Select a vocabulary');
  262. $vocs = tripal_get_cv_select_options();
  263. $cv_id = isset($form_state['values'][$field_name]['und'][0]['vocabulary']) ? $form_state['values'][$field_name]['und'][0]['vocabulary'] : 0;
  264. // Try getting the cv_id from cvterm for existing records
  265. if (!$cv_id && $type_id) {
  266. $cvterm = tripal_get_cvterm(array('cvterm_id' => $type_id));
  267. if (isset($cvterm->cv_id->cv_id)) {
  268. $cv_id = $cvterm->cv_id->cv_id;
  269. $default_term = $cvterm->name;
  270. }
  271. }
  272. if (!$cv_id) {
  273. $cv_id = $default_voc;
  274. }
  275. $widget['vocabulary'] = array(
  276. '#type' => 'select',
  277. '#title' => t('Vocabulary'),
  278. '#options' => $vocs,
  279. '#required' => $element['#required'],
  280. '#default_value' => $cv_id,
  281. '#ajax' => array(
  282. 'callback' => "chado_linker__relationship_widget_form_ajax_callback",
  283. 'wrapper' => "$field_table-$delta",
  284. 'effect' => 'fade',
  285. 'method' => 'replace'
  286. ),
  287. );
  288. if ($cv_id) {
  289. $options = array();
  290. $widget['type_name'] = array(
  291. '#type' => 'textfield',
  292. '#title' => t('Relationship Type'),
  293. '#size' => 15,
  294. '#default_value' => $default_term,
  295. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id"
  296. );
  297. }
  298. }
  299. $widget['object_name'] = array(
  300. '#type' => 'textfield',
  301. '#title' => t('Object'),
  302. '#default_value' => $object_uniquename,
  303. '#required' => $element['#required'],
  304. '#maxlength' => array_key_exists('length', $schema['fields'][$object_id_key]) ? $schema['fields'][$object_id_key]['length'] : 255,
  305. '#size' => 35,
  306. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  307. );
  308. }
  309. /**
  310. * Performs validation of the widgetForm.
  311. *
  312. * Use this validate to ensure that form values are entered correctly. Note
  313. * this is different from the validate() function which ensures that the
  314. * field data meets expectations.
  315. *
  316. * @param $form
  317. * @param $form_state
  318. */
  319. public function validate($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  320. $field_name = $this->field['field_name'];
  321. $field_type = $this->field['type'];
  322. $field_table = $this->field['settings']['chado_table'];
  323. $field_column = $this->field['settings']['chado_column'];
  324. $base_table = $this->field['settings']['base_table'];
  325. $schema = chado_get_schema($field_table);
  326. $fkeys = $schema['foreign keys'];
  327. // Handle special cases
  328. $subject_id_key = 'subject_id';
  329. $object_id_key = 'object_id';
  330. if ($field_table == 'nd_reagent_relationship') {
  331. $subject_id_key = 'subject_reagent_id';
  332. $object_id_key = 'object_reagent_id';
  333. }
  334. else if ($field_table == 'project_relationship') {
  335. $subject_id_key = 'subject_project_id';
  336. $object_id_key = 'object_project_id';
  337. }
  338. foreach ($items as $delta => $item) {
  339. $subject_id = $item['chado' . $field_table . '__' . $subject_id_key];
  340. $object_id = $item['chado' . $field_table . '__' . $object_id_key];
  341. $type_id = $item['chado' . $field_table . '__type_id'];
  342. $type_id = isset($item['type_id']) ? $item['chado' . $field_table . '__type_id'] : $type_id;
  343. $type_name = isset($item['type_name']) ? $item['type_name'] : '';
  344. $subject_name = $item['subject_name'];
  345. $object_name = $item['object_name'];
  346. // If the row is empty then just continue, there's nothing to validate.
  347. if (!$type_id and !$type_name and !$subject_name and !$object_name) {
  348. continue;
  349. }
  350. // Make sure we have values for all of the fields.
  351. $form_error = FALSE;
  352. if (!$type_name && !$type_id) {
  353. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  354. 'error' => 'chado_linker__relationship',
  355. 'message' => t("Please provide the type of relationship."),
  356. );
  357. }
  358. if ($entity and !$subject_name) {
  359. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  360. 'error' => 'chado_linker__relationship',
  361. 'message' => t("Please provide the subject of the relationship."),
  362. );
  363. }
  364. if ($entity and !$object_name) {
  365. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  366. 'error' => 'chado_linker__relationship',
  367. 'message' => t("Please provide the object of the relationship."),
  368. );
  369. }
  370. if ($form_error) {
  371. continue;
  372. }
  373. // Before submitting this form we need to make sure that our subject_id and
  374. // object_ids are real records. There are two ways to get the record, either
  375. // just with the text value or with an [id: \d+] string embedded. If the
  376. // later we will pull it out.
  377. $subject_id = '';
  378. $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
  379. $matches = array();
  380. if ($entity) {
  381. if(preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  382. $subject_id = $matches[1];
  383. $values = array($fkey_rcolumn => $subject_id);
  384. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  385. if (count($subject) == 0) {
  386. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  387. 'error' => 'chado_linker__relationship',
  388. 'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
  389. );
  390. }
  391. }
  392. else {
  393. $values = array('uniquename' => $subject_name);
  394. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  395. if (count($subject) == 0) {
  396. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  397. 'error' => 'chado_linker__relationship',
  398. 'message' => t("The subject record cannot be found. Please check spelling."),
  399. );
  400. }
  401. elseif (count($subject) > 1) {
  402. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  403. 'error' => 'chado_linker__relationship',
  404. 'message' => t("The subject is not unique and therefore the relationship cannot be made."),
  405. );
  406. }
  407. }
  408. }
  409. // Now check for a matching object.
  410. $object_id = '';
  411. $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
  412. $matches = array();
  413. if ($entity) {
  414. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  415. $object_id = $matches[1];
  416. $values = array($fkey_rcolumn => $object_id);
  417. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  418. if (count($subject) == 0) {
  419. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  420. 'error' => 'chado_linker__relationship',
  421. 'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
  422. );
  423. }
  424. }
  425. else {
  426. $values = array('uniquename' => $object_name);
  427. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  428. if (count($object) == 0) {
  429. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  430. 'error' => 'chado_linker__relationship',
  431. 'message' => t("The object record cannot be found. Please check spelling."),
  432. );;
  433. }
  434. elseif (count($object) > 1) {
  435. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  436. 'error' => 'chado_linker__relationship',
  437. 'message' => t("The object is not unique and therefore the relationship cannot be made."),
  438. );
  439. }
  440. }
  441. }
  442. // Make sure that either our object or our subject refers to the base record.
  443. if ($entity) {
  444. $chado_record_id = $entity->chado_record_id;
  445. if ($object_id != $chado_record_id and $subject_id != $chado_record_id) {
  446. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  447. 'error' => 'chado_linker__relationship',
  448. 'message' => t("Either the subject or the object in the relationship must refer to this record."),
  449. );
  450. }
  451. // Make sure that the object and subject are not both the same thing.
  452. if ($object_id == $subject_id) {
  453. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  454. 'error' => 'chado_linker__relationship',
  455. 'message' => t("The subject and the object in the relationship cannot both refer to the same record."),
  456. );
  457. }
  458. }
  459. }
  460. }
  461. /**
  462. *
  463. * @see TripalFieldWidget::submit()
  464. */
  465. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  466. $field_name = $this->field['field_name'];
  467. $field_type = $this->field['type'];
  468. $field_table = $this->field['settings']['chado_table'];
  469. $field_column = $this->field['settings']['chado_column'];
  470. $base_table = $this->field['settings']['base_table'];
  471. $chado_record_id = $entity->chado_record_id;
  472. $schema = chado_get_schema($field_table);
  473. $fkeys = $schema['foreign keys'];
  474. // Handle special cases
  475. $subject_id_key = 'subject_id';
  476. $object_id_key = 'object_id';
  477. if ($field_table == 'nd_reagent_relationship') {
  478. $subject_id_key = 'subject_reagent_id';
  479. $object_id_key = 'object_reagent_id';
  480. }
  481. else if ($field_table == 'project_relationship') {
  482. $subject_id_key = 'subject_project_id';
  483. $object_id_key = 'object_project_id';
  484. }
  485. $type_name = array_key_exists('type_name', $item) ? $item['type_name'] : '';
  486. $subject_id = $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $subject_id_key];
  487. $object_id = $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $object_id_key];
  488. $type_id = $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__type_id'];
  489. $subject_name = $form_state['values'][$field_name][$langcode][$delta]['subject_name'];
  490. $object_name = $form_state['values'][$field_name][$langcode][$delta]['object_name'];
  491. // If the row is empty then skip this one, there's nothing to validate.
  492. if (!($type_id or !$type_name) and !$subject_name and !$object_name) {
  493. return;
  494. }
  495. // Get the subject ID.
  496. $subject_id = '';
  497. $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
  498. $matches = array();
  499. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  500. $subject_id = $matches[1];
  501. }
  502. else {
  503. $values = array('uniquename' => $subject_name);
  504. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  505. $subject_id = $subject[0]->$fkey_rcolumn;
  506. }
  507. // Get the object ID.
  508. $object_id = '';
  509. $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
  510. $matches = array();
  511. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  512. $object_id = $matches[1];
  513. }
  514. else {
  515. $values = array('uniquename' => $object_name);
  516. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  517. $object_id = $object[0]->$fkey_rcolumn;
  518. }
  519. // Set the IDs according to the values that were determined above.
  520. $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $subject_id_key] = $subject_id;
  521. $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $object_id_key] = $object_id;
  522. $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__type_id'] = $type_name;
  523. $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__rank'] = $item['_weight'];
  524. }
  525. }
  526. /**
  527. * Theme function for the chado_linker__relationship_widget.
  528. */
  529. function theme_chado_linker__relationship_widget($variables) {
  530. $element = $variables['element'];
  531. $field_name = $element['#field_name'];
  532. $field = field_info_field($field_name);
  533. $field_type = $field['type'];
  534. $field_table = $field['settings']['chado_table'];
  535. $field_column = $field['settings']['chado_column'];
  536. $layout = "
  537. <div class=\"chado-linker--relationship-widget\">
  538. <div class=\"chado-linker--relationship-widget-item\">" .
  539. drupal_render($element['subject_name']) . "
  540. </div>
  541. <div class=\"chado-linker--relationship-widget-item\">" .
  542. drupal_render($element['vocabulary']) . "
  543. </div>
  544. <div class=\"chado-linker--relationship-widget-item\">" .
  545. drupal_render($element['type_name']) . "
  546. </div>
  547. <div class=\"chado-linker--relationship-widget-item\">" .
  548. drupal_render($element['type_id']) . "
  549. </div>
  550. <div class=\"chado-linker--relationship-widget-item\">" .
  551. drupal_render($element['object_name']) . "
  552. </div>
  553. </div>
  554. ";
  555. return $layout;
  556. }
  557. function theme_chado_linker__relationship_instance_settings ($variables) {
  558. $element = $variables['element'];
  559. $option1 = $element['option1'];
  560. $option1_vocabs = $element['option1_vocabs'];
  561. $option2 = $element['option2'];
  562. $option2_vocab = $element['option2_vocab'];
  563. $option2_parent = $element['option2_parent'];
  564. $option3 = $element['option3'];
  565. $rtype = $element['relationship_types'];
  566. $layout = drupal_render($option1);
  567. $layout .= drupal_render($option1_vocabs);
  568. $layout .=
  569. drupal_render($option2) .
  570. "<div class=\"chado-linker--relationship-instance-settings-option2\">" .
  571. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  572. drupal_render($option2_vocab) .
  573. "</div>" .
  574. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  575. drupal_render($option2_parent) .
  576. "</div>" .
  577. "</div>";
  578. $layout .= drupal_render($option3);
  579. $layout .= drupal_render($rtype);
  580. return $layout;
  581. }
  582. /**
  583. * An Ajax callback for the relationshp widget.
  584. */
  585. function chado_linker__relationship_widget_form_ajax_callback(&$form, $form_state) {
  586. // Get the triggering element
  587. $form_element_name = $form_state['triggering_element']['#name'];
  588. preg_match('/(.+?)\[(.+?)\]\[(.+?)\]/', $form_element_name, $matches);
  589. $field = $matches[1];
  590. $lang = $matches[2];
  591. $delta = $matches[3];
  592. // Return the widget that triggered the AJAX call
  593. if (isset($form[$field][$lang][$delta])) {
  594. return $form[$field][$lang][$delta];
  595. }
  596. // Alternatively, return the default value widget for the widget setting form
  597. else {
  598. return $form['instance']['default_value_widget'][$field];
  599. }
  600. }
  601. /**
  602. * An Ajax callback for the relationshp instance setting form.
  603. */
  604. function chado_linker__relationship_instance_settings_form_ajax_callback(&$form, &$form_state) {
  605. $acpath = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'];
  606. $acpath .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'] . '/';
  607. $urlval = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'];
  608. $urlval .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'];
  609. // Reset value if a different vocabulary is selected
  610. $form['instance']['settings']['relationships']['option2_parent']['#value'] = NULL;
  611. $form_state['values']['instance']['settings']['relationships']['option2_parent'] = NULL;
  612. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'] = $acpath;
  613. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'] = $urlval;
  614. return $form['instance']['settings']['relationships']['option2_parent'];
  615. }