views_handler_filter_boolean_operator.inc

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

Definition of views_handler_filter_boolean_operator.

File

handlers/views_handler_filter_boolean_operator.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_filter_boolean_operator.
  5. */
  6. /**
  7. * Simple filter to handle matching of boolean values
  8. *
  9. * Definition items:
  10. * - label: (REQUIRED) The label for the checkbox.
  11. * - type: For basic 'true false' types, an item can specify the following:
  12. * - true-false: True/false (this is the default)
  13. * - yes-no: Yes/No
  14. * - on-off: On/Off
  15. * - enabled-disabled: Enabled/Disabled
  16. * - accept null: Treat a NULL value as false.
  17. * - use equal: If you use this flag the query will use = 1 instead of <> 0.
  18. * This might be helpful for performance reasons.
  19. *
  20. * @ingroup views_filter_handlers
  21. */
  22. class views_handler_filter_boolean_operator extends views_handler_filter {
  23. // exposed filter options
  24. var $always_multiple = TRUE;
  25. // Don't display empty space where the operator would be.
  26. var $no_operator = TRUE;
  27. // Whether to accept NULL as a false value or not
  28. var $accept_null = FALSE;
  29. function construct() {
  30. $this->value_value = t('True');
  31. if (isset($this->definition['label'])) {
  32. $this->value_value = $this->definition['label'];
  33. }
  34. if (isset($this->definition['accept null'])) {
  35. $this->accept_null = (bool) $this->definition['accept null'];
  36. }
  37. else if (isset($this->definition['accept_null'])) {
  38. $this->accept_null = (bool) $this->definition['accept_null'];
  39. }
  40. $this->value_options = NULL;
  41. parent::construct();
  42. }
  43. /**
  44. * Return the possible options for this filter.
  45. *
  46. * Child classes should override this function to set the possible values
  47. * for the filter. Since this is a boolean filter, the array should have
  48. * two possible keys: 1 for "True" and 0 for "False", although the labels
  49. * can be whatever makes sense for the filter. These values are used for
  50. * configuring the filter, when the filter is exposed, and in the admin
  51. * summary of the filter. Normally, this should be static data, but if it's
  52. * dynamic for some reason, child classes should use a guard to reduce
  53. * database hits as much as possible.
  54. */
  55. function get_value_options() {
  56. if (isset($this->definition['type'])) {
  57. if ($this->definition['type'] == 'yes-no') {
  58. $this->value_options = array(1 => t('Yes'), 0 => t('No'));
  59. }
  60. if ($this->definition['type'] == 'on-off') {
  61. $this->value_options = array(1 => t('On'), 0 => t('Off'));
  62. }
  63. if ($this->definition['type'] == 'enabled-disabled') {
  64. $this->value_options = array(1 => t('Enabled'), 0 => t('Disabled'));
  65. }
  66. }
  67. // Provide a fallback if the above didn't set anything.
  68. if (!isset($this->value_options)) {
  69. $this->value_options = array(1 => t('True'), 0 => t('False'));
  70. }
  71. }
  72. function option_definition() {
  73. $options = parent::option_definition();
  74. $options['value']['default'] = FALSE;
  75. return $options;
  76. }
  77. function operator_form(&$form, &$form_state) {
  78. $form['operator'] = array();
  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 (!empty($form_state['exposed']) && !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['expose']['required'])) {
  106. $any_label = variable_get('views_exposed_filter_any_label', 'new_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. function value_validate($form, &$form_state) {
  115. if ($form_state['values']['options']['value'] == 'All' && !empty($form_state['values']['options']['expose']['required'])) {
  116. form_set_error('value', t('You must select a value unless this is an non-required exposed filter.'));
  117. }
  118. }
  119. function admin_summary() {
  120. if ($this->is_a_group()) {
  121. return t('grouped');
  122. }
  123. if (!empty($this->options['exposed'])) {
  124. return t('exposed');
  125. }
  126. if (empty($this->value_options)) {
  127. $this->get_value_options();
  128. }
  129. // Now that we have the valid options for this filter, just return the
  130. // human-readable label based on the current value. The value_options
  131. // array is keyed with either 0 or 1, so if the current value is not
  132. // empty, use the label for 1, and if it's empty, use the label for 0.
  133. return $this->value_options[!empty($this->value)];
  134. }
  135. function expose_options() {
  136. parent::expose_options();
  137. $this->options['expose']['operator_id'] = '';
  138. $this->options['expose']['label'] = $this->value_value;
  139. $this->options['expose']['required'] = TRUE;
  140. }
  141. function query() {
  142. $this->ensure_my_table();
  143. $field = "$this->table_alias.$this->real_field";
  144. if (empty($this->value)) {
  145. if ($this->accept_null) {
  146. $or = db_or()
  147. ->condition($field, 0, '=')
  148. ->condition($field, NULL, 'IS NULL');
  149. $this->query->add_where($this->options['group'], $or);
  150. }
  151. else {
  152. $this->query->add_where($this->options['group'], $field, 0, '=');
  153. }
  154. }
  155. else {
  156. if (!empty($this->definition['use equal'])) {
  157. $this->query->add_where($this->options['group'], $field, 1, '=');
  158. }
  159. else {
  160. $this->query->add_where($this->options['group'], $field, 0, '<>');
  161. }
  162. }
  163. }
  164. }