1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please read the |
7
|
|
|
* LICENSE.txt file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace DERHANSEN\SfEventMgt\Service; |
11
|
|
|
|
12
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Event; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* CalendarService |
16
|
|
|
*/ |
17
|
|
|
class CalendarService |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Returns an array with weeks/days for the calendar view |
21
|
|
|
* |
22
|
|
|
* @param int $month |
23
|
|
|
* @param int $year |
24
|
|
|
* @param int $today |
25
|
|
|
* @param int $firstDayOfWeek |
26
|
|
|
* @param array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface $events |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function getCalendarArray($month, $year, $today, $firstDayOfWeek = 0, $events = null) |
30
|
|
|
{ |
31
|
|
|
$weeks = []; |
32
|
|
|
$dateRange = $this->getCalendarDateRange($month, $year, $firstDayOfWeek); |
33
|
|
|
$currentDay = $dateRange['firstDayOfCalendar']; |
34
|
|
|
while ($currentDay <= $dateRange['lastDayOfCalendar']) { |
35
|
|
|
$week = []; |
36
|
|
|
for ($d = 0; $d < 7; $d++) { |
37
|
|
|
$day = []; |
38
|
|
|
$day['timestamp'] = $currentDay; |
39
|
|
|
$day['day'] = (int)date('j', $currentDay); |
40
|
|
|
$day['month'] = (int)date('n', $currentDay); |
41
|
|
|
$day['weekNumber'] = (int)date('W', $currentDay); |
42
|
|
|
$day['isCurrentMonth'] = $day['month'] === (int)$month; |
43
|
|
|
$day['isCurrentDay'] = date('Ymd', $today) === date('Ymd', $day['timestamp']); |
44
|
|
|
if ($events) { |
45
|
|
|
$searchDay = new \DateTime(); |
46
|
|
|
$searchDay->setTimestamp($currentDay); |
47
|
|
|
$day['events'] = $this->getEventsForDay($events, $searchDay); |
48
|
|
|
} |
49
|
|
|
$currentDay = strtotime('+1 day', $currentDay); |
50
|
|
|
$week[] = $day; |
51
|
|
|
} |
52
|
|
|
$weeks[] = $week; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $weeks; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns an array with meta information about the calendar date range for the month of the given year |
60
|
|
|
* respecting the firstDayOfWeek setting |
61
|
|
|
* |
62
|
|
|
* @param int $month |
63
|
|
|
* @param int $year |
64
|
|
|
* @param int $firstDayOfWeek |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function getCalendarDateRange($month, $year, $firstDayOfWeek = 0) |
68
|
|
|
{ |
69
|
|
|
$firstDayOfMonth = mktime(0, 0, 0, $month, 1, $year); |
70
|
|
|
$dayOfWeekOfFirstDay = (int)date('w', $firstDayOfMonth); |
71
|
|
|
$firstDayOfCalendarOffset = 1 - $dayOfWeekOfFirstDay + $firstDayOfWeek; |
72
|
|
|
if ($firstDayOfCalendarOffset > 1) { |
73
|
|
|
$firstDayOfCalendarOffset -= 7; |
74
|
|
|
} |
75
|
|
|
$firstDayOfCalendar = mktime(0, 0, 0, $month, 0 + $firstDayOfCalendarOffset, $year); |
76
|
|
|
|
77
|
|
|
$lastDayOfMonth = mktime(0, 0, 0, $month + 1, 0, $year); |
78
|
|
|
$dayOfWeekOfLastDay = (int)date('w', $lastDayOfMonth); |
79
|
|
|
$lastDayOfCalendarOffset = 6 - $dayOfWeekOfLastDay + $firstDayOfWeek; |
80
|
|
|
if ($dayOfWeekOfLastDay === 0 && $firstDayOfWeek === 1) { |
81
|
|
|
$lastDayOfCalendarOffset = 0; |
82
|
|
|
} |
83
|
|
|
$lastDayOfCalendar = strtotime('+' . $lastDayOfCalendarOffset . ' days', $lastDayOfMonth); |
84
|
|
|
|
85
|
|
|
return [ |
86
|
|
|
'firstDayOfMonth' => $firstDayOfMonth, |
87
|
|
|
'lastDayOfMonth' => $lastDayOfMonth, |
88
|
|
|
'firstDayOfCalendar' => $firstDayOfCalendar, |
89
|
|
|
'lastDayOfCalendar' => $lastDayOfCalendar |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns an array of events for the given day |
95
|
|
|
* |
96
|
|
|
* @param array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface $events |
97
|
|
|
* @param \DateTime $currentDay |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
protected function getEventsForDay($events, $currentDay) |
101
|
|
|
{ |
102
|
|
|
$foundEvents = []; |
103
|
|
|
/** @var Event $event */ |
104
|
|
|
foreach ($events as $event) { |
105
|
|
|
$eventBeginDate = $event->getStartdate()->format('Y-m-d'); |
106
|
|
|
$day = date('Y-m-d', $currentDay->getTimestamp()); |
107
|
|
|
if ($event->getEnddate() === null) { |
108
|
|
|
if ($eventBeginDate === $day) { |
109
|
|
|
$foundEvents[] = $event; |
110
|
|
|
} |
111
|
|
|
} else { |
112
|
|
|
$eventEndDate = clone $event->getEnddate(); |
113
|
|
|
$eventEndDate->setTime(23, 59, 59); |
114
|
|
|
$eventBeginDate = clone $event->getStartdate(); |
115
|
|
|
$eventBeginDate->setTime(0, 0); |
116
|
|
|
$currentDay->setTime(0, 0); |
117
|
|
|
if ($eventBeginDate <= $currentDay && $eventEndDate >= $currentDay) { |
118
|
|
|
$foundEvents[] = $event; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $foundEvents; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns a date configuration for the given modifier |
128
|
|
|
* |
129
|
|
|
* @param int $month |
130
|
|
|
* @param int $year |
131
|
|
|
* @param string $modifier |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function getDateConfig($month, $year, $modifier = '') |
135
|
|
|
{ |
136
|
|
|
$date = \DateTime::createFromFormat('d.m.Y', sprintf('1.%s.%s', $month, $year)); |
137
|
|
|
$date->setTime(0, 0, 0); |
138
|
|
|
if (!empty($modifier)) { |
139
|
|
|
$date->modify($modifier); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return [ |
143
|
|
|
'date' => $date, |
144
|
|
|
'month' => (int)$date->format('n'), |
145
|
|
|
'year' => (int)$date->format('Y') |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|