From 18f009c55feecf219edc0b9a3eac7aedcf94937a Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 19 Mar 2021 15:42:15 +0100 Subject: [PATCH] Utilize `ipl\Orm\Query::filter()` where appropriate --- application/controllers/HostgroupController.php | 8 +++----- application/controllers/HostsController.php | 12 +++--------- application/controllers/ServicegroupController.php | 8 +++----- application/controllers/ServicesController.php | 12 ++++-------- library/Icingadb/Common/Auth.php | 2 +- .../Web/Control/SearchBar/ObjectSuggestions.php | 9 ++++----- library/Icingadb/Web/Controller.php | 10 ++++++++-- library/Icingadb/Widget/Detail/ObjectDetail.php | 9 ++++----- 8 files changed, 30 insertions(+), 40 deletions(-) diff --git a/application/controllers/HostgroupController.php b/application/controllers/HostgroupController.php index 516a1daa..e521416c 100644 --- a/application/controllers/HostgroupController.php +++ b/application/controllers/HostgroupController.php @@ -10,7 +10,6 @@ use Icinga\Module\Icingadb\Model\Hostgroupsummary; use Icinga\Module\Icingadb\Web\Controller; use Icinga\Module\Icingadb\Widget\HostList; use Icinga\Module\Icingadb\Widget\ItemList\HostgroupList; -use ipl\Orm\Compat\FilterProcessor; use ipl\Stdlib\Filter; class HostgroupController extends Controller @@ -26,10 +25,9 @@ class HostgroupController extends Controller $query = Hostgroupsummary::on($this->getDb()); - FilterProcessor::apply( - Filter::equal('hostgroup.name', $name), - $query - ); + foreach ($query->getUnions() as $unionPart) { + $unionPart->filter(Filter::equal('hostgroup.name', $name)); + } $this->applyRestrictions($query); diff --git a/application/controllers/HostsController.php b/application/controllers/HostsController.php index b5e8ef23..85c05591 100644 --- a/application/controllers/HostsController.php +++ b/application/controllers/HostsController.php @@ -18,7 +18,6 @@ use Icinga\Module\Icingadb\Widget\Detail\ObjectsDetail; use Icinga\Module\Icingadb\Widget\HostList; use Icinga\Module\Icingadb\Widget\HostStatusBar; use Icinga\Module\Icingadb\Widget\ShowMore; -use ipl\Orm\Compat\FilterProcessor; use ipl\Stdlib\Filter; use ipl\Web\Filter\QueryString; use ipl\Web\Url; @@ -145,7 +144,7 @@ class HostsController extends Controller $comments = Host::on($db)->with(['comment']); $comments->getWith()['host.comment']->setJoinType('INNER'); // TODO: This should be automatically done by the model/resolver and added as ON condition - FilterProcessor::apply(Filter::equal('comment.object_type', 'host'), $comments); + $comments->filter(Filter::equal('comment.object_type', 'host')); $this->filter($comments); $summary->comments_total = $comments->count(); @@ -185,13 +184,8 @@ class HostsController extends Controller switch ($this->getRequest()->getActionName()) { case 'acknowledge': - FilterProcessor::apply( - Filter::all([ - Filter::equal('state.is_problem', 'y'), - Filter::equal('state.is_acknowledged', 'n') - ]), - $hosts - ); + $hosts->filter(Filter::equal('state.is_problem', 'y')) + ->filter(Filter::equal('state.is_acknowledged', 'n')); break; } diff --git a/application/controllers/ServicegroupController.php b/application/controllers/ServicegroupController.php index d5e86a28..0b073d14 100644 --- a/application/controllers/ServicegroupController.php +++ b/application/controllers/ServicegroupController.php @@ -10,7 +10,6 @@ use Icinga\Module\Icingadb\Model\ServicegroupSummary; use Icinga\Module\Icingadb\Web\Controller; use Icinga\Module\Icingadb\Widget\ServiceList; use Icinga\Module\Icingadb\Widget\ItemList\ServicegroupList; -use ipl\Orm\Compat\FilterProcessor; use ipl\Stdlib\Filter; class ServicegroupController extends Controller @@ -26,10 +25,9 @@ class ServicegroupController extends Controller $query = ServicegroupSummary::on($this->getDb()); - FilterProcessor::apply( - Filter::equal('servicegroup.name', $name), - $query - ); + foreach ($query->getUnions() as $unionPart) { + $unionPart->filter(Filter::equal('servicegroup.name', $name)); + } $this->applyRestrictions($query); diff --git a/application/controllers/ServicesController.php b/application/controllers/ServicesController.php index 07dd08c8..d2267abe 100644 --- a/application/controllers/ServicesController.php +++ b/application/controllers/ServicesController.php @@ -18,7 +18,6 @@ use Icinga\Module\Icingadb\Widget\Detail\ObjectsDetail; use Icinga\Module\Icingadb\Widget\ServiceList; use Icinga\Module\Icingadb\Widget\ServiceStatusBar; use Icinga\Module\Icingadb\Widget\ShowMore; -use ipl\Orm\Compat\FilterProcessor; use ipl\Stdlib\Filter; use ipl\Web\Filter\QueryString; use ipl\Web\Url; @@ -153,6 +152,8 @@ class ServicesController extends Controller $comments = Service::on($db)->with(['comment']); $comments->getWith()['service.comment']->setJoinType('INNER'); + // TODO: This should be automatically done by the model/resolver and added as ON condition + $comments->filter(Filter::equal('comment.object_type', 'service')); $this->filter($comments); $summary->comments_total = $comments->count(); @@ -196,13 +197,8 @@ class ServicesController extends Controller switch ($this->getRequest()->getActionName()) { case 'acknowledge': - FilterProcessor::apply( - Filter::all([ - Filter::equal('state.is_problem', 'y'), - Filter::equal('state.is_acknowledged', 'n') - ]), - $services - ); + $services->filter(Filter::equal('state.is_problem', 'y')) + ->filter(Filter::equal('state.is_acknowledged', 'n')); break; } diff --git a/library/Icingadb/Common/Auth.php b/library/Icingadb/Common/Auth.php index 21b73bb4..37e7c37f 100644 --- a/library/Icingadb/Common/Auth.php +++ b/library/Icingadb/Common/Auth.php @@ -156,7 +156,7 @@ trait Auth } } - FilterProcessor::apply($queryFilter, $query); + $query->filter($queryFilter); } } diff --git a/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php b/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php index f271d3b4..d820dfab 100644 --- a/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php +++ b/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php @@ -10,7 +10,6 @@ use Icinga\Module\Icingadb\Model\Behavior\ReRoute; use Icinga\Module\Icingadb\Model\CustomvarFlat; use InvalidArgumentException; use ipl\Html\HtmlElement; -use ipl\Orm\Compat\FilterProcessor; use ipl\Orm\Model; use ipl\Orm\Relation\BelongsToMany; use ipl\Orm\Resolver; @@ -144,7 +143,7 @@ class ObjectSuggestions extends Suggestions // resolve the filter leads to false-positives. $flatnameFilter->noOptimization = true; - FilterProcessor::apply($flatnameFilter, $query); + $query->filter($flatnameFilter); } $inputFilter = Filter::equal($columnPath, $searchTerm); @@ -153,14 +152,14 @@ class ObjectSuggestions extends Suggestions // This had so many iterations, if it still doesn't work, consider removing it entirely :( if ($searchFilter instanceof Filter\None) { - FilterProcessor::apply($inputFilter, $query); + $query->filter($inputFilter); } elseif ($searchFilter instanceof Filter\All) { $searchFilter->add($inputFilter); } else { $searchFilter = $inputFilter; } - FilterProcessor::apply($searchFilter, $query); + $query->filter($searchFilter); $this->applyRestrictions($query); try { @@ -253,7 +252,7 @@ class ObjectSuggestions extends Suggestions $customVars->columns('flatname'); $this->applyRestrictions($customVars); - FilterProcessor::apply(Filter::equal('flatname', $searchTerm), $customVars); + $customVars->filter(Filter::equal('flatname', $searchTerm)); $idColumn = $resolver->qualifyColumnsAndAliases((array) 'id', $customVars->getModel(), false); $customVars = $customVars->assembleSelect(); diff --git a/library/Icingadb/Web/Controller.php b/library/Icingadb/Web/Controller.php index 9c775104..e532420c 100644 --- a/library/Icingadb/Web/Controller.php +++ b/library/Icingadb/Web/Controller.php @@ -16,8 +16,8 @@ use InvalidArgumentException; use ipl\Html\Html; use ipl\Html\ValidHtml; use ipl\Orm\Common\SortUtil; -use ipl\Orm\Compat\FilterProcessor; use ipl\Orm\Query; +use ipl\Orm\UnionQuery; use ipl\Stdlib\Contract\Paginatable; use ipl\Stdlib\Filter; use ipl\Web\Compat\CompatController; @@ -354,7 +354,13 @@ class Controller extends CompatController { $this->applyRestrictions($query); - FilterProcessor::apply($filter ?: $this->getFilter(), $query); + if ($query instanceof UnionQuery) { + foreach ($query->getUnions() as $query) { + $query->filter($filter ?: $this->getFilter()); + } + } else { + $query->filter($filter ?: $this->getFilter()); + } return $this; } diff --git a/library/Icingadb/Widget/Detail/ObjectDetail.php b/library/Icingadb/Widget/Detail/ObjectDetail.php index 7d98e862..a41e7991 100644 --- a/library/Icingadb/Widget/Detail/ObjectDetail.php +++ b/library/Icingadb/Widget/Detail/ObjectDetail.php @@ -32,7 +32,6 @@ use ipl\Html\BaseHtmlElement; use ipl\Html\Html; use ipl\Html\HtmlElement; use ipl\Html\HtmlString; -use ipl\Orm\Compat\FilterProcessor; use ipl\Orm\ResultSet; use ipl\Stdlib\Filter; use ipl\Web\Widget\Icon; @@ -120,7 +119,7 @@ class ObjectDetail extends BaseHtmlElement ->limit(3) ->peekAhead(); // TODO: This should be automatically done by the model/resolver and added as ON condition - FilterProcessor::apply(Filter::equal('object_type', $this->objectType), $comments); + $comments->filter(Filter::equal('object_type', $this->objectType)); $comments = $comments->execute(); /** @var ResultSet $comments */ @@ -170,7 +169,7 @@ class ObjectDetail extends BaseHtmlElement ->limit(3) ->peekAhead(); // TODO: This should be automatically done by the model/resolver and added as ON condition - FilterProcessor::apply(Filter::equal('object_type', $this->objectType), $downtimes); + $downtimes->filter(Filter::equal('object_type', $this->objectType)); $downtimes = $downtimes->execute(); /** @var ResultSet $downtimes */ @@ -409,14 +408,14 @@ class ObjectDetail extends BaseHtmlElement ); $userQuery = User::on($this->getDb()); - FilterProcessor::apply($objectFilter, $userQuery); + $userQuery->filter($objectFilter); $this->applyRestrictions($userQuery); foreach ($userQuery as $user) { $users[$user->name] = $user; } $usergroupQuery = Usergroup::on($this->getDb()); - FilterProcessor::apply($objectFilter, $usergroupQuery); + $usergroupQuery->filter($objectFilter); $this->applyRestrictions($usergroupQuery); foreach ($usergroupQuery as $usergroup) { $usergroups[$usergroup->name] = $usergroup;