app/Plugin/ProductContact42/Event.php line 82

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of ProductContact42
  4.  *
  5.  * Copyright(c) U-Mebius Inc. All Rights Reserved.
  6.  *
  7.  * https://umebius.com/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\ProductContact42;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Event\EccubeEvents;
  15. use Eccube\Event\EventArgs;
  16. use Eccube\Event\TemplateEvent;
  17. use Eccube\Repository\ProductRepository;
  18. use Eccube\Util\StringUtil;
  19. use Plugin\ProductContact42\Repository\ConfigRepository;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class Event implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var EntityManagerInterface
  25.      */
  26.     protected $entityManager;
  27.     /**
  28.      * @var ProductRepository
  29.      */
  30.     protected $productRepository;
  31.     /**
  32.      * @var ConfigRepository
  33.      */
  34.     protected $configRepository;
  35.     public function __construct(
  36.         ConfigRepository $configRepository,
  37.         EntityManagerInterface $entityManager,
  38.         ProductRepository $productRepository
  39.     ) {
  40.         $this->configRepository $configRepository;
  41.         $this->entityManager $entityManager;
  42.         $this->productRepository $productRepository;
  43.     }
  44.     /**
  45.      * @return array
  46.      */
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             'Product/detail.twig' => 'onRenderProductDetail',
  51.              EccubeEvents::FRONT_CONTACT_INDEX_INITIALIZE => 'onFrontContactIndexInitialize',
  52.              'plugin.contact.index.complete' => 'onFrontContactIndexComplete',
  53.         ];
  54.     }
  55.     public function onRenderProductDetail(TemplateEvent $event)
  56.     {
  57.         $label 'この商品を問い合わせる';
  58.         $Config $this->configRepository->get();
  59.         if ($Config && StringUtil::isNotBlank($Config->getName())) {
  60.             $label $Config->getName();
  61.         }
  62.         $event->setParameter('contact_button_label'$label);
  63.         if (!$Config || $Config->isInsertButtonFlg()) {
  64.             // 自動でボタンを挿入
  65.             $event->addSnippet('@ProductContact42/Product/contact_button.twig');
  66.         }
  67.     }
  68.     public function onFrontContactIndexInitialize(EventArgs $event)
  69.     {
  70.         $request $event->getRequest();
  71.         $builder $event->getArgument('builder');
  72.         if ($request->query->get('product')) {
  73.             $Product $this->productRepository->find($request->query->get('product'));
  74.             if ($Product) {
  75.                 $data $builder->getData();
  76.                 $data['Product'] = $Product;
  77.                 $builder->setData($data);
  78.             }
  79.         }
  80.     }
  81.     public function onFrontContactIndexComplete(EventArgs $event)
  82.     {
  83.         $Contact $event->getArgument('Contact');
  84.         $data $event->getArgument('data');
  85.         // エンティティを更新
  86.         $Contact
  87.             ->setProduct($data['Product']);
  88.         // DB更新
  89.         $this->entityManager->persist($Contact);
  90.         $this->entityManager->flush();
  91.     }
  92. }