partkeepr

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

commit e842609d4e3f54875189502025d57a5e7238f4c9
parent 62e2456c107a6d0bbeef257d8c436fe782c41be4
Author: Felicitus <felicitus@felicitus.org>
Date:   Mon,  3 Aug 2015 15:15:42 +0200

Added category path listener for the PartCategory entity, added getter for the partCategory

Diffstat:
Mapp/config/config.yml | 7+++++++
Msrc/PartKeepr/FrontendBundle/Resources/public/js/Components/Part/PartsGrid.js | 10++++++++--
Msrc/PartKeepr/PartBundle/Entity/Part.php | 17++++++++++++++++-
Asrc/PartKeepr/PartBundle/Listeners/CategoryPathListener.php | 49+++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 80 insertions(+), 3 deletions(-)

diff --git a/app/config/config.yml b/app/config/config.yml @@ -144,6 +144,13 @@ services: tags: - { name: doctrine.event_listener, event: onFlush } + part.category_path_listener: + class: PartKeepr\PartBundle\Listeners\CategoryPathListener + arguments: + - "@service_container" + tags: + - { name: doctrine.event_listener, event: onFlush } + my_event_listener: class: PartKeepr\UploadedFileBundle\EventListener\TemporaryFileEventListener arguments: diff --git a/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Part/PartsGrid.js b/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Part/PartsGrid.js @@ -285,8 +285,7 @@ Ext.define('PartKeepr.PartsGrid', { renderer: this.footprintRenderer }, { header: i18n("Category"), - dataIndex: 'categoryPath', - renderer: Ext.util.Format.htmlEncode, + renderer: this.categoryPathRenderer, hidden: true }, { header: i18n("Create Date"), @@ -306,6 +305,13 @@ Ext.define('PartKeepr.PartsGrid', { /** * Renders the storage location */ + categoryPathRenderer: function (val, q, rec) + { + return rec.getCategory().get("categoryPath"); + }, + /** + * Renders the storage location + */ footprintRenderer: function (val, q, rec) { if (rec.getFootprint()) { diff --git a/src/PartKeepr/PartBundle/Entity/Part.php b/src/PartKeepr/PartBundle/Entity/Part.php @@ -26,7 +26,7 @@ class Part extends BaseEntity /** * The category of the part * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\PartCategory") - * + * @Groups({"default"}) * @var PartCategory */ private $category; @@ -382,6 +382,21 @@ class Part extends BaseEntity } /** + * Returns the category path + * @Groups({"default"}) + * + * @return string + */ + public function getCategoryPath() + { + if ($this->category !== null) { + return $this->category->getCategoryPath(); + } else { + return ""; + } + } + + /** * Sets the category for this part * * @param PartCategory $category The category diff --git a/src/PartKeepr/PartBundle/Listeners/CategoryPathListener.php b/src/PartKeepr/PartBundle/Listeners/CategoryPathListener.php @@ -0,0 +1,49 @@ +<?php +namespace PartKeepr\PartBundle\Listeners; + +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Event\OnFlushEventArgs; +use PartKeepr\PartBundle\Entity\PartCategory; +use Symfony\Component\DependencyInjection\ContainerAware; + +class CategoryPathListener extends ContainerAware +{ + /** + * Updates the child category paths when their parent name has changed. + * + * @param OnFlushEventArgs $eventArgs The event arguments as given by Doctrine + */ + public function onFlush(OnFlushEventArgs $eventArgs) + { + $entityManager = $eventArgs->getEntityManager(); + $uow = $entityManager->getUnitOfWork(); + + foreach ($uow->getScheduledEntityUpdates() as $updated) { + if ($updated instanceof PartCategory) { + $this->updateCategoryPaths($updated, $eventArgs); + } + } + } + + /** + * Recursively updates the category paths. + * + * @param FootprintCategory $partCategory The footprint category to update + * @param EntityManager $entityManager The entity manager + */ + public function updateCategoryPaths(PartCategory $partCategory, OnFlushEventArgs $eventArgs) + { + $entityManager = $eventArgs->getEntityManager(); + + $partCategory->setCategoryPath($partCategory->generateCategoryPath()); + + $entityManager->getUnitOfWork()->recomputeSingleEntityChangeSet( + $entityManager->getClassMetadata(get_class($partCategory)), + $partCategory + ); + + foreach ($partCategory->getChildren() as $child) { + $this->updateCategoryPaths($child, $eventArgs); + } + } +}