sbo__relationship_widget.inc 27 KB

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