Refactor ical action to dedicated controller, add route enhancer

This commit is contained in:
Björn Fromme
2019-12-04 14:05:13 +01:00
parent 51570b2300
commit 23ae4db820
8 changed files with 157 additions and 60 deletions
+17
View File
@@ -168,6 +168,22 @@ routeEnhancers:
defaultController: 'Search::staticresult'
requirements:
filter_settings: '[A-Z\d%\-]+'
ContingentsIcalPlugin:
type: Extbase
extension: EpProducts
plugin: ical
routes:
-
routePath: '/contingents/{hotel}'
_controller: 'Ical::contingents'
_arguments:
hotel: hotel
defaultController: 'Ical::contingents'
aspects:
hotel:
type: PersistedAliasMapper
tableName: tx_epproducts_domain_model_hotel
routeFieldName: path_segment
ResellerPlugin:
type: Extbase
extension: EpProducts
@@ -220,5 +236,6 @@ routeEnhancers:
'/json': 1701
'/html': 1702
'.json': 1803
'.ical': 1710
'sitemap.xml': 1533906435
'yoast-snippetpreview.json': 1480321830
@@ -27,13 +27,10 @@ namespace EP\EpProducts\Controller;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use EP\EpProducts\Domain\Model\Contingent;
use EP\EpProducts\Domain\Model\Hotel;
use EP\EpProducts\Domain\Model\Product;
use EP\EpProducts\Domain\Repository\ContingentRepository;
use EP\EpProducts\Service\ContingentDataService;
use Kigkonsult\Icalcreator\Vcalendar;
use League\Period\Period;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
class AjaxContingentController extends ActionController
@@ -103,52 +100,4 @@ class AjaxContingentController extends ActionController
return \json_encode($rooms);
}
/**
* @param Hotel $hotel
* @param string $year
* @param string $month
* @param int $months
*/
public function icalAction(Hotel $hotel)
{
$range = Period::after(new \DateTime('now'), '1 year');
$events = $this->contingentRepository->getEvents(
\DateTime::createFromImmutable($range->getStartDate()),
\DateTime::createFromImmutable($range->getEndDate()),
['hotel' => $hotel]
);
$vcalendar = Vcalendar::factory([ Vcalendar::UNIQUE_ID => 'www.ep-reisen.de' ]);
foreach ($events as $event) {
/** @var Contingent $event */
$paxTotal = $event->getPax();
$paxAvaiable = $event->getAvailable();
if ($paxTotal > 0) {
$percentageBooked = 100 - round($paxAvaiable / $paxTotal * 100);
} else {
$percentageBooked = 100;
}
if ($percentageBooked >= 75) {
$dtStart = $event->getBegin();
$dtEnd = clone $event->getBegin();
$dtEnd->modify('+1 day');
$vcalendar
->newVevent()
->setDtstart($dtStart)
->setDtend($dtEnd)
->setSummary('Unavailable')
->setDescription(sprintf('Unavailable on %s', $dtStart->format('d M Y')))
;
}
}
return $vcalendar->vtimezonePopulate()->createCalendar();
}
}
@@ -0,0 +1,102 @@
<?php
namespace EP\EpProducts\Controller;
/***************************************************************
*
* Copyright notice
*
* (c) 2016 Björn Fromme <[email protected]>, dreipunktnull
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project 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 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script 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.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use EP\EpProducts\Domain\Model\Contingent;
use EP\EpProducts\Domain\Model\Hotel;
use EP\EpProducts\Domain\Repository\ContingentRepository;
use Kigkonsult\Icalcreator\Vcalendar;
use League\Period\Period;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
class IcalController extends ActionController
{
/**
* @var \EP\EpProducts\Domain\Repository\ContingentRepository
*/
protected $contingentRepository;
/**
* @param ContingentRepository $contingentRepository
*/
public function __construct(ContingentRepository $contingentRepository)
{
parent::__construct();
$this->contingentRepository = $contingentRepository;
}
/**
* @param Hotel $hotel
* @param string $year
* @param string $month
* @param int $months
*/
public function contingentsAction(Hotel $hotel)
{
$range = Period::after(new \DateTime('now'), '1 year');
$events = $this->contingentRepository->getEvents(
\DateTime::createFromImmutable($range->getStartDate()),
\DateTime::createFromImmutable($range->getEndDate()),
['hotel' => $hotel]
);
$vcalendar = Vcalendar::factory([ Vcalendar::UNIQUE_ID => 'www.ep-reisen.de' ]);
foreach ($events as $event) {
/** @var Contingent $event */
$paxTotal = $event->getPax();
$paxAvaiable = $event->getAvailable();
if ($paxTotal > 0) {
$percentageBooked = 100 - round($paxAvaiable / $paxTotal * 100);
} else {
$percentageBooked = 100;
}
if ($percentageBooked >= 75) {
$dtStart = $event->getBegin();
$dtEnd = clone $event->getBegin();
$dtEnd->modify('+1 day');
$vcalendar
->newVevent()
->setDtstart($dtStart)
->setDtend($dtEnd)
->setSummary('Unavailable')
->setDescription(sprintf('Unavailable on %s', $dtStart->format('d M Y')))
;
}
}
header(sprintf('Content-disposition: attachment; filename=%s.ical', $hotel->getPathSegment()));
return $vcalendar->vtimezonePopulate()->createCalendar();
}
}
@@ -241,6 +241,11 @@ class Hotel extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity implements Te
*/
protected $externalLink;
/**
* @var string
*/
protected $pathSegment;
public function __construct()
{
$this->initStorageObjects();
@@ -923,6 +928,14 @@ class Hotel extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity implements Te
return $this->externalLink;
}
/**
* @return string
*/
public function getPathSegment()
{
return $this->pathSegment;
}
/**
* @param string $externalLink
*/
@@ -28,7 +28,7 @@ return [
address, longitude, latitude, headline, teaser, description, features, room_types, additional_information,
teaser_images, header_images, images, reseller_images, code, type, category, country, region, city, facts,
teamer, products, header_title,header_subtitle, earlybird, new, low_contingent, show_as_teaser,
external_link',
external_link, path_segment',
],
'types' => [
'1' => [
@@ -41,7 +41,7 @@ return [
],
'palettes' => [
'top' => ['showitem' => 'hidden,earlybird,new,low_contingent,show_as_teaser,
--linebreak--,code,groupsch_id,detail_page,--linebreak--,external_link'],
--linebreak--,code,groupsch_id,detail_page,--linebreak--,external_link,path_segment'],
'geolocation' => ['showitem' => 'address, longitude, latitude'],
'destinations' => ['showitem' => 'country,region,city'],
'name' => ['showitem' => 'name,short_name,--linebreak--,header_title,header_subtitle,--linebreak--,headline'],
@@ -638,5 +638,21 @@ return [
'eval' => 'trim',
],
],
'path_segment' => [
'exclude' => 0,
'label' => 'URL Segment',
'config' => [
'type' => 'slug',
'generatorOptions' => [
'fields' => ['name'],
'prefixParentPageSlug' => false,
'replacements' => [
'/' => '-',
],
],
'fallbackCharacter' => '-',
'eval' => 'uniqueInSite',
],
],
],
];
@@ -153,7 +153,6 @@ tx_epproducts_ical {
disablePrefixComment = 1
additionalHeaders.1.header = Cache-Control: no-cache, no-store, must-revalidate
additionalHeaders.10.header = Content-type: text/calendar
additionalHeaders.20.header = Content-disposition: attachment; filename=ep-reisen.ical
xhtml_cleaning = 0
admPanel = 0
debug = 0
@@ -166,11 +165,11 @@ tx_epproducts_ical {
extensionName = EpProducts
pluginName = ical
vendorName = EP
controller = AjaxContingent
action = ical
controller = Ical
action = contingents
switchableControllerActions {
AjaxContingent {
1 = ical
Ical {
1 = contingents
}
}
}
@@ -362,10 +362,10 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\EP\EpProducts\T
'EP.' . $_EXTKEY,
'ical',
[
'AjaxContingent' => 'ical',
'Ical' => 'contingents',
],
[
'AjaxContingent' => 'ical',
'Ical' => 'contingents',
]
);
@@ -224,6 +224,7 @@ CREATE TABLE tx_epproducts_domain_model_hotel
code varchar(255) DEFAULT '' NOT NULL,
groupsch_id varchar(8) DEFAULT '' NOT NULL,
external_link varchar(255) DEFAULT '' NOT NULL,
path_segment varchar(255) DEFAULT '' NOT NULL,
type varchar(255) DEFAULT '' NOT NULL,
category int(11) DEFAULT '0' NOT NULL,
country int(11) unsigned DEFAULT '0',