3 * Simple filter to handle matching of boolean values
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)
11 * - enabled-disabled: Enabled/Disabled
12 * - accept null: Treat a NULL value as false.
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
;
22 function construct() {
23 $this->value_value
= t('True');
24 if (isset($this->definition
['label'])) {
25 $this->value_value
= $this->definition
['label'];
27 if (isset($this->definition
['accept null'])) {
28 $this->accept_null
= (bool
) $this->definition
['accept null'];
30 else if (isset($this->definition
['accept_null'])) {
31 $this->accept_null
= (bool
) $this->definition
['accept_null'];
33 $this->value_options
= NULL
;
38 * Return the possible options for this filter.
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.
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'));
54 if ($this->definition
['type'] == 'on-off') {
55 $this->value_options
= array(1 => t('On'), 0 => t('Off'));
57 if ($this->definition
['type'] == 'enabled-disabled') {
58 $this->value_options
= array(1 => t('Enabled'), 0 => t('Disabled'));
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'));
68 function option_definition() {
69 $options = parent
::option_definition();
71 $options['value']['default'] = FALSE
;
76 function operator_form(&$form, &$form_state) {
77 $form['operator'] = array();
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();
85 if (!empty($form_state['exposed'])) {
86 // Exposed filter: use a select box to save space.
87 $filter_form_type = 'select';
90 // Configuring a filter: use radios for clarity.
91 $filter_form_type = 'radios';
93 $form['value'] = array(
94 '#type' => $filter_form_type,
95 '#title' => $this->value_value
,
96 '#options' => $this->value_options
,
97 '#default_value' => $this->value
,
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
;
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);
110 $form['value']['#options'] = array('All' => $any_label) + $form['value']['#options'];
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.'));
121 function admin_summary() {
122 if (!empty($this->options
['exposed'])) {
125 if (empty($this->value_options
)) {
126 $this->get_value_options();
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
)];
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
;
143 $this->ensure_my_table();
145 $where = "$this->table_alias.$this->real_field ";
147 if (empty($this->value
)) {
149 if ($this->accept_null
) {
150 $where = '(' .
$where .
" OR $this->table_alias.$this->real_field IS NULL)";
154 if (!empty($this->definition
['use equal'])) {
161 $this->query
->add_where($this->options
['group'], $where);