partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit b534e87d69387ba518c06b01c3c709a3d8d9ee39
parent 59a0ff11c3f68c8ccb32b988c4b56f85fc0508c0
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat, 31 Dec 2011 13:56:55 +0100

Added a generic filtering method to modify queries/filters by a callback

Diffstat:
Msrc/backend/de/RaumZeitLabor/PartKeepr/Manager/AbstractManager.php | 11+++++++++--
Msrc/backend/de/RaumZeitLabor/PartKeepr/Manager/ManagerFilter.php | 23+++++++++++++++++++++++
2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Manager/AbstractManager.php b/src/backend/de/RaumZeitLabor/PartKeepr/Manager/AbstractManager.php @@ -93,7 +93,7 @@ abstract class AbstractManager extends Singleton { $qb->select("COUNT(q.id)"); - //$qb->where("1"); + $qb->where("1=1"); if ($filter->getFilter() !== null && $filter->getFilterField() !== null) { $aOrWhereFields = array(); @@ -106,10 +106,17 @@ abstract class AbstractManager extends Singleton { $aOrWhereFields[] = "q.".$filter->getFilterField()." = :filter"; } - $qb->orWhere($aOrWhereFields); + foreach ($aOrWhereFields as $or) { + $qb->orWhere($or); + } + $qb->setParameter("filter", "%".$filter->getFilter()."%"); } + if ($filter->getFilterCallback() !== null) { + call_user_func($filter->getFilterCallback(), $qb); + } + $qb->from($this->getEntityName(),"q"); $totalQuery = $qb->getQuery(); diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Manager/ManagerFilter.php b/src/backend/de/RaumZeitLabor/PartKeepr/Manager/ManagerFilter.php @@ -43,6 +43,12 @@ class ManagerFilter { protected $direction = "asc"; /** + * A callback which is called when creating the filter + * @var function The callback + */ + protected $callback = null; + + /** * Sets the start position * @param int $start */ @@ -174,6 +180,23 @@ class ManagerFilter { } /** + * Sets the filter callback + * @param function $callback A function which is called when creating a filter. The callback function receives the + * query builder as first argument. + */ + public function setFilterCallback ($callback) { + $this->callback = $callback; + } + + /** + * Returns the filter callback + * @return function The callback function + */ + public function getFilterCallback () { + return $this->callback; + } + + /** * Constructs a new filter set. * * If a service is passed, the constructor automatically extracts the parameters from the service