<?php
namespace Plugin\CustomProductSearch\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Environment;
use Eccube\Event\TemplateEvent;
use Eccube\Event\EccubeEvents;
use Plugin\CustomProductSearch\Form\Type\SearchProductType;
use Symfony\Component\HttpFoundation\Request;
class Event implements EventSubscriberInterface
{
/**
* @var FormFactoryInterface
*/
private $formFactory;
/**
* @var Environment
*/
private $twig;
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var Request
*/
private $request;
public function __construct(
FormFactoryInterface $formFactory,
Environment $twig,
RequestStack $requestStack
) {
$this->formFactory = $formFactory;
$this->twig = $twig;
$this->requestStack = $requestStack;
$this->request = $requestStack->getCurrentRequest();
}
/**
* Register events
*/
public static function getSubscribedEvents()
{
return [
// Hook into product list template
'Product/list.twig' => [
['onProductListRender', 10]
],
'Block/header.twig' => [
['onHeaderRender', 10]
],
];
}
/**
* Handle rendering search form on product list page
*/
public function onProductListRender(TemplateEvent $event)
{
try {
// Create search form
$searchForm = $this->createSearchForm();
// Render search form
$searchFormHtml = $this->renderSearchForm($searchForm);
// Insert search form into template
$this->insertSearchFormIntoTemplate($event, $searchFormHtml);
} catch (\Exception $e) {
// Log error if any
$this->logError('Product List Render Error', $e);
}
}
/**
* Handle rendering search form on product list page
*/
public function onHeaderRender(TemplateEvent $event)
{
try {
// Create search form
$searchForm = $this->createSearchForm();
// Render search form
$searchFormHtml = $this->renderHeaderSearchForm($searchForm);
// Insert search form into template
$this->insertSearchFormIntoTemplate($event, $searchFormHtml);
} catch (\Exception $e) {
// Log error if any
$this->logError('Product List Render Error', $e);
}
}
/**
* Handle rendering search form on search page
*/
public function onProductSearchRender(TemplateEvent $event)
{
try {
// Create search form
$searchForm = $this->createSearchForm();
// Render search form
$searchFormHtml = $this->renderSearchForm($searchForm);
// Insert search form into template
$this->insertSearchFormIntoTemplate($event, $searchFormHtml);
} catch (\Exception $e) {
// Log error if any
$this->logError('Product Search Render Error', $e);
}
}
/**
* Create search form
*/
private function createSearchForm()
{
// Get data from the current request
$searchData = $this->request->query->all();
// Create form with initial data
$searchForm = $this->formFactory->create(SearchProductType::class, $searchData, [
'method' => 'GET', // Use GET method
'csrf_protection' => false, // Disable CSRF to allow searching
]);
return $searchForm;
}
/**
* Render search form to HTML
*/
private function renderSearchForm($searchForm)
{
return $this->twig->render(
'Block/search_form.twig',
[
'searchForm' => $searchForm->createView(),
'category' => [],
'category17' => [],
'category19' => [],
]
);
}
/**
* Render search form to HTML
*/
private function renderHeaderSearchForm($searchForm)
{
return $this->twig->render(
'Block/header_search_form.twig',
[
'searchForm' => $searchForm->createView(),
'category' => [],
'category17' => [],
'category19' => [],
]
);
}
/**
* Insert search form into template
*/
private function insertSearchFormIntoTemplate(TemplateEvent $event, $searchFormHtml)
{
// Get source template
$source = $event->getSource();
// Patterns to insert search form
$insertPatterns = [
'/(<div class="product_form_search">)/'
];
// Try to insert using patterns
foreach ($insertPatterns as $pattern) {
if (preg_match($pattern, $source)) {
$modifiedSource = preg_replace(
$pattern,
$searchFormHtml . '$1',
$source
);
// If insertion is successful
if ($modifiedSource !== $source) {
$event->setSource($modifiedSource);
return;
}
}
}
// If no insertion position found, add to the beginning of the template
$event->setSource($searchFormHtml . $source);
}
/**
* Log error
*/
private function logError($message, \Exception $e)
{
// Use EC-CUBE logger to log errors
// You need to inject logger if you want to use it
error_log(sprintf(
"%s: %s\nTrace: %s",
$message,
$e->getMessage(),
$e->getTraceAsString()
));
}
}