data__accession_widget.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 = array('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'] = array(
  40. '#type' => 'value',
  41. '#value' => $dbxref_id,
  42. );
  43. $widget['chado-' . $field_table . '__' . $field_column] = array(
  44. '#type' => 'value',
  45. '#default_value' => $dbxref_id,
  46. );
  47. $widget['db_id'] = array(
  48. '#type' => 'select',
  49. '#title' => t('Database'),
  50. '#options' => $options,
  51. '#required' => $element['#required'],
  52. '#default_value' => $db_id,
  53. '#ajax' => array(
  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'] = array(
  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', array('db_id' => $db_id, 'accession' => $accession));
  103. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  104. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
  105. $form_state['values'][$field_name]['und'][$delta]['value'] = $dbxref->dbxref_id;
  106. }
  107. }
  108. }
  109. /**
  110. * @see TripalFieldWidget::submit()
  111. */
  112. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  113. $field_name = $this->field['field_name'];
  114. $settings = $this->field['settings'];
  115. $field_name = $this->field['field_name'];
  116. $field_type = $this->field['type'];
  117. $field_table = $this->instance['settings']['chado_table'];
  118. $field_column = $this->instance['settings']['chado_column'];
  119. $dbxref_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'];
  120. $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
  121. $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];
  122. // If the accession doesn't exist then add it.
  123. if ($db_id and $accession) {
  124. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  125. if (!$dbxref) {
  126. $values = array(
  127. 'db_id' => $db_id,
  128. 'accession' => $accession,
  129. );
  130. $dbxref = chado_insert_dbxref($values);
  131. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
  132. $form_state['values'][$field_name]['und'][$delta]['value'] = $dbxref->dbxref_id;
  133. }
  134. }
  135. }
  136. /**
  137. * @see TripalFieldWidget::theme()
  138. */
  139. public function theme($element) {
  140. $layout = "
  141. <div class=\"primary-dbxref-widget\">
  142. <div class=\"primary-dbxref-widget-item\">" .
  143. drupal_render($element['db_id']) . "
  144. </div>
  145. <div class=\"primary-dbxref-widget-item\">" .
  146. drupal_render($element['accession']) . "
  147. </div>
  148. </div>
  149. ";
  150. $fieldset = array(
  151. '#title' => $element['#title'],
  152. '#value' => '',
  153. '#description' => $element['#description'],
  154. '#children' => $layout,
  155. );
  156. return theme('fieldset', array('element' => $fieldset));
  157. }
  158. }
  159. /**
  160. * An Ajax callback for the tripal_chado_admin_publish_form..
  161. */
  162. function data__accession_widget_form_ajax_callback($form, $form_state) {
  163. $instance = $form['#instance'];
  164. $field_name = $form_state['triggering_element']['#parents'][0];
  165. $dbxref_id = $form_state['input'][$field_name]['und'][0]['chado-' . $field_table . '__dbxref_id'];
  166. $db_id = $form_state['input'][$field_name]['und'][0]['db_id'];
  167. $accession = $form_state['input'][$field_name]['und'][0]['accession'];
  168. // If we don't have a match then this must be new accession. Because
  169. // this is a database defined access we will automatically add the
  170. // accession.
  171. if ($db_id and $accession) {
  172. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  173. if (!$dbxref) {
  174. drupal_set_message('The accession provided does not exist in the database and will be added.', 'warning');
  175. }
  176. }
  177. return $form[$field_name];
  178. }