Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add "Override" Entity as 1:n relation on a schedule #2

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions Classes/Controller/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace DSKZPT\Openinghours\Controller;

use DateTime;
use DSKZPT\Openinghours\Exception\NoCurrentScheduleFoundException;
use DSKZPT\Openinghours\Provider\OpeningHoursProviderInterface;
use Spatie\OpeningHours\Exceptions\MaximumLimitExceeded;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class ScheduleController extends ActionController
Expand Down Expand Up @@ -38,36 +36,34 @@ public function tableAction(): void

public function exceptionsAction(): void
{
$openingHours = $this->openingHoursProvider->getCurrent();

$dateString = $this->settings['dateString'];
$referenceDate = new \DateTime($dateString);

$openingHours = $this->openingHoursProvider->getCurrent();
$exceptions = $openingHours->getExceptions($referenceDate);

$this->view->assign('exceptions', $exceptions);
$this->view->assignMultiple([
'openinghours' => $openingHours,
'exceptions' => $exceptions
]);
}

/**
* @throws MaximumLimitExceeded
* @throws NoCurrentScheduleFoundException
*/
public function currentOpenRangeAction(): void
{
$now = new DateTime();

$openingHours = $this->openingHoursProvider->getCurrent();

if ($openingHours === null) {
throw new NoCurrentScheduleFoundException();
}

$range = $openingHours->currentOpenRange($now);
$range = $openingHours->getCurrentOpenRange();

$this->view->assignMultiple([
'openingHours' => $openingHours,
'range' => $range,
'previousClose' => $openingHours->previousClose($now),
'nextOpen' => $openingHours->nextOpen($now),
]);

if ($range) {
Expand Down
24 changes: 24 additions & 0 deletions Classes/Domain/Model/Override.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace DSKZPT\Openinghours\Domain\Model;

use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

class Override extends AbstractEntity
{
protected string $text = '';

public function getText(): string
{
return $this->text;
}

public function setText(string $text): self
{
$this->text = $text;

return $this;
}
}
40 changes: 38 additions & 2 deletions Classes/Domain/Model/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ class Schedule extends AbstractEntity
*/
protected ObjectStorage $exceptions;

/**
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<Override>
*
* @ORM\Cascade("remove")
*/
protected ObjectStorage $overrides;

public function __construct()
{
$this->monday = new ObjectStorage();
Expand Down Expand Up @@ -369,14 +376,14 @@ public function setSunday(ObjectStorage $sunday): self

public function addExceptions(Exception $exception): self
{
$this->sunday->attach($exception);
$this->exceptions->attach($exception);

return $this;
}

public function removeException(Exception $exception): self
{
$this->sunday->detach($exception);
$this->exceptions->detach($exception);

return $this;
}
Expand All @@ -396,6 +403,35 @@ public function setExceptions(ObjectStorage $exceptions): self
return $this;
}

public function addOverride(Override $override): self
{
$this->overrides->attach($override);

return $this;
}

public function removeOverride(Override $override): self
{
$this->overrides->detach($override);

return $this;
}

/**
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<Override>
*/
public function getOverrides(): ObjectStorage
{
return $this->overrides;
}

public function setOverrides(ObjectStorage $overrides): self
{
$this->overrides = $overrides;

return $this;
}

/**
* @return array<string, ObjectStorage>
*/
Expand Down
63 changes: 60 additions & 3 deletions Classes/Domain/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
namespace DSKZPT\Openinghours\Domain;

use DateTime;
use DSKZPT\Openinghours\Domain\Model\Override;
use DSKZPT\Openinghours\Domain\Model\Schedule;
use Spatie\OpeningHours\Day;
use Spatie\OpeningHours\OpeningHours as BaseOpeningHours;
use Spatie\OpeningHours\OpeningHoursForDay;

/**
* Wrapper for Spatie\OpeningHours\OpeningHours.
* Wrappes its methods to make them callable in Fluid Templates.
* Wraps its methods to make them callable in Fluid Templates.
*/
class OpeningHours extends BaseOpeningHours
{
private ?Schedule $schedule = null;

/**
* @return Day[]
*/
Expand All @@ -24,7 +28,7 @@ public function getForWeek(): array
}

/**
* @inheritDoc
* @see forWeekCombined()
*/
public function getForWeekCombined(): array
{
Expand Down Expand Up @@ -56,7 +60,7 @@ public function getExceptions(\DateTimeInterface $exceptionsSince = null): array
}

/**
* @inheritDoc
* @see forWeekConsecutiveDays
*/
public function getForWeekConsecutiveDays(): array
{
Expand All @@ -71,6 +75,11 @@ public function getUpcomingExceptions(): array
return $this->getExceptions(new \DateTime());
}

/**
* @return mixed[]
*
* @throws \Exception
*/
public function getForWeekWithExceptions(string $startDay = 'next monday'): array
{
$return = [];
Expand All @@ -83,4 +92,52 @@ public function getForWeekWithExceptions(string $startDay = 'next monday'): arra

return $return;
}

public function getSchedule(): ?Schedule
{
return $this->schedule;
}

public function getCurrentOpenRange()
{
return $this->currentOpenRange(new \DateTime());
}

public function getPreviousClose()
{
return $this->previousClose(new \DateTime());
}

public function getNextOpen()
{
return $this->nextOpen(new \DateTime());
}

public function getNextClose()
{
return $this->nextClose(new \DateTime());
}

public function setSchedule(?Schedule $schedule): self
{
$this->schedule = $schedule;

return $this;
}

public function hasActiveOverride(): bool
{
$overrides = $this->schedule->getOverrides();

return $overrides->count() > 0;
}

public function getOverride(): ?Override
{
if ($this->hasActiveOverride() === false) {
return null;
}

return $this->schedule->getOverrides()[0];
}
}
9 changes: 9 additions & 0 deletions Classes/Exception/NoCurrentScheduleFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace DSKZPT\Openinghours\Exception;

class NoCurrentScheduleFoundException extends \Exception
{
}
5 changes: 4 additions & 1 deletion Classes/Provider/OpeningHoursProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function getCurrent(): ?OpeningHours
throw new \InvalidArgumentException('Es gibt keinen Olan!!!! @todo');
}

return $this->openingHoursFactory->createFromSchedule($currentSchedule);
$openingHours = $this->openingHoursFactory->createFromSchedule($currentSchedule);
$openingHours->setSchedule($currentSchedule);

return $openingHours;
}
}
110 changes: 110 additions & 0 deletions Configuration/TCA/tx_openinghours_domain_model_override.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

return [
'ctrl' => [
'title' => 'LLL:EXT:openinghours/Resources/Private/Language/locallang_db.xlf:tx_openinghours_domain_model_override',
'label' => 'text',
'tstamp' => 'tstamp',
'crdate' => 'crdate',
'cruser_id' => 'cruser_id',
'sortby' => 'sorting',
'hideTable' => true,
'versioningWS' => true,
'languageField' => 'sys_language_uid',
'transOrigPointerField' => 'l10n_parent',
'transOrigDiffSourceField' => 'l10n_diffsource',
'delete' => 'deleted',
'enablecolumns' => [
'disabled' => 'hidden',
'starttime' => 'starttime',
'endtime' => 'endtime',
],
'searchFields' => 'text',
'iconfile' => 'EXT:openinghours/Resources/Public/Icons/tx_openinghours_domain_model_override.gif',
],
'types' => [
'1' => ['showitem' => 'text, --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:language, sys_language_uid, l10n_parent, l10n_diffsource, --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:access, hidden, starttime, endtime'],
],
'columns' => [
'sys_language_uid' => [
'exclude' => true,
'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.language',
'config' => [
'type' => 'language',
],
],
'l10n_parent' => [
'displayCond' => 'FIELD:sys_language_uid:>:0',
'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.l18n_parent',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'default' => 0,
'items' => [
['', 0],
],
'foreign_table' => 'tx_openinghours_domain_model_override',
'foreign_table_where' => 'AND {#tx_openinghours_domain_model_override}.{#pid}=###CURRENT_PID### AND {#tx_openinghours_domain_model_override}.{#sys_language_uid} IN (-1,0)',
],
],
'l10n_diffsource' => [
'config' => [
'type' => 'passthrough',
],
],
'hidden' => [
'exclude' => true,
'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.visible',
'config' => [
'type' => 'check',
'renderType' => 'checkboxToggle',
'items' => [
[
0 => '',
1 => '',
'invertStateDisplay' => true,
],
],
],
],
'starttime' => [
'exclude' => true,
'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.starttime',
'config' => [
'type' => 'input',
'renderType' => 'inputDateTime',
'eval' => 'datetime,int,required',
'default' => 0,
'behaviour' => [
'allowLanguageSynchronization' => true,
],
],
],
'endtime' => [
'exclude' => true,
'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.endtime',
'config' => [
'type' => 'input',
'renderType' => 'inputDateTime',
'eval' => 'datetime,int,required',
'default' => 0,
'range' => [
'upper' => mktime(0, 0, 0, 1, 1, 2038),
],
'behaviour' => [
'allowLanguageSynchronization' => true,
],
],
],
'text' => [
'exclude' => true,
'label' => 'LLL:EXT:openinghours/Resources/Private/Language/locallang_db.xlf:tx_openinghours_domain_model_override.text',
'config' => [
'type' => 'input',
'size' => 30,
'eval' => 'trim',
'default' => '',
],
],
],
];
Loading