sbo__database_cross_reference_widget.inc

File

tripal_chado/includes/TripalFields/sbo__database_cross_reference/sbo__database_cross_reference_widget.inc
View source
  1. <?php
  2. class sbo__database_cross_reference_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Cross reference';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('sbo__database_cross_reference');
  7. /**
  8. *
  9. * @see TripalFieldWidget::form()
  10. */
  11. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  12. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  13. $field_name = $this->field['field_name'];
  14. $field_type = $this->field['type'];
  15. $field_table = $this->instance['settings']['chado_table'];
  16. $field_column = $this->instance['settings']['chado_column'];
  17. $base_table = $this->instance['settings']['base_table'];
  18. $schema = chado_get_schema($field_table);
  19. $pkey = $schema['primary key'][0];
  20. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  21. $fkey = $fkeys[0];
  22. // Get the field defaults.
  23. $record_id = '';
  24. $fkey_value = $element['#entity'] ? $element['#entity']->chado_record_id : '';
  25. $dbxref_id = '';
  26. $db_id = '';
  27. $accession = '';
  28. // If the field already has a value then it will come through the $items
  29. // array. This happens when editing an existing record.
  30. if (count($items) > 0 and array_key_exists($delta, $items)) {
  31. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  32. $fkey_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $fkey, $fkey_value);
  33. $dbxref_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__dbxref_id', $dbxref_id);
  34. $db_id = tripal_get_field_item_keyval($items, $delta, 'db_id', $db_id);
  35. $accession = tripal_get_field_item_keyval($items, $delta, 'accession', $accession);
  36. }
  37. // Check $form_state['values'] to see if an AJAX call set the values.
  38. if (array_key_exists('values', $form_state) and
  39. array_key_exists($field_name, $form_state['values'])) {
  40. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  41. $fkey_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey];
  42. $dbxref_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'];
  43. $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
  44. $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];
  45. }
  46. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id-$delta'>";
  47. $widget['#suffix'] = "</span>";
  48. $widget['value'] = array(
  49. '#type' => 'value',
  50. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  51. );
  52. $widget['chado-' . $field_table . '__' . $pkey] = array(
  53. '#type' => 'value',
  54. '#default_value' => $record_id,
  55. );
  56. $widget['chado-' . $field_table . '__' . $fkey] = array(
  57. '#type' => 'value',
  58. '#default_value' => $fkey_value,
  59. );
  60. $widget['chado-' . $field_table . '__dbxref_id'] = array(
  61. '#type' => 'value',
  62. '#default_value' => $dbxref_id,
  63. );
  64. $options = chado_get_db_select_options();
  65. $widget['db_id'] = array(
  66. '#type' => 'select',
  67. '#title' => t('Database'),
  68. '#options' => $options,
  69. '#required' => $element['#required'],
  70. '#default_value' => $db_id,
  71. '#ajax' => array(
  72. 'callback' => "sbo__database_cross_reference_widget_form_ajax_callback",
  73. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  74. 'effect' => 'fade',
  75. 'method' => 'replace'
  76. ),
  77. );
  78. $schema = chado_get_schema('dbxref');
  79. $widget['accession'] = array(
  80. '#type' => 'textfield',
  81. '#title' => t('Accession'),
  82. '#default_value' => $accession,
  83. '#required' => $element['#required'],
  84. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  85. '#size' => 15,
  86. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  87. '#disabled' => $db_id ? FALSE : TRUE,
  88. );
  89. }
  90. /**
  91. * @see TripalFieldWidget::validate()
  92. */
  93. public function validate($element, $form, &$form_state, $langcode, $delta) {
  94. $field_name = $this->field['field_name'];
  95. $field_type = $this->field['type'];
  96. $table_name = $this->instance['settings']['chado_table'];
  97. $field_table = $this->instance['settings']['chado_table'];
  98. $field_column = $this->instance['settings']['chado_column'];
  99. $base_table = $this->instance['settings']['base_table'];
  100. $schema = chado_get_schema($table_name);
  101. $pkey = $schema['primary key'][0];
  102. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  103. $fkey = $fkeys[0];
  104. // Get the field values.
  105. $dbxref_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'];
  106. $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
  107. $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];
  108. // If user did not select a database, we want to remove the dbxref record.
  109. // We do this by setting all values to empty except the value and the
  110. // primary key.
  111. if (!$db_id) {
  112. $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
  113. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey] = '';
  114. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = '';
  115. }
  116. // If the dbxref_id does not match the db_id + accession then the user
  117. // has selected a new dbxref record and we need to update the hidden
  118. // value accordingly.
  119. if ($db_id and $accession) {
  120. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  121. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  122. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
  123. $form_state['values'][$field_name]['und'][$delta]['value'] = $dbxref->dbxref_id;
  124. }
  125. }
  126. }
  127. /**
  128. * @see TripalFieldWidget::theme()
  129. */
  130. public function theme($element) {
  131. $layout = "
  132. <div class=\"secondary-dbxref-widget\">
  133. <div class=\"secondary-dbxref-widget-item\">" .
  134. drupal_render($element['db_id']) . "
  135. </div>
  136. <div class=\"secondary-dbxref-widget-item\">" .
  137. drupal_render($element['accession']) . "
  138. </div>
  139. </div>
  140. ";
  141. return $layout;
  142. }
  143. }
  144. /**
  145. * An Ajax callback for the dbxref widget.
  146. */
  147. function sbo__database_cross_reference_widget_form_ajax_callback($form, $form_state) {
  148. // Get the triggering element
  149. $form_element_name = $form_state['triggering_element']['#name'];
  150. preg_match('/(.+?)\[(.+?)\]\[(.+?)\]/', $form_element_name, $matches);
  151. $field = $matches[1];
  152. $lang = $matches[2];
  153. $delta = $matches[3];
  154. // Return the widget that triggered the AJAX call
  155. if (isset($form[$field][$lang][$delta])) {
  156. return $form[$field][$lang][$delta];
  157. }
  158. // Alternatively, return the default value widget for the widget setting form
  159. else {
  160. return $form['instance']['default_value_widget'][$field];
  161. }
  162. }