data__accession_widget.inc 7.9 KB

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