app/Plugin/SearchPlus42/Event/ProductEvent.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : SearchPlus
  4. *
  5. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Plugin\SearchPlus42\Event;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Event\TemplateEvent;
  14. use Plugin\SearchPlus42\Service\SearchPlusService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ProductEvent implements EventSubscriberInterface
  17. {
  18.     private $entityManager;
  19.     private $searchPlusService;
  20.     public function __construct(
  21.             EntityManagerInterface $entityManager,
  22.             SearchPlusService $searchPlusService
  23.             )
  24.     {
  25.         $this->entityManager $entityManager;
  26.         $this->searchPlusService $searchPlusService;
  27.     }
  28.     /**
  29.      * @return array
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             'Product/list.twig' => 'onTemplateProductList',
  35.         ];
  36.     }
  37.     public function onTemplateProductList(TemplateEvent $event)
  38.     {
  39.         $source $event->getSource();
  40.         if(preg_match("/\{\%\sfor\sitem\sin\ssearch\_form\s\%\}/",$source$result)){
  41.             $search $result[0];
  42.             $replace $search '
  43.                 {% if item.vars.value is iterable %}
  44.                     {% for value in item.vars.value %}
  45.                     <input type="hidden"
  46.                            name="{{ item.vars.full_name }}[]"
  47.                            value="{{ value }}"/>
  48.                     {% endfor %}
  49.                 {% else %}';
  50.             $source str_replace($search$replace$source);
  51.         }
  52.         if(preg_match('/value\=\"\{\{\sitem\.vars\.value\s\}\}\"\s\{\%\sendif\s\%\}\/\>/',$source$result)){
  53.             $search $result[0];
  54.             $replace $search "{% endif %}";
  55.             $source str_replace($search$replace$source);
  56.         }
  57.         $event->setSource($source);
  58.         $twig '@default/Block/search_item.twig';
  59.         $event->addSnippet($twig);
  60.         $js '@SearchPlus42/default/Product/list_result.js';
  61.         $event->addAsset($js);
  62.         $parameters $event->getParameters();
  63.         if($this->searchPlusService->checkInstallPlugin('ProductPlus42')){
  64.             $productItemRepository $this->entityManager->getRepository('Plugin\ProductPlus42\Entity\ProductItem');
  65.             $ProductItems $productItemRepository->findBy(['search_flg' => true],['sort_no' => 'DESC']);
  66.             $parameters['ProductItems'] = $ProductItems;
  67.         }
  68.         $event->setParameters($parameters);
  69.     }
  70. }