views_handler_filter_numeric.inc

  1. 3.x handlers/views_handler_filter_numeric.inc
  2. 2.x handlers/views_handler_filter_numeric.inc

Definition of views_handler_filter_numeric.

File

handlers/views_handler_filter_numeric.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_filter_numeric.
  5. */
  6. /**
  7. * Simple filter to handle greater than/less than filters
  8. *
  9. * @ingroup views_filter_handlers
  10. */
  11. class views_handler_filter_numeric extends views_handler_filter {
  12. var $always_multiple = TRUE;
  13. function option_definition() {
  14. $options = parent::option_definition();
  15. $options['value'] = array(
  16. 'contains' => array(
  17. 'min' => array('default' => ''),
  18. 'max' => array('default' => ''),
  19. 'value' => array('default' => ''),
  20. ),
  21. );
  22. return $options;
  23. }
  24. function operators() {
  25. $operators = array(
  26. '<' => array(
  27. 'title' => t('Is less than'),
  28. 'method' => 'op_simple',
  29. 'short' => t('<'),
  30. 'values' => 1,
  31. ),
  32. '<=' => array(
  33. 'title' => t('Is less than or equal to'),
  34. 'method' => 'op_simple',
  35. 'short' => t('<='),
  36. 'values' => 1,
  37. ),
  38. '=' => array(
  39. 'title' => t('Is equal to'),
  40. 'method' => 'op_simple',
  41. 'short' => t('='),
  42. 'values' => 1,
  43. ),
  44. '!=' => array(
  45. 'title' => t('Is not equal to'),
  46. 'method' => 'op_simple',
  47. 'short' => t('!='),
  48. 'values' => 1,
  49. ),
  50. '>=' => array(
  51. 'title' => t('Is greater than or equal to'),
  52. 'method' => 'op_simple',
  53. 'short' => t('>='),
  54. 'values' => 1,
  55. ),
  56. '>' => array(
  57. 'title' => t('Is greater than'),
  58. 'method' => 'op_simple',
  59. 'short' => t('>'),
  60. 'values' => 1,
  61. ),
  62. 'between' => array(
  63. 'title' => t('Is between'),
  64. 'method' => 'op_between',
  65. 'short' => t('between'),
  66. 'values' => 2,
  67. ),
  68. 'not between' => array(
  69. 'title' => t('Is not between'),
  70. 'method' => 'op_between',
  71. 'short' => t('not between'),
  72. 'values' => 2,
  73. ),
  74. );
  75. // if the definition allows for the empty operator, add it.
  76. if (!empty($this->definition['allow empty'])) {
  77. $operators += array(
  78. 'empty' => array(
  79. 'title' => t('Is empty (NULL)'),
  80. 'method' => 'op_empty',
  81. 'short' => t('empty'),
  82. 'values' => 0,
  83. ),
  84. 'not empty' => array(
  85. 'title' => t('Is not empty (NOT NULL)'),
  86. 'method' => 'op_empty',
  87. 'short' => t('not empty'),
  88. 'values' => 0,
  89. ),
  90. );
  91. }
  92. // Add regexp support for MySQL.
  93. if (Database::getConnection()->databaseType() == 'mysql') {
  94. $operators += array(
  95. 'regular_expression' => array(
  96. 'title' => t('Regular expression'),
  97. 'short' => t('regex'),
  98. 'method' => 'op_regex',
  99. 'values' => 1,
  100. ),
  101. );
  102. }
  103. return $operators;
  104. }
  105. /**
  106. * Provide a list of all the numeric operators
  107. */
  108. function operator_options($which = 'title') {
  109. $options = array();
  110. foreach ($this->operators() as $id => $info) {
  111. $options[$id] = $info[$which];
  112. }
  113. return $options;
  114. }
  115. function operator_values($values = 1) {
  116. $options = array();
  117. foreach ($this->operators() as $id => $info) {
  118. if ($info['values'] == $values) {
  119. $options[] = $id;
  120. }
  121. }
  122. return $options;
  123. }
  124. /**
  125. * Provide a simple textfield for equality
  126. */
  127. function value_form(&$form, &$form_state) {
  128. $form['value']['#tree'] = TRUE;
  129. // We have to make some choices when creating this as an exposed
  130. // filter form. For example, if the operator is locked and thus
  131. // not rendered, we can't render dependencies; instead we only
  132. // render the form items we need.
  133. $which = 'all';
  134. if (!empty($form['operator'])) {
  135. $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
  136. }
  137. if (!empty($form_state['exposed'])) {
  138. $identifier = $this->options['expose']['identifier'];
  139. if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
  140. // exposed and locked.
  141. $which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value';
  142. }
  143. else {
  144. $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
  145. }
  146. }
  147. if ($which == 'all') {
  148. $form['value']['value'] = array(
  149. '#type' => 'textfield',
  150. '#title' => empty($form_state['exposed']) ? t('Value') : '',
  151. '#size' => 30,
  152. '#default_value' => $this->value['value'],
  153. '#dependency' => array($source => $this->operator_values(1)),
  154. );
  155. if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['value'])) {
  156. $form_state['input'][$identifier]['value'] = $this->value['value'];
  157. }
  158. }
  159. elseif ($which == 'value') {
  160. // When exposed we drop the value-value and just do value if
  161. // the operator is locked.
  162. $form['value'] = array(
  163. '#type' => 'textfield',
  164. '#title' => empty($form_state['exposed']) ? t('Value') : '',
  165. '#size' => 30,
  166. '#default_value' => $this->value['value'],
  167. );
  168. if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
  169. $form_state['input'][$identifier] = $this->value['value'];
  170. }
  171. }
  172. if ($which == 'all' || $which == 'minmax') {
  173. $form['value']['min'] = array(
  174. '#type' => 'textfield',
  175. '#title' => empty($form_state['exposed']) ? t('Min') : '',
  176. '#size' => 30,
  177. '#default_value' => $this->value['min'],
  178. );
  179. $form['value']['max'] = array(
  180. '#type' => 'textfield',
  181. '#title' => empty($form_state['exposed']) ? t('And max') : t('And'),
  182. '#size' => 30,
  183. '#default_value' => $this->value['max'],
  184. );
  185. if ($which == 'all') {
  186. $dependency = array(
  187. '#dependency' => array($source => $this->operator_values(2)),
  188. );
  189. $form['value']['min'] += $dependency;
  190. $form['value']['max'] += $dependency;
  191. }
  192. if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['min'])) {
  193. $form_state['input'][$identifier]['min'] = $this->value['min'];
  194. }
  195. if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['max'])) {
  196. $form_state['input'][$identifier]['max'] = $this->value['max'];
  197. }
  198. if (!isset($form['value'])) {
  199. // Ensure there is something in the 'value'.
  200. $form['value'] = array(
  201. '#type' => 'value',
  202. '#value' => NULL
  203. );
  204. }
  205. }
  206. }
  207. function query() {
  208. $this->ensure_my_table();
  209. $field = "$this->table_alias.$this->real_field";
  210. $info = $this->operators();
  211. if (!empty($info[$this->operator]['method'])) {
  212. $this->{$info[$this->operator]['method']}($field);
  213. }
  214. }
  215. function op_between($field) {
  216. if ($this->operator == 'between') {
  217. $this->query->add_where($this->options['group'], $field, array($this->value['min'], $this->value['max']), 'BETWEEN');
  218. }
  219. else {
  220. $this->query->add_where($this->options['group'], db_or()->condition($field, $this->value['min'], '<=')->condition($field, $this->value['max'], '>='));
  221. }
  222. }
  223. function op_simple($field) {
  224. $this->query->add_where($this->options['group'], $field, $this->value['value'], $this->operator);
  225. }
  226. function op_empty($field) {
  227. if ($this->operator == 'empty') {
  228. $operator = "IS NULL";
  229. }
  230. else {
  231. $operator = "IS NOT NULL";
  232. }
  233. $this->query->add_where($this->options['group'], $field, NULL, $operator);
  234. }
  235. function op_regex($field) {
  236. $this->query->add_where($this->options['group'], $field, $this->value['value'], 'RLIKE');
  237. }
  238. function admin_summary() {
  239. if ($this->is_a_group()) {
  240. return t('grouped');
  241. }
  242. if (!empty($this->options['exposed'])) {
  243. return t('exposed');
  244. }
  245. $options = $this->operator_options('short');
  246. $output = check_plain($options[$this->operator]);
  247. if (in_array($this->operator, $this->operator_values(2))) {
  248. $output .= ' ' . t('@min and @max', array('@min' => $this->value['min'], '@max' => $this->value['max']));
  249. }
  250. elseif (in_array($this->operator, $this->operator_values(1))) {
  251. $output .= ' ' . check_plain($this->value['value']);
  252. }
  253. return $output;
  254. }
  255. /**
  256. * Do some minor translation of the exposed input
  257. */
  258. function accept_exposed_input($input) {
  259. if (empty($this->options['exposed'])) {
  260. return TRUE;
  261. }
  262. // rewrite the input value so that it's in the correct format so that
  263. // the parent gets the right data.
  264. if (!empty($this->options['expose']['identifier'])) {
  265. $value = &$input[$this->options['expose']['identifier']];
  266. if (!is_array($value)) {
  267. $value = array(
  268. 'value' => $value,
  269. );
  270. }
  271. }
  272. $rc = parent::accept_exposed_input($input);
  273. if (empty($this->options['expose']['required'])) {
  274. // We have to do some of our own checking for non-required filters.
  275. $info = $this->operators();
  276. if (!empty($info[$this->operator]['values'])) {
  277. switch ($info[$this->operator]['values']) {
  278. case 1:
  279. if ($value['value'] === '') {
  280. return FALSE;
  281. }
  282. break;
  283. case 2:
  284. if ($value['min'] === '' && $value['max'] === '') {
  285. return FALSE;
  286. }
  287. break;
  288. }
  289. }
  290. }
  291. return $rc;
  292. }
  293. }