app/Plugin/ApgExtendCartIn42/Event.php line 43

Open in your IDE?
  1. <?php
  2. namespace Plugin\ApgExtendCartIn42;
  3. use Eccube\Entity\Product;
  4. use Eccube\Event\TemplateEvent;
  5. use Plugin\ApgExtendCartIn42\Entity\Domain\CartType;
  6. use Plugin\ApgExtendCartIn42\Entity\Domain\ConfigSettingType;
  7. use Plugin\ApgExtendCartIn42\Repository\ConfigRepository;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class Event implements EventSubscriberInterface
  10. {
  11.     const TEMPLATE_NAMESPACE '@ApgExtendCartIn42';
  12.     /** @var \Twig_Environment */
  13.     protected $twig;
  14.     /** @var ConfigRepository */
  15.     protected $configRepository;
  16.     public function __construct(
  17.         \Twig_Environment $twig,
  18.         ConfigRepository $configRepository
  19.     )
  20.     {
  21.         $this->twig $twig;
  22.         $this->configRepository $configRepository;
  23.     }
  24.     /**
  25.      * @return array
  26.      */
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             'Product/detail.twig' => 'onRenderProductDetail',
  31.         ];
  32.     }
  33.     public function onRenderProductDetail(TemplateEvent $event)
  34.     {
  35.         $source $event->getSource();
  36.         // data
  37.         $parameters $event->getParameters();
  38.         // setting
  39.         $loader $this->twig->getLoader();
  40.         $config $this->configRepository->getOrNew();
  41.         /** @var Product $Product */
  42.         $Product $parameters['Product'];
  43.         if ($config->getSettingType() === ConfigSettingType::INDIVIDUAL) {
  44.             $cartType $Product->getApgCartType();
  45.         } else {
  46.             $cartType $config->getCartType();
  47.         }
  48.         if ($Product->hasProductClass()) {
  49.             if ($cartType === CartType::LIST) {
  50.                 $pattern '|<form action="{{ url\(\'product_add_cart\', {id:Product.id}\) }}"(.*?)>|s';
  51.                 $addRow $this->twig->getLoader()->getSourceContext('Block/plg_cart_in_list.twig')->getCode();
  52.                 if (preg_match($pattern$source$matchesPREG_OFFSET_CAPTURE)) {
  53.                     $replacement $addRow $matches[0][0];
  54.                     $source preg_replace($pattern$replacement$source);
  55.                 }
  56.                 $parameters['cartType'] = $cartType;
  57.                 $event->setSource($source);
  58.                 $event->setParameters($parameters);
  59.                 $event->addAsset(self::TEMPLATE_NAMESPACE '/front/add_cart_list_css.twig');
  60.                 $event->addSnippet(self::TEMPLATE_NAMESPACE '/front/add_cart_list_js.twig');
  61.             } elseif ($cartType === CartType::GRID) {
  62.                 // data
  63.                 $pattern '|<form action="{{ url\(\'product_add_cart\', {id:Product.id}\) }}"(.*?)>|s';
  64.                 $addRow $this->twig->getLoader()->getSourceContext('Block/plg_cart_in_grid.twig')->getCode();
  65.                 if (preg_match($pattern$source$matchesPREG_OFFSET_CAPTURE)) {
  66.                     $replacement $addRow $matches[0][0];
  67.                     $source preg_replace($pattern$replacement$source);
  68.                 }
  69.                 $parameters['cartType'] = $cartType;
  70.                 $parameters['classCategoryNames1'] = $Product->getClassCategoryNames1();
  71.                 $parameters['classCategoryNames2'] = $Product->getClassCategoryNames2();
  72.                 $event->setSource($source);
  73.                 $event->setParameters($parameters);
  74.                 $event->addAsset(self::TEMPLATE_NAMESPACE '/front/add_cart_grid_css.twig');
  75.                 $event->addSnippet(self::TEMPLATE_NAMESPACE '/front/add_cart_grid_js.twig');
  76.             }
  77.         }
  78.     }
  79. }