events/Classes/Controller/DateController.php

185 lines
5.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Wrm\Events\Controller;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Extbase\Annotation as Extbase;
2022-11-22 12:58:36 +01:00
use TYPO3\CMS\Extbase\Service\ExtensionService;
use Wrm\Events\Domain\Model\Date;
use Wrm\Events\Domain\Model\Dto\DateDemandFactory;
2021-07-13 15:50:14 +02:00
use Wrm\Events\Domain\Repository\CategoryRepository;
use Wrm\Events\Domain\Repository\DateRepository;
2019-08-14 17:22:01 +02:00
use Wrm\Events\Domain\Repository\RegionRepository;
use Wrm\Events\Events\Controller\DateListVariables;
use Wrm\Events\Events\Controller\DateSearchVariables;
use Wrm\Events\Pagination\Factory;
use Wrm\Events\Service\DataProcessingForModels;
class DateController extends AbstractController
{
/**
* @var DateDemandFactory
*/
protected $demandFactory;
/**
* @var dateRepository
*/
2019-08-14 17:22:01 +02:00
protected $dateRepository;
/**
* @var regionRepository
*/
protected $regionRepository;
2021-07-13 15:50:14 +02:00
/**
* @var CategoryRepository
*/
protected $categoryRepository;
/**
* @var Factory
*/
protected $paginationFactory;
/**
* @var DataProcessingForModels
*/
protected $dataProcessing;
2022-11-22 12:58:36 +01:00
/**
* @var EventDispatcher
*/
protected $eventDispatcher;
/**
* @var ExtensionService
*/
protected $extensionService;
2019-08-14 17:22:01 +02:00
public function __construct(
DateDemandFactory $demandFactory,
2021-07-13 15:50:14 +02:00
DateRepository $dateRepository,
2022-11-22 12:58:36 +01:00
RegionRepository $regionRepository,
CategoryRepository $categoryRepository,
2022-11-22 12:58:36 +01:00
Factory $paginationFactory,
DataProcessingForModels $dataProcessing,
EventDispatcher $eventDispatcher,
2022-11-22 12:58:36 +01:00
ExtensionService $extensionService
2019-08-14 17:22:01 +02:00
) {
$this->demandFactory = $demandFactory;
$this->dateRepository = $dateRepository;
2022-11-22 12:58:36 +01:00
$this->regionRepository = $regionRepository;
2021-07-13 15:50:14 +02:00
$this->categoryRepository = $categoryRepository;
2022-11-22 12:58:36 +01:00
$this->paginationFactory = $paginationFactory;
$this->dataProcessing = $dataProcessing;
$this->eventDispatcher = $eventDispatcher;
2022-11-22 12:58:36 +01:00
$this->extensionService = $extensionService;
}
protected function initializeAction(): void
{
parent::initializeAction();
$contentObject = $this->configurationManager->getContentObject();
if ($contentObject !== null) {
$this->demandFactory->setContentObjectRenderer($contentObject);
}
$this->dataProcessing->setConfigurationManager($this->configurationManager);
2022-11-22 12:58:36 +01:00
$this->handlePostRequest();
}
/**
* @param array $search
* @param int $currentPage
*/
public function listAction(
array $search = [],
int $currentPage = 1
): void {
$demand = $this->demandFactory->fromSettings($this->settings);
if ($search !== []) {
2022-11-22 13:49:57 +01:00
$demand = $this->demandFactory->createFromRequestValues($search, $this->settings);
2019-11-11 12:43:50 +01:00
}
$dates = $this->dateRepository->findByDemand($demand);
$event = $this->eventDispatcher->dispatch(new DateListVariables(
$search,
$demand,
$dates,
$this->paginationFactory->create(
$currentPage,
$this->settings['itemsPerPage'] ?? 25,
$this->settings['maximumLinks'] ?? 5,
$dates
)
));
if (!$event instanceof DateListVariables) {
throw new \Exception('Did not retrieve DateSearchVariables from event dispatcher, got: ' . get_class($event), 1657542318);
}
$this->view->assignMultiple($event->getVariablesForView());
}
/**
* @param array $search
*/
public function searchAction(array $search = []): void
2019-08-14 17:22:01 +02:00
{
$demand = $this->demandFactory->fromSettings($this->settings);
if ($search !== []) {
2022-11-22 13:49:57 +01:00
$demand = $this->demandFactory->createFromRequestValues($search, $this->settings);
}
$event = $this->eventDispatcher->dispatch(new DateSearchVariables(
$search,
$demand,
$this->regionRepository->findAll(),
$this->categoryRepository->findAllCurrentlyAssigned($this->settings['uids']['categoriesParent'] ?? 0, 'categories'),
$this->categoryRepository->findAllCurrentlyAssigned($this->settings['uids']['featuresParent'] ?? 0, 'features')
));
if (!$event instanceof DateSearchVariables) {
throw new \Exception('Did not retrieve DateSearchVariables from event dispatcher, got: ' . get_class($event), 1657542318);
}
$this->view->assignMultiple($event->getVariablesForView());
2019-08-14 17:22:01 +02:00
}
public function teaserAction(): void
{
$dates = $this->dateRepository->findByUids($this->settings['eventUids']);
$this->view->assign('dates', $dates);
}
/**
* @Extbase\IgnoreValidation("date")
*/
public function showAction(Date $date): void
{
$this->view->assign('date', $date);
}
2022-11-22 12:58:36 +01:00
/**
* Convert POST to proper GET.
*
* @see: https://en.wikipedia.org/wiki/Post/Redirect/Get
*/
private function handlePostRequest(): void
2019-08-14 17:22:01 +02:00
{
2022-11-22 12:58:36 +01:00
if (
$this->request->getMethod() === 'POST'
&& $this->request->hasArgument('search')
&& is_array($this->request->getArgument('search'))
) {
$namespace = $this->extensionService->getPluginNamespace(null, null);
$this->redirectToUri($this->configurationManager->getContentObject()->typoLink_URL([
'parameter' => 't3://page?uid=current',
'additionalParams' => '&' . http_build_query([
$namespace => [
'search' => array_filter($this->request->getArgument('search')),
2022-11-22 12:58:36 +01:00
],
]),
]));
}
}
}