calendar/Classes/Controller/Frontend/CalendarController.php

236 lines
7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace WerkraumMedia\Calendar\Controller\Frontend;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
2024-09-17 15:00:31 +02:00
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter;
2024-09-17 15:00:31 +02:00
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use WerkraumMedia\Calendar\Domain\Model\Context;
use WerkraumMedia\Calendar\Domain\Model\ContextSpecificFactory;
use WerkraumMedia\Calendar\Domain\Model\Day;
use WerkraumMedia\Calendar\Domain\Model\ForeignDataFactory;
use WerkraumMedia\Calendar\Domain\Model\Month;
use WerkraumMedia\Calendar\Domain\Model\Week;
use WerkraumMedia\Calendar\Domain\Model\Year;
use WerkraumMedia\Calendar\Events\AssignTemplateVariables;
2024-09-17 15:00:31 +02:00
final class CalendarController extends ActionController
{
/**
* @var ForeignDataFactory
*/
private $foreignDataFactory;
/**
* @var TypoScriptService
*/
private $typoScriptService;
public function __construct(
ForeignDataFactory $foreignDataFactory,
TypoScriptService $typoScriptService
) {
$this->foreignDataFactory = $foreignDataFactory;
$this->typoScriptService = $typoScriptService;
}
2024-09-17 15:00:31 +02:00
public function initializeAction(): void
{
if ($this->foreignDataFactory instanceof ContextSpecificFactory) {
$this->foreignDataFactory->setContext(
2024-09-17 15:00:31 +02:00
Context::createFromContentObjectRenderer($this->getContentObjectRenderer())
);
}
}
2024-09-17 15:00:31 +02:00
public function initializeYearAction(): void
{
if ($this->request->hasArgument('year') === false) {
2024-09-17 15:00:31 +02:00
$this->request = $this->request->withArguments([
'year' => [
'year' => $this->getDefaultArgumentValue('year'),
],
]);
}
$this->arguments->getArgument('year')
->getPropertyMappingConfiguration()
->allowAllProperties();
}
/**
* @Extbase\IgnoreValidation("year")
*/
2024-09-17 15:00:31 +02:00
public function yearAction(Year $year): ResponseInterface
{
$this->assignVariables([
'year' => $year,
]);
2024-09-17 15:00:31 +02:00
return $this->htmlResponse();
}
2024-09-17 15:00:31 +02:00
public function initializeMonthAction(): void
{
if ($this->request->hasArgument('month') === false) {
2024-09-17 15:00:31 +02:00
$this->request = $this->request->withArguments([
'month' => [
'month' => $this->getDefaultArgumentValue('month'),
'year' => $this->getDefaultArgumentValue('year'),
],
]);
}
$this->arguments->getArgument('month')
->getPropertyMappingConfiguration()
->allowAllProperties();
}
/**
* @Extbase\IgnoreValidation("month")
*/
2024-09-17 15:00:31 +02:00
public function monthAction(Month $month): ResponseInterface
{
$this->assignVariables([
'month' => $month,
]);
2024-09-17 15:00:31 +02:00
return $this->htmlResponse();
}
2024-09-17 15:00:31 +02:00
public function initializeWeekAction(): void
{
if ($this->request->hasArgument('week') === false) {
2024-09-17 15:00:31 +02:00
$this->request = $this->request->withArguments([
'week' => [
'week' => $this->getDefaultArgumentValue('week'),
'year' => $this->getDefaultArgumentValue('year'),
],
]);
}
$this->arguments->getArgument('week')
->getPropertyMappingConfiguration()
->allowAllProperties();
}
/**
* @Extbase\IgnoreValidation("week")
*/
2024-09-17 15:00:31 +02:00
public function weekAction(Week $week): ResponseInterface
{
$this->assignVariables([
'week' => $week,
]);
2024-09-17 15:00:31 +02:00
return $this->htmlResponse();
}
2024-09-17 15:00:31 +02:00
public function initializeDayAction(): void
{
if ($this->request->hasArgument('day') === false) {
2024-09-17 15:00:31 +02:00
$this->request = $this->request->withArguments([
'day' => [
'day' => $this->getDefaultArgumentValue('day'),
],
]);
}
$propertyMappingConfiguration = $this->arguments->getArgument('day')
->getPropertyMappingConfiguration();
$propertyMappingConfiguration->allowAllProperties();
$propertyMappingConfiguration
->forProperty('day')
->setTypeConverterOption(
DateTimeConverter::class,
DateTimeConverter::CONFIGURATION_DATE_FORMAT,
'Y-m-d'
);
}
/**
* @Extbase\IgnoreValidation("day")
*/
2024-09-17 15:00:31 +02:00
public function dayAction(Day $day): ResponseInterface
{
$this->assignVariables([
'day' => $day,
]);
2024-09-17 15:00:31 +02:00
return $this->htmlResponse();
}
private function assignVariables(array $variables): void
{
$event = GeneralUtility::makeInstance(
AssignTemplateVariables::class,
$variables,
$this->request->getPluginName()
);
$this->eventDispatcher->dispatch($event);
$this->view->assignMultiple($event->getVariables());
}
/**
* Checks for TypoScript and transforms TypoScript into expected value.
* Allows to define defaults other than "now" for arguments.
*/
private function getDefaultArgumentValue(string $argumentName): string
{
$arguments = $this->typoScriptService->convertPlainArrayToTypoScriptArray(
$this->settings['arguments'] ?? []
);
$fallbackValues = [
'year' => date('Y'),
'month' => date('m'),
'week' => date('W'),
'day' => date('Y-m-d'),
];
2024-09-17 15:00:31 +02:00
$value = $this->getContentObjectRenderer()->stdWrapValue(
$argumentName,
$arguments,
$fallbackValues[$argumentName]
);
2024-09-17 15:00:31 +02:00
return (string)$value;
}
private function getContentObjectRenderer(): ContentObjectRenderer
{
$contentObjectRenderer = $this->request->getAttribute('currentContentObject');
if (! $contentObjectRenderer instanceof ContentObjectRenderer) {
throw new RuntimeException('Could not fetch currentContentObject from request.', 1726490796);
}
return $contentObjectRenderer;
}
}