sbo__relationship_widget.inc 30 KB

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