sbo__relationship_widget.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php
  2. class sbo__relationship_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_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. $field_name = $this->field['field_name'];
  15. $field_type = $this->field['type'];
  16. $field_table = $this->instance['settings']['chado_table'];
  17. $field_column = $this->instance['settings']['chado_column'];
  18. $base_table = $this->instance['settings']['base_table'];
  19. // @debug dpm($this, 'this');
  20. // Get the FK column that links to the base table.
  21. $base_table = $this->instance['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. // 'nd_reagent_relationship' and 'project_relationship' have different column names from
  59. // subject_id/object_id. Do a pattern matching to get the column names.
  60. $subject_id_key = 'subject_id';
  61. $object_id_key = 'object_id';
  62. foreach ($schema['foreign keys'][$base_table]['columns'] AS $lcolum => $rcolum) {
  63. if (preg_match('/^subject_.*id/', $lcolum)) {
  64. $subject_id_key = $lcolum;
  65. }
  66. else if (preg_match('/^object_.*id/', $lcolum)) {
  67. $object_id_key = $lcolum;
  68. }
  69. }
  70. $widget['#subject_id_key'] = $subject_id_key;
  71. $widget['#object_id_key'] = $object_id_key;
  72. // If the field already has a value then it will come through the $items
  73. // array. This happens when editing an existing record.
  74. if (count($items) > 0 and array_key_exists($delta, $items)) {
  75. // Sometimes empty/initialized items are getting through.
  76. // To determine if it this one of them, the type_id must always be there.
  77. $type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__type_id', $type_id);
  78. if (!empty($type_id)) {
  79. // Check for element values that correspond to fields in the Chado table.
  80. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  81. $subject_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $subject_id_key, $subject_id);
  82. $object_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $object_id_key, $object_id);
  83. $type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__type_id', $type_id);
  84. // Not all Chado tables have a value and rank. So we'll only get
  85. // those if applicable.
  86. if (array_key_exists('value', $schema['fields'])) {
  87. $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__value', $value);
  88. }
  89. if (array_key_exists('rank', $schema['fields'])) {
  90. $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
  91. }
  92. // Get element values added to help support insert/updates.
  93. $object_uniquename = tripal_get_field_item_keyval($items, $delta, 'object_name', $object_uniquename);
  94. $subject_uniquename = tripal_get_field_item_keyval($items, $delta, 'subject_name', $subject_uniquename);
  95. $type = tripal_get_field_item_keyval($items, $delta, 'type_name', $type);
  96. //@debug dpm(array($subject_id, $type_id, $object_id), 'have an item!');
  97. }
  98. }
  99. // Check $form_state['values'] to see if an AJAX call set the values.
  100. if (array_key_exists('values', $form_state) and
  101. array_key_exists($field_name, $form_state['values'])) {
  102. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  103. $subject_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key];
  104. $object_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $object_id_key];
  105. $type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
  106. if (array_key_exists('value', $schema['fields'])) {
  107. $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
  108. }
  109. if (array_key_exists('rank', $schema['fields'])) {
  110. $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
  111. }
  112. $object_uniquename = $form_state['values'][$field_name]['und'][$delta]['object_name'];
  113. $subject_uniquename = $form_state['values'][$field_name]['und'][$delta]['subject_name'];
  114. $type = $form_state['values'][$field_name]['und'][$delta]['type_name'];
  115. //@debug dpm(array($subject_id, $type_id, $object_id), 'have an item (AJAX)!');
  116. }
  117. // Check if we have autocomplete available for this base table
  118. $autocomplete_path = "admin/tripal/storage/chado/auto_name/$base_table";
  119. $has_autocomplete = db_query('SELECT 1 FROM menu_router WHERE path=:path',
  120. array(':path' => $autocomplete_path.'/%'))->fetchField();
  121. $widget['#table_name'] = $field_table;
  122. $widget['#fkeys'] = $schema['foreign keys'];
  123. $widget['#base_table'] = $base_table;
  124. $widget['#chado_record_id'] = isset($form['#entity']) ? $form['#entity']->chado_record_id : '';
  125. //$widget['#element_validate'] = array('sbo__relationship_validate');
  126. $widget['#prefix'] = "<span id='$field_table-$delta'>";
  127. $widget['#suffix'] = "</span>";
  128. $widget['value'] = array(
  129. '#type' => 'value',
  130. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  131. );
  132. $widget['chado-' . $field_table . '__' . $pkey] = array(
  133. '#type' => 'value',
  134. '#default_value' => $record_id,
  135. );
  136. $widget['chado-' . $field_table . '__' . $subject_id_key] = array(
  137. '#type' => 'value',
  138. '#default_value' => $subject_id,
  139. );
  140. $widget['chado-' . $field_table . '__type_id'] = array(
  141. '#type' => 'value',
  142. '#default_value' => $type_id,
  143. );
  144. $widget['chado-' . $field_table . '__' . $object_id_key] = array(
  145. '#type' => 'value',
  146. '#default_value' => $object_id,
  147. );
  148. if (array_key_exists('value', $schema['fields'])) {
  149. $widget['chado-' . $field_table . '__value'] = array(
  150. '#type' => 'value',
  151. '#default_value' => $value,
  152. );
  153. }
  154. if (array_key_exists('rank', $schema['fields'])) {
  155. $widget['chado-' . $field_table . '__rank'] = array(
  156. '#type' => 'value',
  157. '#default_value' => $rank,
  158. );
  159. }
  160. $widget['subject_name'] = array(
  161. '#type' => 'textfield',
  162. '#title' => t('Subject'),
  163. '#default_value' => $subject_uniquename,
  164. '#required' => $element['#required'],
  165. '#maxlength' => array_key_exists($subject_id_key, $schema['fields']) && array_key_exists('length', $schema['fields'][$subject_id_key]) ? $schema['fields'][$subject_id_key]['length'] : 255,
  166. '#size' => 35,
  167. );
  168. // Add autocomplete if we have one for this base table.
  169. if ($has_autocomplete) {
  170. $widget['subject_name']['#autocomplete_path'] = $autocomplete_path;
  171. }
  172. // Getting default values for the relationship type element.
  173. $default_voc = '';
  174. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
  175. $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
  176. }
  177. $default_term = '';
  178. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
  179. $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
  180. }
  181. $default_type_id = $type_id;
  182. if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
  183. $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
  184. }
  185. // Option 3: Custom list of Relationship Types
  186. $rtype_options = array();
  187. $rtype_options[] = 'Select a Type';
  188. if ($option3_rtypes) {
  189. $rtypes = explode(PHP_EOL, $option3_rtypes);
  190. foreach($rtypes AS $rtype) {
  191. // Ignore empty lines
  192. if (trim($rtype) == '') {
  193. continue;
  194. }
  195. $term = chado_get_cvterm(array('name' => trim($rtype)));
  196. // Try to get term with vocabulary specified
  197. if (!$term) {
  198. $tmp = explode('|', trim($rtype), 2);
  199. $cv = chado_get_cv(array('name' => trim($tmp[0])));
  200. $rtype = trim($tmp[1]);
  201. $term = chado_get_cvterm(array('name' => $rtype, 'cv_id' => $cv->cv_id));
  202. }
  203. $rtype_options[$term->cvterm_id] = $term->name;
  204. }
  205. $widget['type_id'] = array(
  206. '#type' => 'select',
  207. '#title' => t('Relationship Type'),
  208. '#options' => $rtype_options,
  209. '#default_value' => $default_type_id,
  210. );
  211. if ($type_id && !key_exists($type_id, $rtype_options)) {
  212. 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');
  213. }
  214. }
  215. // Option 2: Child terms of a selected cvterm
  216. else if ($option2_vocab) {
  217. $values = array(
  218. 'cv_id' => $option2_vocab,
  219. 'name' => $option2_parent
  220. );
  221. $parent_term = chado_get_cvterm($values);
  222. // If the term wasn't found then see if it's a synonym.
  223. if(!$parent_term) {
  224. $values = array(
  225. 'synonym' => array(
  226. 'name' => trim($option2_parent),
  227. )
  228. );
  229. $synonym = chado_get_cvterm($values);
  230. if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
  231. $parent_term = $synonym;
  232. }
  233. }
  234. // Get the child terms of the parent term found above.
  235. $sql = "
  236. SELECT subject_id,
  237. (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
  238. FROM {cvtermpath}
  239. WHERE
  240. object_id = :parent_cvterm_id AND
  241. cv_id = :parent_cv_id
  242. ORDER BY name
  243. ";
  244. $args = array(
  245. ':parent_cvterm_id' => $parent_term->cvterm_id,
  246. ':parent_cv_id' => $parent_term->cv_id->cv_id
  247. );
  248. $results = chado_query($sql, $args);
  249. while($child = $results->fetchObject()) {
  250. $rtype_options[$child->subject_id] = $child->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. // Option 1: All terms of selected vocabularies
  263. else if ($option1_test && array_pop($option1_test)) {
  264. $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
  265. $results = chado_query($sql, array(':cv_id' => $option1_vocabs));
  266. while ($obj = $results->fetchObject()) {
  267. $rtype_options[$obj->cvterm_id] = $obj->name;
  268. }
  269. $widget['type_id'] = array(
  270. '#type' => 'select',
  271. '#title' => t('Relationship Type'),
  272. '#options' => $rtype_options,
  273. '#default_value' => $default_type_id,
  274. );
  275. if ($type_id && !key_exists($type_id, $rtype_options)) {
  276. 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');
  277. }
  278. }
  279. // Default option:
  280. else {
  281. // Set up available cvterms for selection
  282. $vocs = array(0 => 'Select a vocabulary');
  283. $vocs = chado_get_cv_select_options();
  284. $cv_id = isset($form_state['values'][$field_name]['und'][0]['vocabulary']) ? $form_state['values'][$field_name]['und'][0]['vocabulary'] : 0;
  285. // Try getting the cv_id from cvterm for existing records
  286. if (!$cv_id && $type_id) {
  287. $cvterm = chado_get_cvterm(array('cvterm_id' => $type_id));
  288. if (isset($cvterm->cv_id->cv_id)) {
  289. $cv_id = $cvterm->cv_id->cv_id;
  290. $default_term = $cvterm->name;
  291. }
  292. }
  293. if (!$cv_id) {
  294. $cv_id = $default_voc;
  295. }
  296. $widget['vocabulary'] = array(
  297. '#type' => 'select',
  298. '#title' => t('Vocabulary'),
  299. '#options' => $vocs,
  300. '#required' => $element['#required'],
  301. '#default_value' => $cv_id,
  302. '#ajax' => array(
  303. 'callback' => "sbo__relationship_widget_form_ajax_callback",
  304. 'wrapper' => "$field_table-$delta",
  305. 'effect' => 'fade',
  306. 'method' => 'replace'
  307. ),
  308. );
  309. $widget['type_name'] = array(
  310. '#type' => 'textfield',
  311. '#title' => t('Relationship Type'),
  312. '#size' => 15,
  313. '#default_value' => $default_term,
  314. '#disabled' => TRUE,
  315. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id"
  316. );
  317. if ($cv_id) {
  318. $widget['type_name']['#disabled'] = FALSE;
  319. }
  320. }
  321. $widget['object_name'] = array(
  322. '#type' => 'textfield',
  323. '#title' => t('Object'),
  324. '#default_value' => $object_uniquename,
  325. '#required' => $element['#required'],
  326. '#maxlength' => array_key_exists($object_id_key, $schema['fields']) && array_key_exists('length', $schema['fields'][$object_id_key]) ? $schema['fields'][$object_id_key]['length'] : 255,
  327. '#size' => 35,
  328. );
  329. // Add autocomplete if we have one for this base table.
  330. if ($has_autocomplete) {
  331. $widget['object_name']['#autocomplete_path'] = $autocomplete_path;
  332. }
  333. }
  334. /**
  335. *
  336. * @see TripalFieldWidget::validate()
  337. */
  338. public function validate($element, $form, &$form_state, $langcode, $delta) {
  339. $field_name = $this->field['field_name'];
  340. $field_type = $this->field['type'];
  341. $field_table = $this->instance['settings']['chado_table'];
  342. $field_column = $this->instance['settings']['chado_column'];
  343. $base_table = $this->instance['settings']['base_table'];
  344. $chado_record_id = array_key_exists('#entity', $element)? $element['#entity']->chado_record_id : NULL;
  345. // @debug ddl(array_keys($form[$field_name][$langcode][$delta]), "widget form keys: $delta");
  346. $schema = chado_get_schema($field_table);
  347. $fkeys = $schema['foreign keys'];
  348. // 'nd_reagent_relationship' and 'project_relationship' have different column names from
  349. // subject_id/object_id. Retrieve the column names determined in the form.
  350. $subject_id_key = $form[$field_name][$langcode][$delta]['#subject_id_key'];
  351. $object_id_key = $form[$field_name][$langcode][$delta]['#object_id_key'];
  352. // Retrieve the values from the form for the current $delta.
  353. $voc_id = array_key_exists('vocabulary', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : '';
  354. $type_name = array_key_exists('type_name', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['type_name'] : '';
  355. $subject_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] : '';
  356. $object_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key]: '';
  357. $type_id = $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'];
  358. $subject_name = $form_state['values'][$field_name][$langcode][$delta]['subject_name'];
  359. $object_name = $form_state['values'][$field_name][$langcode][$delta]['object_name'];
  360. // If the row is empty then skip this one, there's nothing to validate.
  361. if (!($type_id or !$type_name) and !$subject_name and !$object_name) {
  362. return;
  363. }
  364. else if ($type_name && $voc_id) {
  365. $val = array(
  366. 'cv_id' => $voc_id,
  367. 'name' => $type_name
  368. );
  369. $cvterm = chado_generate_var('cvterm', $val);
  370. $type_id = $cvterm->cvterm_id;
  371. }
  372. // Do not proceed if subject ID or object ID does not exist
  373. if (!key_exists($subject_id_key, $fkeys[$base_table]['columns']) ||
  374. !key_exists($object_id_key, $fkeys[$base_table]['columns'])) {
  375. return;
  376. }
  377. // Get the subject ID.
  378. $subject_id = '';
  379. $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
  380. $matches = array();
  381. // First check if it's in the textfield due to use of the autocomplete.
  382. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  383. $subject_id = $matches[1];
  384. }
  385. // Otherwise, try the uniquename.
  386. // The base table does not always have a uniquename (e.g. organism for organism_relationship).
  387. // @uniquename this will cause an error on organism/project-based content types.
  388. else {
  389. $values = array('uniquename' => $subject_name);
  390. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  391. if(count($subject) > 0) {
  392. $subject_id = $subject[0]->$fkey_rcolumn;
  393. }
  394. }
  395. // Get the object ID.
  396. $object_id = '';
  397. $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
  398. $matches = array();
  399. // First check if it's in the textfield due to use of the autocomplete.
  400. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  401. $object_id = $matches[1];
  402. }
  403. // Otherwise, try the uniquename.
  404. // The base table does not always have a uniquename (e.g. organism for organism_relationship).
  405. // @uniquename this will cause an error on organism/project-based content types.
  406. else {
  407. $values = array('uniquename' => $object_name);
  408. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  409. if (count($object) > 0) {
  410. $object_id = $object[0]->$fkey_rcolumn;
  411. }
  412. }
  413. // If we have all three values required for a relationship...
  414. // Then set them as the chado field storage expects them to be set.
  415. if ($subject_id && $object_id && $type_id) {
  416. // Set the IDs according to the values that were determined above.
  417. $form_state['values'][$field_name][$langcode][$delta]['value'] = 'value must be set but is not used';
  418. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $subject_id;
  419. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = $object_id;
  420. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
  421. if (array_key_exists('rank', $schema['fields'])) {
  422. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
  423. }
  424. }
  425. // Otherwise, leave them blank?
  426. else {
  427. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = '';
  428. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = '';
  429. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = '';
  430. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__value'] = '';
  431. if (array_key_exists('rank', $schema['fields'])) {
  432. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = '';
  433. }
  434. }
  435. // @debug ddl($form_state['values'][$field_name][$langcode][$delta], "form state values: $delta");
  436. }
  437. /**
  438. * Theme function for the sbo__relationship_widget.
  439. */
  440. public function theme($element) {
  441. $layout = "
  442. <div class=\"chado-linker--relationship-widget\">
  443. <div class=\"chado-linker--relationship-widget-item\">" .
  444. drupal_render($element['subject_name']) . "
  445. </div>
  446. <div class=\"chado-linker--relationship-widget-item\">" .
  447. drupal_render($element['vocabulary']) . "
  448. </div>
  449. <div class=\"chado-linker--relationship-widget-item\">" .
  450. drupal_render($element['type_name']) . "
  451. </div>
  452. <div class=\"chado-linker--relationship-widget-item\">" .
  453. drupal_render($element['type_id']) . "
  454. </div>
  455. <div>" .
  456. drupal_render($element['object_name']) . "
  457. </div>
  458. </div>
  459. ";
  460. return $layout;
  461. }
  462. }
  463. function theme_sbo__relationship_instance_settings ($variables) {
  464. $element = $variables['element'];
  465. $option1 = $element['option1'];
  466. $option1_vocabs = $element['option1_vocabs'];
  467. $option2 = $element['option2'];
  468. $option2_vocab = $element['option2_vocab'];
  469. $option2_parent = $element['option2_parent'];
  470. $option3 = $element['option3'];
  471. $rtype = $element['relationship_types'];
  472. $layout = drupal_render($option1);
  473. $layout .= drupal_render($option1_vocabs);
  474. $layout .=
  475. drupal_render($option2) .
  476. "<div class=\"chado-linker--relationship-instance-settings-option2\">" .
  477. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  478. drupal_render($option2_vocab) .
  479. "</div>" .
  480. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  481. drupal_render($option2_parent) .
  482. "</div>" .
  483. "</div>";
  484. $layout .= drupal_render($option3);
  485. $layout .= drupal_render($rtype);
  486. return $layout;
  487. }
  488. /**
  489. * An Ajax callback for the relationshp widget.
  490. */
  491. function sbo__relationship_widget_form_ajax_callback(&$form, $form_state) {
  492. // Get the triggering element
  493. $form_element_name = $form_state['triggering_element']['#name'];
  494. preg_match('/(.+?)\[(.+?)\]\[(.+?)\]/', $form_element_name, $matches);
  495. $field = $matches[1];
  496. $lang = $matches[2];
  497. $delta = $matches[3];
  498. // Return the widget that triggered the AJAX call
  499. if (isset($form[$field][$lang][$delta])) {
  500. return $form[$field][$lang][$delta];
  501. }
  502. // Alternatively, return the default value widget for the widget setting form
  503. else {
  504. return $form['instance']['default_value_widget'][$field];
  505. }
  506. }
  507. /**
  508. * An Ajax callback for the relationshp instance setting form.
  509. */
  510. function sbo__relationship_instance_settings_form_ajax_callback(&$form, &$form_state) {
  511. $acpath = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'];
  512. $acpath .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'] . '/';
  513. $urlval = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'];
  514. $urlval .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'];
  515. // Reset value if a different vocabulary is selected
  516. $form['instance']['settings']['relationships']['option2_parent']['#value'] = NULL;
  517. $form_state['values']['instance']['settings']['relationships']['option2_parent'] = NULL;
  518. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'] = $acpath;
  519. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'] = $urlval;
  520. return $form['instance']['settings']['relationships']['option2_parent'];
  521. }