views_handler_filter_in_operator.inc

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

File

handlers/views_handler_filter_in_operator.inc
View source
  1. <?php
  2. /**
  3. * Simple filter to handle matching of multiple options selectable via checkboxes
  4. *
  5. * Definition items:
  6. * - numeric: If set to true, this item will use numeric operators instead of string.
  7. *
  8. */
  9. class views_handler_filter_in_operator extends views_handler_filter {
  10. var $value_form_type = 'checkboxes';
  11. function construct() {
  12. parent::construct();
  13. $this->value_title = t('Options');
  14. $this->value_options = NULL;
  15. }
  16. /**
  17. * Child classes should be used to override this function and set the
  18. * 'value options', unless 'options callback' is defined as a valid function
  19. * or static public method to generate these values.
  20. *
  21. * This can use a guard to be used to reduce database hits as much as
  22. * possible.
  23. */
  24. function get_value_options() {
  25. if (isset($this->value_options)) {
  26. return;
  27. }
  28. if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
  29. $this->value_options = call_user_func($this->definition['options callback']);
  30. }
  31. else {
  32. $this->value_options = array(t('Yes'), t('No'));
  33. }
  34. }
  35. function expose_options() {
  36. parent::expose_options();
  37. $this->options['expose']['reduce'] = FALSE;
  38. }
  39. function expose_form_right(&$form, &$form_state) {
  40. parent::expose_form_right($form, $form_state);
  41. $form['expose']['reduce'] = array(
  42. '#type' => 'checkbox',
  43. '#title' => t('Limit list to selected items'),
  44. '#description' => t('If checked, the only items presented to the user will be the ones selected here.'),
  45. '#default_value' => !empty($this->options['expose']['reduce']), // safety
  46. );
  47. }
  48. function option_definition() {
  49. $options = parent::option_definition();
  50. $options['operator']['default'] = 'in';
  51. $options['value']['default'] = array();
  52. return $options;
  53. }
  54. /**
  55. * This kind of construct makes it relatively easy for a child class
  56. * to add or remove functionality by overriding this function and
  57. * adding/removing items from this array.
  58. */
  59. function operators() {
  60. $operators = array(
  61. 'in' => array(
  62. 'title' => t('Is one of'),
  63. 'short' => t('in'),
  64. 'short_single' => t('='),
  65. 'method' => 'op_simple',
  66. 'values' => 1,
  67. ),
  68. 'not in' => array(
  69. 'title' => t('Is not one of'),
  70. 'short' => t('not in'),
  71. 'short_single' => t('<>'),
  72. 'method' => 'op_simple',
  73. 'values' => 1,
  74. ),
  75. );
  76. // if the definition allows for the empty operator, add it.
  77. if (!empty($this->definition['allow empty'])) {
  78. $operators += array(
  79. 'empty' => array(
  80. 'title' => t('Is empty (NULL)'),
  81. 'method' => 'op_empty',
  82. 'short' => t('empty'),
  83. 'values' => 0,
  84. ),
  85. 'not empty' => array(
  86. 'title' => t('Is not empty (NOT NULL)'),
  87. 'method' => 'op_empty',
  88. 'short' => t('not empty'),
  89. 'values' => 0,
  90. ),
  91. );
  92. }
  93. return $operators;
  94. }
  95. /**
  96. * Build strings from the operators() for 'select' options
  97. */
  98. function operator_options($which = 'title') {
  99. $options = array();
  100. foreach ($this->operators() as $id => $info) {
  101. $options[$id] = $info[$which];
  102. }
  103. return $options;
  104. }
  105. function operator_values($values = 1) {
  106. $options = array();
  107. foreach ($this->operators() as $id => $info) {
  108. if (isset($info['values']) && $info['values'] == $values) {
  109. $options[] = $id;
  110. }
  111. }
  112. return $options;
  113. }
  114. function value_form(&$form, &$form_state) {
  115. $form['value'] = array();
  116. $this->get_value_options();
  117. $options = $this->value_options;
  118. $default_value = (array) $this->value;
  119. $which = 'all';
  120. if (!empty($form['operator'])) {
  121. $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
  122. }
  123. if (!empty($form_state['exposed'])) {
  124. $identifier = $this->options['expose']['identifier'];
  125. if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator'])) {
  126. // exposed and locked.
  127. $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
  128. }
  129. else {
  130. $source = 'edit-' . form_clean_id($this->options['expose']['operator']);
  131. }
  132. if (!empty($this->options['expose']['reduce'])) {
  133. $options = $this->reduce_value_options();
  134. if (empty($this->options['expose']['single']) && !empty($this->options['expose']['optional'])) {
  135. $default_value = array();
  136. }
  137. }
  138. if (!empty($this->options['expose']['single'])) {
  139. if (!empty($this->options['expose']['optional']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
  140. $default_value = 'All';
  141. }
  142. else if (empty($default_value)) {
  143. $keys = array_keys($options);
  144. $default_value = array_shift($keys);
  145. }
  146. else {
  147. $copy = $default_value;
  148. $default_value = array_shift($copy);
  149. }
  150. }
  151. }
  152. if ($which == 'all' || $which == 'value') {
  153. $form['value'] = array(
  154. '#type' => $this->value_form_type,
  155. '#title' => $this->value_title,
  156. '#options' => $options,
  157. '#default_value' => $default_value,
  158. // These are only valid for 'select' type, but do no harm to checkboxes.
  159. '#multiple' => TRUE,
  160. '#size' => count($options) > 8 ? 8 : count($options),
  161. );
  162. if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
  163. $form_state['input'][$identifier] = $default_value;
  164. }
  165. $process = array();
  166. if ($this->value_form_type == 'checkboxes') {
  167. // If this form element will use checkboxes in the UI, we need to
  168. // check_plain() all the options ourselves since FAPI is inconsistent
  169. // about this. However, instead of directly doing that to the #options
  170. // right now, we define a #process callback since we might change our
  171. // mind later and convert this into a 'select' form element, which
  172. // would lead to double-escaping the options.
  173. $process[] = 'views_process_check_options';
  174. }
  175. if ($which == 'all') {
  176. if (empty($form_state['exposed']) && ($this->value_form_type == 'checkboxes' || $this->value_form_type == 'radios')) {
  177. $process[] = "expand_$this->value_form_type";
  178. $form['value']['#prefix'] = '<div id="edit-options-value-wrapper">';
  179. $form['value']['#suffix'] = '</div>';
  180. }
  181. $process[] = 'views_process_dependency';
  182. $form['value']['#dependency'] = array($source => $this->operator_values(1));
  183. }
  184. if (!empty($process)) {
  185. $form['value']['#process'] = $process;
  186. }
  187. }
  188. }
  189. /**
  190. * When using exposed filters, we may be required to reduce the set.
  191. */
  192. function reduce_value_options($input = NULL) {
  193. if (!isset($input)) {
  194. $input = $this->value_options;
  195. }
  196. // Because options may be an array of strings, or an array of mixed arrays
  197. // and strings (optgroups) or an array of objects, we have to
  198. // step through and handle each one individually.
  199. $options = array();
  200. foreach ($input as $id => $option) {
  201. if (is_array($option)) {
  202. $options[$id] = $this->reduce_value_options($option);
  203. continue;
  204. }
  205. else if (is_object($option)) {
  206. $keys = array_keys($option->option);
  207. $key = array_shift($keys);
  208. if (isset($this->options['value'][$key])) {
  209. $options[$id] = $option;
  210. }
  211. }
  212. else if (isset($this->options['value'][$id])) {
  213. $options[$id] = $option;
  214. }
  215. }
  216. return $options;
  217. }
  218. function accept_exposed_input($input) {
  219. // A very special override because the All state for this type of
  220. // filter could have a default:
  221. if (empty($this->options['exposed'])) {
  222. return TRUE;
  223. }
  224. // If this is single and optional, this says that yes this filter will
  225. // participate, but using the default settings, *if* 'limit is true.
  226. if (!empty($this->options['expose']['single']) && !empty($this->options['expose']['optional']) && !empty($this->options['expose']['limit'])) {
  227. $identifier = $this->options['expose']['identifier'];
  228. if ($input[$identifier] == 'All') {
  229. return TRUE;
  230. }
  231. }
  232. return parent::accept_exposed_input($input);
  233. }
  234. function value_submit($form, &$form_state) {
  235. // Drupal's FAPI system automatically puts '0' in for any checkbox that
  236. // was not set, and the key to the checkbox if it is set.
  237. // Unfortunately, this means that if the key to that checkbox is 0,
  238. // we are unable to tell if that checkbox was set or not.
  239. // Luckily, the '#value' on the checkboxes form actually contains
  240. // *only* a list of checkboxes that were set, and we can use that
  241. // instead.
  242. $form_state['values']['options']['value'] = $form['value']['#value'];
  243. // $form_state['values']['options']['value'] = array_filter($form_state['values']['options']['value']);
  244. }
  245. function admin_summary() {
  246. if (!empty($this->options['exposed'])) {
  247. return t('exposed');
  248. }
  249. $info = $this->operators();
  250. $this->get_value_options();
  251. if (!is_array($this->value)) {
  252. return;
  253. }
  254. $operator = check_plain($info[$this->operator]['short']);
  255. $values = '';
  256. if (in_array($this->operator, $this->operator_values(1))) {
  257. // Remove every element which is not known.
  258. foreach ($this->value as $value) {
  259. if (!isset($this->value_options[$value])) {
  260. unset($this->value[$value]);
  261. }
  262. }
  263. // Choose different kind of ouput for 0, a single and multiple values.
  264. if (count($this->value) == 0) {
  265. $values = t('Unknown');
  266. }
  267. else if (count($this->value) == 1) {
  268. // If any, use the 'single' short name of the operator instead.
  269. if (isset($info[$this->operator]['short_single'])) {
  270. $operator = check_plain($info[$this->operator]['short_single']);
  271. }
  272. $keys = $this->value;
  273. $value = array_shift($keys);
  274. $values = check_plain($this->value_options[$value]);
  275. }
  276. else {
  277. foreach ($this->value as $value) {
  278. if ($values !== '') {
  279. $values .= ', ';
  280. }
  281. if (strlen($values) > 8) {
  282. $values .= '...';
  283. break;
  284. }
  285. $values .= check_plain($this->value_options[$value]);
  286. }
  287. }
  288. }
  289. return $operator . (($values !== '') ? ' ' . $values : '');
  290. }
  291. function query() {
  292. $info = $this->operators();
  293. if (!empty($info[$this->operator]['method'])) {
  294. $this->{$info[$this->operator]['method']}();
  295. }
  296. }
  297. function op_simple() {
  298. if (empty($this->value)) {
  299. return;
  300. }
  301. $this->ensure_my_table();
  302. $placeholder = !empty($this->definition['numeric']) ? '%d' : "'%s'";
  303. $replace = array_fill(0, sizeof($this->value), $placeholder);
  304. $in = ' (' . implode(", ", $replace) . ')';
  305. // We use array_values() because the checkboxes keep keys and that can cause
  306. // array addition problems.
  307. $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field " . $this->operator . $in, array_values($this->value));
  308. }
  309. function op_empty() {
  310. $this->ensure_my_table();
  311. $field = "$this->table_alias.$this->real_field";
  312. if ($this->operator == 'empty') {
  313. $operator = "IS NULL";
  314. }
  315. else {
  316. $operator = "IS NOT NULL";
  317. }
  318. $this->query->add_where($this->options['group'], "$field $operator");
  319. }
  320. }