diff --git a/src/Elcodi/Admin/ProductBundle/EventListener/ProductHasOnlyOneCategoryEventListener.php b/src/Elcodi/Admin/ProductBundle/EventListener/ProductHasOnlyOneCategoryEventListener.php new file mode 100644 index 00000000..75cbf091 --- /dev/null +++ b/src/Elcodi/Admin/ProductBundle/EventListener/ProductHasOnlyOneCategoryEventListener.php @@ -0,0 +1,82 @@ + + * @author Aldo Chiecchia + * @author Elcodi Team + */ + +namespace Elcodi\Admin\ProductBundle\EventListener; + +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\ORM\Event\PreFlushEventArgs; +use Doctrine\ORM\Event\PreUpdateEventArgs; + +use Elcodi\Component\Product\Entity\Interfaces\CategoryInterface; +use Elcodi\Component\Product\Entity\Interfaces\ProductInterface; + +/** + * Class ProductHasOnlyOneCategoryEventListener + */ +class ProductHasOnlyOneCategoryEventListener +{ + /** + * This method checks that when when a new product is created the only + * category assigned is the one selected as principal category. + * + * @param PreFlushEventArgs $args The pre flush event args. + */ + public function preFlush(PreFlushEventArgs $args) + { + $entityManager = $args->getEntityManager(); + $scheduledInsertions = $entityManager + ->getUnitOfWork() + ->getScheduledEntityInsertions(); + + foreach ($scheduledInsertions as $entity) { + if ($entity instanceof ProductInterface) { + $this->fixProductCategory($entity); + } + } + } + + /** + * This method ensures that when a product is modified the only category + * assigned is the one selected as principal category. + * + * @param PreUpdateEventArgs $event The pre update event args. + */ + public function preUpdate(PreUpdateEventArgs $event) + { + $entity = $event->getEntity(); + if ($entity instanceof ProductInterface) { + $this->fixProductCategory($entity); + } + } + + /** + * Overrides the product categories assigning the one saved as principal + * category. + * + * @param ProductInterface $product The product being saved + */ + protected function fixProductCategory(ProductInterface $product) + { + $principalCategory = $product->getPrincipalCategory(); + + if ($principalCategory instanceof CategoryInterface) { + $categories = new ArrayCollection(); + $categories->add($principalCategory); + $product->setCategories($categories); + } + } +} diff --git a/src/Elcodi/Admin/ProductBundle/Resources/config/eventListeners.yml b/src/Elcodi/Admin/ProductBundle/Resources/config/eventListeners.yml index 9b812a02..2a63ae06 100644 --- a/src/Elcodi/Admin/ProductBundle/Resources/config/eventListeners.yml +++ b/src/Elcodi/Admin/ProductBundle/Resources/config/eventListeners.yml @@ -7,3 +7,9 @@ services: class: Elcodi\Admin\ProductBundle\EventListener\NewCategoryPositionEventListener tags: - { name: doctrine.event_listener, event: preFlush, method: preFlush } + + elcodi.product.category.event_listener.product_has_only_one_category: + class: Elcodi\Admin\ProductBundle\EventListener\ProductHasOnlyOneCategoryEventListener + tags: + - { name: doctrine.event_listener, event: preFlush, method: preFlush, priority: -10 } + - { name: doctrine.event_listener, event: preUpdate, method: preUpdate , priority: -10 }