sbo__relationship_widget.inc 30 KB

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