View source
<?php
class views_handler_filter extends views_handler {
public $value = NULL;
public $operator = '=';
public $group_info = NULL;
public $always_multiple = FALSE;
public $no_operator = FALSE;
public $always_required = FALSE;
public function init(&$view, &$options) {
parent::init($view, $options);
$this->operator = $this->options['operator'];
$this->value = $this->options['value'];
$this->group_info = $this->options['group_info']['default_group'];
if (!empty($options['exposed']) && !empty($options['expose']['optional']) && !isset($options['expose']['required'])) {
$this->options['expose']['required'] = !$options['expose']['optional'];
}
if (!empty($options['exposed']) && !empty($options['expose']['single']) && !isset($options['expose']['multiple'])) {
$this->options['expose']['multiple'] = !$options['expose']['single'];
}
if (!empty($options['exposed']) && !empty($options['expose']['operator']) && !isset($options['expose']['operator_id'])) {
$this->options['expose']['operator_id'] = $options['expose']['operator_id'] = $options['expose']['operator'];
}
if ($this
->multiple_exposed_input()) {
$this->group_info = NULL;
if (!empty($options['group_info']['default_group_multiple'])) {
$this->group_info = array_filter($options['group_info']['default_group_multiple']);
}
$this->options['expose']['multiple'] = TRUE;
}
if ($this->view->display_handler
->get_option('relationships')) {
$this->definition['allow empty'] = TRUE;
}
}
public function option_definition() {
$options = parent::option_definition();
$options['operator'] = array(
'default' => '=',
);
$options['value'] = array(
'default' => '',
);
$options['group'] = array(
'default' => '1',
);
$options['exposed'] = array(
'default' => FALSE,
'bool' => TRUE,
);
$options['expose'] = array(
'contains' => array(
'operator_id' => array(
'default' => FALSE,
),
'label' => array(
'default' => '',
'translatable' => TRUE,
),
'description' => array(
'default' => '',
'translatable' => TRUE,
),
'use_operator' => array(
'default' => FALSE,
'bool' => TRUE,
),
'operator_label' => array(
'default' => '',
'translatable' => TRUE,
),
'operator' => array(
'default' => '',
),
'limit_operators' => array(
'default' => FALSE,
'bool' => TRUE,
),
'available_operators' => array(
'default' => array(),
),
'identifier' => array(
'default' => '',
),
'required' => array(
'default' => FALSE,
'bool' => TRUE,
),
'remember' => array(
'default' => FALSE,
'bool' => TRUE,
),
'multiple' => array(
'default' => FALSE,
'bool' => TRUE,
),
'remember_roles' => array(
'default' => array(
DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
),
),
),
);
$options['is_grouped'] = array(
'default' => FALSE,
'bool' => TRUE,
);
$options['group_info'] = array(
'contains' => array(
'label' => array(
'default' => '',
'translatable' => TRUE,
),
'description' => array(
'default' => '',
'translatable' => TRUE,
),
'identifier' => array(
'default' => '',
),
'optional' => array(
'default' => TRUE,
'bool' => TRUE,
),
'widget' => array(
'default' => 'select',
),
'multiple' => array(
'default' => FALSE,
'bool' => TRUE,
),
'remember' => array(
'default' => 0,
),
'default_group' => array(
'default' => 'All',
),
'default_group_multiple' => array(
'default' => array(),
),
'group_items' => array(
'default' => array(),
),
),
);
return $options;
}
public function admin_summary() {
return check_plain((string) $this->operator) . ' ' . check_plain((string) $this->value);
}
public function can_expose() {
return TRUE;
}
public function can_build_group() {
return $this
->is_exposed() && count($this
->operator_options()) > 0;
}
public function is_a_group() {
return $this
->is_exposed() && !empty($this->options['is_grouped']);
}
public function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
if ($this
->can_expose()) {
$this
->show_expose_button($form, $form_state);
}
if ($this
->can_build_group()) {
$this
->show_build_group_button($form, $form_state);
}
$form['clear_markup_start'] = array(
'#markup' => '<div class="clearfix">',
);
if ($this
->is_a_group()) {
if ($this
->can_build_group()) {
$form['clear_markup_start'] = array(
'#markup' => '<div class="clearfix">',
);
$this
->show_build_group_form($form, $form_state);
$form['clear_markup_end'] = array(
'#markup' => '</div>',
);
}
}
else {
$this
->show_operator_form($form, $form_state);
$this
->show_value_form($form, $form_state);
$form['clear_markup_end'] = array(
'#markup' => '</div>',
);
if ($this
->can_expose()) {
$this
->show_expose_form($form, $form_state);
}
}
}
public function options_validate(&$form, &$form_state) {
$this
->operator_validate($form, $form_state);
$this
->value_validate($form, $form_state);
if (!empty($this->options['exposed']) && !$this
->is_a_group()) {
$this
->expose_validate($form, $form_state);
}
if ($this
->is_a_group()) {
$this
->build_group_validate($form, $form_state);
}
}
public function options_submit(&$form, &$form_state) {
unset($form_state['values']['expose_button']);
unset($form_state['values']['group_button']);
if (!$this
->is_a_group()) {
$this
->operator_submit($form, $form_state);
$this
->value_submit($form, $form_state);
}
if (!empty($this->options['exposed'])) {
$options =& $form_state['values']['options']['expose'];
$options['available_operators'] = !empty($options['use_operator']) && !empty($options['limit_operators']) ? array_filter($options['available_operators']) : array();
$this
->expose_submit($form, $form_state);
}
if ($this
->is_a_group()) {
$this
->build_group_submit($form, $form_state);
}
}
public function show_operator_form(&$form, &$form_state) {
$this
->operator_form($form, $form_state);
$form['operator']['#prefix'] = '<div class="views-group-box views-left-30">';
$form['operator']['#suffix'] = '</div>';
}
public function operator_form(&$form, &$form_state) {
$options = $this
->operator_options();
if (!empty($options)) {
$available = $this->options['expose']['available_operators'];
if ($this->options['expose']['limit_operators'] && count($available)) {
foreach ($options as $key => $value) {
if (!isset($available[$key])) {
unset($options[$key]);
}
}
if (!isset($options[$this->operator])) {
$this->operator = key($options);
}
}
$form['operator'] = array(
'#type' => count($options) < 10 ? 'radios' : 'select',
'#title' => t('Operator'),
'#default_value' => $this->operator,
'#options' => $options,
);
}
}
public function operator_options() {
return array();
}
public function operator_validate($form, &$form_state) {
}
public function operator_submit($form, &$form_state) {
}
public function show_value_form(&$form, &$form_state) {
$this
->value_form($form, $form_state);
if (empty($this->no_operator)) {
$form['value']['#prefix'] = '<div class="views-group-box views-right-70">' . (isset($form['value']['#prefix']) ? $form['value']['#prefix'] : '');
$form['value']['#suffix'] = (isset($form['value']['#suffix']) ? $form['value']['#suffix'] : '') . '</div>';
}
}
public function value_form(&$form, &$form_state) {
$form['value'] = array();
}
public function value_validate($form, &$form_state) {
}
public function value_submit($form, &$form_state) {
}
public function show_build_group_form(&$form, &$form_state) {
if (empty($this->options['is_grouped'])) {
return;
}
$this
->build_group_form($form, $form_state);
if (!empty($form_state['force_build_group_options'])) {
foreach (element_children($form['group_info']) as $id) {
if (isset($form['group_info'][$id]['#default_value']) && !isset($form['group_info'][$id]['#value'])) {
$form['group_info'][$id]['#value'] = $form['group_info'][$id]['#default_value'];
}
}
}
}
public function show_build_group_button(&$form, &$form_state) {
$form['group_button'] = array(
'#prefix' => '<div class="views-grouped clearfix">',
'#suffix' => '</div>',
'#weight' => -190,
);
$grouped_description = t('Grouped filters allow a choice between predefined operator|value pairs.');
$form['group_button']['radios'] = array(
'#theme_wrappers' => array(
'container',
),
'#attributes' => array(
'class' => array(
'js-only',
),
),
);
$form['group_button']['radios']['radios'] = array(
'#title' => t('Filter type to expose'),
'#description' => $grouped_description,
'#type' => 'radios',
'#options' => array(
t('Single filter'),
t('Grouped filters'),
),
);
if (empty($this->options['is_grouped'])) {
$form['group_button']['markup'] = array(
'#markup' => '<div class="description grouped-description">' . $grouped_description . '</div>',
);
$form['group_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => t('Grouped filters'),
'#submit' => array(
'views_ui_config_item_form_build_group',
),
);
$form['group_button']['radios']['radios']['#default_value'] = 0;
}
else {
$form['group_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => t('Single filter'),
'#submit' => array(
'views_ui_config_item_form_build_group',
),
);
$form['group_button']['radios']['radios']['#default_value'] = 1;
}
}
public function show_expose_button(&$form, &$form_state) {
$form['expose_button'] = array(
'#prefix' => '<div class="views-expose clearfix">',
'#suffix' => '</div>',
'#weight' => -200,
);
$form['expose_button']['checkbox'] = array(
'#theme_wrappers' => array(
'container',
),
'#attributes' => array(
'class' => array(
'js-only',
),
),
);
$form['expose_button']['checkbox']['checkbox'] = array(
'#title' => t('Expose this filter to visitors, to allow them to change it'),
'#type' => 'checkbox',
);
if (empty($this->options['exposed'])) {
$form['expose_button']['markup'] = array(
'#markup' => '<div class="description exposed-description">' . t('This filter is not exposed. Expose it to allow the users to change it.') . '</div>',
);
$form['expose_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => t('Expose filter'),
'#submit' => array(
'views_ui_config_item_form_expose',
),
);
$form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
}
else {
$form['expose_button']['markup'] = array(
'#markup' => '<div class="description exposed-description">' . t('This filter is exposed. If you hide it, users will not be able to change it.') . '</div>',
);
$form['expose_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => t('Hide filter'),
'#submit' => array(
'views_ui_config_item_form_expose',
),
);
$form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
}
}
public function expose_form(&$form, &$form_state) {
$form['#theme'] = 'views_ui_expose_filter_form';
array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
$form['expose']['#flatten'] = TRUE;
if (empty($this->always_required)) {
$form['expose']['required'] = array(
'#type' => 'checkbox',
'#title' => t('Required'),
'#default_value' => $this->options['expose']['required'],
);
$operator_options = $this
->operator_options();
if (count($operator_options)) {
$form['expose']['limit_operators'] = array(
'#type' => 'checkbox',
'#title' => t('Limit operators'),
'#description' => t('When checked, the operator will be exposed to the user'),
'#default_value' => !empty($this->options['expose']['limit_operators']),
'#dependency' => array(
'edit-options-expose-use-operator' => array(
1,
),
),
'#description' => t('Restrict which operators will be available to select in the exposed operator form.'),
);
$form['expose']['available_operators'] = array(
'#type' => 'checkboxes',
'#title' => t('Limit the exposed operators'),
'#default_value' => $this->options['expose']['available_operators'],
'#prefix' => '<div id="edit-options-expose-available-operators-wrapper"><div id="edit-options-expose-available-operators">',
'#suffix' => '</div></div>',
'#description' => t('Select which operators will be available to select in the exposed operator form. If none are selected, all the operators listed here will be used.'),
'#options' => $operator_options,
'#dependency' => array(
'edit-options-expose-limit-operators' => array(
1,
),
),
);
}
}
else {
$form['expose']['required'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
$form['expose']['label'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['label'],
'#title' => t('Label'),
'#size' => 40,
);
$form['expose']['description'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['description'],
'#title' => t('Description'),
'#size' => 60,
);
if (!empty($form['operator']['#type'])) {
$form['operator']['#prefix'] = '<div class="views-group-box views-left-40">';
$form['operator']['#suffix'] = '</div>';
$form['value']['#prefix'] = '<div class="views-group-box views-right-60">';
$form['value']['#suffix'] = '</div>';
$form['expose']['use_operator'] = array(
'#type' => 'checkbox',
'#title' => t('Expose operator'),
'#description' => t('Allow the user to choose the operator.'),
'#default_value' => !empty($this->options['expose']['use_operator']),
);
$form['expose']['operator_label'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['operator_label'],
'#title' => t('Operator label'),
'#size' => 40,
'#description' => t('This will appear before your operator select field.'),
'#dependency' => array(
'edit-options-expose-use-operator' => array(
1,
),
),
);
$form['expose']['operator_id'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['operator_id'],
'#title' => t('Operator identifier'),
'#size' => 40,
'#description' => t('This will appear in the URL after the ? to identify this operator.'),
'#dependency' => array(
'edit-options-expose-use-operator' => array(
1,
),
),
'#fieldset' => 'more',
);
}
else {
$form['expose']['operator_id'] = array(
'#type' => 'value',
'#value' => '',
);
}
if (empty($this->always_multiple)) {
$form['expose']['multiple'] = array(
'#type' => 'checkbox',
'#title' => t('Allow multiple selections'),
'#description' => t('Enable to allow users to select multiple items.'),
'#default_value' => $this->options['expose']['multiple'],
);
}
$form['expose']['remember'] = array(
'#type' => 'checkbox',
'#title' => t('Remember the last selection'),
'#description' => t('Enable to remember the last selection made by the user.'),
'#default_value' => $this->options['expose']['remember'],
);
$role_options = array_map('check_plain', user_roles());
$form['expose']['remember_roles'] = array(
'#type' => 'checkboxes',
'#title' => t('User roles'),
'#description' => t('Remember exposed selection only for the selected user role(s). If you select no roles, the exposed data will never be stored.'),
'#default_value' => $this->options['expose']['remember_roles'],
'#options' => $role_options,
'#dependency' => array(
'edit-options-expose-remember' => array(
1,
),
),
);
$form['expose']['identifier'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['identifier'],
'#title' => t('Filter identifier'),
'#size' => 40,
'#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
'#fieldset' => 'more',
);
}
public function expose_validate($form, &$form_state) {
if (empty($form_state['values']['options']['expose']['identifier'])) {
form_error($form['expose']['identifier'], t('The identifier is required if the filter is exposed.'));
}
if (!empty($form_state['values']['options']['expose']['identifier'])) {
$illegal_identifiers = array(
'value',
'q',
);
if (in_array($form_state['values']['options']['expose']['identifier'], $illegal_identifiers)) {
form_error($form['expose']['identifier'], t('This identifier is not allowed.'));
}
}
if (!$this->view->display_handler
->is_identifier_unique($form_state['id'], $form_state['values']['options']['expose']['identifier'])) {
form_error($form['expose']['identifier'], t('This identifier is used by another handler.'));
}
if (!empty($form_state['values']['options']['expose']['remember_roles'])) {
$form_state['values']['options']['expose']['remember_roles'] = array_filter($form_state['values']['options']['expose']['remember_roles']);
}
}
public function build_group_validate($form, &$form_state) {
if (!empty($form_state['values']['options']['group_info'])) {
if (empty($form_state['values']['options']['group_info']['identifier'])) {
form_error($form['group_info']['identifier'], t('The identifier is required if the filter is exposed.'));
}
if (!empty($form_state['values']['options']['group_info']['identifier'])) {
$illegal_identifiers = array(
'value',
'q',
);
if (in_array($form_state['values']['options']['group_info']['identifier'], $illegal_identifiers)) {
form_error($form['group_info']['identifier'], t('This identifier is not allowed.'));
}
}
if (!$this->view->display_handler
->is_identifier_unique($form_state['id'], $form_state['values']['options']['group_info']['identifier'])) {
form_error($form['group_info']['identifier'], t('This identifier is used by another handler.'));
}
}
if (!empty($form_state['values']['options']['group_info']['group_items'])) {
foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
if (empty($group['remove'])) {
if (!empty($group['title'])) {
if (!in_array($group['operator'], array(
'empty',
'not empty',
))) {
if (!is_array($group['value']) && trim($group['value']) == "" || is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) == 0) {
form_error($form['group_info']['group_items'][$id]['value'], t('The value is required if title for this item is defined.'));
}
}
}
if (!is_array($group['value']) && trim($group['value']) != "" || is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) > 0) {
if (empty($group['title'])) {
form_error($form['group_info']['group_items'][$id]['title'], t('The title is required if value for this item is defined.'));
}
}
}
}
}
}
public function build_group_submit($form, &$form_state) {
$groups = array();
uasort($form_state['values']['options']['group_info']['group_items'], 'drupal_sort_weight');
$new_id = 1;
$new_default = 'All';
foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
if (empty($group['remove'])) {
unset($group['remove']);
unset($group['weight']);
$groups[$new_id] = $group;
if ($form_state['values']['options']['group_info']['default_group'] === $id) {
$new_default = $new_id;
}
}
$new_id++;
}
if ($new_default != 'All') {
$form_state['values']['options']['group_info']['default_group'] = $new_default;
}
$filter_default_multiple = array_filter($form_state['values']['options']['group_info']['default_group_multiple']);
$form_state['values']['options']['group_info']['default_group_multiple'] = $filter_default_multiple;
$form_state['values']['options']['group_info']['group_items'] = $groups;
}
public function expose_options() {
$this->options['expose'] = array(
'use_operator' => FALSE,
'operator' => $this->options['id'] . '_op',
'identifier' => $this->options['id'],
'label' => $this->definition['title'],
'description' => NULL,
'remember' => FALSE,
'multiple' => FALSE,
'required' => FALSE,
);
}
public function build_group_options() {
$this->options['group_info'] = array(
'label' => $this->definition['title'],
'description' => NULL,
'identifier' => $this->options['id'],
'optional' => TRUE,
'widget' => 'select',
'multiple' => FALSE,
'remember' => FALSE,
'default_group' => 'All',
'default_group_multiple' => array(),
'group_items' => array(),
);
}
public function group_form(&$form, &$form_state) {
if (!empty($this->options['group_info']['optional']) && !$this
->multiple_exposed_input()) {
$old_any = $this->options['group_info']['widget'] == 'select' ? '<Any>' : '<Any>';
$any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? $old_any : t('- Any -');
$groups = array(
'All' => $any_label,
);
}
foreach ($this->options['group_info']['group_items'] as $id => $group) {
if (!empty($group['title'])) {
$groups[$id] = $id != 'All' ? t($group['title']) : $group['title'];
}
}
if (count($groups)) {
$value = $this->options['group_info']['identifier'];
$form[$value] = array(
'#type' => $this->options['group_info']['widget'],
'#default_value' => $this->group_info,
'#options' => $groups,
);
if (!empty($this->options['group_info']['multiple'])) {
if (count($groups) < 5) {
$form[$value]['#type'] = 'checkboxes';
}
else {
$form[$value]['#type'] = 'select';
$form[$value]['#size'] = 5;
$form[$value]['#multiple'] = TRUE;
}
unset($form[$value]['#default_value']);
if (empty($form_state['input'][$value])) {
$form_state['input'][$value] = $this->group_info;
}
}
$this->options['expose']['label'] = '';
}
}
public function exposed_form(&$form, &$form_state) {
if (empty($this->options['exposed'])) {
return;
}
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
$operator = $this->options['expose']['operator_id'];
$this
->operator_form($form, $form_state);
$form[$operator] = $form['operator'];
$form[$operator]['#title'] = $this->options['expose']['operator_label'];
$form[$operator]['#title_display'] = 'invisible';
$this
->exposed_translate($form[$operator], 'operator');
unset($form['operator']);
}
if (!empty($this->options['expose']['identifier'])) {
$value = $this->options['expose']['identifier'];
if ($this->operator == 'empty' || $this->operator == 'not empty') {
$boolean = new views_handler_filter_boolean_operator();
$boolean->value = $this->value = 'All';
$boolean->value_value = $this->value_value = '';
$boolean->value_options = $this->value_options = array(
1 => t('Yes'),
0 => t('No'),
);
$boolean
->value_form($form, $form_state);
}
else {
$this
->value_form($form, $form_state);
}
$form[$value] = $form['value'];
if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
unset($form[$value]['#title']);
}
$this
->exposed_translate($form[$value], 'value');
if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || $form['#type'] == 'select' && !empty($form['#multiple']))) {
unset($form[$value]['#default_value']);
}
if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
$form[$value]['#default_value'] = 'All';
}
if ($value != 'value') {
unset($form['value']);
}
}
}
public function build_group_form(&$form, &$form_state) {
if (empty($this->options['exposed']) || empty($this->options['is_grouped'])) {
return;
}
$form['#theme'] = 'views_ui_build_group_filter_form';
array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
$form['group_info']['#flatten'] = TRUE;
if (!empty($this->options['group_info']['identifier'])) {
$identifier = $this->options['group_info']['identifier'];
}
else {
$identifier = 'group_' . $this->options['expose']['identifier'];
}
$form['group_info']['identifier'] = array(
'#type' => 'textfield',
'#default_value' => $identifier,
'#title' => t('Filter identifier'),
'#size' => 40,
'#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
'#fieldset' => 'more',
);
$form['group_info']['label'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['group_info']['label'],
'#title' => t('Label'),
'#size' => 40,
);
$form['group_info']['optional'] = array(
'#type' => 'checkbox',
'#title' => t('Optional'),
'#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
'#default_value' => $this->options['group_info']['optional'],
);
$form['group_info']['multiple'] = array(
'#type' => 'checkbox',
'#title' => t('Allow multiple selections'),
'#description' => t('Enable to allow users to select multiple items.'),
'#default_value' => $this->options['group_info']['multiple'],
);
$form['group_info']['widget'] = array(
'#type' => 'radios',
'#default_value' => $this->options['group_info']['widget'],
'#title' => t('Widget type'),
'#options' => array(
'radios' => t('Radios'),
'select' => t('Select'),
),
'#description' => t('Select which kind of widget will be used to render the group of filters'),
);
$form['group_info']['remember'] = array(
'#type' => 'checkbox',
'#title' => t('Remember'),
'#description' => t('Remember the last setting the user gave this filter.'),
'#default_value' => $this->options['group_info']['remember'],
);
if (!empty($this->options['group_info']['identifier'])) {
$identifier = $this->options['group_info']['identifier'];
}
else {
$identifier = 'group_' . $this->options['expose']['identifier'];
}
$form['group_info']['identifier'] = array(
'#type' => 'textfield',
'#default_value' => $identifier,
'#title' => t('Filter identifier'),
'#size' => 40,
'#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
'#fieldset' => 'more',
);
$form['group_info']['label'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['group_info']['label'],
'#title' => t('Label'),
'#size' => 40,
);
$form['group_info']['description'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['group_info']['description'],
'#title' => t('Description'),
'#size' => 60,
);
$form['group_info']['optional'] = array(
'#type' => 'checkbox',
'#title' => t('Optional'),
'#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
'#default_value' => $this->options['group_info']['optional'],
);
$form['group_info']['widget'] = array(
'#type' => 'radios',
'#default_value' => $this->options['group_info']['widget'],
'#title' => t('Widget type'),
'#options' => array(
'radios' => t('Radios'),
'select' => t('Select'),
),
'#description' => t('Select which kind of widget will be used to render the group of filters'),
);
$form['group_info']['remember'] = array(
'#type' => 'checkbox',
'#title' => t('Remember'),
'#description' => t('Remember the last setting the user gave this filter.'),
'#default_value' => $this->options['group_info']['remember'],
);
$groups = array(
'All' => '- Any -',
);
if (count($this->options['group_info']['group_items']) == 0) {
$this->options['group_info']['group_items'] = array_fill(1, 3, array());
}
$default_weight = 0;
foreach ($this->options['group_info']['group_items'] as $item_id => $item) {
if (!empty($form_state['values']['options']['group_info']['group_items'][$item_id]['remove'])) {
continue;
}
$row = array();
$groups[$item_id] = '';
$this
->operator_form($row, $form_state);
$row['operator']['#type'] = 'select';
$row['operator']['#title'] = '';
$this
->value_form($row, $form_state);
$without_children = TRUE;
foreach (element_children($row['value']) as $children) {
if (isset($row['value'][$children]['#dependency']['edit-options-operator'])) {
$row['value'][$children]['#dependency']["edit-options-group-info-group-items-{$item_id}-operator"] = $row['value'][$children]['#dependency']['edit-options-operator'];
unset($row['value'][$children]['#dependency']['edit-options-operator']);
$row['value'][$children]['#title'] = '';
if (!empty($this->options['group_info']['group_items'][$item_id]['value'][$children])) {
$row['value'][$children]['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'][$children];
}
}
$without_children = FALSE;
}
if ($without_children) {
if (!empty($this->options['group_info']['group_items'][$item_id]['value'])) {
$row['value']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'];
}
}
if (!empty($this->options['group_info']['group_items'][$item_id]['operator'])) {
$row['operator']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['operator'];
}
$default_title = '';
if (!empty($this->options['group_info']['group_items'][$item_id]['title'])) {
$default_title = $this->options['group_info']['group_items'][$item_id]['title'];
}
$form['group_info']['group_items'][$item_id] = array(
'title' => array(
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $default_title,
),
'operator' => $row['operator'],
'value' => $row['value'],
'remove' => array(
'#type' => 'checkbox',
'#id' => 'views-removed-' . $item_id,
'#attributes' => array(
'class' => array(
'views-remove-checkbox',
),
),
'#default_value' => 0,
),
'weight' => array(
'#type' => 'weight',
'#delta' => count($this->options['group_info']['group_items']),
'#default_value' => $default_weight++,
'#attributes' => array(
'class' => array(
'weight',
),
),
),
);
}
$form['group_info']['default_group'] = array(
'#type' => 'radios',
'#options' => $groups,
'#default_value' => $this->options['group_info']['default_group'],
'#required' => TRUE,
'#attributes' => array(
'class' => array(
'default-radios',
),
),
);
$form['group_info']['default_group_multiple'] = array(
'#type' => 'checkboxes',
'#options' => $groups,
'#default_value' => $this->options['group_info']['default_group_multiple'],
'#attributes' => array(
'class' => array(
'default-checkboxes',
),
),
);
$form['group_info']['add_group'] = array(
'#prefix' => '<div class="views-build-group clear-block">',
'#suffix' => '</div>',
'#type' => 'submit',
'#value' => t('Add another item'),
'#submit' => array(
'views_ui_config_item_form_add_group',
),
);
$js = array();
$js['tableDrag']['views-filter-groups']['weight'][0] = array(
'target' => 'weight',
'source' => NULL,
'relationship' => 'sibling',
'action' => 'order',
'hidden' => TRUE,
'limit' => 0,
);
if (!empty($form_state['js settings']) && is_array($js)) {
$form_state['js settings'] = array_merge($form_state['js settings'], $js);
}
else {
$form_state['js settings'] = $js;
}
}
public function exposed_translate(&$form, $type) {
if (!isset($form['#type'])) {
return;
}
if ($form['#type'] == 'radios') {
$form['#type'] = 'select';
}
if ($form['#type'] == 'checkboxes') {
if (empty($form['#no_convert']) || empty($this->options['expose']['multiple'])) {
$form['#type'] = 'select';
}
if (!empty($this->options['expose']['multiple'])) {
$form['#multiple'] = TRUE;
}
}
if (empty($this->options['expose']['multiple']) && isset($form['#multiple'])) {
unset($form['#multiple']);
$form['#size'] = NULL;
}
if ($form['#type'] == 'select') {
$this
->prepare_filter_select_options($form['#options']);
}
if ($type == 'value' && empty($this->always_required) && empty($this->options['expose']['required']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
$any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('<Any>') : t('- Any -');
$form['#options'] = array(
'All' => $any_label,
) + $form['#options'];
if (empty($form['#default_value'])) {
$form['#default_value'] = 'All';
}
}
if (!empty($this->options['expose']['required'])) {
$form['#required'] = TRUE;
}
}
public function prepare_filter_select_options(&$options) {
foreach ($options as $value => $label) {
if (is_array($label)) {
$this
->prepare_filter_select_options($options[$value]);
}
elseif (is_object($label)) {
$this
->prepare_filter_select_options($options[$value]->option);
}
else {
$options[$value] = strip_tags(decode_entities($label));
}
}
}
public function exposed_info() {
if (empty($this->options['exposed'])) {
return;
}
if ($this
->is_a_group()) {
return array(
'value' => $this->options['group_info']['identifier'],
'label' => $this->options['group_info']['label'],
'description' => $this->options['group_info']['description'],
);
}
return array(
'operator' => $this->options['expose']['operator_id'],
'value' => $this->options['expose']['identifier'],
'label' => $this->options['expose']['label'],
'description' => $this->options['expose']['description'],
);
}
public function convert_exposed_input(&$input, $selected_group_id = NULL) {
if ($this
->is_a_group()) {
if (!empty($selected_group_id)) {
$selected_group = $selected_group_id;
}
else {
$selected_group = $input[$this->options['group_info']['identifier']];
}
if ($selected_group == 'All' && !empty($this->options['group_info']['optional'])) {
return NULL;
}
if ($selected_group != 'All' && empty($this->options['group_info']['group_items'][$selected_group])) {
return FALSE;
}
if (isset($selected_group) && isset($this->options['group_info']['group_items'][$selected_group])) {
$input[$this->options['expose']['operator']] = $this->options['group_info']['group_items'][$selected_group]['operator'];
if (!empty($this->options['group_info']['group_items'][$selected_group]['value'])) {
$input[$this->options['expose']['identifier']] = $this->options['group_info']['group_items'][$selected_group]['value'];
}
$this->options['expose']['use_operator'] = TRUE;
$this->group_info = $input[$this->options['group_info']['identifier']];
return TRUE;
}
else {
return FALSE;
}
}
}
public function group_multiple_exposed_input(&$input) {
if (!empty($input[$this->options['group_info']['identifier']])) {
return array_filter($input[$this->options['group_info']['identifier']]);
}
return array();
}
public function multiple_exposed_input() {
return $this
->is_a_group() && !empty($this->options['group_info']['multiple']);
}
public function store_group_input($input, $status) {
if (!$this
->is_a_group() || empty($this->options['group_info']['identifier'])) {
return TRUE;
}
if (empty($this->options['group_info']['remember'])) {
return;
}
$display_id = $this->view->display_handler
->is_defaulted('filters') ? 'default' : $this->view->current_display;
if ($status === FALSE && isset($_SESSION['views'][$this->view->name][$display_id])) {
$session =& $_SESSION['views'][$this->view->name][$display_id];
if (isset($session[$this->options['group_info']['identifier']])) {
unset($session[$this->options['group_info']['identifier']]);
}
}
if ($status !== FALSE) {
if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
$_SESSION['views'][$this->view->name][$display_id] = array();
}
$session =& $_SESSION['views'][$this->view->name][$display_id];
$session[$this->options['group_info']['identifier']] = $input[$this->options['group_info']['identifier']];
}
}
public function accept_exposed_input($input) {
if (empty($this->options['exposed'])) {
return TRUE;
}
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
$this->operator = $input[$this->options['expose']['operator_id']];
}
if (!empty($this->options['expose']['identifier'])) {
if (isset($input[$this->options['expose']['identifier']])) {
$value = $input[$this->options['expose']['identifier']];
}
else {
return FALSE;
}
if (empty($this->options['expose']['required'])) {
if ($this->operator == 'empty' || $this->operator == 'not empty') {
if (is_array($value) && array_key_exists('value', $value)) {
$value = $value['value'];
}
$this->operator = $this->operator == 'empty' && empty($value) || $this->operator == 'not empty' && !empty($value) ? 'not empty' : 'empty';
}
if ($value == 'All' || $value === array()) {
return FALSE;
}
if (!empty($this->always_multiple) && $value === '') {
return FALSE;
}
}
if (isset($value)) {
$this->value = $value;
if (empty($this->always_multiple) && empty($this->options['expose']['multiple'])) {
$this->value = array(
$value,
);
}
}
else {
return FALSE;
}
}
return TRUE;
}
public function store_exposed_input($input, $status) {
if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
return TRUE;
}
if (empty($this->options['expose']['remember'])) {
return;
}
global $user;
$allowed_rids = empty($this->options['expose']['remember_roles']) ? array() : array_filter($this->options['expose']['remember_roles']);
$intersect_rids = array_intersect_key($allowed_rids, $user->roles);
if (empty($intersect_rids)) {
return;
}
$display_id = $this->view->display_handler
->is_defaulted('filters') ? 'default' : $this->view->current_display;
$operator = !empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']);
if (!$status && isset($_SESSION['views'][$this->view->name][$display_id])) {
$session =& $_SESSION['views'][$this->view->name][$display_id];
if ($operator && isset($session[$this->options['expose']['operator_id']])) {
unset($session[$this->options['expose']['operator_id']]);
}
if (isset($session[$this->options['expose']['identifier']])) {
unset($session[$this->options['expose']['identifier']]);
}
}
if ($status) {
if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
$_SESSION['views'][$this->view->name][$display_id] = array();
}
$session =& $_SESSION['views'][$this->view->name][$display_id];
if ($operator && isset($input[$this->options['expose']['operator_id']])) {
$session[$this->options['expose']['operator_id']] = $input[$this->options['expose']['operator_id']];
}
if (isset($input[$this->options['expose']['identifier']])) {
$session[$this->options['expose']['identifier']] = $input[$this->options['expose']['identifier']];
}
}
}
public function query() {
$this
->ensure_my_table();
$this->query
->add_where($this->options['group'], "{$this->table_alias}.{$this->real_field}", $this->value, $this->operator);
}
public function can_group() {
return TRUE;
}
}
class views_handler_filter_broken extends views_handler_filter {
public function ui_name($short = FALSE) {
return t('Broken/missing handler');
}
public function ensure_my_table() {
}
public function query($group_by = FALSE) {
}
public function options_form(&$form, &$form_state) {
$form['markup'] = array(
'#markup' => '<div class="form-item description">' . t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.') . '</div>',
);
}
public function broken() {
return TRUE;
}
}
function _views_array_filter_zero($var) {
return trim($var) != '';
}