Code cleanup
This commit is contained in:
@@ -6,7 +6,7 @@ namespace EP\EpProducts\Controller;
|
||||
*
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2016 Björn Fromme <[email protected]>, dreipunktnull
|
||||
* (c) 2019 Björn Fromme <[email protected]>, dreipunktnull
|
||||
*
|
||||
* All rights reserved
|
||||
*
|
||||
@@ -27,76 +27,32 @@ 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\Repository\ContingentRepository;
|
||||
use Kigkonsult\Icalcreator\Vcalendar;
|
||||
use League\Period\Period;
|
||||
use EP\EpProducts\Service\IcalService;
|
||||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
|
||||
|
||||
class IcalController extends ActionController
|
||||
{
|
||||
/**
|
||||
* @var \EP\EpProducts\Domain\Repository\ContingentRepository
|
||||
* @var IcalService
|
||||
*/
|
||||
protected $contingentRepository;
|
||||
protected $icalService;
|
||||
|
||||
/**
|
||||
* @param ContingentRepository $contingentRepository
|
||||
*/
|
||||
public function __construct(ContingentRepository $contingentRepository)
|
||||
public function __construct(IcalService $icalService)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->contingentRepository = $contingentRepository;
|
||||
$this->icalService = $icalService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Hotel $hotel
|
||||
* @param string $year
|
||||
* @param string $month
|
||||
* @param int $months
|
||||
* @return string
|
||||
*/
|
||||
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();
|
||||
return $this->icalService->createContingentsFeed($hotel);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
namespace EP\EpProducts\Service;
|
||||
|
||||
/***************************************************************
|
||||
*
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2019 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 Doctrine\DBAL\DBALException;
|
||||
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\Core\SingletonInterface;
|
||||
|
||||
class IcalService implements SingletonInterface
|
||||
{
|
||||
const THRESHOLD = 75;
|
||||
|
||||
/**
|
||||
* @var ContingentRepository
|
||||
*/
|
||||
protected $contingentRepository;
|
||||
|
||||
/**
|
||||
* @param ContingentRepository $contingentRepository
|
||||
*/
|
||||
public function __construct(ContingentRepository $contingentRepository)
|
||||
{
|
||||
$this->contingentRepository = $contingentRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Hotel $hotel
|
||||
* @return string
|
||||
*/
|
||||
public function createContingentsFeed(Hotel $hotel): string
|
||||
{
|
||||
$range = Period::after(new \DateTime('now'), '1 year');
|
||||
try {
|
||||
$events = $this->contingentRepository->getEvents(
|
||||
\DateTime::createFromImmutable($range->getStartDate()),
|
||||
\DateTime::createFromImmutable($range->getEndDate()),
|
||||
['hotel' => $hotel]
|
||||
);
|
||||
}
|
||||
catch (DBALException $e) {
|
||||
$events = [];
|
||||
}
|
||||
|
||||
$vcalendar = $this->createCalendarInstance();
|
||||
|
||||
$dateStart = $dateEnd = null;
|
||||
|
||||
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 >= self::THRESHOLD) {
|
||||
if (null !== $dateStart) {
|
||||
continue;
|
||||
}
|
||||
$dateStart = $event->getBegin();
|
||||
} elseif (null !== $dateStart) {
|
||||
$dateEnd = clone $event->getBegin();
|
||||
$dateEnd->modify('-1 day');
|
||||
$this->addCalendarItem($vcalendar, $dateStart, $dateEnd);
|
||||
$dateStart = $dateEnd = null;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$feed = $vcalendar->vtimezonePopulate()->createCalendar();
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
$feed = '';
|
||||
}
|
||||
|
||||
return $feed;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Vcalendar $vcalendar
|
||||
* @param \DateTime $dateStart
|
||||
* @param \DateTime $dateEnd
|
||||
*/
|
||||
protected function addCalendarItem(Vcalendar $vcalendar, \DateTime $dateStart, \DateTime $dateEnd): void
|
||||
{
|
||||
try {
|
||||
$vcalendar
|
||||
->newVevent()
|
||||
->setDtstart($dateStart)
|
||||
->setDtend($dateEnd)
|
||||
->setSummary('Unavailable')
|
||||
->setDescription(sprintf(
|
||||
'Unavailable from %s to %s',
|
||||
$dateStart->format('d M Y'),
|
||||
$dateEnd->format('d.M Y')
|
||||
))
|
||||
;
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Vcalendar
|
||||
*/
|
||||
protected function createCalendarInstance(): Vcalendar
|
||||
{
|
||||
$vcalendar = Vcalendar::factory([ Vcalendar::UNIQUE_ID => 'www.ep-reisen.de' ]);
|
||||
$vcalendar->setXprop(
|
||||
Vcalendar::X_WR_TIMEZONE,
|
||||
'Europe/Berlin'
|
||||
);
|
||||
|
||||
return $vcalendar;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user