sbo__relationship_widget.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. $widget['#table_name'] = $field_table;
  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. }
  106. }
  107. // Check $form_state['values'] to see if an AJAX call set the values.
  108. if (array_key_exists('values', $form_state) and
  109. array_key_exists($field_name, $form_state['values'])) {
  110. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  111. $subject_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key];
  112. $object_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $object_id_key];
  113. $type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
  114. if (array_key_exists('value', $this->schema['fields'])) {
  115. $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
  116. }
  117. if (array_key_exists('rank', $this->schema['fields'])) {
  118. $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
  119. }
  120. $object_label = $form_state['values'][$field_name]['und'][$delta]['object_name'];
  121. $subject_label = $form_state['values'][$field_name]['und'][$delta]['subject_name'];
  122. $type = $form_state['values'][$field_name]['und'][$delta]['type_name'];
  123. }
  124. // Getting default values for the relationship type element.
  125. $default_voc = '';
  126. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
  127. $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
  128. }
  129. $default_term = '';
  130. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
  131. $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
  132. }
  133. $default_type_id = $type_id;
  134. if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
  135. $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
  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. // Save some values for later...
  142. $widget['#table_name'] = $field_table;
  143. $widget['#fkeys'] = $this->schema['foreign keys'];
  144. $widget['#base_table'] = $base_table;
  145. $widget['#chado_record_id'] = isset($form['#entity']) ? $form['#entity']->chado_record_id : '';
  146. //$widget['#element_validate'] = array('sbo__relationship_validate');
  147. $widget['#prefix'] = "<span id='$field_table-$delta'>";
  148. $widget['#suffix'] = "</span>";
  149. // Save the values needed by the Chado Storage API.
  150. //-------------------------------------------------
  151. $widget['value'] = array(
  152. '#type' => 'value',
  153. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  154. );
  155. $widget['chado-' . $field_table . '__' . $pkey] = array(
  156. '#type' => 'value',
  157. '#default_value' => $record_id,
  158. );
  159. $widget['chado-' . $field_table . '__' . $subject_id_key] = array(
  160. '#type' => 'value',
  161. '#default_value' => $subject_id,
  162. );
  163. $widget['chado-' . $field_table . '__type_id'] = array(
  164. '#type' => 'value',
  165. '#default_value' => $type_id,
  166. );
  167. $widget['chado-' . $field_table . '__' . $object_id_key] = array(
  168. '#type' => 'value',
  169. '#default_value' => $object_id,
  170. );
  171. if (array_key_exists('value', $this->schema['fields'])) {
  172. $widget['chado-' . $field_table . '__value'] = array(
  173. '#type' => 'value',
  174. '#default_value' => $value,
  175. );
  176. }
  177. if (array_key_exists('rank', $this->schema['fields'])) {
  178. $widget['chado-' . $field_table . '__rank'] = array(
  179. '#type' => 'value',
  180. '#default_value' => $rank,
  181. );
  182. }
  183. // Subject:
  184. //----------
  185. $widget['subject_name'] = array(
  186. '#type' => 'textfield',
  187. '#title' => t('Subject'),
  188. '#default_value' => $subject_label,
  189. '#required' => $element['#required'],
  190. '#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,
  191. '#size' => 35,
  192. );
  193. // Add autocomplete if we have one for this base table.
  194. if ($has_autocomplete) {
  195. $widget['subject_name']['#autocomplete_path'] = $autocomplete_path;
  196. }
  197. // Type:
  198. //-------
  199. $rtype_options = $this->get_rtype_select_options();
  200. if ($rtype_options) {
  201. $widget['type_id'] = array(
  202. '#type' => 'select',
  203. '#title' => t('Relationship Type'),
  204. '#options' => $rtype_options,
  205. '#empty_option' => 'Select a Type',
  206. '#default_value' => $default_type_id,
  207. );
  208. if ($type_id && !key_exists($type_id, $rtype_options)) {
  209. 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');
  210. }
  211. }
  212. // Default option:
  213. // If we were determine an type_id option set...
  214. // then we will need to provide a cv + type autocomplete.
  215. else {
  216. // Set up available cvterms for selection
  217. $vocs = array();
  218. $vocs = chado_get_cv_select_options();
  219. unset($vocs[0]);
  220. $cv_id = isset($form_state['values'][$field_name][$langcode][$delta]['vocabulary']) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : 0;
  221. // Try getting the cv_id from cvterm for existing records
  222. if (!$cv_id && $type_id) {
  223. $cvterm = chado_get_cvterm(array('cvterm_id' => $type_id));
  224. if (isset($cvterm->cv_id->cv_id)) {
  225. $cv_id = $cvterm->cv_id->cv_id;
  226. $default_term = $cvterm->name;
  227. }
  228. }
  229. if (!$cv_id) {
  230. $cv_id = $default_voc;
  231. }
  232. $widget['vocabulary'] = array(
  233. '#type' => 'select',
  234. '#title' => t('Vocabulary'),
  235. '#options' => $vocs,
  236. '#required' => $element['#required'],
  237. '#default_value' => $cv_id,
  238. '#empty_option' => 'Select a Vocabulary',
  239. '#ajax' => array(
  240. 'callback' => "sbo__relationship_widget_form_ajax_callback",
  241. 'wrapper' => "$field_table-$delta",
  242. 'effect' => 'fade',
  243. 'method' => 'replace'
  244. ),
  245. );
  246. $widget['type_name'] = array(
  247. '#type' => 'textfield',
  248. '#title' => t('Relationship Type'),
  249. '#size' => 15,
  250. '#default_value' => $default_term,
  251. '#disabled' => TRUE,
  252. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id"
  253. );
  254. if ($cv_id) {
  255. $widget['type_name']['#disabled'] = FALSE;
  256. }
  257. }
  258. // Object:
  259. //--------
  260. $widget['object_name'] = array(
  261. '#type' => 'textfield',
  262. '#title' => t('Object'),
  263. '#default_value' => $object_label,
  264. '#required' => $element['#required'],
  265. '#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,
  266. '#size' => 35,
  267. );
  268. // Add autocomplete if we have one for this base table.
  269. if ($has_autocomplete) {
  270. $widget['object_name']['#autocomplete_path'] = $autocomplete_path;
  271. }
  272. }
  273. /**
  274. *
  275. * @see TripalFieldWidget::validate()
  276. */
  277. public function validate($element, $form, &$form_state, $langcode, $delta) {
  278. $field_name = $this->field['field_name'];
  279. $field_type = $this->field['type'];
  280. $field_table = $this->instance['settings']['chado_table'];
  281. $field_column = $this->instance['settings']['chado_column'];
  282. $base_table = $this->instance['settings']['base_table'];
  283. $chado_record_id = array_key_exists('#entity', $element)? $element['#entity']->chado_record_id : NULL;
  284. $fkeys = $this->schema['foreign keys'];
  285. // 'nd_reagent_relationship' and 'project_relationship' have different column names from
  286. // subject_id/object_id. Retrieve the column names determined in the form.
  287. $subject_id_key = $this->subject_id_column;
  288. $object_id_key = $this->object_id_column;
  289. // Retrieve the values from the form for the current $delta.
  290. $voc_id = array_key_exists('vocabulary', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : '';
  291. $type_name = array_key_exists('type_name', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['type_name'] : '';
  292. $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] : '';
  293. $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]: '';
  294. $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'] : '';
  295. $subject_name = isset($form_state['values'][$field_name][$langcode][$delta]['subject_name']) ? $form_state['values'][$field_name][$langcode][$delta]['subject_name'] : '';
  296. $object_name = isset($form_state['values'][$field_name][$langcode][$delta]['object_name']) ? $form_state['values'][$field_name][$langcode][$delta]['object_name'] : '';
  297. // Validation:
  298. //------------
  299. // If the row is empty then skip this one, there's nothing to validate.
  300. if (!($type_id || $type_name) && !$subject_name && !$object_name) {
  301. return;
  302. }
  303. // Do not proceed if subject ID or object ID does not exist
  304. if (!key_exists($subject_id_key, $fkeys[$base_table]['columns']) ||
  305. !key_exists($object_id_key, $fkeys[$base_table]['columns'])) {
  306. return;
  307. }
  308. // Validation is occuring in the field::validate() but we need to know if it finds errors.
  309. // As such, I'm calling it here to check.
  310. // Also, field::validate() doesn't seem to always show it's errors
  311. // OR stop form submission? so we need to ensure that happens here.
  312. // sbo__relationship::validate($entity_type, $entity, $langcode, $items, &$errors)
  313. $errors = $this->field_instance->validateItem($form_state['values'][$field_name][$langcode][$delta], $element['#chado_record_id']);
  314. if ($errors) {
  315. foreach ($errors as $error) {
  316. switch ($error['element']) {
  317. case 'subject':
  318. form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][subject_name', $error['message']);
  319. break;
  320. case 'type':
  321. form_set_error('sbo__relationship]['.$langcode.']['.$delta, $error['message']);
  322. break;
  323. case 'object':
  324. form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][object_name', $error['message']);
  325. break;
  326. default:
  327. form_set_error('sbo__relationship]['.$langcode.']['.$delta, $error['message']);
  328. }
  329. }
  330. // Ensure data is prepared for the storage backend:
  331. //-------------------------------------------------
  332. }
  333. else {
  334. if ($type_name && $voc_id) {
  335. $val = array(
  336. 'cv_id' => $voc_id,
  337. 'name' => $type_name
  338. );
  339. $cvterm = chado_generate_var('cvterm', $val);
  340. if (isset($cvterm->cvterm_id)) {
  341. $type_id = $cvterm->cvterm_id;
  342. }
  343. }
  344. // Get the subject ID.
  345. $subject_id = '';
  346. $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
  347. $matches = array();
  348. // First check if it's in the textfield due to use of the autocomplete.
  349. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  350. $subject_id = $matches[1];
  351. }
  352. // Otherwise we need to look it up using the name field determined in the
  353. // constructor for the current field. There may be more then one name field
  354. // (e.g. organism: genus + species) so we want to check both.
  355. else {
  356. $sql = 'SELECT ' . $fkey_rcolumn . ' FROM {' . $base_table . '} WHERE ' . implode('||', $this->base_name_columns) . '=:keyword';
  357. $subject = chado_query($sql, [':keyword' => $subject_name])->fetchAll();
  358. if(count($subject) > 0) {
  359. $subject_id = $subject[0]->$fkey_rcolumn;
  360. }
  361. }
  362. // Get the object ID.
  363. $object_id = '';
  364. $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
  365. $matches = array();
  366. // First check if it's in the textfield due to use of the autocomplete.
  367. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  368. $object_id = $matches[1];
  369. }
  370. // Otherwise we need to look it up using the name field determined in the
  371. // constructor for the current field. There may be more then one name field
  372. // (e.g. organism: genus + species) so we want to check both.
  373. else {
  374. $sql = 'SELECT ' . $fkey_rcolumn . ' FROM {' . $base_table . '} WHERE ' . implode('||', $this->base_name_columns) . '=:keyword';
  375. $object = chado_query($sql, [':keyword' => $object_name])->fetchAll();
  376. if (count($object) > 0) {
  377. $object_id = $object[0]->$fkey_rcolumn;
  378. }
  379. }
  380. // If we have all three values required for a relationship...
  381. // Then set them as the chado field storage expects them to be set.
  382. if ($subject_id && $object_id && $type_id) {
  383. // Set the IDs according to the values that were determined above.
  384. $form_state['values'][$field_name][$langcode][$delta]['value'] = 'value must be set but is not used';
  385. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $subject_id;
  386. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = $object_id;
  387. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
  388. if (array_key_exists('rank', $this->schema['fields'])) {
  389. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
  390. }
  391. }
  392. // Otherwise, maybe we are creating the entity...
  393. // The storage API sohuld handle this case and automagically add the key in once
  394. // the chado record is created... so all we need to do is set the other columns.
  395. elseif ($subject_name && $object_id && $type_id) {
  396. $form_state['values'][$field_name][$langcode][$delta]['value'] = 'value must be set but is not used';
  397. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = $object_id;
  398. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
  399. if (array_key_exists('rank', $this->schema['fields'])) {
  400. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
  401. }
  402. }
  403. elseif ($subject_id && $object_name && $type_id) {
  404. $form_state['values'][$field_name][$langcode][$delta]['value'] = 'value must be set but is not used';
  405. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $subject_id;
  406. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
  407. if (array_key_exists('rank', $this->schema['fields'])) {
  408. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
  409. }
  410. }
  411. // Otherwise, we don't have a vallue to insert so leave them blank.
  412. else {
  413. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = '';
  414. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = '';
  415. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = '';
  416. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__value'] = '';
  417. if (array_key_exists('rank', $this->schema['fields'])) {
  418. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = '';
  419. }
  420. }
  421. }
  422. return $errors;
  423. }
  424. /**
  425. * Theme function for the sbo__relationship_widget.
  426. */
  427. public function theme($element) {
  428. $layout = "
  429. <div class=\"chado-linker--relationship-widget\">
  430. <div class=\"chado-linker--relationship-widget-item\">" .
  431. drupal_render($element['subject_name']) . "
  432. </div>
  433. <div class=\"chado-linker--relationship-widget-item\">" .
  434. drupal_render($element['vocabulary']) . "
  435. </div>
  436. <div class=\"chado-linker--relationship-widget-item\">" .
  437. drupal_render($element['type_name']) . "
  438. </div>
  439. <div class=\"chado-linker--relationship-widget-item\">" .
  440. drupal_render($element['type_id']) . "
  441. </div>
  442. <div>" .
  443. drupal_render($element['object_name']) . "
  444. </div>
  445. </div>
  446. ";
  447. return $layout;
  448. }
  449. /**
  450. * Retrieve options for the type drop-down for the relationship widget.
  451. */
  452. public function get_rtype_select_options() {
  453. // This is slated for Release 2 of this widget.
  454. // It still needs extensive functional and automated testing.
  455. // Thus for now we are falling back on the Default option:
  456. // Form will provide a type autocomplete + vocab select.
  457. // @todo test this.
  458. return FALSE;
  459. // Get the instance settings. There are three options for how this widget
  460. // will be displayed. Those are controlled in the instance settings
  461. // of the field.
  462. // Option 1: relationship types are limited to a specific vocabulary.
  463. // Option 2: relationship types are limited to a subset of one vocabulary.
  464. // Option 3: relationship types are limited to a predefined set.
  465. $instance = $this->instance;
  466. $settings = '';
  467. $option1_vocabs = '';
  468. $option2_parent = '';
  469. $option2_vocab = '';
  470. $option3_rtypes = '';
  471. if (array_key_exists('relationships', $instance)) {
  472. $settings = $instance['settings']['relationships'];
  473. $option1_vocabs = $settings['option1_vocabs'];
  474. $option2_vocab = $settings['option2_vocab'];
  475. $option2_parent = $settings['option2_parent'];
  476. $option3_rtypes = $settings['relationship_types'];
  477. }
  478. // For testing if there are selected vocabs for option1 we'll copy the
  479. // contents in a special variable for later.
  480. $option1_test = $option1_vocabs;
  481. // Option 3: Custom list of Relationship Types
  482. $rtype_options = array();
  483. if ($option3_rtypes) {
  484. $rtypes = explode(PHP_EOL, $option3_rtypes);
  485. foreach($rtypes AS $rtype) {
  486. // Ignore empty lines
  487. if (trim($rtype) == '') {
  488. continue;
  489. }
  490. $term = chado_get_cvterm(array('name' => trim($rtype)));
  491. // Try to get term with vocabulary specified
  492. if (!$term) {
  493. $tmp = explode('|', trim($rtype), 2);
  494. $cv = chado_get_cv(array('name' => trim($tmp[0])));
  495. $rtype = trim($tmp[1]);
  496. $term = chado_get_cvterm(array('name' => $rtype, 'cv_id' => $cv->cv_id));
  497. }
  498. $rtype_options[$term->cvterm_id] = $term->name;
  499. }
  500. return $rtype_options;
  501. }
  502. // Option 2: Child terms of a selected cvterm
  503. else if ($option2_vocab) {
  504. $values = array(
  505. 'cv_id' => $option2_vocab,
  506. 'name' => $option2_parent
  507. );
  508. $parent_term = chado_get_cvterm($values);
  509. // If the term wasn't found then see if it's a synonym.
  510. if(!$parent_term) {
  511. $values = array(
  512. 'synonym' => array(
  513. 'name' => trim($option2_parent),
  514. )
  515. );
  516. $synonym = chado_get_cvterm($values);
  517. if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
  518. $parent_term = $synonym;
  519. }
  520. }
  521. // Get the child terms of the parent term found above.
  522. $sql = "
  523. SELECT subject_id,
  524. (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
  525. FROM {cvtermpath}
  526. WHERE
  527. object_id = :parent_cvterm_id AND
  528. cv_id = :parent_cv_id
  529. ORDER BY name
  530. ";
  531. $args = array(
  532. ':parent_cvterm_id' => $parent_term->cvterm_id,
  533. ':parent_cv_id' => $parent_term->cv_id->cv_id
  534. );
  535. $results = chado_query($sql, $args);
  536. while($child = $results->fetchObject()) {
  537. $rtype_options[$child->subject_id] = $child->name;
  538. }
  539. return $rtype_options;
  540. }
  541. // Option 1: All terms of selected vocabularies
  542. else if ($option1_test && array_pop($option1_test)) {
  543. $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
  544. $results = chado_query($sql, array(':cv_id' => $option1_vocabs));
  545. while ($obj = $results->fetchObject()) {
  546. $rtype_options[$obj->cvterm_id] = $obj->name;
  547. }
  548. return $rtype_options;
  549. }
  550. // Default option:
  551. // Let the form deal with this by providing a type autocomplete?
  552. else {
  553. return FALSE;
  554. }
  555. }
  556. }
  557. function theme_sbo__relationship_instance_settings ($variables) {
  558. $element = $variables['element'];
  559. $option1 = $element['option1'];
  560. $option1_vocabs = $element['option1_vocabs'];
  561. $option2 = $element['option2'];
  562. $option2_vocab = $element['option2_vocab'];
  563. $option2_parent = $element['option2_parent'];
  564. $option3 = $element['option3'];
  565. $rtype = $element['relationship_types'];
  566. $layout = drupal_render($option1);
  567. $layout .= drupal_render($option1_vocabs);
  568. $layout .=
  569. drupal_render($option2) .
  570. "<div class=\"chado-linker--relationship-instance-settings-option2\">" .
  571. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  572. drupal_render($option2_vocab) .
  573. "</div>" .
  574. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  575. drupal_render($option2_parent) .
  576. "</div>" .
  577. "</div>";
  578. $layout .= drupal_render($option3);
  579. $layout .= drupal_render($rtype);
  580. return $layout;
  581. }
  582. /**
  583. * An Ajax callback for the relationshp widget.
  584. */
  585. function sbo__relationship_widget_form_ajax_callback(&$form, $form_state) {
  586. // Get the triggering element
  587. $form_element_name = $form_state['triggering_element']['#name'];
  588. preg_match('/(.+?)\[(.+?)\]\[(.+?)\]/', $form_element_name, $matches);
  589. $field = $matches[1];
  590. $lang = $matches[2];
  591. $delta = $matches[3];
  592. // Return the widget that triggered the AJAX call
  593. if (isset($form[$field][$lang][$delta])) {
  594. return $form[$field][$lang][$delta];
  595. }
  596. // Alternatively, return the default value widget for the widget setting form
  597. else {
  598. return $form['instance']['default_value_widget'][$field];
  599. }
  600. }
  601. /**
  602. * An Ajax callback for the relationshp instance setting form.
  603. */
  604. function sbo__relationship_instance_settings_form_ajax_callback(&$form, &$form_state) {
  605. $acpath = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'];
  606. $acpath .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'] . '/';
  607. $urlval = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'];
  608. $urlval .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'];
  609. // Reset value if a different vocabulary is selected
  610. $form['instance']['settings']['relationships']['option2_parent']['#value'] = NULL;
  611. $form_state['values']['instance']['settings']['relationships']['option2_parent'] = NULL;
  612. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'] = $acpath;
  613. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'] = $urlval;
  614. return $form['instance']['settings']['relationships']['option2_parent'];
  615. }