forked from staxDB/humhub-modules-external-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Events.php
124 lines (107 loc) · 3.99 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
<?php
namespace humhub\modules\external_calendar;
use humhub\modules\external_calendar\models\ExternalCalendarEntry;
use Yii;
use yii\helpers\Url;
use yii\base\Object;
use humhub\modules\external_calendar\models\ExternalCalendar;
use humhub\modules\external_calendar\integration\calendar\CalendarExtension;
class Events extends Object
{
/**
* @param $event \humhub\modules\calendar\interfaces\CalendarItemTypesEvent
* @return mixed
*/
public static function onGetCalendarItemTypes($event)
{
$contentContainer = $event->contentContainer;
if (!$contentContainer || $contentContainer->isModuleEnabled('external_calendar')) {
CalendarExtension::addItemTypes($event);
}
}
/**
* @param $event
*/
public static function onFindCalendarItems($event)
{
$contentContainer = $event->contentContainer;
if (!$contentContainer || $contentContainer->isModuleEnabled('external_calendar')) {
CalendarExtension::addItems($event);
}
}
/**
* Defines what to do if admin menu is initialized.
*
* @param $event
*/
public static function onAdminMenuInit($event)
{
$event->sender->addItem(array(
'label' => "external_calendar",
'url' => Url::to(['/external_calendar/admin']),
'group' => 'manage',
'icon' => '<i class="fa fa-certificate" style="color: #6fdbe8;"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'external_calendar' && Yii::$app->controller->id == 'admin'),
'sortOrder' => 99999,
));
}
/**
* Defines what to do if cron runs.
*
* @param $event
* @return void
*/
public static function onCronRun($event)
{
$calendarModels = ExternalCalendar::find()->all();
foreach ($calendarModels as $calendarModel) {
if (!isset($calendarModel)) {
continue;
}
if (Yii::$app->controller->action->id == 'hourly') {
if ($calendarModel->sync_mode == ExternalCalendar::SYNC_MODE_NONE || $calendarModel->sync_mode == ExternalCalendar::SYNC_MODE_DAILY) {
continue;
}
} elseif (Yii::$app->controller->action->id == 'daily') {
if ($calendarModel->sync_mode == ExternalCalendar::SYNC_MODE_NONE || $calendarModel->sync_mode == ExternalCalendar::SYNC_MODE_HOURLY) {
continue;
}
}
$ical = SyncUtils::createICal($calendarModel->url);
if (!$ical) {
continue;
}
// add info to CalendarModel
$calendarModel->addAttributes($ical);
$calendarModel->save();
// check events
if ($ical->hasEvents()) {
// get formatted array
$events = SyncUtils::getEvents($calendarModel, $ical);
$result = SyncUtils::checkAndSubmitModels($events, $calendarModel);
if (!$result) {
continue;
}
}
}
return;
}
/**
* Callback to validate module database records.
*
* @param Event $event
* @throws \Exception
*/
public static function onIntegrityCheck($event)
{
$integrityController = $event->sender;
$integrityController->showTestHeadline("External Calendar Module - Entries (" . ExternalCalendarEntry::find()->count() . " entries)");
foreach (ExternalCalendarEntry::find()->joinWith('calendar')->all() as $entry) {
if ($entry->calendar === null) {
if ($integrityController->showFix("Deleting external calendar entry id " . $entry->id . " without existing calendar!")) {
$entry->delete();
}
}
}
}
}