<?php
/*
* Plugin Name : SearchPlus
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\SearchPlus42\Event;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Event\TemplateEvent;
use Plugin\SearchPlus42\Service\SearchPlusService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductEvent implements EventSubscriberInterface
{
private $entityManager;
private $searchPlusService;
public function __construct(
EntityManagerInterface $entityManager,
SearchPlusService $searchPlusService
)
{
$this->entityManager = $entityManager;
$this->searchPlusService = $searchPlusService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Product/list.twig' => 'onTemplateProductList',
];
}
public function onTemplateProductList(TemplateEvent $event)
{
$source = $event->getSource();
if(preg_match("/\{\%\sfor\sitem\sin\ssearch\_form\s\%\}/",$source, $result)){
$search = $result[0];
$replace = $search . '
{% if item.vars.value is iterable %}
{% for value in item.vars.value %}
<input type="hidden"
name="{{ item.vars.full_name }}[]"
value="{{ value }}"/>
{% endfor %}
{% else %}';
$source = str_replace($search, $replace, $source);
}
if(preg_match('/value\=\"\{\{\sitem\.vars\.value\s\}\}\"\s\{\%\sendif\s\%\}\/\>/',$source, $result)){
$search = $result[0];
$replace = $search . "{% endif %}";
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
$twig = '@default/Block/search_item.twig';
$event->addSnippet($twig);
$js = '@SearchPlus42/default/Product/list_result.js';
$event->addAsset($js);
$parameters = $event->getParameters();
if($this->searchPlusService->checkInstallPlugin('ProductPlus42')){
$productItemRepository = $this->entityManager->getRepository('Plugin\ProductPlus42\Entity\ProductItem');
$ProductItems = $productItemRepository->findBy(['search_flg' => true],['sort_no' => 'DESC']);
$parameters['ProductItems'] = $ProductItems;
}
$event->setParameters($parameters);
}
}