views_handler_filter_chado_select_string.inc

Purpose: This Handler provides a generic select list for any chado field that is a string The select list includes all distinct values for that field.

File

tripal_views/views/handlers/deprecated/views_handler_filter_chado_select_string.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Purpose: This Handler provides a generic select list for any chado field that is a string
  5. * The select list includes all distinct values for that field.
  6. *
  7. * @ingroup views_filter_handlers
  8. * @ingroup tripal_core
  9. */
  10. class views_handler_filter_chado_select_string extends views_handler_filter_string {
  11. function options_form(&$form, &$form_state) {
  12. parent::options_form($form, $form_state);
  13. $form['values_form_type'] = array(
  14. '#type' => 'radios',
  15. '#title' => t('Filter Type'),
  16. '#options' => array(
  17. 'textfield' => 'Text Field',
  18. 'select' => 'Drop-Down Box',
  19. ),
  20. '#default_value' => ($this->options['values_form_type']) ? $this->options['values_form_type'] : 'select',
  21. );
  22. $form['multiple'] = array(
  23. '#type' => 'checkbox',
  24. '#title' => t('Select Multiple'),
  25. '#description' => t('Allows more then one option to be selected.'),
  26. '#default_value' => (isset($this->options['multiple'])) ? $this->options['multiple'] : FALSE,
  27. );
  28. $form['optional'] = array(
  29. '#type' => 'checkbox',
  30. '#title' => t('Optional'),
  31. '#description' => t('Adds --Any-- to the available options.'),
  32. '#default_value' => (isset($this->options['optional'])) ? $this->options['optional'] : TRUE,
  33. );
  34. $form['max_length'] = array(
  35. '#type' => 'textfield',
  36. '#title' => t('Max Width'),
  37. '#description' => t('Specify the maximum width of the select box'),
  38. '#default_value' => (isset($this->options['max_length'])) ? $this->options['max_length'] : 40,
  39. );
  40. $form['max_length'] = array(
  41. '#type' => 'markup',
  42. '#value' => t('<strong><font color="red">Note:</font></strong> If another filter exists for the same table then '.
  43. 'the values shown in the drop box will only include those from rows that are not filtered.'),
  44. );
  45. }
  46. /**
  47. * Defines the value field in both the views filter options form
  48. * and the exposed form
  49. */
  50. function value_form(&$form, &$form_state) {
  51. parent::value_form($form, $form_state);
  52. if (preg_match('/textfield/', $this->options['values_form_type'])) {
  53. $form['value'] = array(
  54. '#type' => 'textfield',
  55. '#title' => t('%label', array('%label' => $this->options['label'])),
  56. '#default_value' => $this->value,
  57. );
  58. }
  59. else {
  60. // build a where clause that will filter the list in the drop box
  61. // using fields that are not exposed and that are for the table
  62. // from whcih the values in the drop box will be slected and
  63. // we only want to use non-exposed fields because these are not
  64. // available to the user to edit--their fixed.
  65. $where = '';
  66. $filters = $this->view->filter;
  67. foreach ($filters as $filter_name => $details) {
  68. // we only want to inclue non-exposed filters
  69. if ($details->options['exposed'] == FALSE) {
  70. // we only want to filter on the table we're getting the list from
  71. if (strcmp($details->table, $this->table)==0) {
  72. $where .= "$details->field $details->operator ". $details->value['value'];
  73. $where .= ' AND ';
  74. }
  75. }
  76. }
  77. if ($where) {
  78. $where = "WHERE $where";
  79. $where = substr($where, 0, -5); # remove the final ' AND '
  80. }
  81. // get the values from the table
  82. $sql = "SELECT $this->real_field FROM $this->table $where ORDER BY $this->field ASC";
  83. $results = chado_query($sql);
  84. // Build the select box options
  85. $max_length = $this->options['max_length'];
  86. if (!$max_length) {
  87. $max_length = 40;
  88. }
  89. if ($this->options['optional']) {
  90. //$options['<select '.$this->table.'>'] = '--None--';
  91. $options['All'] = '--Any--';
  92. }
  93. while ($r = db_fetch_object($results)) {
  94. if (drupal_strlen($r->{$this->field}) > $max_length) {
  95. $options[$r->{$this->field}] = drupal_substr($r->{$this->field}, 0, $max_length) . '...';
  96. }
  97. else {
  98. $options[$r->{$this->field}] = $r->{$this->field};
  99. }
  100. }
  101. //Select List
  102. $form['value'] = array(
  103. '#type' => 'select',
  104. '#title' => t('%label', array('%label' => $this->options['label'])),
  105. '#options' => $options,
  106. '#default_value' => $this->value,
  107. );
  108. if ($this->options['multiple']) {
  109. $form['value']['#multiple'] = TRUE;
  110. }
  111. }
  112. }
  113. /**
  114. * Ensures the select list gets rendered when the filter is exposed
  115. */
  116. function exposed_form(&$form, &$form_state) {
  117. if (empty($this->options['exposed'])) {
  118. return;
  119. }
  120. $value = $this->options['expose']['identifier'];
  121. $this->value_form($form, $form_state);
  122. $form[$value] = $form['value'];
  123. if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
  124. unset($form[$value]['#title']);
  125. }
  126. $this->exposed_translate($form[$value], 'value');
  127. if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
  128. unset($form[$value]['#default_value']);
  129. }
  130. if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
  131. $form[$value]['#default_value'] = 'All';
  132. }
  133. if ($value != 'value') {
  134. unset($form['value']);
  135. }
  136. }
  137. /**
  138. *
  139. */
  140. function query() {
  141. $this->ensure_my_table();
  142. $field = "$this->table_alias.$this->real_field";
  143. $upper = $this->case_transform();
  144. if ($this->options['multiple']) {
  145. // Remove any if it's there
  146. unset($this->value['All']);
  147. if (sizeof($this->value)) {
  148. $holders = array();
  149. foreach ($this->value as $v) {
  150. if (preg_match('/^[\d\.]+$/', $v)) {
  151. $holders[] = '%d';
  152. }
  153. else {
  154. $holders[] = "'%s'";
  155. }
  156. }
  157. $where = "$field IN (" . implode(", ", $holders) . ")";
  158. $this->query->add_where($this->options['group'], $where, $this->value);
  159. }
  160. }
  161. else {
  162. // Deal with All/Any as value
  163. if (preg_match('/All/', $this->value)) {
  164. // Don't do anything
  165. }
  166. else {
  167. $info = $this->operators();
  168. if (!empty($info[$this->operator]['method'])) {
  169. $this->{$info[$this->operator]['method']}($field, $upper);
  170. }
  171. }
  172. }
  173. }
  174. }

Related topics