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

File

handlers/views_handler_filter_boolean_operator.inc
View source
  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. * - accept null: Treat a NULL value as false.
  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. function construct() {
  21. $this->value_value = t('True');
  22. if (isset($this->definition['label'])) {
  23. $this->value_value = $this->definition['label'];
  24. }
  25. if (isset($this->definition['accept null'])) {
  26. $this->accept_null = (bool) $this->definition['accept null'];
  27. }
  28. else if (isset($this->definition['accept_null'])) {
  29. $this->accept_null = (bool) $this->definition['accept_null'];
  30. }
  31. $this->value_options = NULL;
  32. parent::construct();
  33. }
  34. /**
  35. * Return the possible options for this filter.
  36. *
  37. * Child classes should override this function to set the possible values
  38. * for the filter. Since this is a boolean filter, the array should have
  39. * two possible keys: 1 for "True" and 0 for "False", although the labels
  40. * can be whatever makes sense for the filter. These values are used for
  41. * configuring the filter, when the filter is exposed, and in the admin
  42. * summary of the filter. Normally, this should be static data, but if it's
  43. * dynamic for some reason, child classes should use a guard to reduce
  44. * database hits as much as possible.
  45. */
  46. function get_value_options() {
  47. if (isset($this->definition['type'])) {
  48. if ($this->definition['type'] == 'yes-no') {
  49. $this->value_options = array(1 => t('Yes'), 0 => t('No'));
  50. }
  51. if ($this->definition['type'] == 'on-off') {
  52. $this->value_options = array(1 => t('On'), 0 => t('Off'));
  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. function option_definition() {
  61. $options = parent::option_definition();
  62. $options['value']['default'] = FALSE;
  63. return $options;
  64. }
  65. function operator_form(&$form, &$form_state) {
  66. $form['operator'] = array();
  67. }
  68. function value_form(&$form, &$form_state) {
  69. if (empty($this->value_options)) {
  70. // Initialize the array of possible values for this filter.
  71. $this->get_value_options();
  72. }
  73. if (!empty($form_state['exposed'])) {
  74. // Exposed filter: use a select box to save space.
  75. $filter_form_type = 'select';
  76. }
  77. else {
  78. // Configuring a filter: use radios for clarity.
  79. $filter_form_type = 'radios';
  80. }
  81. $form['value'] = array(
  82. '#type' => $filter_form_type,
  83. '#title' => $this->value_value,
  84. '#options' => $this->value_options,
  85. '#default_value' => $this->value,
  86. );
  87. if (!empty($this->options['exposed'])) {
  88. $identifier = $this->options['expose']['identifier'];
  89. if (!isset($form_state['input'][$identifier])) {
  90. $form_state['input'][$identifier] = $this->value;
  91. }
  92. // If we're configuring an exposed filter, add an <Any> option.
  93. if (empty($form_state['exposed']) || !empty($this->options['optional'])) {
  94. $any_label = variable_get('views_exposed_filter_any_label', 'old_any') == 'old_any' ? '<Any>' : t('- Any -');
  95. if ($form['value']['#type'] != 'select') {
  96. $any_label = check_plain($any_label);
  97. }
  98. $form['value']['#options'] = array('All' => $any_label) + $form['value']['#options'];
  99. }
  100. }
  101. }
  102. function value_validate(&$form, &$form_state) {
  103. if ($form_state['values']['options']['value'] == 'All' && empty($form_state['values']['options']['expose']['optional'])) {
  104. form_set_error('value', t('You must select a value unless this is an optional exposed filter.'));
  105. }
  106. }
  107. function admin_summary() {
  108. if (!empty($this->options['exposed'])) {
  109. return t('exposed');
  110. }
  111. if (empty($this->value_options)) {
  112. $this->get_value_options();
  113. }
  114. // Now that we have the valid options for this filter, just return the
  115. // human-readable label based on the current value. The value_options
  116. // array is keyed with either 0 or 1, so if the current value is not
  117. // empty, use the label for 1, and if it's empty, use the label for 0.
  118. return $this->value_options[!empty($this->value)];
  119. }
  120. function expose_options() {
  121. parent::expose_options();
  122. $this->options['expose']['operator'] = '';
  123. $this->options['expose']['label'] = $this->value_value;
  124. $this->options['expose']['optional'] = FALSE;
  125. }
  126. function query() {
  127. $this->ensure_my_table();
  128. $where = "$this->table_alias.$this->real_field ";
  129. if (empty($this->value)) {
  130. $where .= '= 0';
  131. if ($this->accept_null) {
  132. $where = '(' . $where . " OR $this->table_alias.$this->real_field IS NULL)";
  133. }
  134. }
  135. else {
  136. if (!empty($this->definition['use equal'])) {
  137. $where .= '= 1';
  138. }
  139. else {
  140. $where .= '<> 0';
  141. }
  142. }
  143. $this->query->add_where($this->options['group'], $where);
  144. }
  145. }