app/Plugin/ProductContact4/Event.php line 89

Open in your IDE?
  1. <?php
  2. namespace Plugin\ProductContact4;
  3. use Doctrine\ORM\EntityManager;
  4. use Eccube\Event\EccubeEvents;
  5. use Eccube\Event\EventArgs;
  6. use Eccube\Event\TemplateEvent;
  7. use Eccube\Repository\ProductRepository;
  8. use Eccube\Util\StringUtil;
  9. use Plugin\ProductContact4\Repository\ConfigRepository;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class Event implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var EntityManager
  15.      */
  16.     protected $entityManager;
  17.     /**
  18.      * @var ProductRepository
  19.      */
  20.     protected $productRepository;
  21.     /**
  22.      * @var ConfigRepository
  23.      */
  24.     protected $configRepository;
  25.     public function __construct(
  26.         ConfigRepository $configRepository,
  27.         EntityManager $entityManager null,
  28.         ProductRepository $productRepository null
  29.     )
  30.     {
  31.         $this->configRepository $configRepository;
  32.         $this->entityManager $entityManager;
  33.         $this->productRepository $productRepository;
  34.     }
  35.     /**
  36.      * @return array
  37.      */
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             'Product/detail.twig' => 'onRenderProductDetail',
  42.              EccubeEvents::FRONT_CONTACT_INDEX_INITIALIZE => 'onFrontContactIndexInitialize',
  43.              'plugin.contact.index.complete' => 'onFrontContactIndexComplete',
  44.         ];
  45.     }
  46.     public function onRenderProductDetail(TemplateEvent $event)
  47.     {
  48.         $label 'この商品を問い合わせる';
  49.         $Config $this->configRepository->get();
  50.         if ($Config && StringUtil::isNotBlank($Config->getName())) {
  51.             $label $Config->getName();
  52.         }
  53.         $event->setParameter('contact_button_label'$label);
  54.         $event->addSnippet('@ProductContact4/Product/contact_button.twig');
  55.     }
  56.     public function onFrontContactIndexInitialize(EventArgs $event)
  57.     {
  58.         $request $event->getRequest();
  59.         $builder $event->getArgument('builder');
  60.         if ($request->query->get('product'))
  61.         {
  62.            $Product $this->productRepository->find($request->query->get('product'));
  63.            if ($Product) {
  64.                $data $builder->getData();
  65.                $data['Product'] = $Product;
  66.                $builder->setData($data);
  67.            }
  68.         }
  69.     }
  70.     public function onFrontContactIndexComplete(EventArgs $event) {
  71.         $Contact $event->getArgument('Contact');
  72.         $data $event->getArgument('data');
  73.         // エンティティを更新
  74.         $Contact
  75.             ->setProduct($data['Product']);
  76.         // DB更新
  77.         $this->entityManager->persist($Contact);
  78.         $this->entityManager->flush($Contact);
  79.     }
  80. }