This repository has been archived by the owner on May 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check that products are assigned to only one category
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
src/Elcodi/Admin/ProductBundle/EventListener/ProductHasOnlyOneCategoryEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters