sbo__relationship_widget.inc 26 KB

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