<?php declare(strict_types=1);
namespace FTBPlugin\Subscriber;
use Monolog\Logger;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Shopware\Storefront\Page\Suggest\SuggestPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FTBSubscriber implements EventSubscriberInterface
{
private $search_notfound_repository;
private $logger;
public function __construct(Logger $logger, EntityRepositoryInterface $dreiwm_search_notfound_repo)
{
$this->logger = $logger;
$this->search_notfound_repository = $dreiwm_search_notfound_repo;
}
public static function getSubscribedEvents(): array
{
return [
SuggestPageLoadedEvent::class => 'onSearch',
SearchPageLoadedEvent::class => 'onSearchPage',
CustomerEvents:: CUSTOMER_REGISTER_EVENT => 'customerRegistered',
];
}
/**
* Suche (Ergebniss-Seite):
* Wenn bei einer Suche keine Ergebnisse gefunden werden, soll der String, nach dem gesucht wird,
* in die DB gespeichert werden
*/
public function onSearchPage(SearchPageLoadedEvent $event)
{
if ($event->getPage()->getListing()->getTotal() === 0) {
$this->saveSearchTerm(
$event->getPage()->getSearchTerm(),
$event->getContext()
);
}
}
/**
* Ajax-Suche (Live):
* Wenn bei einer Suche keine Ergebnisse gefunden werden, soll der String, nach dem gesucht wird,
* in die DB gespeichert werden
*/
public function onSearch(SuggestPageLoadedEvent $event)
{
if ($event->getPage()->getSearchResult()->getTotal() === 0) {
$this->saveSearchTerm(
$event->getPage()->getSearchTerm(),
$event->getContext()
);
}
}
/**
* schreiben den Searchterm in die DB
*/
private function saveSearchTerm($as_searchTerm, $context)
{
$sessionId = session_id();
try {
if ($this->searchTermNotWritten($as_searchTerm, $sessionId, $context)) {
$this->search_notfound_repository
->create([
[
'searchTerm' => strip_tags($as_searchTerm),
'session' => $sessionId ?: '',
'date' => new \DateTime()
]
], $context);
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
}
/**
* Return true, wenn es bereits nicht geschrieben wurde
* retunr false, wenn es bereits min einen Eintrag gibt.
* @param $as_searchTerm
* @param $as_sessionId
* @param $context
* @return bool
*/
private function searchTermNotWritten($as_searchTerm, $as_sessionId, $context)
{
$criteria = new Criteria([]);
$criteria->addFilter(new EqualsFilter('session', $as_sessionId));
$criteria->addFilter(new EqualsFilter('searchTerm', $as_searchTerm));
$criteria->addFilter(new RangeFilter('date', [
RangeFilter::GTE => date('Y-m-d H:i:s', strtotime('now -2min')),
]));
$res = $this->search_notfound_repository->search($criteria, $context);
return $res->count() === 0;
}
}