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