data__accession_widget.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. $fk_val = '';
  18. $db_id = '';
  19. $accession = '';
  20. $version = '';
  21. $description = '';
  22. // If the field already has a value then it will come through the $items
  23. // array. This happens when editing an existing record.
  24. if (count($items) > 0 and array_key_exists($delta, $items)) {
  25. $fk_val = $items[$delta]['chado-' . $field_table . '__' . $field_column];
  26. $db_id = $items[$delta]['db_id'];
  27. $accession = $items[$delta]['accession'];
  28. $version = $items[$delta]['version'];
  29. $description = $items[$delta]['description'];
  30. }
  31. // Check $form_state['values'] to see if an AJAX call set the values.
  32. if (array_key_exists('values', $form_state)) {
  33. $fk_val = 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] : '';
  34. $db_id = isset($form_state['values'][$field_name]['und'][$delta]['db_id']) ? $form_state['values'][$field_name]['und'][$delta]['db_id'] : '';
  35. $accession = isset($form_state['values'][$field_name]['und'][$delta]['accession']) ? $form_state['values'][$field_name]['und'][$delta]['accession'] : '';
  36. $version = isset($form_state['values'][$field_name]['und'][$delta]['version']) ? $form_state['values'][$field_name]['und'][$delta]['version'] : '';
  37. $description = isset($form_state['values'][$field_name]['und'][$delta]['description']) ? $form_state['values'][$field_name]['und'][$delta]['description'] : '';
  38. }
  39. $schema = chado_get_schema('dbxref');
  40. $options = tripal_get_db_select_options();
  41. //$widget['#element_validate'] = array('chado_base__dbxref_id_widget_validate');
  42. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id'>";
  43. $widget['#suffix'] = "</span>";
  44. $widget['value'] = array(
  45. '#type' => 'value',
  46. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  47. );
  48. $widget['chado-' . $field_table . '__' . $field_column] = array(
  49. '#type' => 'value',
  50. '#default_value' => $fk_val,
  51. );
  52. $widget['db_id'] = array(
  53. '#type' => 'select',
  54. '#title' => t('Database'),
  55. '#options' => $options,
  56. '#required' => $element['#required'],
  57. '#default_value' => $db_id,
  58. '#ajax' => array(
  59. 'callback' => "data__accession_widget_form_ajax_callback",
  60. 'wrapper' => "$field_name-dbxref--db-id",
  61. 'effect' => 'fade',
  62. 'method' => 'replace'
  63. ),
  64. );
  65. $widget['accession'] = array(
  66. '#type' => 'textfield',
  67. '#title' => t('Accession'),
  68. '#default_value' => $accession,
  69. '#required' => $element['#required'],
  70. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  71. '#size' => 15,
  72. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  73. '#ajax' => array(
  74. 'callback' => "tripal_chado_dbxref_widget_form_ajax_callback",
  75. 'wrapper' => "$field_name-dbxref--db-id",
  76. 'effect' => 'fade',
  77. 'method' => 'replace'
  78. ),
  79. '#disabled' => $db_id ? FALSE : TRUE,
  80. );
  81. $widget['version'] = array(
  82. '#type' => 'textfield',
  83. '#title' => t('Version'),
  84. '#default_value' => $version,
  85. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  86. '#size' => 5,
  87. '#disabled' => $db_id ? FALSE : TRUE,
  88. );
  89. $widget['description'] = array(
  90. '#type' => 'textfield',
  91. '#title' => t('Description'),
  92. '#default_value' => $description,
  93. '#size' => 20,
  94. '#disabled' => $db_id ? FALSE : TRUE,
  95. );
  96. $widget['links'] = array(
  97. '#type' => 'item',
  98. '#markup' => l('Add a new database', 'admin/tripal/loaders/chado_db/add', array('attributes' => array('target' => '_blank')))
  99. );
  100. }
  101. /**
  102. * @see TripalFieldWidget::submit()
  103. */
  104. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  105. $field_name = $this->field['field_name'];
  106. $settings = $this->field['settings'];
  107. $field_name = $this->field['field_name'];
  108. $field_type = $this->field['type'];
  109. $field_table = $this->instance['settings']['chado_table'];
  110. $field_column = $this->instance['settings']['chado_column'];
  111. $fk_val = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $field_column]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $field_column] : '';
  112. $db_id = isset($form_state['values'][$field_name][$langcode][$delta]['db_id']) ? $form_state['values'][$field_name][$langcode][$delta]['db_id'] : '';
  113. $accession = isset($form_state['values'][$field_name][$langcode][$delta]['accession']) ? $form_state['values'][$field_name][$langcode][$delta]['accession'] : '';
  114. if (!$accession) {
  115. $accession = $form_state['values'][$field_name][$langcode][$delta]['value'];
  116. }
  117. $version = isset($form_state['values'][$field_name][$langcode][$delta]['version']) ? $form_state['values'][$field_name][$langcode][$delta]['version'] : '';
  118. // If user did not select a database, we want to remove dbxref_id from the
  119. // field.
  120. if (!$db_id) {
  121. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__dbxref_id'] = '__NULL__';
  122. }
  123. // If the dbxref_id does not match the db_id + accession then the user
  124. // has selected a new dbxref record and we need to update the hidden
  125. // value accordingly.
  126. if ($db_id and $accession) {
  127. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  128. if ($dbxref and $dbxref->dbxref_id != $fk_val) {
  129. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
  130. }
  131. }
  132. }
  133. /**
  134. * @see TripalFieldWidget::theme()
  135. */
  136. public function theme($element) {
  137. $layout = "
  138. <div class=\"primary-dbxref-widget\">
  139. <div class=\"primary-dbxref-widget-item\">" .
  140. drupal_render($element['db_id']) . "
  141. </div>
  142. <div class=\"primary-dbxref-widget-item\">" .
  143. drupal_render($element['accession']) . "
  144. </div>
  145. <div class=\"primary-dbxref-widget-item\">" .
  146. drupal_render($element['version']) . "
  147. </div>
  148. <div class=\"primary-dbxref-widget-item\">" .
  149. drupal_render($element['description']) . "
  150. </div>
  151. <div class=\"primary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  152. </div>
  153. ";
  154. $fieldset = array(
  155. '#title' => $element['#title'],
  156. '#value' => '',
  157. '#description' => $element['#description'],
  158. '#children' => $layout,
  159. // '#attributes' => array('class' => $classes),
  160. );
  161. return theme('fieldset', array('element' => $fieldset));
  162. }
  163. }
  164. /**
  165. * An Ajax callback for the tripal_chado_admin_publish_form..
  166. */
  167. function data__accession_widget_form_ajax_callback($form, $form_state) {
  168. $instance = $form['#instance'];
  169. $field_name = $form_state['triggering_element']['#parents'][0];
  170. $field = field_info_field($field_name);
  171. $field_type = $field['type'];
  172. $field_table = $instance['settings']['chado_table'];
  173. $field_column = $instance['settings']['chado_column'];
  174. $field_prefix = 'chado-' . $field_table . '__' . $field_column;
  175. // $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--db_id');
  176. // $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--accession');
  177. if ($db_id and $accession) {
  178. $values = array(
  179. 'db_id' => $db_id,
  180. 'accession' => $accession,
  181. );
  182. $options = array('is_duplicate' => TRUE);
  183. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  184. if (!$has_duplicate) {
  185. drupal_set_message('The selected cross reference is new and will be added for future auto completions.', 'warning');
  186. }
  187. }
  188. return $form[$field_name];
  189. }