summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.15/sites/all/modules/views/handlers/views_handler_filter_combine.inc
blob: c9def53494391fd100dfb5dd4e9eb0efb22ae9e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php

/**
 * @file
 * Definition of views_handler_filter_combine.
 */

/**
 * Filter handler which allows to search on multiple fields.
 *
 * @ingroup views_field_handlers
 */
class views_handler_filter_combine extends views_handler_filter_string {
  /**
   * @var views_plugin_query_default
   */
  var $query;

  function option_definition() {
    $options = parent::option_definition();
    $options['fields'] = array('default' => array());

    return $options;
  }

  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $this->view->init_style();

    // Allow to choose all fields as possible
    if ($this->view->style_plugin->uses_fields()) {
      $options = array();
      foreach ($this->view->display_handler->get_handlers('field') as $name => $field) {
        $options[$name] = $field->ui_name(TRUE);
      }
      if ($options) {
        $form['fields'] = array(
          '#type' => 'select',
          '#title' => t('Choose fields to combine for filtering'),
          '#description' => t("This filter doesn't work for very special field handlers."),
          '#multiple' => TRUE,
          '#options' => $options,
          '#default_value' => $this->options['fields'],
        );
      }
      else {
        form_set_error('', t('You have to add some fields to be able to use this filter.'));
      }
    }
  }

  function query() {
    $this->view->_build('field');
    $fields = array();
    // Only add the fields if they have a proper field and table alias.
    foreach ($this->options['fields'] as $id) {
      $field = $this->view->field[$id];
      // Always add the table of the selected fields to be sure a table alias exists.
      $field->ensure_my_table();
      if (!empty($field->field_alias) && !empty($field->field_alias)) {
        $fields[] = "$field->table_alias.$field->real_field";
      }
    }
    if ($fields) {
      $count = count($fields);
      $seperated_fields = array();
      foreach ($fields as $key => $field) {
        $seperated_fields[] = $field;
        if ($key < $count-1) {
          $seperated_fields[] = "' '";
        }
      }
      $expression = implode(', ', $seperated_fields);
      $expression = "CONCAT_WS(' ', $expression)";

      $info = $this->operators();
      if (!empty($info[$this->operator]['method'])) {
        $this->{$info[$this->operator]['method']}($expression);
      }
    }
  }

  // By default things like op_equal uses add_where, that doesn't support
  // complex expressions, so override all operators.

  function op_equal($field) {
    $placeholder = $this->placeholder();
    $operator = $this->operator();
    $this->query->add_where_expression($this->options['group'], "$field $operator $placeholder", array($placeholder => $this->value));
  }

  function op_contains($field) {
    $placeholder = $this->placeholder();
    $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
  }

  function op_starts($field) {
    $placeholder = $this->placeholder();
    $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
  }

  function op_not_starts($field) {
    $placeholder = $this->placeholder();
    $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
  }

  function op_ends($field) {
    $placeholder = $this->placeholder();
    $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
  }

  function op_not_ends($field) {
    $placeholder = $this->placeholder();
    $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
  }

  function op_not($field) {
    $placeholder = $this->placeholder();
    $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
  }

  function op_regex($field) {
    $placeholder = $this->placeholder();
    $this->query->add_where_expression($this->options['group'], "$field RLIKE $placeholder", array($placeholder => $this->value));
  }

  function op_empty($field) {
    if ($this->operator == 'empty') {
      $operator = "IS NULL";
    }
    else {
      $operator = "IS NOT NULL";
    }

    $this->query->add_where_expression($this->options['group'], "$field $operator");
  }
}