Combine.php 4.75 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\filter\Combine.
merlinofchaos's avatar
merlinofchaos committed
6
7
 */

8
namespace Drupal\views\Plugin\views\filter;
9

10
use Drupal\Component\Annotation\PluginID;
dawehner's avatar
dawehner committed
11

merlinofchaos's avatar
merlinofchaos committed
12
13
14
15
/**
 * Filter handler which allows to search on multiple fields.
 *
 * @ingroup views_field_handlers
16
 *
17
 * @PluginID("combine")
dawehner's avatar
dawehner committed
18
 */
19
class Combine extends String {
20

merlinofchaos's avatar
merlinofchaos committed
21
22
23
24
25
  /**
   * @var views_plugin_query_default
   */
  var $query;

26
27
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
28
29
30
31
32
    $options['fields'] = array('default' => array());

    return $options;
  }

33
34
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);
35
    $this->view->initStyle();
merlinofchaos's avatar
merlinofchaos committed
36
37

    // Allow to choose all fields as possible
38
    if ($this->view->style_plugin->usesFields()) {
merlinofchaos's avatar
merlinofchaos committed
39
      $options = array();
40
      foreach ($this->view->display_handler->getHandlers('field') as $name => $field) {
41
        $options[$name] = $field->adminLabel(TRUE);
merlinofchaos's avatar
merlinofchaos committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
      }
      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.'));
      }
    }
  }

59
  public function query() {
merlinofchaos's avatar
merlinofchaos committed
60
61
62
63
64
65
    $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.
66
      $field->ensureMyTable();
merlinofchaos's avatar
merlinofchaos committed
67
      if (!empty($field->field_alias) && !empty($field->field_alias)) {
68
        $fields[] = "$field->tableAlias.$field->realField";
merlinofchaos's avatar
merlinofchaos committed
69
70
71
72
      }
    }
    if ($fields) {
      $count = count($fields);
73
      $separated_fields = array();
merlinofchaos's avatar
merlinofchaos committed
74
      foreach ($fields as $key => $field) {
75
        $separated_fields[] = $field;
merlinofchaos's avatar
merlinofchaos committed
76
        if ($key < $count-1) {
77
          $separated_fields[] = "' '";
merlinofchaos's avatar
merlinofchaos committed
78
79
        }
      }
80
      $expression = implode(', ', $separated_fields);
merlinofchaos's avatar
merlinofchaos committed
81
82
83
84
85
86
87
88
89
      $expression = "CONCAT_WS(' ', $expression)";

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

90
  // By default things like opEqual uses add_where, that doesn't support
merlinofchaos's avatar
merlinofchaos committed
91
92
  // complex expressions, so override all operators.

93
  function opEqual($expression) {
merlinofchaos's avatar
merlinofchaos committed
94
95
    $placeholder = $this->placeholder();
    $operator = $this->operator();
96
    $this->query->add_where_expression($this->options['group'], "$expression $operator $placeholder", array($placeholder => $this->value));
merlinofchaos's avatar
merlinofchaos committed
97
98
  }

99
  protected function opContains($expression) {
merlinofchaos's avatar
merlinofchaos committed
100
    $placeholder = $this->placeholder();
101
    $this->query->add_where_expression($this->options['group'], "$expression LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
merlinofchaos's avatar
merlinofchaos committed
102
103
  }

104
  protected function opStartsWith($expression) {
merlinofchaos's avatar
merlinofchaos committed
105
    $placeholder = $this->placeholder();
106
    $this->query->add_where_expression($this->options['group'], "$expression LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
merlinofchaos's avatar
merlinofchaos committed
107
108
  }

109
  protected function opNotStartsWith($expression) {
merlinofchaos's avatar
merlinofchaos committed
110
    $placeholder = $this->placeholder();
111
    $this->query->add_where_expression($this->options['group'], "$expression NOT LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
merlinofchaos's avatar
merlinofchaos committed
112
113
  }

114
  protected function opEndsWith($expression) {
merlinofchaos's avatar
merlinofchaos committed
115
    $placeholder = $this->placeholder();
116
    $this->query->add_where_expression($this->options['group'], "$expression LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
merlinofchaos's avatar
merlinofchaos committed
117
118
  }

119
  protected function opNotEnds($expression) {
merlinofchaos's avatar
merlinofchaos committed
120
    $placeholder = $this->placeholder();
121
    $this->query->add_where_expression($this->options['group'], "$expression NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
merlinofchaos's avatar
merlinofchaos committed
122
123
  }

124
  function op_not($expression) {
merlinofchaos's avatar
merlinofchaos committed
125
    $placeholder = $this->placeholder();
126
    $this->query->add_where_expression($this->options['group'], "$expression NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
merlinofchaos's avatar
merlinofchaos committed
127
128
  }

129
  protected function opRegex($expression) {
merlinofchaos's avatar
merlinofchaos committed
130
    $placeholder = $this->placeholder();
131
    $this->query->add_where_expression($this->options['group'], "$expression RLIKE $placeholder", array($placeholder => $this->value));
merlinofchaos's avatar
merlinofchaos committed
132
133
  }

134
  function op_empty($expression) {
merlinofchaos's avatar
merlinofchaos committed
135
136
137
138
139
140
141
    if ($this->operator == 'empty') {
      $operator = "IS NULL";
    }
    else {
      $operator = "IS NOT NULL";
    }

142
    $this->query->add_where_expression($this->options['group'], "$expression $operator");
merlinofchaos's avatar
merlinofchaos committed
143
  }
144

merlinofchaos's avatar
merlinofchaos committed
145
}