data__accession_widget.inc

File

tripal_chado/includes/TripalFields/data__accession/data__accession_widget.inc
View source
  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. // If user did not select a database, we want to remove dbxref_id from the
  85. // field. We use '__NULL__' because this field is part of the base table
  86. // and this tells the Chado backend to insert a null rather than an empty
  87. // string.
  88. if (!$db_id) {
  89. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = '__NULL__';
  90. }
  91. // If the dbxref_id does not match the db_id + accession then the user
  92. // has selected a new dbxref record and we need to update the hidden
  93. // value accordingly.
  94. if ($db_id and $accession) {
  95. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  96. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  97. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
  98. $form_state['values'][$field_name]['und'][$delta]['value'] = $dbxref->dbxref_id;
  99. }
  100. }
  101. }
  102. /**
  103. * @see TripalFieldWidget::submit()
  104. */
  105. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  106. $field_name = $this->field['field_name'];
  107. $settings = $this->field['settings'];
  108. $field_name = $this->field['field_name'];
  109. $field_type = $this->field['type'];
  110. $field_table = $this->instance['settings']['chado_table'];
  111. $field_column = $this->instance['settings']['chado_column'];
  112. $dbxref_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'];
  113. $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
  114. $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];
  115. // If the accession doesn't exist then add it.
  116. if ($db_id and $accession) {
  117. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  118. if (!$dbxref) {
  119. $values = array(
  120. 'db_id' => $db_id,
  121. 'accession' => $accession,
  122. );
  123. $dbxref = chado_insert_dbxref($values);
  124. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
  125. $form_state['values'][$field_name]['und'][$delta]['value'] = $dbxref->dbxref_id;
  126. }
  127. }
  128. }
  129. /**
  130. * @see TripalFieldWidget::theme()
  131. */
  132. public function theme($element) {
  133. $layout = "
  134. <div class=\"primary-dbxref-widget\">
  135. <div class=\"primary-dbxref-widget-item\">" .
  136. drupal_render($element['db_id']) . "
  137. </div>
  138. <div class=\"primary-dbxref-widget-item\">" .
  139. drupal_render($element['accession']) . "
  140. </div>
  141. </div>
  142. ";
  143. $fieldset = array(
  144. '#title' => $element['#title'],
  145. '#value' => '',
  146. '#description' => $element['#description'],
  147. '#children' => $layout,
  148. );
  149. return theme('fieldset', array('element' => $fieldset));
  150. }
  151. }
  152. /**
  153. * An Ajax callback for the tripal_chado_admin_publish_form..
  154. */
  155. function data__accession_widget_form_ajax_callback($form, $form_state) {
  156. $instance = $form['#instance'];
  157. $field_name = $form_state['triggering_element']['#parents'][0];
  158. $dbxref_id = $form_state['input'][$field_name]['und'][0]['chado-' . $field_table . '__dbxref_id'];
  159. $db_id = $form_state['input'][$field_name]['und'][0]['db_id'];
  160. $accession = $form_state['input'][$field_name]['und'][0]['accession'];
  161. // If we don't have a match then this must be new accession. Because
  162. // this is a database defined access we will automatically add the
  163. // accession.
  164. if ($db_id and $accession) {
  165. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  166. if (!$dbxref) {
  167. drupal_set_message('The accession provided does not exist in the database and will be added.', 'warning');
  168. }
  169. }
  170. return $form[$field_name];
  171. }