Combine.php 5.74 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 11
use Drupal\Core\Form\FormStateInterface;

merlinofchaos's avatar
merlinofchaos committed
12 13 14 15
/**
 * Filter handler which allows to search on multiple fields.
 *
 * @ingroup views_field_handlers
16
 *
17
 * @ViewsFilter("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
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
34
    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
      }
      if ($options) {
        $form['fields'] = array(
          '#type' => 'select',
46 47
          '#title' => $this->t('Choose fields to combine for filtering'),
          '#description' => $this->t("This filter doesn't work for very special field handlers."),
merlinofchaos's avatar
merlinofchaos committed
48 49 50 51 52 53
          '#multiple' => TRUE,
          '#options' => $options,
          '#default_value' => $this->options['fields'],
        );
      }
      else {
54
        $form_state->setErrorByName('', $this->t('You have to add some fields to be able to use this filter.'));
merlinofchaos's avatar
merlinofchaos committed
55 56 57 58
      }
    }
  }

59
  public function query() {
merlinofchaos's avatar
merlinofchaos committed
60 61 62 63
    $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) {
64 65 66 67 68 69 70 71
      // Overridden fields can lead to fields missing from a display that are
      // still set in the non-overridden combined filter.
      if (!isset($this->view->field[$id])) {
        // If fields are no longer available that are needed to filter by, make
        // sure no results are shown to prevent displaying more then intended.
        $this->view->build_info['fail'] = TRUE;
        continue;
      }
merlinofchaos's avatar
merlinofchaos committed
72 73
      $field = $this->view->field[$id];
      // Always add the table of the selected fields to be sure a table alias exists.
74
      $field->ensureMyTable();
merlinofchaos's avatar
merlinofchaos committed
75
      if (!empty($field->field_alias) && !empty($field->field_alias)) {
76
        $fields[] = "$field->tableAlias.$field->realField";
merlinofchaos's avatar
merlinofchaos committed
77 78 79
      }
    }
    if ($fields) {
80 81 82 83 84 85 86 87 88
      $count = count($fields);
      $separated_fields = array();
      foreach ($fields as $key => $field) {
        $separated_fields[] = $field;
        if ($key < $count-1) {
          $separated_fields[] = "' '";
        }
      }
      $expression = implode(', ', $separated_fields);
merlinofchaos's avatar
merlinofchaos committed
89 90 91 92 93 94 95 96 97
      $expression = "CONCAT_WS(' ', $expression)";

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

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  /**
   * {@inheritdoc}
   */
  public function validate() {
    $errors = parent::validate();
    $fields = $this->view->display_handler->getHandlers('field');
    foreach ($this->options['fields'] as $id) {
      if (!isset($fields[$id])) {
        // Combined field filter only works with fields that are in the field
        // settings.
        $errors[] = $this->t('Field %field set in %filter is not set in this display.', array('%field' => $id, '%filter' => $this->adminLabel()));
        break;
      }
    }
    return $errors;
  }

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

118
  function opEqual($expression) {
merlinofchaos's avatar
merlinofchaos committed
119 120
    $placeholder = $this->placeholder();
    $operator = $this->operator();
121
    $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => $this->value));
merlinofchaos's avatar
merlinofchaos committed
122 123
  }

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

129
  protected function opStartsWith($expression) {
merlinofchaos's avatar
merlinofchaos committed
130
    $placeholder = $this->placeholder();
131
    $this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
merlinofchaos's avatar
merlinofchaos committed
132 133
  }

134
  protected function opNotStartsWith($expression) {
merlinofchaos's avatar
merlinofchaos committed
135
    $placeholder = $this->placeholder();
136
    $this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
merlinofchaos's avatar
merlinofchaos committed
137 138
  }

139
  protected function opEndsWith($expression) {
merlinofchaos's avatar
merlinofchaos committed
140
    $placeholder = $this->placeholder();
141
    $this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
merlinofchaos's avatar
merlinofchaos committed
142 143
  }

144
  protected function opNotEndsWith($expression) {
merlinofchaos's avatar
merlinofchaos committed
145
    $placeholder = $this->placeholder();
146
    $this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
merlinofchaos's avatar
merlinofchaos committed
147 148
  }

149
  protected function opNotLike($expression) {
merlinofchaos's avatar
merlinofchaos committed
150
    $placeholder = $this->placeholder();
151
    $this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
merlinofchaos's avatar
merlinofchaos committed
152 153
  }

154
  protected function opRegex($expression) {
merlinofchaos's avatar
merlinofchaos committed
155
    $placeholder = $this->placeholder();
156
    $this->query->addWhereExpression($this->options['group'], "$expression REGEXP $placeholder", array($placeholder => $this->value));
merlinofchaos's avatar
merlinofchaos committed
157 158
  }

159
  protected function opEmpty($expression) {
merlinofchaos's avatar
merlinofchaos committed
160 161 162 163 164 165 166
    if ($this->operator == 'empty') {
      $operator = "IS NULL";
    }
    else {
      $operator = "IS NOT NULL";
    }

167
    $this->query->addWhereExpression($this->options['group'], "$expression $operator");
merlinofchaos's avatar
merlinofchaos committed
168
  }
169

merlinofchaos's avatar
merlinofchaos committed
170
}