tripal_views_handler_filter_select_string.inc

  1. 2.x tripal_views/views/handlers/tripal_views_handler_filter_select_string.inc
  2. 3.x tripal/views_handlers/tripal_views_handler_filter_select_string.inc
  3. 3.x tripal_chado_views/views/handlers/tripal_views_handler_filter_select_string.inc
  4. 1.x tripal_views/views/handlers/tripal_views_handler_filter_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/tripal_views_handler_filter_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 tripal_views_handler_filter_select_string extends chado_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['note'] = 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--they're fixed.
  65. $where = '';
  66. $filters = (is_array($this->view->filter)) ? $this->view->filter : array();
  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. $options = array();
  90. if ($this->options['optional']) {
  91. //$options['<select '.$this->table.'>'] = '--None--';
  92. $options['All'] = '--Any--';
  93. }
  94. while ($r = db_fetch_object($results)) {
  95. if (drupal_strlen($r->{$this->field}) > $max_length) {
  96. $options[$r->{$this->field}] = drupal_substr($r->{$this->field}, 0, $max_length) . '...';
  97. }
  98. else {
  99. $options[$r->{$this->field}] = $r->{$this->field};
  100. }
  101. }
  102. //Select List
  103. $form['value'] = array(
  104. '#type' => 'select',
  105. '#title' => t('%label', array('%label' => $this->options['label'])),
  106. '#options' => $options,
  107. '#default_value' => $this->value,
  108. );
  109. if ($this->options['multiple']) {
  110. $form['value']['#multiple'] = TRUE;
  111. }
  112. }
  113. }
  114. /**
  115. * Ensures the select list gets rendered when the filter is exposed
  116. */
  117. function exposed_form(&$form, &$form_state) {
  118. if (empty($this->options['exposed'])) {
  119. return;
  120. }
  121. $value = $this->options['expose']['identifier'];
  122. $this->value_form($form, $form_state);
  123. $form[$value] = $form['value'];
  124. if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
  125. unset($form[$value]['#title']);
  126. }
  127. $this->exposed_translate($form[$value], 'value');
  128. if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
  129. unset($form[$value]['#default_value']);
  130. }
  131. if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
  132. $form[$value]['#default_value'] = 'All';
  133. }
  134. if ($value != 'value') {
  135. unset($form['value']);
  136. }
  137. }
  138. /**
  139. * Adds this filter to the where clause of the views query
  140. */
  141. function query() {
  142. // make optional
  143. // if it is not set or empty then don't restrict the query
  144. if (!$this->value) {
  145. return;
  146. }
  147. $this->ensure_my_table();
  148. $table = $this->query->get_table_info($this->table);
  149. if (preg_match('/aggregator/', $table['join']->definition['handler'])) {
  150. $this->aggregated = TRUE;
  151. }
  152. else {
  153. $this->aggregated = FALSE;
  154. }
  155. // filter the aggregates
  156. if ($this->options['agg']['aggregates_with']) {
  157. $this->query_restrict_curr_table_records();
  158. }
  159. // filter the base table
  160. if ($this->options['agg']['records_with']) {
  161. $this->query_restrict_base_records();
  162. }
  163. }
  164. /**
  165. * This function alters the query by adding the appropriate WHERE
  166. * to filter the base table to only those with the value in the aggregate array.
  167. *
  168. * Note: this function is called only from query()
  169. */
  170. function query_restrict_base_records() {
  171. if (!$this->aggregated) {
  172. // Not Aggregated ---------------
  173. $this->ensure_my_table();
  174. $field = "$this->table_alias.$this->real_field";
  175. $upper = $this->case_transform();
  176. if ($this->options['multiple'] AND is_array($this->value)) {
  177. // Remove any if it's there
  178. unset($this->value['All']);
  179. if (sizeof($this->value)) {
  180. $holders = array();
  181. foreach ($this->value as $v) {
  182. if (preg_match('/^[\d\.]+$/', $v)) {
  183. $holders[] = '%f';
  184. }
  185. else {
  186. $holders[] = "'%s'";
  187. }
  188. }
  189. $where = "$field IN (" . implode(", ", $holders) . ")";
  190. $this->query->add_where($this->options['group'], $where, $this->value);
  191. }
  192. }
  193. else {
  194. // Deal with All/Any as value
  195. if (preg_match('/All/', $this->value)) {
  196. // Don't do anything
  197. }
  198. else {
  199. $info = $this->operators();
  200. if (!empty($info[$this->operator]['method'])) {
  201. $this->{$info[$this->operator]['method']}($field, $upper);
  202. }
  203. }
  204. }
  205. }
  206. else {
  207. // Is Aggregated ----------------
  208. $this->ensure_my_table();
  209. $field = "$this->table_alias.$this->real_field";
  210. $upper = $this->case_transform();
  211. if ($this->options['multiple'] AND is_array($this->value)) {
  212. // Remove any if it's there
  213. unset($this->value['All']);
  214. if (sizeof($this->value) > 1) {
  215. $holders = array();
  216. foreach ($this->value as $v) {
  217. $holders[] = "'%s'";
  218. }
  219. $where = $field .' && ARRAY[' . implode(", ", $holders) . ']';
  220. $this->query->add_where($this->options['group'], $where, $this->value);
  221. }
  222. elseif (sizeof($this->value) == 1) {
  223. $where = "'%s' = ANY($field)";
  224. $this->query->add_where($this->options['group'], $where, array_pop($this->value));
  225. }
  226. }
  227. else {
  228. // Deal with All/Any as value
  229. if (preg_match('/All/', $this->value)) {
  230. // Don't do anything
  231. }
  232. else {
  233. $where = "'%s' = ANY($field)";
  234. $this->query->add_where($this->options['group'], $where, $this->value);
  235. }
  236. }
  237. }
  238. }
  239. /**
  240. * This function alters the query by adding the appropriate WHERE
  241. * to filter the aggregates shown based on the value. Doesn't affect
  242. * the number of base table records.
  243. *
  244. * Note: this function is called only from query()
  245. */
  246. function query_restrict_curr_table_records() {
  247. if (!$this->aggregated) {
  248. // Not Aggregated ---------------
  249. // Warn the admin/user that they have selected that the aggregates should be filtered
  250. // on a field that isn't aggregated...
  251. watchdog(
  252. 'tripal_views',
  253. 'You have chosen to filter the aggregates shown for %table %field
  254. in %view; however, that field is not aggregated (ie: it is part of the base table
  255. or is a 1:1 relationship to the base table)',
  256. array(
  257. '%field' => $this->field,
  258. '%table' => $this->table,
  259. '%view' => $this->view->name
  260. ),
  261. WATCHDOG_WARNING
  262. );
  263. // Do nothing!
  264. }
  265. else {
  266. // Is Aggregated ----------------
  267. $this->ensure_my_table();
  268. $field = "$this->table_alias.$this->real_field";
  269. $upper = $this->case_transform();
  270. if ($this->options['multiple'] AND is_array($this->value)) {
  271. // Remove any if it's there
  272. unset($this->value['All']);
  273. if (sizeof($this->value) > 1) {
  274. $holders = array();
  275. foreach ($this->value as $v) {
  276. $holders[] = "'%s'";
  277. }
  278. $where = $field .' IN (' . implode(", ", $holders) . ')';
  279. $where = vsprintf($where, $this->value);
  280. // Add the where to the chado aggregated join object for this table
  281. // then the views_handler_join_chado_aggregator will add this to the WHERE
  282. // clause of the sub-query generating the aggregated listing
  283. $this->query->table_queue[ $this->table ]['join']->filter[] = $where;
  284. }
  285. elseif (sizeof($this->value) == 1) {
  286. $where = "$field = '%s'";
  287. $where = vsprintf($where, $this->value);
  288. // Add the where to the chado aggregated join object for this table
  289. // then the views_handler_join_chado_aggregator will add this to the WHERE
  290. // clause of the sub-query generating the aggregated listing
  291. $this->query->table_queue[ $this->table ]['join']->filter[] = $where;
  292. }
  293. }
  294. else {
  295. // Deal with All/Any as value
  296. if (preg_match('/All/', $this->value)) {
  297. // Don't do anything
  298. }
  299. else {
  300. $where = "'%s' = ANY($field)";
  301. $this->query->add_where($this->options['group'], $where, $this->value);
  302. }
  303. }
  304. }
  305. }
  306. }

Related topics