partkeepr

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

commit 775622b57b4e138a9535bba570965343dd66012d
parent 3a38da430fe546bab97d50222f34a9846f9835e3
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat, 14 Apr 2012 02:05:56 +0200

Added support for multiple sorters, fixes #162

Diffstat:
Msrc/backend/de/RaumZeitLabor/PartKeepr/Manager/AbstractManager.php | 6++++--
Msrc/backend/de/RaumZeitLabor/PartKeepr/Manager/ManagerFilter.php | 107+++++++++++++++++++++++++------------------------------------------------------
Asrc/backend/de/RaumZeitLabor/PartKeepr/Manager/Sorter.php | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/backend/de/RaumZeitLabor/PartKeepr/Part/PartManager.php | 26+++++++++++++++++++-------
Msrc/backend/de/RaumZeitLabor/PartKeepr/Stock/StockManager.php | 38++++++++++++++++++++++----------------
5 files changed, 159 insertions(+), 98 deletions(-)

diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Manager/AbstractManager.php b/src/backend/de/RaumZeitLabor/PartKeepr/Manager/AbstractManager.php @@ -221,8 +221,10 @@ abstract class AbstractManager extends Singleton { * @param ManagerFilter $filter The query filter */ protected function applySorting (QueryBuilder $qb, ManagerFilter $filter) { - if ($filter->getSortField() !== null && $filter->getSortField() != "q.") { - $qb->orderBy($filter->getSortField(), $filter->getDir()); + foreach ($filter->getSorters() as $sorter) { + if ($sorter->getSortField() !== null && $sorter->getSortField() != "q.") { + $qb->addOrderBy($sorter->getSortField(), $sorter->getSortDirection()); + } } } } \ No newline at end of file diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Manager/ManagerFilter.php b/src/backend/de/RaumZeitLabor/PartKeepr/Manager/ManagerFilter.php @@ -30,16 +30,10 @@ class ManagerFilter { protected $filterField = null; /** - * Specifies the field to sort by - * @var string - */ - protected $sortField = null; - - /** - * Specifies the direction (either ASC or DESC) - * @var string + * Specifies the fields to sort by + * @var array */ - protected $direction = "asc"; + protected $sorters = array(); /** * A callback which is called when creating the filter @@ -88,75 +82,43 @@ class ManagerFilter { } /** - * Sets the filter. Specify null if no filter is wanted. - * @param mixed $filter A string to filter for, or null to disable - */ - public function setFilter ($filter) { - $this->filter = $filter; - } - - /** - * Returns the filter. - * @return mixed Either a string to filter for, or null if disabled - */ - public function getFilter () { - return $this->filter; - } - - /** - * Sets the direction to order by - * @param string $direction Either "asc" or "desc". + * Sets the sorters for this filter. + * + * @param array $sorters An array of Sorter instances */ - public function setDirection ($direction) { - switch (strtolower($direction)) { - case "desc": - $this->direction = "desc"; - break; - case "asc": - default: - $this->direction = "asc"; - break; + public function setSorters (array $sorters) { + // Make sure that each sorter is an instance of the Sorter class. + foreach ($sorters as $sorter) { + if (!($sorter instanceof Sorter)) { + throw new InvalidArgumentException("The passed sorters needs to be an array of Sorter instances"); + } } + + $this->sorters = $sorters; } /** - * Sets the direction to order by. Shorthand function for setDirection(). - * @param string $dir The direction, either "asc" or "desc" - */ - public function setDir ($dir) { - $this->setDirection($dir); - } - - /** - * Returns the direction to order by. - * @return either "asc" or "desc". - */ - public function getDirection () { - return $this->direction; - } - - /** - * Shorthand function for getDirection(). - * @see getDirection + * Returns the sorters for this filter. + * @return array An array of Sorter instances */ - public function getDir () { - return $this->getDirection(); + public function getSorters () { + return $this->sorters; } /** - * Sets the field to sort by - * @param string $sortField The sort field + * Sets the filter. Specify null if no filter is wanted. + * @param mixed $filter A string to filter for, or null to disable */ - public function setSortField ($sortField) { - $this->sortField = $sortField; + public function setFilter ($filter) { + $this->filter = $filter; } /** - * Returns the sort field - * @return string the field to sort by + * Returns the filter. + * @return mixed Either a string to filter for, or null if disabled */ - public function getSortField () { - return $this->sortField; + public function getFilter () { + return $this->filter; } /** @@ -216,16 +178,15 @@ class ManagerFilter { if ($service->hasParameter("sort")) { $tmp = json_decode($service->getParameter("sort"), true); - - $aSortParams = $tmp[0]; - } else { - $aSortParams = array( - "property" => null, - "direction" => "ASC"); - } - $this->setSortField("q.".$aSortParams["property"]); - $this->setDirection($aSortParams["direction"]); + $aSorters = array(); + + foreach ($tmp as $key => $sortParam) { + $aSorters[] = new Sorter("q.".$sortParam["property"], $sortParam["direction"]); + } + + $this->setSorters($aSorters); + } if ($service->hasParameter("query")) { $this->setFilter($service->getParameter("query")); diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Manager/Sorter.php b/src/backend/de/RaumZeitLabor/PartKeepr/Manager/Sorter.php @@ -0,0 +1,79 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Manager; + +/** + * Represents a sorter, which is used with the ManagerFilter class. + * + * This allows the developer to specify multiple sorters. One sorter contains a sort field and a sort direction. + */ +class Sorter { + /** + * The field to sort by + * @var string + */ + private $sortField = null; + + /** + * The direction to sort by + * @var string + */ + private $sortDirection = null; + + /** + * Constructs a new sorter. + * + * @param string $field The field to sort by + * @param string $direction The direction, either "asc" or "desc" + */ + public function __construct ($field = null, $direction = null) { + if ($field !== null) { + $this->setSortField($field); + } + + if ($direction !== null) { + $this->setSortDirection($direction); + } + } + + /** + * Sets the sort field for this sorter + * @param string $field The field to sort by + */ + public function setSortField ($field) { + $this->sortField = $field; + } + + /** + * Sets the sort direction for this sorter + * @param string $direction Either "asc" or "desc" + */ + public function setSortDirection ($direction) { + switch (strtolower($direction)) { + case "desc": + case "d": + $this->sortDirection = "desc"; + break; + case "asc": + case "a": + default: + $this->sortDirection = "asc"; + break; + } + } + + /** + * Returns the sort field for this sorter + * @return string The field name + */ + public function getSortField () { + return $this->sortField; + } + + /** + * Returns the sort order for this sorter + * @return string Either "asc" or "desc" + */ + public function getSortDirection () { + return $this->sortDirection; + } +}+ \ No newline at end of file diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Part/PartManager.php b/src/backend/de/RaumZeitLabor/PartKeepr/Part/PartManager.php @@ -1,6 +1,8 @@ <?php namespace de\RaumZeitLabor\PartKeepr\Part; +use de\RaumZeitLabor\PartKeepr\Logger\Logger; + use de\RaumZeitLabor\PartKeepr\UploadedFile\TempUploadedFile, de\RaumZeitLabor\PartKeepr\Manager\ManagerFilter, Doctrine\ORM\QueryBuilder, @@ -72,14 +74,24 @@ class PartManager extends AbstractManager { ->leftJoin("q.footprint", "f") ->join("q.category", "c") ->leftJoin("q.partUnit", "pu"); + + // Apply special handling for non-direct fields in relations, where the frontend has no idea about. + foreach ($filter->getSorters() as $sorter) { + switch ($sorter->getSortField()) { + case "q.categoryPath": + $sorter->setSortField("c.categoryPath"); + break; + case "q.storageLocationName": + $sorter->setSortField("st.name"); + break; + case "q.footprintName": + $sorter->setSortField("f.name"); + break; + default: + break; + } + } - switch ($filter->getSortField()) { - case "q.categoryPath": - $filter->setSortField("c.categoryPath"); - break; - default: - break; - } } /** diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Stock/StockManager.php b/src/backend/de/RaumZeitLabor/PartKeepr/Stock/StockManager.php @@ -39,22 +39,28 @@ class StockManager extends AbstractManager { * @param ManagerFilter $filter The query filter */ protected function applyCustomQuery (QueryBuilder $qb, ManagerFilter $filter) { - switch ($filter->getSortField()) { - case "q.part_name": - $qb->join("q.part", "p"); - $filter->setSortField("p.name"); - break; - case "q.user_id": - $qb->leftJoin("q.user", "u"); - $filter->setSortField("u.username"); - break; - case "q.direction": - $filter->setSortField("q.dateTime"); - break; - case "q.storageLocation_name": - $qb->join("q.part", "p")->join("p.storageLocation", "st"); - $filter->setSortField("st.name"); - break; + + // Apply special handling for non-direct fields in relations, where the frontend has no idea about. + foreach ($filter->getSorters() as $sorter) { + switch ($sorter->getSortField()) { + case "q.part_name": + $qb->join("q.part", "p"); + $sorter->setSortField("p.name"); + break; + case "q.user_id": + $qb->leftJoin("q.user", "u"); + $sorter->setSortField("u.username"); + break; + case "q.direction": + $sorter->setSortField("q.dateTime"); + break; + case "q.storageLocation_name": + $qb->join("q.part", "p")->join("p.storageLocation", "st"); + $sorter->setSortField("st.name"); + break; + default: + break; + } } } } \ No newline at end of file