|
- <?php
-
- namespace Lc\CaracoleBundle\EventSubscriber\Product;
-
- use Doctrine\ORM\EntityManagerInterface;
-
- use Lc\CaracoleBundle\Model\Product\ProductFamilySectionPropertyInterface;
- use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-
- class UpdateProductFamilySectionPropertyEventSubscriber implements EventSubscriberInterface
- {
- protected EntityManagerInterface $entityManager;
-
- public function __construct(EntityManagerInterface $entityManager)
- {
- $this->entityManager = $entityManager;
- }
-
- public static function getSubscribedEvents()
- {
- return [
- EntityManagerEvent::PRE_CREATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
- EntityManagerEvent::PRE_UPDATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
- ];
- }
-
- public function processBeforePersistProductFamilySectionInterface(EntityManagerEvent $event)
- {
- $productFamilySectionProperty = $event->getEntity();
-
- if($productFamilySectionProperty instanceof ProductFamilySectionPropertyInterface) {
- $section = $productFamilySectionProperty->getSection();
- if($productFamilySectionProperty->getStatus() == 1) {
- $productCategoryArray = $productFamilySectionProperty->getProductFamily()->getProductCategories();
- foreach($productCategoryArray as $productCategory) {
- if($productCategory->getSection() == $section) {
- // mise à jour du statut
- $productCategory->setStatus(1);
- $this->entityManager->update($productCategory);
-
- // mise à jour du statut du parent
- $productCategoryParent = $productCategory->getParent();
- if($productCategoryParent) {
- $productCategoryParent->setStatus(1);
- $this->entityManager->update($productCategoryParent);
- }
- }
- }
- }
- }
- }
-
- }
|