source: sipes/modules_contrib/views/handlers/views_handler_filter_boolean_operator.inc @ 65dadeb

stableversion-3.0
Last change on this file since 65dadeb was 59029b2, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se actualizo la version del modulo views

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