/** * @param QueryWrapperInterface $query * @return QueryWrapperInterface * @throws \Exception if given an incompatible query type. */ public function queryFilter(QueryWrapperInterface $query) { $this->canQueryFilter = true; if ($this->getNextFilter()->canQueryFilter === false) { $this->canQueryFilter = false; } if ($this->canQueryFilter === true) { $query = $this->getNextFilter()->queryFilter($query); $choiceKey = FilterControls::getControl($this->id, "value", array_keys($this->options)[0]); $choice = $this->options[$choiceKey]; $fieldName = array_search("{$this->relationName}Id", $query->getUnqualifiedPascalCasedColumnNames()); switch ($choice) { case static::ALL: break; case static::ANY: $query = $query->filterBy($fieldName, null, QueryWrapperInterface::CONDITION_NOT_EQUAL); break; case static::NONE: $query = $query->filterBy($fieldName, null, QueryWrapperInterface::CONDITION_EQUAL); break; default: $relation = $choice; if (is_string($relation) === true) { $relation = $this->relations[array_search($relation, $this->options)]; } $query = $query->filterBy($fieldName, $relation->getPrimaryKey(), QueryWrapperInterface::CONDITION_EQUAL); break; } } return $query; }
/** * @param string $id * @param FilterStatementInterface[] $classes * @param string[] $data * @param FilterInterface|null $nextFilter */ public function __construct($id, array $classes, array $data, FilterInterface $nextFilter = null) { $statements = []; for ($i = 0; $i <= 5; $i++) { $fieldname = FilterControls::getControl($id, "fieldname{$i}"); $operation = FilterControls::getControl($id, "operation{$i}"); $value = FilterControls::getControl($id, "value{$i}"); if ($fieldname !== "" && $operation !== "" && $value !== "") { $statements[] = new ExcludingFilterStatement($fieldname, $operation, $value, null); } } $feedbackStatements = []; foreach ($statements as $statement) { $fieldname = $statement->getFieldName(); $value = $statement->getCriterion(); $operation = $statement->getCondition(); $feedbackStatement = $fieldname . " " . $operation . " " . $value; $feedbackStatements[] = preg_replace('/[^a-zA-Z0-9 -.]/', '', $feedbackStatement); } if ($feedbackStatements !== []) { $feedbackStatements[] = ' <a href="#" class="search-clear" onclick="athens.search.clearSearch(\'' . trim($id) . '\'); return false;"> Clear </a>'; } $this->feedback = SafeString::fromString(implode(', ', $feedbackStatements)); parent::__construct($id, $classes, $data, $statements, $nextFilter); }
/** * @param string $id * @param string[] $classes * @param array $data * @param FilterStatementInterface[] $statements * @param string $default * @param FilterInterface|null $nextFilter */ public function __construct($id, array $classes, array $data, array $statements, $default, FilterInterface $nextFilter = null) { $this->options = array_keys($statements); $this->default = $default; $selection = FilterControls::getControl($id, "value", $default); $statements = [$statements[$selection]]; parent::__construct($id, $classes, $data, $statements, $nextFilter); }
/** * @param string $id * @param string[] $classes * @param array $data * @param FilterInterface|null $nextFilter */ public function __construct($id, array $classes, array $data, FilterInterface $nextFilter = null) { $statements = []; if (FilterControls::controlIsSet($id, "fieldname") === true) { $fieldName = FilterControls::getControl($id, "fieldname"); $order = FilterControls::getControl($id, "order", FilterStatement::COND_SORT_ASC); $statements[] = new SortingFilterStatement($fieldName, $order, null, null); } parent::__construct($id, $classes, $data, $statements, $nextFilter); }
/** * @return FilterInterface * @throws \Exception If an appropriate combination of fields have not been set. */ public function build() { $this->validateId(); $type = $this->retrieveOrException("type", __METHOD__); $statements = []; switch ($type) { case Filter::TYPE_STATIC: $fieldName = $this->retrieveOrException("fieldName", __METHOD__); $condition = $this->retrieveOrException("condition", __METHOD__); if (in_array($condition, [FilterStatementInterface::COND_SORT_ASC, FilterStatementInterface::COND_SORT_DESC]) === true) { $criterion = $this->criterion; $statements[] = new SortingFilterStatement($fieldName, $condition, $criterion, null); } else { $criterion = $this->criterionHasBeenSet === true ? $this->criterion : $this->retrieveOrException("criterion", __METHOD__); $statements[] = new ExcludingFilterStatement($fieldName, $condition, $criterion, null); } return new Filter($this->id, $this->classes, $this->data, $statements, $this->nextFilter); break; case Filter::TYPE_PAGINATION: $maxPerPage = $this->maxPerPage === null ? Settings::getInstance()->getDefaultPagination() : $this->maxPerPage; $page = $this->page === null ? FilterControls::getControl($this->id, "page", 1) : $this->page; return new PaginationFilter($this->id, $this->classes, $maxPerPage, $page, $this->nextFilter); break; case Filter::TYPE_SORT: return new SortFilter($this->id, $this->classes, $this->data, $this->nextFilter); break; case Filter::TYPE_SEARCH: return new SearchFilter($this->id, $this->classes, $this->data, $this->nextFilter); break; case Filter::TYPE_SELECT: $options = $this->retrieveOrException("options", __METHOD__, "chose to create a select filter"); $default = $this->retrieveOrException("default", __METHOD__, "chose to create a select filter"); if (array_key_exists($default, $options) === false) { $optionsText = implode(", ", array_keys($options)); throw new \Exception("For select filter '{$this->id}', your default choice " . "'{$default}' must be among options '{$optionsText}'."); } $options = array_merge([" " . $default => $options[$default], " " => $options[$default]], $options); $statements = array_map(function ($option) { return new ExcludingFilterStatement($option[0], $option[1], $option[2], null); }, $options); return new SelectFilter($this->id, $this->classes, $this->data, $statements, $default, $this->nextFilter); break; case Filter::TYPE_RELATION: $query = $this->retrieveOrException("query", __METHOD__, "chose to create a relation filter"); $default = $this->retrieveOrException("default", __METHOD__, "chose to create a relation filter"); return new RelationFilter($this->id, $this->classes, $this->data, $query, $default, $this->nextFilter); break; default: throw new \Exception("Invalid filter type."); } }
public function testFilterControlsFromDefault() { $handle = (string) rand(); $key = (string) rand(); $default = (string) rand(); $this->assertEquals($default, FilterControls::getControl($handle, $key, $default)); }