Skip to content
This repository has been archived by the owner on May 30, 2019. It is now read-only.

Commit

Permalink
Check that products are assigned to only one category
Browse files Browse the repository at this point in the history
  • Loading branch information
rogergros committed May 22, 2015
1 parent cf00386 commit cd9093c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the Elcodi package.
*
* Copyright (c) 2014-2015 Elcodi.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
* @author Aldo Chiecchia <[email protected]>
* @author Elcodi Team <[email protected]>
*/

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit cd9093c

Please sign in to comment.