sbo__database_cross_reference_widget.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. class sbo__database_cross_reference_widget extends TripalFieldWidget {
  3. // The default lable for this field.
  4. public static $label = 'Database cross reference';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('sbo__database_cross_reference');
  7. /**
  8. *
  9. * @see TripalFieldWidget::form()
  10. */
  11. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  12. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  13. $field_name = $this->field['field_name'];
  14. $field_type = $this->field['type'];
  15. $field_table = $this->field['settings']['chado_table'];
  16. $field_column = $this->field['settings']['chado_column'];
  17. // Get the FK column that links to the base table.
  18. $chado_table = $this->field['settings']['chado_table'];
  19. $base_table = $this->field['settings']['base_table'];
  20. $schema = chado_get_schema($chado_table);
  21. $pkey = $schema['primary key'][0];
  22. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  23. $fkey = $fkeys[0];
  24. // Get the field defaults.
  25. $record_id = '';
  26. $fkey_value = $element['#entity']->chado_record_id;
  27. $dbxref_id = '';
  28. $db_id = '';
  29. $accession = '';
  30. $version = '';
  31. $description = '';
  32. // If the field already has a value then it will come through the $items
  33. // array. This happens when editing an existing record.
  34. if (count($items) > 0 and array_key_exists($delta, $items)) {
  35. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  36. $fkey_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $fkey, $fkey_value);
  37. $dbxref_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__dbxref_id', $dbxref_id);
  38. $db_id = tripal_get_field_item_keyval($items, $delta, 'db_id', $db_id);
  39. $accession = tripal_get_field_item_keyval($items, $delta, 'accession', $accession);
  40. $version = tripal_get_field_item_keyval($items, $delta, 'version', $version);
  41. $description = tripal_get_field_item_keyval($items, $delta, 'description', $description);
  42. }
  43. // Check $form_state['values'] to see if an AJAX call set the values.
  44. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  45. $record_id = $form_state['values'][$field_name]['und'][$delta][$field_table . '__' . $pkey];
  46. $fkey_value = $form_state['values'][$field_name]['und'][$delta][$field_table . '__' . $fkey];
  47. $dbxref_id = $form_state['values'][$field_name]['und'][$delta][$field_table . '__dbxref_id'];
  48. $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
  49. $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];
  50. $version = $form_state['values'][$field_name]['und'][$delta]['version'];
  51. $description = $form_state['values'][$field_name]['und'][$delta]['description'];
  52. }
  53. $schema = chado_get_schema('dbxref');
  54. $options = tripal_get_db_select_options();
  55. $widget['#table_name'] = $chado_table;
  56. $widget['#fkey_field'] = $fkey;
  57. //$widget['#element_validate'] = array('sbo__database_cross_reference_widget_validate');
  58. $widget['#theme'] = 'sbo__database_cross_reference_widget';
  59. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id-$delta'>";
  60. $widget['#suffix'] = "</span>";
  61. $widget['value'] = array(
  62. '#type' => 'value',
  63. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  64. );
  65. $widget['chado-' . $field_table . '__' . $pkey] = array(
  66. '#type' => 'value',
  67. '#default_value' => $record_id,
  68. );
  69. $widget['chado-' . $field_table . '__' . $fkey] = array(
  70. '#type' => 'value',
  71. '#default_value' => $fkey_value,
  72. );
  73. $widget['chado-' . $field_table . '__dbxref_id'] = array(
  74. '#type' => 'value',
  75. '#default_value' => $dbxref_id,
  76. );
  77. $widget['dbxref_id'] = array(
  78. '#type' => 'value',
  79. '#default_value' => $dbxref_id,
  80. );
  81. $widget['db_id'] = array(
  82. '#type' => 'select',
  83. '#title' => t('Database'),
  84. '#options' => $options,
  85. '#required' => $element['#required'],
  86. '#default_value' => $db_id,
  87. '#ajax' => array(
  88. 'callback' => "sbo__database_cross_reference_widget_form_ajax_callback",
  89. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  90. 'effect' => 'fade',
  91. 'method' => 'replace'
  92. ),
  93. );
  94. $widget['accession'] = array(
  95. '#type' => 'textfield',
  96. '#title' => t('Accession'),
  97. '#default_value' => $accession,
  98. '#required' => $element['#required'],
  99. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  100. '#size' => 15,
  101. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  102. '#ajax' => array(
  103. 'callback' => "sbo__database_cross_reference_widget_form_ajax_callback",
  104. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  105. 'effect' => 'fade',
  106. 'method' => 'replace'
  107. ),
  108. '#disabled' => $db_id ? FALSE : TRUE,
  109. );
  110. $widget['version'] = array(
  111. '#type' => 'textfield',
  112. '#title' => t('Version'),
  113. '#default_value' => $version,
  114. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  115. '#size' => 5,
  116. '#disabled' => $db_id ? FALSE : TRUE,
  117. );
  118. $widget['description'] = array(
  119. '#type' => 'textfield',
  120. '#title' => t('Description'),
  121. '#default_value' => $description,
  122. '#size' => 20,
  123. '#disabled' => $db_id ? FALSE : TRUE,
  124. );
  125. if (!$db_id) {
  126. $widget['links'] = array(
  127. '#type' => 'item',
  128. '#markup' => l('Add a database', 'admin/tripal/legacy/tripal_db/add', array('attributes' => array('target' => '_blank')))
  129. );
  130. }
  131. }
  132. /**
  133. * Performs validation of the widgetForm.
  134. *
  135. * Use this validate to ensure that form values are entered correctly. Note
  136. * this is different from the validate() function which ensures that the
  137. * field data meets expectations.
  138. *
  139. * @param $form
  140. * @param $form_state
  141. */
  142. public function validate($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  143. $field_name = $this->field['field_name'];
  144. $field_type = $this->field['type'];
  145. $table_name = $this->field['settings']['chado_table'];
  146. $field_table = $this->field['settings']['chado_table'];
  147. $field_column = $this->field['settings']['chado_column'];
  148. $base_table = $this->field['settings']['base_table'];
  149. $schema = chado_get_schema($table_name);
  150. $pkey = $schema['primary key'][0];
  151. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  152. $fkey = $fkeys[0];
  153. // Get the field values.
  154. foreach ($items as $delta => $values) {
  155. // Get the field values.
  156. $dbxref_id = $values['chado-' . $field_table . '__dbxref_id'];
  157. $db_id = $values['db_id'];
  158. $accession = $values['accession'];
  159. $version = $values['version'];
  160. $description = $values['description'];
  161. // Make sure that if a database ID is provided that an accession is also
  162. // provided. Here we use the form_set_error function rather than the
  163. // form_error function because the form_error will add a red_highlight
  164. // around all of the fields in the fieldset which is confusing as it's not
  165. // clear to the user what field is required and which isn't. Therefore,
  166. // we borrow the code from the 'form_error' function and append the field
  167. // so that the proper field is highlighted on error.
  168. if (!$db_id and $accession) {
  169. $errors[$field_name][$delta]['und'][] = array(
  170. 'message' => t("A database and the accession must both be provided."),
  171. 'error' => 'sbo__database_cross_reference',
  172. );
  173. }
  174. if ($db_id and !$accession) {
  175. $errors[$field_name][$delta]['und'][] = array(
  176. 'message' => t("A database and the accession must both be provided."),
  177. 'error' => 'sbo__database_cross_reference',
  178. );
  179. }
  180. if (!$db_id and !$accession and ($version or $description)) {
  181. $errors[$field_name][$delta]['und'][] = array(
  182. 'message' => t("A database and the accession must both be provided."),
  183. 'error' => 'sbo__database_cross_reference',
  184. );
  185. }
  186. }
  187. }
  188. /**
  189. *
  190. * @see TripalFieldWidget::submit()
  191. */
  192. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  193. $field_name = $this->field['field_name'];
  194. $field_type = $this->field['type'];
  195. $table_name = $this->field['settings']['chado_table'];
  196. $field_table = $this->field['settings']['chado_table'];
  197. $field_column = $this->field['settings']['chado_column'];
  198. $base_table = $this->field['settings']['base_table'];
  199. $schema = chado_get_schema($table_name);
  200. $pkey = $schema['primary key'][0];
  201. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  202. $fkey = $fkeys[0];
  203. // Get the field values.
  204. $dbxref_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__dbxref_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__dbxref_id'] : '';
  205. $db_id = isset($form_state['values'][$field_name][$langcode][$delta]['db_id']) ? $form_state['values'][$field_name][$langcode][$delta]['db_id'] : '';
  206. $accession = isset($form_state['values'][$field_name][$langcode][$delta]['accession']) ? $form_state['values'][$field_name][$langcode][$delta]['accession'] : '';
  207. $version = isset($form_state['values'][$field_name][$langcode][$delta]['version']) ? $form_state['values'][$field_name][$langcode][$delta]['version'] : '';
  208. $description = isset($form_state['values'][$field_name][$langcode][$delta]['description']) ? $form_state['values'][$field_name][$langcode][$delta]['description'] : '';
  209. // If the dbxref_id does not match the db_id + accession then the user
  210. // has selected a new dbxref record and we need to update the hidden
  211. // value accordingly.
  212. if ($db_id and $accession) {
  213. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  214. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  215. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__dbxref_id'] = $dbxref->dbxref_id;
  216. $form_state['values'][$field_name][$langcode][$delta]['dbxref_id'] = $dbxref->dbxref_id;
  217. }
  218. }
  219. // If the db_id and accession are not set, then remove the linker FK
  220. // value to the base table, but leave the primary key so the record
  221. // can be deleted.
  222. else {
  223. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  224. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__dbxref_id'] = '';
  225. }
  226. }
  227. }