#1102852 by ralf: Add enabled-disabled boolean display format
[project/views.git] / handlers / views_handler_filter_boolean_operator.inc
1 <?php
2 /**
3 * Simple filter to handle matching of boolean values
4 *
5 * Definition items:
6 * - label: (REQUIRED) The label for the checkbox.
7 * - type: For basic 'true false' types, an item can specify the following:
8 * - true-false: True/false (this is the default)
9 * - yes-no: Yes/No
10 * - on-off: On/Off
11 * - enabled-disabled: Enabled/Disabled
12 * - accept null: Treat a NULL value as false.
13 */
14 class views_handler_filter_boolean_operator extends views_handler_filter {
15 // exposed filter options
16 var $no_single = TRUE;
17 // Don't display empty space where the operator would be.
18 var $no_operator = TRUE;
19 // Whether to accept NULL as a false value or not
20 var $accept_null = FALSE;
21
22 function construct() {
23 $this->value_value = t('True');
24 if (isset($this->definition['label'])) {
25 $this->value_value = $this->definition['label'];
26 }
27 if (isset($this->definition['accept null'])) {
28 $this->accept_null = (bool) $this->definition['accept null'];
29 }
30 else if (isset($this->definition['accept_null'])) {
31 $this->accept_null = (bool) $this->definition['accept_null'];
32 }
33 $this->value_options = NULL;
34 parent::construct();
35 }
36
37 /**
38 * Return the possible options for this filter.
39 *
40 * Child classes should override this function to set the possible values
41 * for the filter. Since this is a boolean filter, the array should have
42 * two possible keys: 1 for "True" and 0 for "False", although the labels
43 * can be whatever makes sense for the filter. These values are used for
44 * configuring the filter, when the filter is exposed, and in the admin
45 * summary of the filter. Normally, this should be static data, but if it's
46 * dynamic for some reason, child classes should use a guard to reduce
47 * database hits as much as possible.
48 */
49 function get_value_options() {
50 if (isset($this->definition['type'])) {
51 if ($this->definition['type'] == 'yes-no') {
52 $this->value_options = array(1 => t('Yes'), 0 => t('No'));
53 }
54 if ($this->definition['type'] == 'on-off') {
55 $this->value_options = array(1 => t('On'), 0 => t('Off'));
56 }
57 if ($this->definition['type'] == 'enabled-disabled') {
58 $this->value_options = array(1 => t('Enabled'), 0 => t('Disabled'));
59 }
60 }
61
62 // Provide a fallback if the above didn't set anything.
63 if (!isset($this->value_options)) {
64 $this->value_options = array(1 => t('True'), 0 => t('False'));
65 }
66 }
67
68 function option_definition() {
69 $options = parent::option_definition();
70
71 $options['value']['default'] = FALSE;
72
73 return $options;
74 }
75
76 function operator_form(&$form, &$form_state) {
77 $form['operator'] = array();
78 }
79
80 function value_form(&$form, &$form_state) {
81 if (empty($this->value_options)) {
82 // Initialize the array of possible values for this filter.
83 $this->get_value_options();
84 }
85 if (!empty($form_state['exposed'])) {
86 // Exposed filter: use a select box to save space.
87 $filter_form_type = 'select';
88 }
89 else {
90 // Configuring a filter: use radios for clarity.
91 $filter_form_type = 'radios';
92 }
93 $form['value'] = array(
94 '#type' => $filter_form_type,
95 '#title' => $this->value_value,
96 '#options' => $this->value_options,
97 '#default_value' => $this->value,
98 );
99 if (!empty($this->options['exposed'])) {
100 $identifier = $this->options['expose']['identifier'];
101 if (!isset($form_state['input'][$identifier])) {
102 $form_state['input'][$identifier] = $this->value;
103 }
104 // If we're configuring an exposed filter, add an <Any> option.
105 if (empty($form_state['exposed']) || !empty($this->options['optional'])) {
106 $any_label = variable_get('views_exposed_filter_any_label', 'old_any') == 'old_any' ? '<Any>' : t('- Any -');
107 if ($form['value']['#type'] != 'select') {
108 $any_label = check_plain($any_label);
109 }
110 $form['value']['#options'] = array('All' => $any_label) + $form['value']['#options'];
111 }
112 }
113 }
114
115 function value_validate($form, &$form_state) {
116 if ($form_state['values']['options']['value'] == 'All' && empty($form_state['values']['options']['expose']['optional'])) {
117 form_set_error('value', t('You must select a value unless this is an optional exposed filter.'));
118 }
119 }
120
121 function admin_summary() {
122 if (!empty($this->options['exposed'])) {
123 return t('exposed');
124 }
125 if (empty($this->value_options)) {
126 $this->get_value_options();
127 }
128 // Now that we have the valid options for this filter, just return the
129 // human-readable label based on the current value. The value_options
130 // array is keyed with either 0 or 1, so if the current value is not
131 // empty, use the label for 1, and if it's empty, use the label for 0.
132 return $this->value_options[!empty($this->value)];
133 }
134
135 function expose_options() {
136 parent::expose_options();
137 $this->options['expose']['operator'] = '';
138 $this->options['expose']['label'] = $this->value_value;
139 $this->options['expose']['optional'] = FALSE;
140 }
141
142 function query() {
143 $this->ensure_my_table();
144
145 $where = "$this->table_alias.$this->real_field ";
146
147 if (empty($this->value)) {
148 $where .= '= 0';
149 if ($this->accept_null) {
150 $where = '(' . $where . " OR $this->table_alias.$this->real_field IS NULL)";
151 }
152 }
153 else {
154 if (!empty($this->definition['use equal'])) {
155 $where .= '= 1';
156 }
157 else {
158 $where .= '<> 0';
159 }
160 }
161 $this->query->add_where($this->options['group'], $where);
162 }
163 }