sbo__relationship_widget.inc 30 KB

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