views_handler_filter_string.inc

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

Definition of views_handler_filter_string.

File

handlers/views_handler_filter_string.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_filter_string.
  5. */
  6. /**
  7. * Basic textfield filter to handle string filtering commands
  8. * including equality, like, not like, etc.
  9. *
  10. * @ingroup views_filter_handlers
  11. */
  12. class views_handler_filter_string extends views_handler_filter {
  13. // exposed filter options
  14. var $always_multiple = TRUE;
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['expose']['contains']['required'] = array('default' => FALSE, 'bool' => TRUE);
  18. return $options;
  19. }
  20. /**
  21. * This kind of construct makes it relatively easy for a child class
  22. * to add or remove functionality by overriding this function and
  23. * adding/removing items from this array.
  24. */
  25. function operators() {
  26. $operators = array(
  27. '=' => array(
  28. 'title' => t('Is equal to'),
  29. 'short' => t('='),
  30. 'method' => 'op_equal',
  31. 'values' => 1,
  32. ),
  33. '!=' => array(
  34. 'title' => t('Is not equal to'),
  35. 'short' => t('!='),
  36. 'method' => 'op_equal',
  37. 'values' => 1,
  38. ),
  39. 'contains' => array(
  40. 'title' => t('Contains'),
  41. 'short' => t('contains'),
  42. 'method' => 'op_contains',
  43. 'values' => 1,
  44. ),
  45. 'word' => array(
  46. 'title' => t('Contains any word'),
  47. 'short' => t('has word'),
  48. 'method' => 'op_word',
  49. 'values' => 1,
  50. ),
  51. 'allwords' => array(
  52. 'title' => t('Contains all words'),
  53. 'short' => t('has all'),
  54. 'method' => 'op_word',
  55. 'values' => 1,
  56. ),
  57. 'starts' => array(
  58. 'title' => t('Starts with'),
  59. 'short' => t('begins'),
  60. 'method' => 'op_starts',
  61. 'values' => 1,
  62. ),
  63. 'not_starts' => array(
  64. 'title' => t('Does not start with'),
  65. 'short' => t('not_begins'),
  66. 'method' => 'op_not_starts',
  67. 'values' => 1,
  68. ),
  69. 'ends' => array(
  70. 'title' => t('Ends with'),
  71. 'short' => t('ends'),
  72. 'method' => 'op_ends',
  73. 'values' => 1,
  74. ),
  75. 'not_ends' => array(
  76. 'title' => t('Does not end with'),
  77. 'short' => t('not_ends'),
  78. 'method' => 'op_not_ends',
  79. 'values' => 1,
  80. ),
  81. 'not' => array(
  82. 'title' => t('Does not contain'),
  83. 'short' => t('!has'),
  84. 'method' => 'op_not',
  85. 'values' => 1,
  86. ),
  87. 'shorterthan' => array(
  88. 'title' => t('Length is shorter than'),
  89. 'short' => t('shorter than'),
  90. 'method' => 'op_shorter',
  91. 'values' => 1,
  92. ),
  93. 'longerthan' => array(
  94. 'title' => t('Length is longer than'),
  95. 'short' => t('longer than'),
  96. 'method' => 'op_longer',
  97. 'values' => 1,
  98. ),
  99. );
  100. // if the definition allows for the empty operator, add it.
  101. if (!empty($this->definition['allow empty'])) {
  102. $operators += array(
  103. 'empty' => array(
  104. 'title' => t('Is empty (NULL)'),
  105. 'method' => 'op_empty',
  106. 'short' => t('empty'),
  107. 'values' => 0,
  108. ),
  109. 'not empty' => array(
  110. 'title' => t('Is not empty (NOT NULL)'),
  111. 'method' => 'op_empty',
  112. 'short' => t('not empty'),
  113. 'values' => 0,
  114. ),
  115. );
  116. }
  117. // Add regexp support for MySQL.
  118. if (Database::getConnection()->databaseType() == 'mysql') {
  119. $operators += array(
  120. 'regular_expression' => array(
  121. 'title' => t('Regular expression'),
  122. 'short' => t('regex'),
  123. 'method' => 'op_regex',
  124. 'values' => 1,
  125. ),
  126. );
  127. }
  128. return $operators;
  129. }
  130. /**
  131. * Build strings from the operators() for 'select' options
  132. */
  133. function operator_options($which = 'title') {
  134. $options = array();
  135. foreach ($this->operators() as $id => $info) {
  136. $options[$id] = $info[$which];
  137. }
  138. return $options;
  139. }
  140. function admin_summary() {
  141. if ($this->is_a_group()) {
  142. return t('grouped');
  143. }
  144. if (!empty($this->options['exposed'])) {
  145. return t('exposed');
  146. }
  147. $options = $this->operator_options('short');
  148. $output = '';
  149. if(!empty($options[$this->operator])) {
  150. $output = check_plain($options[$this->operator]);
  151. }
  152. if (in_array($this->operator, $this->operator_values(1))) {
  153. $output .= ' ' . check_plain($this->value);
  154. }
  155. return $output;
  156. }
  157. function operator_values($values = 1) {
  158. $options = array();
  159. foreach ($this->operators() as $id => $info) {
  160. if (isset($info['values']) && $info['values'] == $values) {
  161. $options[] = $id;
  162. }
  163. }
  164. return $options;
  165. }
  166. /**
  167. * Provide a simple textfield for equality
  168. */
  169. function value_form(&$form, &$form_state) {
  170. // We have to make some choices when creating this as an exposed
  171. // filter form. For example, if the operator is locked and thus
  172. // not rendered, we can't render dependencies; instead we only
  173. // render the form items we need.
  174. $which = 'all';
  175. if (!empty($form['operator'])) {
  176. $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
  177. }
  178. if (!empty($form_state['exposed'])) {
  179. $identifier = $this->options['expose']['identifier'];
  180. if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
  181. // exposed and locked.
  182. $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
  183. }
  184. else {
  185. $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
  186. }
  187. }
  188. if ($which == 'all' || $which == 'value') {
  189. $form['value'] = array(
  190. '#type' => 'textfield',
  191. '#title' => t('Value'),
  192. '#size' => 30,
  193. '#default_value' => $this->value,
  194. );
  195. if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
  196. $form_state['input'][$identifier] = $this->value;
  197. }
  198. if ($which == 'all') {
  199. $form['value'] += array(
  200. '#dependency' => array($source => $this->operator_values(1)),
  201. );
  202. }
  203. }
  204. if (!isset($form['value'])) {
  205. // Ensure there is something in the 'value'.
  206. $form['value'] = array(
  207. '#type' => 'value',
  208. '#value' => NULL
  209. );
  210. }
  211. }
  212. function operator() {
  213. return $this->operator == '=' ? 'LIKE' : 'NOT LIKE';
  214. }
  215. /**
  216. * Add this filter to the query.
  217. *
  218. * Due to the nature of fapi, the value and the operator have an unintended
  219. * level of indirection. You will find them in $this->operator
  220. * and $this->value respectively.
  221. */
  222. function query() {
  223. $this->ensure_my_table();
  224. $field = "$this->table_alias.$this->real_field";
  225. $info = $this->operators();
  226. if (!empty($info[$this->operator]['method'])) {
  227. $this->{$info[$this->operator]['method']}($field);
  228. }
  229. }
  230. function op_equal($field) {
  231. $this->query->add_where($this->options['group'], $field, $this->value, $this->operator());
  232. }
  233. function op_contains($field) {
  234. $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'LIKE');
  235. }
  236. function op_word($field) {
  237. $where = $this->operator == 'word' ? db_or() : db_and();
  238. // Don't filter on empty strings.
  239. if (empty($this->value)) {
  240. return;
  241. }
  242. preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->value, $matches, PREG_SET_ORDER);
  243. foreach ($matches as $match) {
  244. $phrase = false;
  245. // Strip off phrase quotes
  246. if ($match[2]{0} == '"') {
  247. $match[2] = substr($match[2], 1, -1);
  248. $phrase = true;
  249. }
  250. $words = trim($match[2], ',?!();:-');
  251. $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
  252. foreach ($words as $word) {
  253. $placeholder = $this->placeholder();
  254. $where->condition($field, '%' . db_like(trim($word, " ,!?")) . '%', 'LIKE');
  255. }
  256. }
  257. if (!$where) {
  258. return;
  259. }
  260. // previously this was a call_user_func_array but that's unnecessary
  261. // as views will unpack an array that is a single arg.
  262. $this->query->add_where($this->options['group'], $where);
  263. }
  264. function op_starts($field) {
  265. $this->query->add_where($this->options['group'], $field, db_like($this->value) . '%', 'LIKE');
  266. }
  267. function op_not_starts($field) {
  268. $this->query->add_where($this->options['group'], $field, db_like($this->value) . '%', 'NOT LIKE');
  269. }
  270. function op_ends($field) {
  271. $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value), 'LIKE');
  272. }
  273. function op_not_ends($field) {
  274. $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value), 'NOT LIKE');
  275. }
  276. function op_not($field) {
  277. $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'NOT LIKE');
  278. }
  279. function op_shorter($field) {
  280. $placeholder = $this->placeholder();
  281. $this->query->add_where_expression($this->options['group'], "LENGTH($field) < $placeholder", array($placeholder => $this->value));
  282. }
  283. function op_longer($field) {
  284. $placeholder = $this->placeholder();
  285. $this->query->add_where_expression($this->options['group'], "LENGTH($field) > $placeholder", array($placeholder => $this->value));
  286. }
  287. function op_regex($field) {
  288. $this->query->add_where($this->options['group'], $field, $this->value, 'RLIKE');
  289. }
  290. function op_empty($field) {
  291. if ($this->operator == 'empty') {
  292. $operator = "IS NULL";
  293. }
  294. else {
  295. $operator = "IS NOT NULL";
  296. }
  297. $this->query->add_where($this->options['group'], $field, NULL, $operator);
  298. }
  299. }