data__accession_widget.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. class data__accession_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = '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. // A temporary element used for theming the fieldset.
  45. $widget['#theme_settings'] = array(
  46. '#title' => $element['#title'],
  47. '#description' => $element['#description'],
  48. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  49. // '#theme' => 'data__accession_widget',
  50. //'#collapsible' => TRUE,
  51. //'#collapsed' => $collapsed,
  52. );
  53. $widget['value'] = array(
  54. '#type' => 'value',
  55. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  56. );
  57. $widget['chado-' . $field_table . '__' . $field_column] = array(
  58. '#type' => 'value',
  59. '#default_value' => $fk_val,
  60. );
  61. $widget['db_id'] = array(
  62. '#type' => 'select',
  63. '#title' => t('Database'),
  64. '#options' => $options,
  65. '#required' => $element['#required'],
  66. '#default_value' => $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. );
  74. $widget['accession'] = array(
  75. '#type' => 'textfield',
  76. '#title' => t('Accession'),
  77. '#default_value' => $accession,
  78. '#required' => $element['#required'],
  79. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  80. '#size' => 15,
  81. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  82. '#ajax' => array(
  83. 'callback' => "tripal_chado_dbxref_widget_form_ajax_callback",
  84. 'wrapper' => "$field_name-dbxref--db-id",
  85. 'effect' => 'fade',
  86. 'method' => 'replace'
  87. ),
  88. '#disabled' => $db_id ? FALSE : TRUE,
  89. );
  90. $widget['version'] = array(
  91. '#type' => 'textfield',
  92. '#title' => t('Version'),
  93. '#default_value' => $version,
  94. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  95. '#size' => 5,
  96. '#disabled' => $db_id ? FALSE : TRUE,
  97. );
  98. $widget['description'] = array(
  99. '#type' => 'textfield',
  100. '#title' => t('Description'),
  101. '#default_value' => $description,
  102. '#size' => 20,
  103. '#disabled' => $db_id ? FALSE : TRUE,
  104. );
  105. $widget['links'] = array(
  106. '#type' => 'item',
  107. '#markup' => l('Add a new database', 'admin/tripal/legacy/tripal_db/add', array('attributes' => array('target' => '_blank')))
  108. );
  109. }
  110. /**
  111. * @see TripalFieldWidget::submit()
  112. */
  113. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  114. $field_name = $this->field['field_name'];
  115. $settings = $this->field['settings'];
  116. $field_name = $this->field['field_name'];
  117. $field_type = $this->field['type'];
  118. $field_table = $this->instance['settings']['chado_table'];
  119. $field_column = $this->instance['settings']['chado_column'];
  120. $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] : '';
  121. $db_id = isset($form_state['values'][$field_name][$langcode][$delta]['db_id']) ? $form_state['values'][$field_name][$langcode][$delta]['db_id'] : '';
  122. $accession = isset($form_state['values'][$field_name][$langcode][$delta]['accession']) ? $form_state['values'][$field_name][$langcode][$delta]['accession'] : '';
  123. $version = isset($form_state['values'][$field_name][$langcode][$delta]['version']) ? $form_state['values'][$field_name][$langcode][$delta]['version'] : '';
  124. // If user did not select a database, we want to remove dbxref_id from the
  125. // field.
  126. if (!$db_id) {
  127. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__dbxref_id'] = '__NULL__';
  128. }
  129. // If the dbxref_id does not match the db_id + accession then the user
  130. // has selected a new dbxref record and we need to update the hidden
  131. // value accordingly.
  132. if ($db_id and $accession) {
  133. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  134. if ($dbxref and $dbxref->dbxref_id != $fk_val) {
  135. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
  136. }
  137. }
  138. }
  139. /**
  140. * @see TripalFieldWidget::theme()
  141. */
  142. public function theme($element) {
  143. $layout = "
  144. <div class=\"primary-dbxref-widget\">
  145. <div class=\"primary-dbxref-widget-item\">" .
  146. drupal_render($element['db_id']) . "
  147. </div>
  148. <div class=\"primary-dbxref-widget-item\">" .
  149. drupal_render($element['accession']) . "
  150. </div>
  151. <div class=\"primary-dbxref-widget-item\">" .
  152. drupal_render($element['version']) . "
  153. </div>
  154. <div class=\"primary-dbxref-widget-item\">" .
  155. drupal_render($element['description']) . "
  156. </div>
  157. <div class=\"primary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  158. </div>
  159. ";
  160. // $classes = array();
  161. // $classes[] = 'collapsible';
  162. // $theme_settings = $element['#theme_settings'];
  163. // if ($theme_settings['#collapsed'] == FALSE) {
  164. // $classes[] = 'collapsed';
  165. // }
  166. $fieldset = array(
  167. '#title' => $element['#title'],
  168. '#value' => '',
  169. '#description' => $element['#description'],
  170. '#children' => $layout,
  171. // '#attributes' => array('class' => $classes),
  172. );
  173. return theme('fieldset', array('element' => $fieldset));
  174. }
  175. }
  176. /**
  177. * An Ajax callback for the tripal_chado_admin_publish_form..
  178. */
  179. function data__accession_widget_form_ajax_callback($form, $form_state) {
  180. $field_name = $form_state['triggering_element']['#parents'][0];
  181. $field = field_info_field($field_name);
  182. $field_type = $field['type'];
  183. $field_table = $this->instance['settings']['chado_table'];
  184. $field_column = $this->instance['settings']['chado_column'];
  185. $field_prefix = 'chado-' . $field_table . '__' . $field_column;
  186. // $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--db_id');
  187. // $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--accession');
  188. if ($db_id and $accession) {
  189. $values = array(
  190. 'db_id' => $db_id,
  191. 'accession' => $accession,
  192. );
  193. $options = array('is_duplicate' => TRUE);
  194. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  195. if (!$has_duplicate) {
  196. drupal_set_message('The selected cross reference is new and will be added for future auto completions.', 'warning');
  197. }
  198. }
  199. return $form[$field_name];
  200. }