Chris@0: handler = $handler; Chris@0: } Chris@0: Chris@0: public static function defineOptions(&$options) { Chris@0: $options['reduce_duplicates'] = ['default' => FALSE]; Chris@0: } Chris@0: Chris@0: public function buildOptionsForm(&$form, FormStateInterface $form_state) { Chris@0: $form['reduce_duplicates'] = [ Chris@0: '#type' => 'checkbox', Chris@0: '#title' => t('Reduce duplicates'), Chris@0: '#description' => t("This filter can cause items that have more than one of the selected options to appear as duplicate results. If this filter causes duplicate results to occur, this checkbox can reduce those duplicates; however, the more terms it has to search for, the less performant the query will be, so use this with caution. Shouldn't be set on single-value fields, as it may cause values to disappear from display, if used on an incompatible field."), Chris@0: '#default_value' => !empty($this->handler->options['reduce_duplicates']), Chris@0: '#weight' => 4, Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sometimes the handler might want us to use some kind of formula, so give Chris@0: * it that option. If it wants us to do this, it must set $helper->formula = TRUE Chris@0: * and implement handler->getFormula(); Chris@0: */ Chris@0: public function getField() { Chris@0: if (!empty($this->formula)) { Chris@0: return $this->handler->getFormula(); Chris@0: } Chris@0: else { Chris@0: return $this->handler->tableAlias . '.' . $this->handler->realField; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Add a table to the query. Chris@0: * Chris@0: * This is an advanced concept; not only does it add a new instance of the table, Chris@0: * but it follows the relationship path all the way down to the relationship Chris@0: * link point and adds *that* as a new relationship and then adds the table to Chris@0: * the relationship, if necessary. Chris@0: */ Chris@0: public function addTable($join = NULL, $alias = NULL) { Chris@0: // This is used for lookups in the many_to_one table. Chris@0: $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field; Chris@0: Chris@0: if (empty($join)) { Chris@0: $join = $this->getJoin(); Chris@0: } Chris@0: Chris@0: // See if there's a chain between us and the base relationship. If so, we need Chris@0: // to create a new relationship to use. Chris@0: $relationship = $this->handler->relationship; Chris@0: Chris@0: // Determine the primary table to seek Chris@0: if (empty($this->handler->query->relationships[$relationship])) { Chris@0: $base_table = $this->handler->view->storage->get('base_table'); Chris@0: } Chris@0: else { Chris@0: $base_table = $this->handler->query->relationships[$relationship]['base']; Chris@0: } Chris@0: Chris@0: // Cycle through the joins. This isn't as error-safe as the normal Chris@0: // ensurePath logic. Perhaps it should be. Chris@0: $r_join = clone $join; Chris@0: while ($r_join->leftTable != $base_table) { Chris@0: $r_join = HandlerBase::getTableJoin($r_join->leftTable, $base_table); Chris@0: } Chris@0: // If we found that there are tables in between, add the relationship. Chris@0: if ($r_join->table != $join->table) { Chris@0: $relationship = $this->handler->query->addRelationship($this->handler->table . '_' . $r_join->table, $r_join, $r_join->table, $this->handler->relationship); Chris@0: } Chris@0: Chris@0: // And now add our table, using the new relationship if one was used. Chris@0: $alias = $this->handler->query->addTable($this->handler->table, $relationship, $join, $alias); Chris@0: Chris@0: // Store what values are used by this table chain so that other chains can Chris@0: // automatically discard those values. Chris@0: if (empty($this->handler->view->many_to_one_tables[$field])) { Chris@0: $this->handler->view->many_to_one_tables[$field] = $this->handler->value; Chris@0: } Chris@0: else { Chris@0: $this->handler->view->many_to_one_tables[$field] = array_merge($this->handler->view->many_to_one_tables[$field], $this->handler->value); Chris@0: } Chris@0: Chris@0: return $alias; Chris@0: } Chris@0: Chris@0: public function getJoin() { Chris@0: return $this->handler->getJoin(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provide the proper join for summary queries. This is important in part because Chris@0: * it will cooperate with other arguments if possible. Chris@0: */ Chris@0: public function summaryJoin() { Chris@0: $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field; Chris@0: $join = $this->getJoin(); Chris@0: Chris@0: // shortcuts Chris@0: $options = $this->handler->options; Chris@0: $view = $this->handler->view; Chris@0: $query = $this->handler->query; Chris@0: Chris@0: if (!empty($options['require_value'])) { Chris@0: $join->type = 'INNER'; Chris@0: } Chris@0: Chris@0: if (empty($options['add_table']) || empty($view->many_to_one_tables[$field])) { Chris@0: return $query->ensureTable($this->handler->table, $this->handler->relationship, $join); Chris@0: } Chris@0: else { Chris@0: if (!empty($view->many_to_one_tables[$field])) { Chris@0: foreach ($view->many_to_one_tables[$field] as $value) { Chris@0: $join->extra = [ Chris@0: [ Chris@0: 'field' => $this->handler->realField, Chris@0: 'operator' => '!=', Chris@0: 'value' => $value, Chris@0: 'numeric' => !empty($this->definition['numeric']), Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: } Chris@0: return $this->addTable($join); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Override ensureMyTable so we can control how this joins in. Chris@0: * The operator actually has influence over joining. Chris@0: */ Chris@0: public function ensureMyTable() { Chris@0: if (!isset($this->handler->tableAlias)) { Chris@0: // Case 1: Operator is an 'or' and we're not reducing duplicates. Chris@0: // We hence get the absolute simplest: Chris@0: $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field; Chris@0: if ($this->handler->operator == 'or' && empty($this->handler->options['reduce_duplicates'])) { Chris@0: if (empty($this->handler->options['add_table']) && empty($this->handler->view->many_to_one_tables[$field])) { Chris@0: // query optimization, INNER joins are slightly faster, so use them Chris@0: // when we know we can. Chris@0: $join = $this->getJoin(); Chris@0: if (isset($join)) { Chris@0: $join->type = 'INNER'; Chris@0: } Chris@0: $this->handler->tableAlias = $this->handler->query->ensureTable($this->handler->table, $this->handler->relationship, $join); Chris@0: $this->handler->view->many_to_one_tables[$field] = $this->handler->value; Chris@0: } Chris@0: else { Chris@0: $join = $this->getJoin(); Chris@0: $join->type = 'LEFT'; Chris@0: if (!empty($this->handler->view->many_to_one_tables[$field])) { Chris@0: foreach ($this->handler->view->many_to_one_tables[$field] as $value) { Chris@0: $join->extra = [ Chris@0: [ Chris@0: 'field' => $this->handler->realField, Chris@0: 'operator' => '!=', Chris@0: 'value' => $value, Chris@0: 'numeric' => !empty($this->handler->definition['numeric']), Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->handler->tableAlias = $this->addTable($join); Chris@0: } Chris@0: Chris@0: return $this->handler->tableAlias; Chris@0: } Chris@0: Chris@0: // Case 2: it's an 'and' or an 'or'. Chris@0: // We do one join per selected value. Chris@0: if ($this->handler->operator != 'not') { Chris@0: // Clone the join for each table: Chris@0: $this->handler->tableAliases = []; Chris@0: foreach ($this->handler->value as $value) { Chris@0: $join = $this->getJoin(); Chris@0: if ($this->handler->operator == 'and') { Chris@0: $join->type = 'INNER'; Chris@0: } Chris@0: $join->extra = [ Chris@0: [ Chris@0: 'field' => $this->handler->realField, Chris@0: 'value' => $value, Chris@0: 'numeric' => !empty($this->handler->definition['numeric']), Chris@0: ], Chris@0: ]; Chris@0: Chris@0: // The table alias needs to be unique to this value across the Chris@0: // multiple times the filter or argument is called by the view. Chris@0: if (!isset($this->handler->view->many_to_one_aliases[$field][$value])) { Chris@0: if (!isset($this->handler->view->many_to_one_count[$this->handler->table])) { Chris@0: $this->handler->view->many_to_one_count[$this->handler->table] = 0; Chris@0: } Chris@0: $this->handler->view->many_to_one_aliases[$field][$value] = $this->handler->table . '_value_' . ($this->handler->view->many_to_one_count[$this->handler->table]++); Chris@0: } Chris@0: Chris@0: $this->handler->tableAliases[$value] = $this->addTable($join, $this->handler->view->many_to_one_aliases[$field][$value]); Chris@0: // Set tableAlias to the first of these. Chris@0: if (empty($this->handler->tableAlias)) { Chris@0: $this->handler->tableAlias = $this->handler->tableAliases[$value]; Chris@0: } Chris@0: } Chris@0: } Chris@0: // Case 3: it's a 'not'. Chris@0: // We just do one join. We'll add a where clause during Chris@0: // the query phase to ensure that $table.$field IS NULL. Chris@0: else { Chris@0: $join = $this->getJoin(); Chris@0: $join->type = 'LEFT'; Chris@0: $join->extra = []; Chris@0: $join->extraOperator = 'OR'; Chris@0: foreach ($this->handler->value as $value) { Chris@0: $join->extra[] = [ Chris@0: 'field' => $this->handler->realField, Chris@0: 'value' => $value, Chris@0: 'numeric' => !empty($this->handler->definition['numeric']), Chris@0: ]; Chris@0: } Chris@0: Chris@0: $this->handler->tableAlias = $this->addTable($join); Chris@0: } Chris@0: } Chris@0: return $this->handler->tableAlias; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides a unique placeholders for handlers. Chris@0: */ Chris@0: protected function placeholder() { Chris@0: return $this->handler->query->placeholder($this->handler->options['table'] . '_' . $this->handler->options['field']); Chris@0: } Chris@0: Chris@0: public function addFilter() { Chris@0: if (empty($this->handler->value)) { Chris@0: return; Chris@0: } Chris@0: $this->handler->ensureMyTable(); Chris@0: Chris@0: // Shorten some variables: Chris@0: $field = $this->getField(); Chris@0: $options = $this->handler->options; Chris@0: $operator = $this->handler->operator; Chris@0: $formula = !empty($this->formula); Chris@0: $value = $this->handler->value; Chris@0: if (empty($options['group'])) { Chris@0: $options['group'] = 0; Chris@0: } Chris@0: Chris@0: // If $add_condition is set to FALSE, a single expression is enough. If it Chris@0: // is set to TRUE, conditions will be added. Chris@0: $add_condition = TRUE; Chris@0: if ($operator == 'not') { Chris@0: $value = NULL; Chris@0: $operator = 'IS NULL'; Chris@0: $add_condition = FALSE; Chris@0: } Chris@0: elseif ($operator == 'or' && empty($options['reduce_duplicates'])) { Chris@0: if (count($value) > 1) { Chris@0: $operator = 'IN'; Chris@0: } Chris@0: else { Chris@0: $value = is_array($value) ? array_pop($value) : $value; Chris@0: $operator = '='; Chris@0: } Chris@0: $add_condition = FALSE; Chris@0: } Chris@0: Chris@0: if (!$add_condition) { Chris@0: if ($formula) { Chris@0: $placeholder = $this->placeholder(); Chris@0: if ($operator == 'IN') { Chris@0: $operator = "$operator IN($placeholder)"; Chris@0: } Chris@0: else { Chris@0: $operator = "$operator $placeholder"; Chris@0: } Chris@0: $placeholders = [ Chris@0: $placeholder => $value, Chris@0: ]; Chris@0: $this->handler->query->addWhereExpression($options['group'], "$field $operator", $placeholders); Chris@0: } Chris@0: else { Chris@0: $placeholder = $this->placeholder(); Chris@0: if (count($this->handler->value) > 1) { Chris@0: $placeholder .= '[]'; Chris@0: Chris@0: if ($operator == 'IS NULL') { Chris@0: $this->handler->query->addWhereExpression(0, "$field $operator"); Chris@0: } Chris@0: else { Chris@0: $this->handler->query->addWhereExpression(0, "$field $operator($placeholder)", [$placeholder => $value]); Chris@0: } Chris@0: } Chris@0: else { Chris@0: if ($operator == 'IS NULL') { Chris@0: $this->handler->query->addWhereExpression(0, "$field $operator"); Chris@0: } Chris@0: else { Chris@0: $this->handler->query->addWhereExpression(0, "$field $operator $placeholder", [$placeholder => $value]); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if ($add_condition) { Chris@0: $field = $this->handler->realField; Chris@0: $clause = $operator == 'or' ? new Condition('OR') : new Condition('AND'); Chris@0: foreach ($this->handler->tableAliases as $value => $alias) { Chris@0: $clause->condition("$alias.$field", $value); Chris@0: } Chris@0: Chris@0: // implode on either AND or OR. Chris@0: $this->handler->query->addWhere($options['group'], $clause); Chris@0: } Chris@0: } Chris@0: Chris@0: }