-
Notifications
You must be signed in to change notification settings - Fork 46
/
Events.php
414 lines (362 loc) · 14.6 KB
/
Events.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
namespace humhub\modules\calendar;
use DateTime;
use humhub\modules\calendar\helpers\RecurrenceHelper;
use humhub\modules\calendar\models\CalendarEntry;
use humhub\modules\calendar\models\CalendarEntryParticipant;
use humhub\modules\calendar\models\MenuSettings;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use humhub\modules\calendar\interfaces\event\EditableEventIF;
use humhub\modules\calendar\interfaces\event\CalendarItemTypesEvent;
use humhub\modules\calendar\interfaces\recurrence\RecurrentEventIF;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\calendar\helpers\CalendarUtils;
use humhub\modules\calendar\interfaces\event\CalendarEventIF;
use humhub\modules\calendar\integration\BirthdayCalendar;
use humhub\modules\calendar\interfaces\reminder\CalendarEventReminderIF;
use humhub\modules\calendar\models\reminder\ReminderService;
use humhub\modules\calendar\models\reminder\CalendarReminder;
use humhub\modules\calendar\models\reminder\CalendarReminderSent;
use humhub\modules\calendar\models\SnippetModuleSettings;
use humhub\modules\calendar\widgets\DownloadIcsLink;
use humhub\modules\calendar\interfaces\CalendarService;
use humhub\modules\calendar\widgets\ReminderLink;
use humhub\modules\calendar\widgets\UpcomingEvents;
use humhub\modules\content\models\Content;
use humhub\modules\calendar\helpers\Url;
use Yii;
use yii\db\StaleObjectException;
use yii\helpers\Console;
/**
* Description of CalendarEvents
*
* @author luke
*/
class Events
{
/**
* @inheritdoc
*/
public static function onBeforeRequest()
{
try {
static::registerAutoloader();
Yii::$app->getModule('calendar')->set(CalendarService::class, ['class' => CalendarService::class]);
} catch (\Throwable $e) {
Yii::error($e);
}
}
/**
* Register composer autoloader when Reader not found
*/
public static function registerAutoloader()
{
require Yii::getAlias('@calendar/vendor/autoload.php');
}
/**
* @param $event CalendarItemTypesEvent
* @return mixed
*/
public static function onGetCalendarItemTypes($event)
{
try {
BirthdayCalendar::addItemTypes($event);
} catch (\Throwable $e) {
Yii::error($e);
}
}
/**
* @param $event \humhub\modules\calendar\interfaces\event\CalendarItemsEvent;
* @throws \Throwable
*/
public static function onFindCalendarItems($event)
{
try {
BirthdayCalendar::addItems($event);
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onTopMenuInit($event)
{
try {
if (SnippetModuleSettings::instance()->showGlobalCalendarItems() &&
MenuSettings::instance()->show) {
$event->sender->addItem([
'label' => Yii::t('CalendarModule.base', 'Calendar'),
'url' => Url::toGlobalCalendar(),
'icon' => '<i class="fa fa-calendar"></i>',
'isActive' => (Yii::$app->controller->module
&& Yii::$app->controller->module->id == 'calendar'
&& Yii::$app->controller->id == 'global'),
'sortOrder' => MenuSettings::instance()->sortOrder,
]);
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onSpaceMenuInit($event)
{
try {
/* @var Space $space */
$space = $event->sender->space;
if ($space->moduleManager->isEnabled('calendar')) {
$event->sender->addItem([
'label' => Yii::t('CalendarModule.base', 'Calendar'),
'group' => 'modules',
'url' => Url::toCalendar($space),
'icon' => '<i class="fa fa-calendar"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'calendar'),
]);
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onProfileMenuInit($event)
{
try {
/* @var User $user */
$user = $event->sender->user;
if ($user->moduleManager->isEnabled('calendar')) {
$event->sender->addItem([
'label' => Yii::t('CalendarModule.base', 'Calendar'),
'url' => Url::toCalendar($user),
'icon' => '<i class="fa fa-calendar"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'calendar'),
]);
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onSpaceSidebarInit($event)
{
try {
/* @var Space $space */
$space = $event->sender->space;
$settings = SnippetModuleSettings::instantiate();
if ($space->moduleManager->isEnabled('calendar')) {
if ($settings->showUpcomingEventsSnippet()) {
$event->sender->addWidget(UpcomingEvents::class, ['contentContainer' => $space], ['sortOrder' => $settings->upcomingEventsSnippetSortOrder]);
}
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onDashboardSidebarInit($event)
{
try {
$settings = SnippetModuleSettings::instantiate();
if ($settings->showUpcomingEventsSnippet()) {
$event->sender->addWidget(UpcomingEvents::class, [], ['sortOrder' => $settings->upcomingEventsSnippetSortOrder]);
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onProfileSidebarInit($event)
{
try {
if (Yii::$app->user->isGuest) {
return;
}
$user = $event->sender->user;
if ($user != null) {
$settings = SnippetModuleSettings::instantiate();
if ($settings->showUpcomingEventsSnippet()) {
$event->sender->addWidget(UpcomingEvents::class, ['contentContainer' => $user], ['sortOrder' => $settings->upcomingEventsSnippetSortOrder]);
}
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onWallEntryLinks($event)
{
try {
$eventModel = static::getCalendarEvent($event->sender->object);
if (!$eventModel) {
return;
}
if ($eventModel instanceof ContentActiveRecord && $eventModel instanceof CalendarEventIF) {
$event->sender->addWidget(DownloadIcsLink::class, ['calendarEntry' => $eventModel]);
}
/* @var $eventModel CalendarEventIF */
if ($eventModel->getStartDateTime() <= new DateTime()) {
return;
}
if ($eventModel instanceof CalendarEventReminderIF && !RecurrenceHelper::isRecurrentRoot($eventModel)) {
$event->sender->addWidget(ReminderLink::class, ['entry' => $eventModel]);
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
/**
* @param $model
* @return CalendarEventIF|null
*/
private static function getCalendarEvent($model)
{
if ($model instanceof CalendarEventIF) {
return $model;
}
if (method_exists($model, 'getCalendarEvent')) {
$event = $model->getCalendarEvent();
if ($event instanceof CalendarEventIF) {
return $event;
}
}
return null;
}
public static function onRecordBeforeInsert($event)
{
try {
static::onRecordBeforeUpdate($event);
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onRecordBeforeUpdate($event)
{
try {
$model = CalendarUtils::getCalendarEvent($event->sender);
if ($model && ($model instanceof EditableEventIF)) {
/** @var $model EditableEventIF **/
if (empty($model->getUid())) {
$model->setUid(CalendarUtils::generateEventUid($model));
}
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
/**
* @param $event
* @throws \Throwable
* @throws StaleObjectException
*/
public static function onRecordBeforeDelete($event)
{
try {
$model = CalendarUtils::getCalendarEvent($event->sender);
if (!$model || !($model instanceof CalendarEventReminderIF)) {
return;
}
foreach (CalendarReminder::getEntryLevelReminder($model) as $reminder) {
$reminder->delete();
}
if ($model instanceof RecurrentEventIF) {
// When deleting duplicates we want to prevent automatic exdate settings.
if (!static::$duplicateIntegrityRun) {
$model->getRecurrenceQuery()->onDelete();
}
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static $duplicateIntegrityRun = false;
/**
* @param $event
* @throws \Throwable
* @throws StaleObjectException
*/
public static function onIntegrityCheck($event)
{
$integrityController = $event->sender;
$integrityController->showTestHeadline("Calendar Module (" . CalendarReminder::find()->count() . " reminder entries)");
foreach (CalendarReminder::find()->each() as $reminder) {
if ($reminder->isEntryLevelReminder() && !Content::findOne(['id' => $reminder->content_id])) {
if ($integrityController->showFix("Delete calendar reminder " . $reminder->id . " without existing entry relation!")) {
$reminder->delete();
}
}
}
$integrityController->showTestHeadline("Calendar Module (" . CalendarReminderSent::find()->count() . " reminder sent entries)");
foreach (CalendarReminderSent::find()->each() as $reminderSent) {
if (!Content::findOne(['id' => $reminderSent->content_id])) {
if ($integrityController->showFix("Delete calendar reminder sent" . $reminderSent->id . " without existing entry relation!")) {
$reminderSent->delete();
}
}
}
static::$duplicateIntegrityRun = true;
$duplicatedRecurrences = CalendarEntry::find()
->select('id, parent_event_id, recurrence_id, COUNT(*)')
->where('recurrence_id IS NOT NULL')
->andWhere('parent_event_id IS NOT NULL')
->groupBy('parent_event_id, recurrence_id')
->having('COUNT(*) > 1')->asArray(true);
foreach ($duplicatedRecurrences->each() as $duplicatedRecurrenceArr) {
$duplicateQuery = CalendarEntry::find()
->where(['recurrence_id' => $duplicatedRecurrenceArr['recurrence_id']])
->andWhere(['parent_event_id' => $duplicatedRecurrenceArr['parent_event_id']])
->andWhere(['<>', 'id', $duplicatedRecurrenceArr['id']]);
foreach ($duplicateQuery->each() as $duplicate) {
if (RecurrenceHelper::isRecurrentInstance($duplicate) && $duplicate->id !== $duplicatedRecurrenceArr['id']) {
if ($integrityController->showFix('Delete duplicated recurrent event instance ' . $duplicate->id . '!')) {
$duplicate->hardDelete();
}
}
}
}
static::$duplicateIntegrityRun = false;
}
/**
* Callback when a user is completely deleted.
*
* @param \yii\base\Event $event
*/
public static function onUserDelete($event)
{
$user = $event->sender;
foreach (CalendarEntryParticipant::findAll(['user_id' => $user->id]) as $participant) {
$participant->delete();
}
}
public static function onCronRun($event)
{
static::onBeforeRequest();
/* @var $module Module */
$module = Yii::$app->getModule('calendar');
$lastRunTS = $module->settings->get('lastReminderRunTS');
if (!$lastRunTS || ((time() - $lastRunTS) >= $module->getRemidnerProcessIntervalS())) {
try {
$controller = $event->sender;
$controller->stdout("Running reminder process... ");
(new ReminderService())->sendAllReminder();
$controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
} catch (\Throwable $e) {
Yii::error($e);
$controller->stdout('error.' . PHP_EOL, Console::FG_RED);
$controller->stderr("\n" . $e->getTraceAsString() . "\n", Console::BOLD);
}
$module->settings->set('lastReminderRunTS', time());
}
}
public static function onRestApiAddRules()
{
/* @var humhub\modules\rest\Module $restModule */
$restModule = Yii::$app->getModule('rest');
$restModule->addRules([
['pattern' => 'calendar/', 'route' => 'calendar/rest/calendar/find', 'verb' => ['GET', 'HEAD']],
['pattern' => 'calendar/container/<containerId:\d+>', 'route' => 'calendar/rest/calendar/find-by-container', 'verb' => ['GET', 'HEAD']],
['pattern' => 'calendar/container/<containerId:\d+>', 'route' => 'calendar/rest/calendar/delete-by-container', 'verb' => 'DELETE'],
//Calendar entry CRUD
['pattern' => 'calendar/container/<containerId:\d+>', 'route' => 'calendar/rest/calendar/create', 'verb' => 'POST'],
['pattern' => 'calendar/entry/<id:\d+>', 'route' => 'calendar/rest/calendar/view', 'verb' => ['GET', 'HEAD']],
['pattern' => 'calendar/entry/<id:\d+>', 'route' => 'calendar/rest/calendar/update', 'verb' => 'PUT'],
['pattern' => 'calendar/entry/<id:\d+>', 'route' => 'calendar/rest/calendar/delete', 'verb' => 'DELETE'],
//Calendar Entry Management
['pattern' => 'calendar/entry/<id:\d+>/upload-files', 'route' => 'calendar/rest/calendar/attach-files', 'verb' => 'POST'],
['pattern' => 'calendar/entry/<id:\d+>/remove-file/<fileId:\d+>', 'route' => 'calendar/rest/calendar/remove-file', 'verb' => 'DELETE'],
//Participate
['pattern' => 'calendar/entry/<id:\d+>/respond', 'route' => 'calendar/rest/calendar/respond', 'verb' => 'POST'],
], 'calendar');
}
}