Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

75 lines
3.3KB

  1. <?php
  2. namespace Lc\CaracoleBundle\EventSubscriber\Product;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Container\Product\ProductCategoryContainer;
  5. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  6. use Lc\CaracoleBundle\Model\Product\ProductFamilySectionPropertyInterface;
  7. use Lc\CaracoleBundle\Solver\Product\ProductCategorySolver;
  8. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class OnlineOrOfflineProductCategoryAfterProductFamilyEventSubscriber implements EventSubscriberInterface
  11. {
  12. protected EntityManagerInterface $entityManager;
  13. protected ProductCategoryContainer $productCategoryContainer;
  14. public function __construct(
  15. EntityManagerInterface $entityManager,
  16. ProductCategoryContainer $productCategoryContainer
  17. ) {
  18. $this->entityManager = $entityManager;
  19. $this->productCategoryContainer = $productCategoryContainer;
  20. }
  21. public static function getSubscribedEvents()
  22. {
  23. return [
  24. EntityManagerEvent::PRE_CREATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
  25. EntityManagerEvent::PRE_UPDATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
  26. ];
  27. }
  28. public function processBeforePersistProductFamilySectionInterface(EntityManagerEvent $event)
  29. {
  30. if ($event->getEntity() instanceof ProductFamilySectionPropertyInterface) {
  31. $this->setProductCategoryByProductFamilySectionProperty($event->getEntity());
  32. } else {
  33. if ($event->getEntity() instanceof ProductFamilyInterface) {
  34. foreach ($event->getEntity()->getProductFamilySectionProperties() as $productFamilySectionProperty) {
  35. $this->setProductCategoryByProductFamilySectionProperty($productFamilySectionProperty);
  36. }
  37. }
  38. }
  39. }
  40. protected function setProductCategoryByProductFamilySectionProperty($productFamilySectionProperty)
  41. {
  42. $productFamily = $productFamilySectionProperty->getProductFamily();
  43. if ($productFamilySectionProperty->getStatus() == 1 && $productFamily->getStatus() == 1) {
  44. $section = $productFamilySectionProperty->getSection();
  45. $productCategoryArray = $productFamilySectionProperty->getProductFamily()->getProductCategories();
  46. foreach ($productCategoryArray as $productCategory) {
  47. if ($productCategory->getSection() === $section) {
  48. $this->productCategoryContainer->getBuilder()->setOnlineIfOnlineProductfamily($productCategory);
  49. }
  50. }
  51. } else {
  52. if ($productFamilySectionProperty->getStatus() == 0 || $productFamily->getStatus() == 0) {
  53. $section = $productFamilySectionProperty->getSection();
  54. $productCategoryArray = $productFamily->getProductCategories();
  55. foreach ($productCategoryArray as $productCategory) {
  56. if ($productCategory->getSection() === $section) {
  57. $this->productCategoryContainer->getBuilder()->setOfflineIfOfflineProductfamily(
  58. $productCategory
  59. );
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }