/
controller.php
261 lines (232 loc) · 8.53 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
namespace Concrete\Block\CalendarEvent;
use Concrete\Core\Attribute\Key\CollectionKey;
use Concrete\Core\Attribute\Key\EventKey;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Calendar\Calendar;
use Concrete\Core\Calendar\CalendarServiceProvider;
use Concrete\Core\Calendar\Event\Event;
use Concrete\Core\Calendar\Event\EventOccurrence;
use Concrete\Core\Entity\Calendar\CalendarEvent;
use Concrete\Core\Entity\Calendar\CalendarEventVersionOccurrence;
use Concrete\Core\Feature\Features;
use Concrete\Core\Feature\UsesFeatureInterface;
use Concrete\Core\Page\Page;
use Concrete\Core\Url\SeoCanonical;
defined('C5_EXECUTE') or die('Access Denied.');
class Controller extends BlockController implements UsesFeatureInterface
{
/**
* @var int
*/
protected $btInterfaceWidth = 550;
/**
* @var int
*/
protected $btInterfaceHeight = 400;
/**
* @var string
*/
protected $btTable = 'btCalendarEvent';
/**
* @var string
*/
protected $mode = 'S';
/**
* @var string
*/
protected $calendarEventAttributeKeyHandle;
/**
* @var int|null
*/
protected $eventID;
/**
* {@inheritdoc}
*/
public function getRequiredFeatures(): array
{
return [
Features::CALENDAR,
];
}
/**
* {@inheritdoc}
*/
public function getBlockTypeDescription()
{
return t('Displays a calendar event on a page.');
}
/**
* {@inheritdoc}
*/
public function getBlockTypeName()
{
return t('Calendar Event');
}
/**
* @return void
*/
public function add()
{
$this->edit();
}
/**
* @return void
*/
public function edit()
{
/** @var \Concrete\Core\Entity\Attribute\Key\EventKey[] $eventKeys */
/** @phpstan-ignore-next-line */
$eventKeys = EventKey::getList();
$calendars = ['' => t('** Choose a Calendar')];
$calendarEventPageKeys = ['' => t('** Choose an Event')];
/** @phpstan-ignore-next-line */
foreach (CollectionKey::getList() as $ak) {
if ($ak->getAttributeTypeHandle() == 'calendar_event') {
$calendarEventPageKeys[$ak->getAttributeKeyHandle()] = $ak->getAttributeKeyDisplayName();
}
}
/** @phpstan-ignore-next-line */
foreach (Calendar::getList() as $calendar) {
$calendars[$calendar->getID()] = $calendar->getName();
}
$displayEventAttributes = [];
if (isset($this->displayEventAttributes)) {
$displayEventAttributes = json_decode($this->displayEventAttributes);
}
$this->set('calendarEventPageKeys', $calendarEventPageKeys);
$this->set('eventKeys', $eventKeys);
$this->set('calendars', $calendars);
$this->set('displayEventAttributes', $displayEventAttributes);
}
/**
* @param array<string,mixed> $data
*
* @return void
*/
public function save($data)
{
$data['calendarID'] = isset($data['calendarID']) ? (int) ($data['calendarID']) : 0;
$data['eventID'] = isset($data['eventID']) ? (int) ($data['eventID']) : 0;
$data['allowExport'] = isset($data['allowExport']) && $data['allowExport'] ? 1 : 0;
$data['displayEventName'] = isset($data['displayEventName']) && $data['displayEventName'] ? 1 : 0;
$data['displayEventDate'] = isset($data['displayEventDate']) && $data['displayEventDate'] ? 1 : 0;
$data['displayEventDescription'] = isset($data['displayEventDescription']) && $data['displayEventDescription'] ? 1 : 0;
$data['enableLinkToPage'] = isset($data['enableLinkToPage']) && $data['enableLinkToPage'] ? 1 : 0;
$attributes = [];
if (isset($data['displayEventAttributes']) && is_array($data['displayEventAttributes'])) {
$attributes = $data['displayEventAttributes'];
}
$data['displayEventAttributes'] = json_encode($attributes);
parent::save($data);
}
/**
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @return CalendarEventVersionOccurrence|null
*/
public function getOccurrence()
{
$event = $this->getEvent();
if (is_object($event)) {
if ($this->request->query->has('occurrenceID')) {
/** @var CalendarEventVersionOccurrence|null $occurrence */
/** @phpstan-ignore-next-line */
$occurrence = EventOccurrence::getByID($this->request->query->get('occurrenceID'));
if ($occurrence) {
if ($occurrence->getEvent() && $occurrence->getEvent()->getID() == $event->getID()) {
/** @phpstan-ignore-next-line */
if ($event->isApproved()) {
return $occurrence;
}
}
/** @phpstan-ignore-next-line */
if ($occurrence->getEvent() && Event::isRelatedTo($occurrence->getEvent(), $event)) {
// this is a temporary hack. We need to ultimately make this make sure that
// the event matches –but sometimes our events don't match because we imported them as series
// events and not as occurrences of one event.
// @TODO delete this code when we no longer need it and all related events are migrated into multidate events.
/** @phpstan-ignore-next-line */
if ($event->isApproved()) {
return $occurrence;
}
}
}
}
$date = $this->app->make('date')->date('Y-m-d', null, 'app');
$time = $this->app->make('date')->toDateTime($date . ' 00:00:00', 'GMT', 'app')->getTimestamp();
$list = $event->getOccurrenceList();
$list->filterByStartTimeAfter($time);
$list->setItemsPerPage(1);
$results = $list->getResults();
if (!$results) {
$list = $event->getOccurrenceList();
$list->sortBy('startTime', 'desc');
$results = $list->getResults();
}
return $results[0];
}
if ($this->request->query->has('occurrenceID') && $this->mode == 'R') {
// request mode
/** @phpstan-ignore-next-line */
$occurrence = EventOccurrence::getByID($this->request->query->get('occurrenceID'));
if ($occurrence) {
$event = $occurrence->getEvent();
if ($event && $event->isApproved()) {
return $occurrence;
}
}
}
return null;
}
public function on_start()
{
if ($this->request->query->has('occurrenceID') && $this->mode == 'R') {
/** @var SeoCanonical $seo */
$seo = $this->app->make(SeoCanonical::class);
$seo->addIncludedQuerystringParameter('occurrenceID');
}
}
/**
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @return void
*/
public function view()
{
$this->set('event', $this->getEvent());
$displayEventAttributes = [];
if (isset($this->displayEventAttributes)) {
$displayEventAttributes = json_decode($this->displayEventAttributes);
}
$this->set('displayEventAttributes', $displayEventAttributes);
$occurrence = $this->getOccurrence();
$this->set('occurrence', $occurrence);
$provider = $this->app->make(CalendarServiceProvider::class);
if ($occurrence) {
$this->set('event', $occurrence->getEvent());
$linkFormatter = $provider->getLinkFormatter();
$eventOccurrenceLink = $linkFormatter->getEventOccurrenceFrontendViewLink($occurrence);
$this->set('eventOccurrenceLink', $eventOccurrenceLink);
}
$formatter = $provider->getDateFormatter();
$this->set('formatter', $formatter);
}
/**
* @return CalendarEvent|null
*/
protected function getEvent()
{
$event = null;
if ($this->mode == 'P') {
$page = Page::getCurrentPage();
$event = $page->getAttribute($this->calendarEventAttributeKeyHandle);
} else {
if ($this->eventID) {
/** @phpstan-ignore-next-line */
$event = Event::getByID($this->eventID);
}
}
return $event;
}
}