/** * {@inheritDoc} */ public function scope(Query $query, TokenInterface $token) { $column = $this->config('field'); $value = $token->value(); if (!$column || empty($value)) { return $query; } $tableAlias = $this->_table->alias(); $range = $this->_parseRange($token->value()); if ($range['lower'] !== $range['upper']) { $conjunction = $token->negated() ? 'AND NOT' : 'AND'; $conditions = ["{$conjunction}" => ["{$tableAlias}.{$column} >=" => $range['lower'], "{$tableAlias}.{$column} <=" => $range['upper']]]; } else { $cmp = $token->negated() ? '<=' : '>='; $conditions = ["{$tableAlias}.{$column} {$cmp}" => $range['lower']]; } if ($token->where() === 'or') { $query->orWhere($conditions); } elseif ($token->where() === 'and') { $query->andWhere($conditions); } else { $query->where($conditions); } return $query; }
/** * Handles "term" search operator. * * term:term1-slug,term2-slug,... * * @param \Cake\ORM\Query $query The query object * @param \Search\Parser\TokenInterface $token Operator token * @return \Cake\ORM\Query */ public function operatorterm(Query $query, TokenInterface $token) { $terms = explode(',', strtolower($token->value())); $conjunction = $token->negated() ? 'NOT IN' : 'IN'; if (empty($terms)) { return $query; } $conditions = ["Contents.id {$conjunction}" => TableRegistry::get('Taxonomy.EntitiesTerms')->find()->select(['EntitiesTerms.entity_id'])->where(['EntitiesTerms.table_alias' => $this->alias()])->matching('Terms', function ($q) use($terms) { return $q->where(['Terms.slug IN' => $terms]); })]; if (!empty($conditions)) { if ($token->where() === 'or') { $query->orWhere($conditions); } elseif ($token->where() === 'and') { $query->andWhere($conditions); } else { $query->where($conditions); } } return $query; }
/** * Similar to "_scopeWords" but using MySQL's fulltext indexes. * * @param \Cake\ORM\Query $query The query to scope * @param \Search\TokenInterface $token Token describing a words sequence. e.g `this is a phrase` * @return \Cake\ORM\Query Scoped query */ protected function _scopeWordsInFulltext(Query $query, TokenInterface $token) { $value = str_replace(['*', '!'], ['*', '*'], $token->value()); $value = mb_strpos($value, '+') === 0 ? mb_substr($value, 1) : $value; if (empty($value) || in_array($value, $this->_stopWords())) { return $query; } $not = $token->negated() ? 'NOT' : ''; $value = str_replace("'", '"', $value); $conditions = ["{$not} MATCH(SearchDatasets.words) AGAINST('{$value}' IN BOOLEAN MODE) > 0"]; if ($token->where() === 'or') { $query->orWhere($conditions); } elseif ($token->where() === 'and') { $query->andWhere($conditions); } else { $query->where($conditions); } return $query; }