data__accession_widget.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. class data__accession_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Site Accession';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = ['data__accession'];
  7. /**
  8. * @see TripalFieldWidget::form()
  9. */
  10. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  11. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  12. $field_name = $this->field['field_name'];
  13. $field_type = $this->field['type'];
  14. $field_table = $this->instance['settings']['chado_table'];
  15. $field_column = $this->instance['settings']['chado_column'];
  16. // Get the field defaults.
  17. $dbxref_id = '';
  18. $db_id = '';
  19. $accession = '';
  20. // If the field already has a value then it will come through the $items
  21. // array. This happens when editing an existing record.
  22. if (count($items) > 0 and array_key_exists($delta, $items)) {
  23. $dbxref_id = $items[$delta]['chado-' . $field_table . '__' . $field_column];
  24. $db_id = $items[$delta]['db_id'];
  25. $accession = $items[$delta]['accession'];
  26. }
  27. // Check $form_state['values'] to see if an AJAX call set the values.
  28. if (array_key_exists('values', $form_state) and
  29. array_key_exists($field_name, $form_state['values'])) {
  30. $dbxref_id = isset($form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $field_column]) ? $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $field_column] : '';
  31. $db_id = isset($form_state['values'][$field_name]['und'][$delta]['db_id']) ? $form_state['values'][$field_name]['und'][$delta]['db_id'] : '';
  32. $accession = isset($form_state['values'][$field_name]['und'][$delta]['accession']) ? $form_state['values'][$field_name]['und'][$delta]['accession'] : '';
  33. }
  34. $schema = chado_get_schema('dbxref');
  35. $options = chado_get_db_select_options();
  36. //$widget['#element_validate'] = array('chado_base__dbxref_id_widget_validate');
  37. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id'>";
  38. $widget['#suffix'] = "</span>";
  39. $widget['value'] = [
  40. '#type' => 'value',
  41. '#value' => $dbxref_id,
  42. ];
  43. $widget['chado-' . $field_table . '__' . $field_column] = [
  44. '#type' => 'value',
  45. '#default_value' => $dbxref_id,
  46. ];
  47. $widget['db_id'] = [
  48. '#type' => 'select',
  49. '#title' => t('Database'),
  50. '#options' => $options,
  51. '#required' => $element['#required'],
  52. '#default_value' => $db_id,
  53. '#ajax' => [
  54. 'callback' => "data__accession_widget_form_ajax_callback",
  55. 'wrapper' => "$field_name-dbxref--db-id",
  56. 'effect' => 'fade',
  57. 'method' => 'replace',
  58. ],
  59. ];
  60. $widget['accession'] = [
  61. '#type' => 'textfield',
  62. '#title' => t('Accession'),
  63. '#default_value' => $accession,
  64. '#required' => $element['#required'],
  65. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  66. '#size' => 15,
  67. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  68. '#disabled' => $db_id ? FALSE : TRUE,
  69. ];
  70. }
  71. /**
  72. * @see TripalFieldWidget::submit()
  73. */
  74. public function validate($element, $form, &$form_state, $langcode, $delta) {
  75. $field_name = $this->field['field_name'];
  76. $settings = $this->field['settings'];
  77. $field_name = $this->field['field_name'];
  78. $field_type = $this->field['type'];
  79. $field_table = $this->instance['settings']['chado_table'];
  80. $field_column = $this->instance['settings']['chado_column'];
  81. $dbxref_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'];
  82. $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
  83. $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];
  84. // Is this field required?
  85. if ($element['#required'] and !$db_id) {
  86. form_set_error($field_name . '][und][0][db_id', "A database for the accession must be provided.");
  87. }
  88. if ($element['#required'] and !$accession) {
  89. form_set_error($field_name . '][und][0][accession', "An accession number must be provided.");
  90. }
  91. // If user did not select a database, we want to remove dbxref_id from the
  92. // field. We use '__NULL__' because this field is part of the base table
  93. // and this tells the Chado backend to insert a null rather than an empty
  94. // string.
  95. if (!$db_id) {
  96. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = '__NULL__';
  97. }
  98. // If the dbxref_id does not match the db_id + accession then the user
  99. // has selected a new dbxref record and we need to update the hidden
  100. // value accordingly.
  101. if ($db_id and $accession) {
  102. $dbxref = chado_generate_var('dbxref', [
  103. 'db_id' => $db_id,
  104. 'accession' => $accession,
  105. ]);
  106. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  107. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
  108. $form_state['values'][$field_name]['und'][$delta]['value'] = $dbxref->dbxref_id;
  109. }
  110. }
  111. }
  112. /**
  113. * @see TripalFieldWidget::submit()
  114. */
  115. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  116. $field_name = $this->field['field_name'];
  117. $settings = $this->field['settings'];
  118. $field_name = $this->field['field_name'];
  119. $field_type = $this->field['type'];
  120. $field_table = $this->instance['settings']['chado_table'];
  121. $field_column = $this->instance['settings']['chado_column'];
  122. $dbxref_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'];
  123. $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
  124. $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];
  125. // If the accession doesn't exist then add it.
  126. if ($db_id and $accession) {
  127. $dbxref = chado_generate_var('dbxref', [
  128. 'db_id' => $db_id,
  129. 'accession' => $accession,
  130. ]);
  131. if (!$dbxref) {
  132. $values = [
  133. 'db_id' => $db_id,
  134. 'accession' => $accession,
  135. ];
  136. $dbxref = chado_insert_dbxref($values);
  137. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
  138. $form_state['values'][$field_name]['und'][$delta]['value'] = $dbxref->dbxref_id;
  139. }
  140. }
  141. }
  142. /**
  143. * @see TripalFieldWidget::theme()
  144. */
  145. public function theme($element) {
  146. $layout = "
  147. <div class=\"primary-dbxref-widget\">
  148. <div class=\"primary-dbxref-widget-item\">" .
  149. drupal_render($element['db_id']) . "
  150. </div>
  151. <div class=\"primary-dbxref-widget-item\">" .
  152. drupal_render($element['accession']) . "
  153. </div>
  154. </div>
  155. ";
  156. $fieldset = [
  157. '#title' => $element['#title'],
  158. '#value' => '',
  159. '#description' => $element['#description'],
  160. '#children' => $layout,
  161. ];
  162. return theme('fieldset', ['element' => $fieldset]);
  163. }
  164. }
  165. /**
  166. * An Ajax callback for the tripal_chado_admin_publish_form..
  167. */
  168. function data__accession_widget_form_ajax_callback($form, $form_state) {
  169. $instance = $form['#instance'];
  170. $field_name = $form_state['triggering_element']['#parents'][0];
  171. $dbxref_id = $form_state['input'][$field_name]['und'][0]['chado-' . $field_table . '__dbxref_id'];
  172. $db_id = $form_state['input'][$field_name]['und'][0]['db_id'];
  173. $accession = $form_state['input'][$field_name]['und'][0]['accession'];
  174. // If we don't have a match then this must be new accession. Because
  175. // this is a database defined access we will automatically add the
  176. // accession.
  177. if ($db_id and $accession) {
  178. $dbxref = chado_generate_var('dbxref', [
  179. 'db_id' => $db_id,
  180. 'accession' => $accession,
  181. ]);
  182. if (!$dbxref) {
  183. drupal_set_message('The accession provided does not exist in the database and will be added.', 'warning');
  184. }
  185. }
  186. return $form[$field_name];
  187. }