views_handler_filter_in_operator.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_filter_in_operator.
  5. */
  6. /**
  7. * Simple filter to handle matching of multiple options using checkboxes.
  8. *
  9. * Definition items:
  10. * - options callback: The function to call in order to generate the value
  11. * options. If omitted, the options 'Yes' and 'No' will be used.
  12. * - options arguments: An array of arguments to pass to the options callback.
  13. *
  14. * @ingroup views_filter_handlers
  15. */
  16. class views_handler_filter_in_operator extends views_handler_filter {
  17. /**
  18. *
  19. */
  20. public $value_form_type = 'checkboxes';
  21. /**
  22. * @var array
  23. * Stores all operations which are available on the form.
  24. */
  25. public $value_options = NULL;
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function construct() {
  30. parent::construct();
  31. $this->value_title = t('Options');
  32. $this->value_options = NULL;
  33. }
  34. /**
  35. * Child classes should be used to override this function and set the
  36. * 'value options', unless 'options callback' is defined as a valid function
  37. * or static public method to generate these values.
  38. *
  39. * This can use a guard to be used to reduce database hits as much as
  40. * possible.
  41. *
  42. * @return
  43. * Return the stored values in $this->value_options if someone expects it.
  44. */
  45. public function get_value_options() {
  46. if (isset($this->value_options)) {
  47. return;
  48. }
  49. if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
  50. if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) {
  51. $this->value_options = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']);
  52. }
  53. else {
  54. $this->value_options = call_user_func($this->definition['options callback']);
  55. }
  56. }
  57. else {
  58. $this->value_options = array(t('Yes'), t('No'));
  59. }
  60. return $this->value_options;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function expose_options() {
  66. parent::expose_options();
  67. $this->options['expose']['reduce'] = FALSE;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function expose_form(&$form, &$form_state) {
  73. parent::expose_form($form, $form_state);
  74. $form['expose']['reduce'] = array(
  75. '#type' => 'checkbox',
  76. '#title' => t('Limit list to selected items'),
  77. '#description' => t('If checked, the only items presented to the user will be the ones selected here.'),
  78. // Safety.
  79. '#default_value' => !empty($this->options['expose']['reduce']),
  80. );
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function option_definition() {
  86. $options = parent::option_definition();
  87. $options['operator']['default'] = 'in';
  88. $options['value']['default'] = array();
  89. $options['expose']['contains']['reduce'] = array('default' => FALSE, 'bool' => TRUE);
  90. return $options;
  91. }
  92. /**
  93. * This kind of construct makes it relatively easy for a child class to add or
  94. * remove functionality by overriding this function and adding/removing items
  95. * from this array.
  96. */
  97. public function operators() {
  98. $operators = array(
  99. 'in' => array(
  100. 'title' => t('Is one of'),
  101. 'short' => t('in'),
  102. 'short_single' => t('='),
  103. 'method' => 'op_simple',
  104. 'values' => 1,
  105. ),
  106. 'not in' => array(
  107. 'title' => t('Is not one of'),
  108. 'short' => t('not in'),
  109. 'short_single' => t('<>'),
  110. 'method' => 'op_simple',
  111. 'values' => 1,
  112. ),
  113. );
  114. // if the definition allows for the empty operator, add it.
  115. if (!empty($this->definition['allow empty'])) {
  116. $operators += array(
  117. 'empty' => array(
  118. 'title' => t('Is empty (NULL)'),
  119. 'method' => 'op_empty',
  120. 'short' => t('empty'),
  121. 'values' => 0,
  122. ),
  123. 'not empty' => array(
  124. 'title' => t('Is not empty (NOT NULL)'),
  125. 'method' => 'op_empty',
  126. 'short' => t('not empty'),
  127. 'values' => 0,
  128. ),
  129. );
  130. }
  131. return $operators;
  132. }
  133. /**
  134. * Build strings from the operators() for 'select' options.
  135. */
  136. public function operator_options($which = 'title') {
  137. $options = array();
  138. foreach ($this->operators() as $id => $info) {
  139. $options[$id] = $info[$which];
  140. }
  141. return $options;
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function operator_values($values = 1) {
  147. $options = array();
  148. foreach ($this->operators() as $id => $info) {
  149. if (isset($info['values']) && $info['values'] == $values) {
  150. $options[] = $id;
  151. }
  152. }
  153. return $options;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function value_form(&$form, &$form_state) {
  159. $form['value'] = array();
  160. $options = array();
  161. if (empty($form_state['exposed'])) {
  162. // Add a select all option to the value form.
  163. $options = array('all' => t('Select all'));
  164. }
  165. $this->get_value_options();
  166. $options += $this->value_options;
  167. $default_value = (array) $this->value;
  168. $which = 'all';
  169. if (!empty($form['operator'])) {
  170. $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
  171. }
  172. if (!empty($form_state['exposed'])) {
  173. $identifier = $this->options['expose']['identifier'];
  174. if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
  175. // Exposed and locked.
  176. $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
  177. }
  178. else {
  179. $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
  180. }
  181. if (!empty($this->options['expose']['reduce'])) {
  182. $options = $this->reduce_value_options();
  183. if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
  184. $default_value = array();
  185. }
  186. }
  187. if (empty($this->options['expose']['multiple'])) {
  188. if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
  189. $default_value = 'All';
  190. }
  191. elseif (empty($default_value)) {
  192. $keys = array_keys($options);
  193. $default_value = array_shift($keys);
  194. }
  195. else {
  196. $copy = $default_value;
  197. $default_value = array_shift($copy);
  198. }
  199. }
  200. }
  201. if ($which == 'all' || $which == 'value') {
  202. $form['value'] = array(
  203. '#type' => $this->value_form_type,
  204. '#title' => $this->value_title,
  205. '#options' => $options,
  206. '#default_value' => $default_value,
  207. // These are only valid for 'select' type, but do no harm to checkboxes.
  208. '#multiple' => TRUE,
  209. '#size' => count($options) > 8 ? 8 : count($options),
  210. );
  211. if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
  212. $form_state['input'][$identifier] = $default_value;
  213. }
  214. if ($which == 'all') {
  215. if (empty($form_state['exposed']) && (in_array($this->value_form_type, array('checkbox', 'checkboxes', 'radios', 'select')))) {
  216. $form['value']['#prefix'] = '<div id="edit-options-value-wrapper">';
  217. $form['value']['#suffix'] = '</div>';
  218. }
  219. $form['value']['#dependency'] = array($source => $this->operator_values(1));
  220. }
  221. }
  222. }
  223. /**
  224. * When using exposed filters, we may be required to reduce the set.
  225. */
  226. public function reduce_value_options($input = NULL) {
  227. if (!isset($input)) {
  228. $input = $this->value_options;
  229. }
  230. // Because options may be an array of strings, or an array of mixed arrays
  231. // and strings (optgroups) or an array of objects, we have to
  232. // step through and handle each one individually.
  233. $options = array();
  234. foreach ($input as $id => $option) {
  235. if (is_array($option)) {
  236. $options[$id] = $this->reduce_value_options($option);
  237. continue;
  238. }
  239. elseif (is_object($option)) {
  240. $keys = array_keys($option->option);
  241. $key = array_shift($keys);
  242. if (isset($this->options['value'][$key])) {
  243. $options[$id] = $option;
  244. }
  245. }
  246. elseif (isset($this->options['value'][$id])) {
  247. $options[$id] = $option;
  248. }
  249. }
  250. return $options;
  251. }
  252. /**
  253. * {@inheritdoc}
  254. */
  255. public function accept_exposed_input($input) {
  256. // A very special override because the All state for this type of
  257. // filter could have a default.
  258. if (empty($this->options['exposed'])) {
  259. return TRUE;
  260. }
  261. // If this is non-multiple and non-required, then this filter will
  262. // participate, but using the default settings, *if* 'limit is true.
  263. if (empty($this->options['expose']['multiple']) && empty($this->options['expose']['required']) && !empty($this->options['expose']['limit'])) {
  264. $identifier = $this->options['expose']['identifier'];
  265. if ($input[$identifier] == 'All') {
  266. return TRUE;
  267. }
  268. }
  269. return parent::accept_exposed_input($input);
  270. }
  271. /**
  272. * {@inheritdoc}
  273. */
  274. public function value_submit($form, &$form_state) {
  275. // Drupal's FAPI system automatically puts '0' in for any checkbox that
  276. // was not set, and the key to the checkbox if it is set.
  277. // Unfortunately, this means that if the key to that checkbox is 0,
  278. // we are unable to tell if that checkbox was set or not.
  279. // Luckily, the '#value' on the checkboxes form actually contains
  280. // *only* a list of checkboxes that were set, and we can use that
  281. // instead.
  282. $form_state['values']['options']['value'] = $form['value']['#value'];
  283. }
  284. /**
  285. * {@inheritdoc}
  286. */
  287. public function admin_summary() {
  288. if ($this->is_a_group()) {
  289. return t('grouped');
  290. }
  291. if (!empty($this->options['exposed'])) {
  292. return t('exposed');
  293. }
  294. $info = $this->operators();
  295. $this->get_value_options();
  296. if (!is_array($this->value)) {
  297. return;
  298. }
  299. $operator = check_plain($info[$this->operator]['short']);
  300. $values = '';
  301. if (in_array($this->operator, $this->operator_values(1))) {
  302. // Remove every element which is not known.
  303. foreach ($this->value as $value) {
  304. if (!isset($this->value_options[$value])) {
  305. unset($this->value[$value]);
  306. }
  307. }
  308. // Choose different kind of ouput for 0, a single and multiple values.
  309. if (count($this->value) == 0) {
  310. $values = t('Unknown');
  311. }
  312. elseif (count($this->value) == 1) {
  313. // If any, use the 'single' short name of the operator instead.
  314. if (isset($info[$this->operator]['short_single'])) {
  315. $operator = check_plain($info[$this->operator]['short_single']);
  316. }
  317. $keys = $this->value;
  318. $value = array_shift($keys);
  319. if (isset($this->value_options[$value])) {
  320. $values = check_plain($this->value_options[$value]);
  321. }
  322. else {
  323. $values = '';
  324. }
  325. }
  326. else {
  327. foreach ($this->value as $value) {
  328. if ($values !== '') {
  329. $values .= ', ';
  330. }
  331. if (drupal_strlen($values) > 8) {
  332. $values .= '...';
  333. break;
  334. }
  335. if (isset($this->value_options[$value])) {
  336. $values .= check_plain($this->value_options[$value]);
  337. }
  338. }
  339. }
  340. }
  341. return $operator . (($values !== '') ? ' ' . $values : '');
  342. }
  343. /**
  344. * {@inheritdoc}
  345. */
  346. public function query() {
  347. $info = $this->operators();
  348. if (!empty($info[$this->operator]['method'])) {
  349. $this->{$info[$this->operator]['method']}();
  350. }
  351. }
  352. /**
  353. * {@inheritdoc}
  354. */
  355. public function op_simple() {
  356. if (empty($this->value)) {
  357. return;
  358. }
  359. $this->ensure_my_table();
  360. // We use array_values() because the checkboxes keep keys and that can cause
  361. // array addition problems.
  362. $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", array_values($this->value), $this->operator);
  363. }
  364. /**
  365. * {@inheritdoc}
  366. */
  367. public function op_empty() {
  368. $this->ensure_my_table();
  369. if ($this->operator == 'empty') {
  370. $operator = "IS NULL";
  371. }
  372. else {
  373. $operator = "IS NOT NULL";
  374. }
  375. $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", NULL, $operator);
  376. }
  377. /**
  378. * {@inheritdoc}
  379. */
  380. public function validate() {
  381. $this->get_value_options();
  382. $errors = array();
  383. // If the operator is an operator which doesn't require a value, there is
  384. // no need for additional validation.
  385. if (in_array($this->operator, $this->operator_values(0))) {
  386. return array();
  387. }
  388. if (!in_array($this->operator, $this->operator_values(1))) {
  389. $errors[] = t('The operator is invalid on filter: @filter.', array('@filter' => $this->ui_name(TRUE)));
  390. }
  391. if (is_array($this->value)) {
  392. if (!isset($this->value_options)) {
  393. // Don't validate if there are none value options provided, for example
  394. // for special handlers.
  395. return $errors;
  396. }
  397. if ($this->options['exposed'] && !$this->options['expose']['required'] && empty($this->value)) {
  398. // Don't validate if the field is exposed and no default value is
  399. // provided.
  400. return $errors;
  401. }
  402. // Some filter_in_operator usage uses optgroups forms, so flatten it.
  403. $flat_options = form_options_flatten($this->value_options, TRUE);
  404. // Remove every element which is not known.
  405. foreach ($this->value as $value) {
  406. if (!isset($flat_options[$value])) {
  407. unset($this->value[$value]);
  408. }
  409. }
  410. // Choose different kind of ouput for 0, a single and multiple values.
  411. if (count($this->value) == 0) {
  412. $errors[] = t('No valid values found on filter: @filter.', array('@filter' => $this->ui_name(TRUE)));
  413. }
  414. }
  415. elseif (!empty($this->value) && ($this->operator == 'in' || $this->operator == 'not in')) {
  416. $errors[] = t('The value @value is not an array for @operator on filter: @filter', array('@value' => views_var_export($this->value), '@operator' => $this->operator, '@filter' => $this->ui_name(TRUE)));
  417. }
  418. return $errors;
  419. }
  420. }