feat: integrate with new bpn-connect api for calendars

This commit is contained in:
Björn Fromme
2026-06-10 14:42:49 +02:00
parent 14c8783008
commit ea0f5c6b3d
8 changed files with 304 additions and 173 deletions
@@ -27,10 +27,7 @@ namespace EP\EpTheme\ViewHelpers\Calendar;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use CalendR\Event\Collection\Basic;
use CalendR\Period\Day;
use CalendR\Period\Month;
use EP\EpProducts\Domain\Model\Contingent;
use DateInterval;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
@@ -44,17 +41,16 @@ class ContingentViewHelper extends AbstractViewHelper
*/
protected $escapeOutput = false;
const LEVEL_GREEN = 1;
const LEVEL_YELLOW = 2;
const LEVEL_RED = 3;
private const LEVEL_GREEN = 1;
private const LEVEL_YELLOW = 2;
private const LEVEL_RED = 3;
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('month', Month::class, 'The month', true);
$this->registerArgument('day', Day::class, 'The day', true);
$this->registerArgument('events', Basic::class, 'The calendar events', true);
$this->registerArgument('day', \DateTimeInterface::class, 'The day', true);
$this->registerArgument('calendar', 'array', 'The calendar payload', true);
}
/**
@@ -69,105 +65,129 @@ class ContingentViewHelper extends AbstractViewHelper
RenderingContextInterface $renderingContext
)
{
$month = $arguments['month'];
$day = $arguments['day'];
$events = $arguments['events'];
$calendar = $arguments['calendar'];
$now = new \DateTimeImmutable('now');
$dayDate = new \DateTimeImmutable($day->format('Y-m-d H:i:s'), $day->getTimezone());
$dayDate = $dayDate->setTime(0, 0, 0);
if (!$month->includes($day)) {
return '';
}
$now = new \DateTime('now');
if ($now > $day->getBegin()) {
if (!empty($calendar['error'])) {
$class = [ 'text-zinc-400' ];
return static::getHtml($class, $day->format('d'));
return static::getHtml($class, $dayDate->format('d'));
}
$contingents = $events->find($day);
if (count($contingents) === 0) {
$percentage = 100;
$level = static::LEVEL_GREEN;
} else {
$contingent = reset($contingents);
$percentage = $contingent->getPercentageAvailable();
$status = $contingent->getStatus();
$level = static::getOccupancyLevel($percentage, $status);
}
$previousDay = $day->getPrevious();
$previousContingents = $events->find($previousDay);
if (count($previousContingents) === 0) {
$previousLevel = static::LEVEL_GREEN;
} else {
$previousContingent = reset($previousContingents);
$previousPercentage = $previousContingent->getPercentageAvailable();
$previousStatus = $previousContingent->getStatus();
$previousLevel = static::getOccupancyLevel($previousPercentage, $previousStatus);
if ($now > $dayDate) {
$class = [ 'text-zinc-400' ];
return static::getHtml($class, $dayDate->format('d'));
}
$statusMap = static::getStatusMap($calendar);
$status = static::normalizeStatus($statusMap[$dayDate->format('Y-m-d')] ?? 'OK');
$previousStatus = static::normalizeStatus($statusMap[$dayDate->sub(new DateInterval('P1D'))->format('Y-m-d')] ?? 'OK');
$level = static::getOccupancyLevel($status);
$previousLevel = static::getOccupancyLevel($previousStatus);
if ($previousLevel !== $level) {
$class = static::getTransientLabelClasses($previousLevel, $level);
$class = static::getTransientLabelClasses($previousStatus, $status);
} else {
$class = static::getLabelClasses($percentage, $level);
$class = static::getLabelClasses($status);
}
return static::getHtml($class, $day->format('d'));
return static::getHtml($class, $dayDate->format('d'));
}
/**
* @param int $percentage
* @param int $level
*
* @return array
*/
protected static function getLabelClasses($percentage, $level)
protected static function getLabelClasses(string $status): array
{
$classes = static::getTransientLabelClasses($level);
$classes[] = 'available-' . $percentage;
return $classes;
return [
'block p-1 leading-none text-sm text-white',
static::getStatusClass($status),
];
}
/**
* @param int $levelFrom
* @param int|null $levelTo
*
* @return array
*/
protected static function getTransientLabelClasses($levelFrom, $levelTo = null)
protected static function getTransientLabelClasses(string $statusFrom, ?string $statusTo = null): array
{
$classes = [ 'block p-1 leading-none text-sm text-white' ];
if ($levelFrom === static::LEVEL_RED) {
$class = 'availability-none';
} elseif ($levelFrom === static::LEVEL_YELLOW) {
$class = 'availability-partial';
} else {
$class = 'availability-full';
}
if ($levelTo === static::LEVEL_RED) {
$class .= '-to-none';
} elseif ($levelTo === static::LEVEL_YELLOW) {
$class .= '-to-partial';
} elseif ($levelTo !== null) {
$class .= '-to-full';
$class = static::getStatusClass($statusFrom);
if ($statusTo !== null) {
$class .= '-to-' . static::getStatusSuffix($statusTo);
}
$classes[] = $class;
return $classes;
}
/**
* @param int $percentage
* @param int $status
*
* @return int
*/
protected static function getOccupancyLevel($percentage, $status)
protected static function getOccupancyLevel(string $status): int
{
if ($percentage <= 20 || $status === Contingent::STATUS_BLOCKED) {
if ($status === 'BLOCKED') {
return static::LEVEL_RED;
}
if ($percentage <= 80 || $status === Contingent::STATUS_ONREQUEST) {
if ($status === 'ON_REQUEST') {
return static::LEVEL_YELLOW;
}
return static::LEVEL_GREEN;
}
/**
* @return array<string, string>
*/
protected static function getStatusMap(array $calendar): array
{
$statusMap = [];
foreach (($calendar['data'] ?? []) as $row) {
if (!isset($row['date'])) {
continue;
}
$statusMap[$row['date']] = static::normalizeStatus((string)($row['status'] ?? 'OK'));
}
return $statusMap;
}
protected static function normalizeStatus(string $status): string
{
$status = strtoupper(trim($status));
if ($status === 'ONREQUEST') {
return 'ON_REQUEST';
}
if (in_array($status, [ 'OK', 'ON_REQUEST', 'BLOCKED' ], true)) {
return $status;
}
return 'OK';
}
protected static function getStatusClass(string $status): string
{
if ($status === 'BLOCKED') {
return 'availability-none';
}
if ($status === 'ON_REQUEST') {
return 'availability-partial';
}
return 'availability-full';
}
protected static function getStatusSuffix(string $status): string
{
if ($status === 'BLOCKED') {
return 'none';
}
if ($status === 'ON_REQUEST') {
return 'partial';
}
return 'full';
}
/**
* @param array $class
* @param string $label
@@ -1,54 +0,0 @@
<?php
namespace EP\EpTheme\ViewHelpers\Calendar;
/***************************************************************
*
* 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 TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
class IncludesViewHelper extends AbstractConditionViewHelper
{
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('range', 'object', 'Range to check against', true);
$this->registerArgument('period', 'object', 'Period to check if it is included in range', true);
}
/**
* @param array $arguments
* @return bool
*/
protected static function evaluateCondition($arguments = null)
{
/** @var \CalendR\Period\Range $range */
$range = $arguments['range'];
/** @var \CalendR\Period\PeriodInterface $period */
$period = $arguments['period'];
return $range->includes($period);
}
}
@@ -6,21 +6,17 @@
<f:section name="Main">
<div class="calendar__range">
<f:render section="Year" arguments="{year: yearStart, range: range, events: events, months: months, hotel: hotel}"/>
<f:if condition="{yearEnd}">
<f:render section="Year" arguments="{year: yearEnd, range: range, events: events, months: months, hotel: hotel}"/>
<f:if condition="{calendar.error}">
<div class="w-full p-4 text-sm text-red-600">
Der Kalender konnte aktuell nicht geladen werden.
</div>
</f:if>
<f:for each="{calendarMonths}" as="month">
<f:render section="Calendar" arguments="{month: month, calendar: calendar, months: months}"/>
</f:for>
</div>
</f:section>
<f:section name="Year">
<f:for each="{year}" as="month">
<ep:calendar.includes range="{range}" period="{month}">
<f:render section="Calendar" arguments="{_all}"/>
</ep:calendar.includes>
</f:for>
</f:section>
<f:section name="Calendar">
<div class="w-full{f:if(condition: '{months} > 1', then: ' md:w-1/2 lg:w-1/3')}">
<table class="min-w-full border border-zinc-400 border-collapse mb-0">
@@ -34,17 +30,27 @@
</tr>
</thead>
<tbody>
<f:for each="{month}" as="week">
<f:for each="{month.weeks}" as="week">
<tr>
<f:for each="{week}" as="day">
<td class="px-1 py-2 border border-zinc-300 text-center">
<ep:calendar.contingent day="{day}" month="{month}" events="{events}"/>
<f:if condition="{day.inMonth}">
<f:then>
<ep:calendar.contingent day="{day.date}" calendar="{calendar}"/>
</f:then>
<f:else>
&nbsp;
</f:else>
</f:if>
</td>
</f:for>
</tr>
</f:for>
</tbody>
</table>
<div class="px-2 pt-2 text-sm font-semibold text-zinc-700">
{month.label}
</div>
</div>
</f:section>