data__accession_widget.inc 9.4 KB

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