partkeepr

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

commit de3935fef2fa7b7f0111e6e935d9254ef9fee07a
parent 247c59a5ad80b9d86c6a3c272a47ba642937bc44
Author: Felicitus <felicitus@felicitus.org>
Date:   Sun, 18 Mar 2012 10:25:46 +0100

Improved category path caching; fixes #144

Diffstat:
Mcronjobs/CreateRSSFeed.php | 2+-
Msrc/backend/de/RaumZeitLabor/PartKeepr/Category/AbstractCategory.php | 27+++++++++++++++++++++++++++
Msrc/backend/de/RaumZeitLabor/PartKeepr/Category/AbstractCategoryManager.php | 15+++++++++++++++
Msrc/backend/de/RaumZeitLabor/PartKeepr/Category/AbstractCategoryService.php | 7+++++--
Msrc/backend/de/RaumZeitLabor/PartKeepr/Part/Part.php | 26--------------------------
Msrc/backend/de/RaumZeitLabor/PartKeepr/Part/PartManager.php | 12++++++++++--
Msrc/backend/de/RaumZeitLabor/PartKeepr/Setup/PartCategorySetup.php | 9+++++++++
7 files changed, 67 insertions(+), 31 deletions(-)

diff --git a/cronjobs/CreateRSSFeed.php b/cronjobs/CreateRSSFeed.php @@ -44,7 +44,7 @@ foreach ($parts as $part) { $description->appendChild($descriptionContent); $category = $rssDOM->createElement("partkeepr:category"); - $categoryContent = $rssDOM->createTextNode($part->getCategoryPath()); + $categoryContent = $rssDOM->createTextNode($part->getCategory()->getCategoryPath()); $category->appendChild($categoryContent); $pubDate = $rssDOM->createElement("pubDate", $part->getCreateDate()->format(DATE_RFC822)); diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Category/AbstractCategory.php b/src/backend/de/RaumZeitLabor/PartKeepr/Category/AbstractCategory.php @@ -49,6 +49,15 @@ class AbstractCategory extends BaseEntity implements Node, Serializable { private $parent; /** + * Holds the category path. + * + * @Column(type="text") + * + * @var string + */ + private $categoryPath; + + /** * Sets the name of this category * @param string $name The name to set */ @@ -148,4 +157,22 @@ class AbstractCategory extends BaseEntity implements Node, Serializable { return $this->getName(); } + /** + * Retrieves the category path + */ + public function getCategoryPath () { + return $this->categoryPath; + } + + /** + * Sets the category path. + * + * THIS IS ONLY TO BE CALLED FROM THE CATEGORYMANAGER! DON'T MESS WITH IT! + * + * @param string $categoryPath + */ + public function setCategoryPath ($categoryPath) { + $this->categoryPath = $categoryPath; + } + } diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Category/AbstractCategoryManager.php b/src/backend/de/RaumZeitLabor/PartKeepr/Category/AbstractCategoryManager.php @@ -142,6 +142,21 @@ abstract class AbstractCategoryManager extends Singleton { } /** + * Updates the category paths for a given node and all children. + * + * This method is usually called whenever a category is moved. + * + * @param NodeWrapper $startNode The node to start updating at + */ + public function updateCategoryPaths (NodeWrapper $startNode) { + $startNode->getNode()->setCategoryPath($startNode->getPath(" ➤ ", true)); + + foreach ($startNode->getChildren() as $child) { + $this->updateCategoryPaths($child); + } + } + + /** * Creates a category tree by an array of category names * @param array $aCategoryNames * @param Category A category where to start searching at diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Category/AbstractCategoryService.php b/src/backend/de/RaumZeitLabor/PartKeepr/Category/AbstractCategoryService.php @@ -36,6 +36,7 @@ abstract class AbstractCategoryService extends Service { $parent = $this->getCategoryManager()->getCategory($this->getParameter("target")); $category->moveAsLastChildOf($parent); + $this->getCategoryManager()->updateCategoryPaths($category); } } @@ -65,6 +66,8 @@ abstract class AbstractCategoryService extends Service { PartKeepr::getEM()->persist($category->getNode()); + $this->getCategoryManager()->updateCategoryPaths($category); + return array("data" => $this->serializeCategory($category)); } @@ -79,6 +82,8 @@ abstract class AbstractCategoryService extends Service { $category = $this->getCategoryManager()->addCategory($category); + $this->getCategoryManager()->updateCategoryPaths($category); + return array("data" => $this->serializeCategory($category)); } @@ -138,4 +143,3 @@ abstract class AbstractCategoryService extends Service { return $categoryManager::getInstance(); } } -?>- \ No newline at end of file diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Part/Part.php b/src/backend/de/RaumZeitLabor/PartKeepr/Part/Part.php @@ -151,13 +151,6 @@ class Part extends BaseEntity implements Serializable, Deserializable { */ private $createDate; - /** - * Represents the path to the category - * @Column(type="text",nullable=true) - * @var string - */ - private $categoryPath; - public function __construct () { $this->distributors = new \Doctrine\Common\Collections\ArrayCollection(); $this->manufacturers = new \Doctrine\Common\Collections\ArrayCollection(); @@ -261,7 +254,6 @@ class Part extends BaseEntity implements Serializable, Deserializable { */ public function setCategory (PartCategory $category) { $this->category = $category; - $this->updateCategoryCache(); } /** @@ -273,23 +265,6 @@ class Part extends BaseEntity implements Serializable, Deserializable { } /** - * Updates the category cache - */ - public function updateCategoryCache () { - if ($this->getCategory() !== null) { - $this->categoryPath = PartCategoryManager::getInstance()->getNodeManager()->wrapNode($this->getCategory())->getPath(' > ', true); - } - } - - /** - * Returns the category path - * @return string - */ - public function getCategoryPath () { - return $this->categoryPath; - } - - /** * Sets the storage location for this part * @param \de\RaumZeitLabor\PartKeepr\StorageLocation\StorageLocation $storageLocation The storage location */ @@ -416,7 +391,6 @@ class Part extends BaseEntity implements Serializable, Deserializable { public function updateCacheData () { $this->updateStockLevel(); - $this->updateCategoryCache(); $this->updatePrice(); } diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Part/PartManager.php b/src/backend/de/RaumZeitLabor/PartKeepr/Part/PartManager.php @@ -41,7 +41,7 @@ class PartManager extends AbstractManager { */ public function getQueryFields () { return array("name", "averagePrice", "status", "needsReview", "createDate", "id", "stockLevel", - "minStockLevel", "comment", "st.id AS storageLocation_id", "categoryPath", + "minStockLevel", "comment", "st.id AS storageLocation_id", "c.categoryPath AS categoryPath", "st.name as storageLocationName", "f.id AS footprint_id", "f.name AS footprintName", "c.id AS category", "c.name AS categoryName", "pu.id AS partUnit", "pu.name AS partUnitName", "pu.is_default AS partUnitDefault" @@ -70,7 +70,15 @@ class PartManager extends AbstractManager { $qb ->join("q.storageLocation", "st") ->leftJoin("q.footprint", "f") ->join("q.category", "c") - ->leftJoin("q.partUnit", "pu"); + ->leftJoin("q.partUnit", "pu"); + + switch ($filter->getSortField()) { + case "q.categoryPath": + $filter->setSortField("c.categoryPath"); + break; + default: + break; + } } public function addOrUpdatePart ($aParameters) { diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Setup/PartCategorySetup.php b/src/backend/de/RaumZeitLabor/PartKeepr/Setup/PartCategorySetup.php @@ -13,7 +13,16 @@ class PartCategorySetup extends AbstractSetup { PartCategoryManager::getInstance()->ensureRootExists(); } + public function updateCategoryPathCache () { + PartCategoryManager::getInstance()->updateCategoryPaths( + PartCategoryManager::getInstance()->getRootNode() + ); + + PartKeepr::getEM()->flush(); + } + public function run () { $this->setupRootCategory(); + $this->updateCategoryPathCache(); } }