2020-10-22 16:31:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace WerkraumMedia\Calendar\Domain\Model;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
|
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
|
|
|
|
|
|
|
|
class Month
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $month;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $year;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Week[]
|
|
|
|
*/
|
|
|
|
private $weeks = [];
|
|
|
|
|
2021-02-23 13:04:29 +01:00
|
|
|
/**
|
|
|
|
* @var Day[]
|
|
|
|
*/
|
|
|
|
private $days = [];
|
|
|
|
|
2020-10-22 16:31:54 +02:00
|
|
|
public function __construct(
|
|
|
|
int $month,
|
|
|
|
int $year
|
|
|
|
) {
|
|
|
|
$this->month = $month;
|
|
|
|
$this->year = $year;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isActive(): bool
|
|
|
|
{
|
|
|
|
foreach ($this->getWeeks() as $week) {
|
|
|
|
if ($week->isActive()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getWeeks(): array
|
|
|
|
{
|
|
|
|
if ($this->weeks !== []) {
|
|
|
|
return $this->weeks;
|
|
|
|
}
|
|
|
|
|
|
|
|
$month = $this->getDateTimeInstance();
|
|
|
|
$lastDay = $month->modify('last day of this month')->modify('sunday this week');
|
|
|
|
$currentDay = $month->modify('monday this week');
|
|
|
|
|
|
|
|
while ($currentDay <= $lastDay) {
|
|
|
|
$this->weeks[] = new Week(
|
|
|
|
(int) $currentDay->format('W'),
|
2024-09-16 12:29:46 +02:00
|
|
|
(int) $currentDay->format('o')
|
2020-10-22 16:31:54 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$currentDay = $currentDay->modify('+7 days');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->weeks;
|
|
|
|
}
|
|
|
|
|
2021-02-23 13:04:29 +01:00
|
|
|
public function getDays(): array
|
|
|
|
{
|
|
|
|
if ($this->days !== []) {
|
|
|
|
return $this->days;
|
|
|
|
}
|
|
|
|
|
|
|
|
$currentDay = $this->getDateTimeInstance()->modify('first day of this month');
|
|
|
|
$endOfWeek = $this->getDateTimeInstance()->modify('last day of this month');
|
|
|
|
|
|
|
|
while ($currentDay <= $endOfWeek) {
|
|
|
|
$this->days[] = new Day(\DateTime::createFromImmutable($currentDay));
|
|
|
|
$currentDay = $currentDay->modify('+1 day');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->days;
|
|
|
|
}
|
|
|
|
|
2020-10-22 16:31:54 +02:00
|
|
|
public function getPreviousMonth(): Month
|
|
|
|
{
|
|
|
|
$previousMonth = $this->month - 1;
|
|
|
|
$previousYear = $this->year;
|
|
|
|
|
|
|
|
if ($previousMonth <= 0) {
|
|
|
|
$previousMonth = 12;
|
|
|
|
--$previousYear;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new self(
|
|
|
|
$previousMonth,
|
|
|
|
$previousYear
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNextMonth(): Month
|
|
|
|
{
|
|
|
|
$nextMonth = $this->month + 1;
|
|
|
|
$nextYear = $this->year;
|
|
|
|
|
|
|
|
if ($nextMonth > 12) {
|
|
|
|
$nextMonth = 1;
|
|
|
|
++$nextYear;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new self(
|
|
|
|
$nextMonth,
|
|
|
|
$nextYear
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAsUrlArgument(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'month' => $this->month,
|
|
|
|
'year' => $this->year,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDateTimeInstance(): \DateTimeImmutable
|
|
|
|
{
|
|
|
|
return new \DateTimeImmutable($this->year . '-' . $this->month . '-01');
|
|
|
|
}
|
|
|
|
}
|