DataFilter: automagic fix for not(multiple)
This commit is contained in:
parent
6ed24f93da
commit
8f2b9fea07
|
@ -17,11 +17,13 @@ class DataFilter extends FormElement
|
||||||
*/
|
*/
|
||||||
public $helper = 'formDataFilter';
|
public $helper = 'formDataFilter';
|
||||||
|
|
||||||
protected $addTo;
|
private $addTo;
|
||||||
|
|
||||||
protected $removeFilter;
|
private $removeFilter;
|
||||||
|
|
||||||
protected $stripFilter;
|
private $stripFilter;
|
||||||
|
|
||||||
|
private $filter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @codingStandardsIgnoreStart
|
* @codingStandardsIgnoreStart
|
||||||
|
@ -58,70 +60,113 @@ class DataFilter extends FormElement
|
||||||
return Filter::matchAll();
|
return Filter::matchAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
$filter = null;
|
$this->filter = null;
|
||||||
foreach ($array as $id => $entry) {
|
foreach ($array as $id => $entry) {
|
||||||
$filterId = $this->idToFilterId($id);
|
$filterId = $this->idToFilterId($id);
|
||||||
$sub = $this->entryToFilter($entry);
|
$sub = $this->entryToFilter($entry);
|
||||||
$this->checkEntryForActions($filterId, $entry);
|
$this->checkEntryForActions($filterId, $entry);
|
||||||
$parentId = $this->parentIdFor($filterId);
|
$parentId = $this->parentIdFor($filterId);
|
||||||
|
|
||||||
if ($filter === null) {
|
if ($this->filter === null) {
|
||||||
$filter = $sub;
|
$this->filter = $sub;
|
||||||
} else {
|
} else {
|
||||||
$filter->getById($parentId)->addFilter($sub);
|
$this->filter->getById($parentId)->addFilter($sub);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->removeFilterIfRequested()
|
||||||
|
->stripFilterIfRequested()
|
||||||
|
->addNewFilterIfRequested()
|
||||||
|
->fixNotsWithMultipleChildren();
|
||||||
|
|
||||||
|
return $this->filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function removeFilterIfRequested()
|
||||||
|
{
|
||||||
if ($this->removeFilter !== null) {
|
if ($this->removeFilter !== null) {
|
||||||
if ($filter->getById($this->removeFilter)->isRootNode()) {
|
if ($this->filter->getById($this->removeFilter)->isRootNode()) {
|
||||||
$filter = $this->emptyExpression();
|
$this->filter = $this->emptyExpression();
|
||||||
} else {
|
} else {
|
||||||
$filter->removeId($this->removeFilter);
|
$this->filter->removeId($this->removeFilter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected function stripFilterIfRequested()
|
||||||
|
{
|
||||||
if ($this->stripFilter !== null) {
|
if ($this->stripFilter !== null) {
|
||||||
$strip = $this->stripFilter;
|
$strip = $this->stripFilter;
|
||||||
$subId = $strip . '-1';
|
$subId = $strip . '-1';
|
||||||
if ($filter->getId() === $strip) {
|
if ($this->filter->getId() === $strip) {
|
||||||
$filter = $filter->getById($strip . '-1');
|
$this->filter = $this->filter->getById($strip . '-1');
|
||||||
} else {
|
} else {
|
||||||
$filter->replaceById($strip, $filter->getById($strip . '-1'));
|
$this->filter->replaceById($strip, $this->filter->getById($strip . '-1'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addNewFilterIfRequested()
|
||||||
|
{
|
||||||
if ($this->addTo !== null) {
|
if ($this->addTo !== null) {
|
||||||
$parent = $filter->getById($this->addTo);
|
$parent = $this->filter->getById($this->addTo);
|
||||||
|
|
||||||
if ($parent->isChain()) {
|
if ($parent->isChain()) {
|
||||||
if ($parent->isEmpty()) {
|
if ($parent->isEmpty()) {
|
||||||
$parent->addFilter($this->emptyExpression());
|
$parent->addFilter($this->emptyExpression());
|
||||||
} elseif ($parent->getOperatorName() === 'NOT') {
|
|
||||||
$andNot = Filter::matchAll();
|
|
||||||
foreach ($parent->filters() as $sub) {
|
|
||||||
$andNot->addFilter(clone($sub));
|
|
||||||
}
|
|
||||||
$clone->addFilter($this->emptyExpression());
|
|
||||||
$filter->replaceById(
|
|
||||||
$parent->getId(),
|
|
||||||
Filter::not($andNot)
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
$parent->addFilter($this->emptyExpression());
|
$parent->addFilter($this->emptyExpression());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$replacement = Filter::matchAll(clone($parent));
|
$replacement = Filter::matchAll(clone($parent));
|
||||||
if ($parent->isRootNode()) {
|
if ($parent->isRootNode()) {
|
||||||
$filter = $replacement;
|
$this->filter = $replacement;
|
||||||
} else {
|
} else {
|
||||||
$filter->replaceById($parent->getId(), $replacement);
|
$this->filter->replaceById($parent->getId(), $replacement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function fixNotsWithMultipleChildren(Filter $filter = null)
|
||||||
|
{
|
||||||
|
$this->filter = $this->fixNotsWithMultipleChildrenForFilter($this->filter);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function fixNotsWithMultipleChildrenForFilter(Filter $filter)
|
||||||
|
{
|
||||||
|
if ($filter->isChain()) {
|
||||||
|
if ($filter->getOperatorName() === 'NOT') {
|
||||||
|
if ($filter->count() > 1) {
|
||||||
|
$filter = $this->notToNotAnd($filter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach ($filter->filters() as $sub) {
|
||||||
|
$filter->replaceById($sub->getId(), $this->fixNotsWithMultipleChildrenForFilter($sub));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $filter;
|
return $filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function notToNotAnd(Filter $not)
|
||||||
|
{
|
||||||
|
$and = Filter::matchAll();
|
||||||
|
foreach ($not->filters() as $sub) {
|
||||||
|
$and->addFilter(clone($sub));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Filter::not($and);
|
||||||
|
}
|
||||||
|
|
||||||
protected function emptyExpression()
|
protected function emptyExpression()
|
||||||
{
|
{
|
||||||
return Filter::expression('', '=', '');
|
return Filter::expression('', '=', '');
|
||||||
|
|
Loading…
Reference in New Issue