data__accession_widget.inc 7.8 KB

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