sbo__relationship_widget.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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 = ['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. [':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'] = [
  152. '#type' => 'value',
  153. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  154. ];
  155. $widget['chado-' . $field_table . '__' . $pkey] = [
  156. '#type' => 'value',
  157. '#default_value' => $record_id,
  158. ];
  159. $widget['chado-' . $field_table . '__' . $subject_id_key] = [
  160. '#type' => 'value',
  161. '#default_value' => $subject_id,
  162. ];
  163. $widget['chado-' . $field_table . '__type_id'] = [
  164. '#type' => 'value',
  165. '#default_value' => $type_id,
  166. ];
  167. $widget['chado-' . $field_table . '__' . $object_id_key] = [
  168. '#type' => 'value',
  169. '#default_value' => $object_id,
  170. ];
  171. if (array_key_exists('value', $this->schema['fields'])) {
  172. $widget['chado-' . $field_table . '__value'] = [
  173. '#type' => 'value',
  174. '#default_value' => $value,
  175. ];
  176. }
  177. if (array_key_exists('rank', $this->schema['fields'])) {
  178. $widget['chado-' . $field_table . '__rank'] = [
  179. '#type' => 'value',
  180. '#default_value' => $rank,
  181. ];
  182. }
  183. // Subject:
  184. //----------
  185. $widget['subject_name'] = [
  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'] = [
  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 = [];
  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(['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'] = [
  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' => [
  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'] = [
  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'] = [
  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. // The tables 'nd_reagent_relationship' and 'project_relationship' have
  286. // different column names from subject_id/object_id. Retrieve the column
  287. // names determined in the form.
  288. $subject_id_key = $this->subject_id_column;
  289. $object_id_key = $this->object_id_column;
  290. // Retrieve the values from the form for the current $delta.
  291. $voc_id = array_key_exists('vocabulary', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : '';
  292. $type_name = array_key_exists('type_name', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['type_name'] : '';
  293. $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] : '';
  294. $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] : '';
  295. $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'] : '';
  296. $subject_name = isset($form_state['values'][$field_name][$langcode][$delta]['subject_name']) ? $form_state['values'][$field_name][$langcode][$delta]['subject_name'] : '';
  297. $object_name = isset($form_state['values'][$field_name][$langcode][$delta]['object_name']) ? $form_state['values'][$field_name][$langcode][$delta]['object_name'] : '';
  298. // Validation:
  299. //------------
  300. // If the row is empty then skip this one, there's nothing to validate.
  301. if (!($type_id || $type_name) && !$subject_name && !$object_name) {
  302. return;
  303. }
  304. // Catch the case where a user is trying to remove a relationship. All
  305. // widget fields are empty but the $type_id is not. We must therefore
  306. // make sure everything is empty.
  307. if (!$type_name && !$subject_name && !$object_name) {
  308. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = '';
  309. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = '';
  310. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = '';
  311. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__value'] = '';
  312. if (array_key_exists('rank', $this->schema['fields'])) {
  313. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = '';
  314. }
  315. return;
  316. }
  317. // Do not proceed if subject ID or object ID does not exist.
  318. if (!key_exists($subject_id_key, $fkeys[$base_table]['columns']) ||
  319. !key_exists($object_id_key, $fkeys[$base_table]['columns'])) {
  320. return;
  321. }
  322. // Validation is occurring in the field::validate() but we need to know if
  323. // it finds errors. As such, I'm calling it here to check. Also,
  324. // field::validate() doesn't seem to always show it's errors OR stop form
  325. // submission so we need to ensure that happens here.
  326. // sbo__relationship::validate($entity_type, $entity, $langcode, $items, &$errors)
  327. $errors = $this->field_instance->validateItem($form_state['values'][$field_name][$langcode][$delta], $element['#chado_record_id']);
  328. if ($errors) {
  329. foreach ($errors as $error) {
  330. switch ($error['element']) {
  331. case 'subject':
  332. form_set_error('sbo__relationship][' . $langcode . '][' . $delta . '][subject_name', $error['message']);
  333. break;
  334. case 'type':
  335. form_set_error('sbo__relationship][' . $langcode . '][' . $delta, $error['message']);
  336. break;
  337. case 'object':
  338. form_set_error('sbo__relationship][' . $langcode . '][' . $delta . '][object_name', $error['message']);
  339. break;
  340. default:
  341. form_set_error('sbo__relationship][' . $langcode . '][' . $delta, $error['message']);
  342. }
  343. }
  344. }
  345. // If there are no other validation errors then ensure data is prepared for
  346. // the storage back-end.
  347. else {
  348. // Get the relationship type record.
  349. if ($type_name && $voc_id) {
  350. $val = [
  351. 'cv_id' => $voc_id,
  352. 'name' => $type_name,
  353. ];
  354. $cvterm = chado_generate_var('cvterm', $val);
  355. if (isset($cvterm->cvterm_id)) {
  356. $type_id = $cvterm->cvterm_id;
  357. }
  358. }
  359. // Get the subject ID.
  360. $subject_id = '';
  361. $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
  362. $matches = [];
  363. // First check if it's in the textfield due to use of the autocomplete.
  364. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  365. $subject_id = $matches[1];
  366. }
  367. // Otherwise we need to look it up using the name field determined in the
  368. // constructor for the current field. There may be more then one name field
  369. // (e.g. organism: genus + species) so we want to check both.
  370. else {
  371. $sql = 'SELECT ' . $fkey_rcolumn . ' FROM {' . $base_table . '} WHERE ' . implode('||', $this->base_name_columns) . '=:keyword';
  372. $subject = chado_query($sql, [':keyword' => $subject_name])->fetchAll();
  373. if (count($subject) > 0) {
  374. $subject_id = $subject[0]->$fkey_rcolumn;
  375. }
  376. }
  377. // Get the object ID.
  378. $object_id = '';
  379. $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
  380. $matches = [];
  381. // First check if it's in the textfield due to use of the autocomplete.
  382. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  383. $object_id = $matches[1];
  384. }
  385. // Otherwise we need to look it up using the name field determined in the
  386. // constructor for the current field. There may be more then one name field
  387. // (e.g. organism: genus + species) so we want to check both.
  388. else {
  389. $sql = 'SELECT ' . $fkey_rcolumn . ' FROM {' . $base_table . '} WHERE ' . implode('||', $this->base_name_columns) . '=:keyword';
  390. $object = chado_query($sql, [':keyword' => $object_name])->fetchAll();
  391. if (count($object) > 0) {
  392. $object_id = $object[0]->$fkey_rcolumn;
  393. }
  394. }
  395. // If we have all three values required for a relationship
  396. // then set them as the chado field storage expects them to be set.
  397. if ($subject_id && $object_id && $type_id) {
  398. // Set the IDs according to the values that were determined above.
  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 . '__' . $subject_id_key] = $subject_id;
  401. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = $object_id;
  402. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
  403. if (array_key_exists('rank', $this->schema['fields'])) {
  404. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
  405. }
  406. }
  407. // Otherwise, maybe we are creating the entity...
  408. // The storage API sohuld handle this case and automagically add the key in once
  409. // the chado record is created... so all we need to do is set the other columns.
  410. elseif ($subject_name && $object_id && $type_id) {
  411. $form_state['values'][$field_name][$langcode][$delta]['value'] = 'value must be set but is not used';
  412. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = $object_id;
  413. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
  414. if (array_key_exists('rank', $this->schema['fields'])) {
  415. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
  416. }
  417. }
  418. elseif ($subject_id && $object_name && $type_id) {
  419. $form_state['values'][$field_name][$langcode][$delta]['value'] = 'value must be set but is not used';
  420. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $subject_id;
  421. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
  422. if (array_key_exists('rank', $this->schema['fields'])) {
  423. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
  424. }
  425. }
  426. // Otherwise, we don't have a vallue to insert so leave them blank.
  427. else {
  428. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = '';
  429. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = '';
  430. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = '';
  431. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__value'] = '';
  432. if (array_key_exists('rank', $this->schema['fields'])) {
  433. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = '';
  434. }
  435. }
  436. }
  437. return $errors;
  438. }
  439. /**
  440. * Theme function for the sbo__relationship_widget.
  441. */
  442. public function theme($element) {
  443. $layout = "
  444. <div class=\"chado-linker--relationship-widget\">
  445. <div class=\"chado-linker--relationship-widget-item\">" .
  446. drupal_render($element['subject_name']) . "
  447. </div>
  448. <div class=\"chado-linker--relationship-widget-item\">" .
  449. drupal_render($element['vocabulary']) . "
  450. </div>
  451. <div class=\"chado-linker--relationship-widget-item\">" .
  452. drupal_render($element['type_name']) . "
  453. </div>
  454. <div class=\"chado-linker--relationship-widget-item\">" .
  455. drupal_render($element['type_id']) . "
  456. </div>
  457. <div>" .
  458. drupal_render($element['object_name']) . "
  459. </div>
  460. </div>
  461. ";
  462. return $layout;
  463. }
  464. /**
  465. * Retrieve options for the type drop-down for the relationship widget.
  466. */
  467. public function get_rtype_select_options() {
  468. // This is slated for Release 2 of this widget.
  469. // It still needs extensive functional and automated testing.
  470. // Thus for now we are falling back on the Default option:
  471. // Form will provide a type autocomplete + vocab select.
  472. // @todo test this.
  473. return FALSE;
  474. // Get the instance settings. There are three options for how this widget
  475. // will be displayed. Those are controlled in the instance settings
  476. // of the field.
  477. // Option 1: relationship types are limited to a specific vocabulary.
  478. // Option 2: relationship types are limited to a subset of one vocabulary.
  479. // Option 3: relationship types are limited to a predefined set.
  480. $instance = $this->instance;
  481. $settings = '';
  482. $option1_vocabs = '';
  483. $option2_parent = '';
  484. $option2_vocab = '';
  485. $option3_rtypes = '';
  486. if (array_key_exists('relationships', $instance)) {
  487. $settings = $instance['settings']['relationships'];
  488. $option1_vocabs = $settings['option1_vocabs'];
  489. $option2_vocab = $settings['option2_vocab'];
  490. $option2_parent = $settings['option2_parent'];
  491. $option3_rtypes = $settings['relationship_types'];
  492. }
  493. // For testing if there are selected vocabs for option1 we'll copy the
  494. // contents in a special variable for later.
  495. $option1_test = $option1_vocabs;
  496. // Option 3: Custom list of Relationship Types
  497. $rtype_options = [];
  498. if ($option3_rtypes) {
  499. $rtypes = explode(PHP_EOL, $option3_rtypes);
  500. foreach ($rtypes AS $rtype) {
  501. // Ignore empty lines
  502. if (trim($rtype) == '') {
  503. continue;
  504. }
  505. $term = chado_get_cvterm(['name' => trim($rtype)]);
  506. // Try to get term with vocabulary specified
  507. if (!$term) {
  508. $tmp = explode('|', trim($rtype), 2);
  509. $cv = chado_get_cv(['name' => trim($tmp[0])]);
  510. $rtype = trim($tmp[1]);
  511. $term = chado_get_cvterm(['name' => $rtype, 'cv_id' => $cv->cv_id]);
  512. }
  513. $rtype_options[$term->cvterm_id] = $term->name;
  514. }
  515. return $rtype_options;
  516. }
  517. // Option 2: Child terms of a selected cvterm
  518. else {
  519. if ($option2_vocab) {
  520. $values = [
  521. 'cv_id' => $option2_vocab,
  522. 'name' => $option2_parent,
  523. ];
  524. $parent_term = chado_get_cvterm($values);
  525. // If the term wasn't found then see if it's a synonym.
  526. if (!$parent_term) {
  527. $values = [
  528. 'synonym' => [
  529. 'name' => trim($option2_parent),
  530. ],
  531. ];
  532. $synonym = chado_get_cvterm($values);
  533. if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
  534. $parent_term = $synonym;
  535. }
  536. }
  537. // Get the child terms of the parent term found above.
  538. $sql = "
  539. SELECT subject_id,
  540. (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
  541. FROM {cvtermpath}
  542. WHERE
  543. object_id = :parent_cvterm_id AND
  544. cv_id = :parent_cv_id
  545. ORDER BY name
  546. ";
  547. $args = [
  548. ':parent_cvterm_id' => $parent_term->cvterm_id,
  549. ':parent_cv_id' => $parent_term->cv_id->cv_id,
  550. ];
  551. $results = chado_query($sql, $args);
  552. while ($child = $results->fetchObject()) {
  553. $rtype_options[$child->subject_id] = $child->name;
  554. }
  555. return $rtype_options;
  556. }
  557. // Option 1: All terms of selected vocabularies
  558. else {
  559. if ($option1_test && array_pop($option1_test)) {
  560. $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
  561. $results = chado_query($sql, [':cv_id' => $option1_vocabs]);
  562. while ($obj = $results->fetchObject()) {
  563. $rtype_options[$obj->cvterm_id] = $obj->name;
  564. }
  565. return $rtype_options;
  566. }
  567. // Default option:
  568. // Let the form deal with this by providing a type autocomplete?
  569. else {
  570. return FALSE;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. function theme_sbo__relationship_instance_settings($variables) {
  577. $element = $variables['element'];
  578. $option1 = $element['option1'];
  579. $option1_vocabs = $element['option1_vocabs'];
  580. $option2 = $element['option2'];
  581. $option2_vocab = $element['option2_vocab'];
  582. $option2_parent = $element['option2_parent'];
  583. $option3 = $element['option3'];
  584. $rtype = $element['relationship_types'];
  585. $layout = drupal_render($option1);
  586. $layout .= drupal_render($option1_vocabs);
  587. $layout .=
  588. drupal_render($option2) .
  589. "<div class=\"chado-linker--relationship-instance-settings-option2\">" .
  590. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  591. drupal_render($option2_vocab) .
  592. "</div>" .
  593. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  594. drupal_render($option2_parent) .
  595. "</div>" .
  596. "</div>";
  597. $layout .= drupal_render($option3);
  598. $layout .= drupal_render($rtype);
  599. return $layout;
  600. }
  601. /**
  602. * An Ajax callback for the relationshp widget.
  603. */
  604. function sbo__relationship_widget_form_ajax_callback(&$form, $form_state) {
  605. // Get the triggering element
  606. $form_element_name = $form_state['triggering_element']['#name'];
  607. preg_match('/(.+?)\[(.+?)\]\[(.+?)\]/', $form_element_name, $matches);
  608. $field = $matches[1];
  609. $lang = $matches[2];
  610. $delta = $matches[3];
  611. // Return the widget that triggered the AJAX call
  612. if (isset($form[$field][$lang][$delta])) {
  613. return $form[$field][$lang][$delta];
  614. }
  615. // Alternatively, return the default value widget for the widget setting form
  616. else {
  617. return $form['instance']['default_value_widget'][$field];
  618. }
  619. }
  620. /**
  621. * An Ajax callback for the relationshp instance setting form.
  622. */
  623. function sbo__relationship_instance_settings_form_ajax_callback(&$form, &$form_state) {
  624. $acpath = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'];
  625. $acpath .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'] . '/';
  626. $urlval = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'];
  627. $urlval .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'];
  628. // Reset value if a different vocabulary is selected
  629. $form['instance']['settings']['relationships']['option2_parent']['#value'] = NULL;
  630. $form_state['values']['instance']['settings']['relationships']['option2_parent'] = NULL;
  631. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'] = $acpath;
  632. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'] = $urlval;
  633. return $form['instance']['settings']['relationships']['option2_parent'];
  634. }