sbo__relationship_widget.inc 26 KB

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