views_handler_filter_string.inc 11 KB

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