forked from staxDB/humhub-modules-external-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncUtils.php
220 lines (193 loc) · 7.79 KB
/
SyncUtils.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
<?php
namespace humhub\modules\external_calendar;
use ICal\Event;
use Yii;
use humhub\modules\content\models\Content;
use humhub\modules\external_calendar\models\ExternalCalendarEntry;
use humhub\modules\external_calendar\models\ExternalCalendar;
require_once(Yii::$app->getModule('external_calendar')->basePath . '/vendors/johngrogg/ics-parser/src/ICal/Event.php');
require_once(Yii::$app->getModule('external_calendar')->basePath . '/vendors/johngrogg/ics-parser/src/ICal/ICal.php');
use ICal\ICal;
use yii\helpers\ArrayHelper;
/**
* Description of SyncUtils
*
* @author davidborn
*/
class SyncUtils
{
public static function createICal($url)
{
if (!isset($url)) {
return false;
}
try {
// load ical and parse it
$ical = new ICal($url, array(
'defaultSpan' => 2, // Default value
'defaultTimeZone' => Yii::$app->timeZone,
'defaultWeekStart' => 'MO', // Default value
'skipRecurrence' => false, // Default value
'useTimeZoneWithRRules' => false, // Default value
));
return $ical;
} catch (\Exception $e) {
return false;
}
}
/**
* @param array $events
* @param ExternalCalendar $calendar
* @return bool
*/
public static function checkAndSubmitModels(array &$events, ExternalCalendar &$calendar)
{
if (!isset($events) && !isset($calendar)) {
return false;
}
$dbModels = $calendar->externalCalendarEntries;
// we need to filter recurring events
$recurringEvents = self::filterRecurringEvents($events);
// models is an array of ExternalCalendarEntry-Models
foreach ($events as $eventKey => $event) {
// first: check if last_modified is set. If not - set it to DateTime('now')
$last_modified = self::getLastModified($event);
foreach ($dbModels as $dbModelKey => $dbModel) {
if (($dbModel->uid == $event->uid) && ($dbModel->last_modified >= $last_modified)) {
// found, but nothing to change
unset($events[$eventKey]);
unset($dbModels[$dbModelKey]);
} elseif ($dbModel->uid == $event->uid) {
// found & update $dbModel
$dbModel->setByEvent($event);
$dbModel->save();
unset($events[$eventKey]);
unset($dbModels[$dbModelKey]); // can't do this here, because of recurring events
} else {
continue;
}
}
}
// handle recurring events
foreach ($recurringEvents as $eventKey => $event) {
// first create entryModel out of event
$entryModel = self::getModel($event, $calendar);
// then check list of remaining dbModels for this event
foreach ($dbModels as $dbModelKey => $dbModel) {
// compare uid & start_datetime
if (($dbModel->uid == $entryModel->uid) && ($dbModel->start_datetime == $entryModel->start_datetime)) {
// found & update $dbModel
$dbModel->setByEvent($event);
// $dbModel->content->refresh(); // refresh updated_at
$dbModel->save();
unset($recurringEvents[$eventKey]);
unset($dbModels[$dbModelKey]); // can't do this here, because of recurring events
continue;
} else {
continue;
}
}
unset($entryModel);
}
foreach ($recurringEvents as $newModelKey => $newModel) {
$model = self::getModel($newModel, $calendar);
// recurring event not in db found --> create new
$calendar->link('externalCalendarEntries', $model);
unset($recurringEvents[$newModelKey]);
}
// link new values
foreach ($events as $eventKey => $event) {
$model = self::getModel($event, $calendar);
if ($model != false) {
$calendar->link('externalCalendarEntries', $model);
unset($events[$eventKey]);
} else { // error while creating model... skip event
unset($events[$eventKey]); // if we want to test if there was an error, comment this and var_dump($events)
continue;
}
}
// finally delete items from db
foreach ($dbModels as $modelKey => $model) {
$calendar->unlink('externalCalendarEntries', $model, true);
unset($dbModels[$modelKey]);
}
return true;
}
public static function getModel($event, ExternalCalendar $calendar)
{
// create ExternalCalendarEntry-model without safe
if (!isset($events) && !isset($calendar)) {
return false;
}
$model = new ExternalCalendarEntry();
$model->setByEvent($event);
// add contentContainer of ExternalCalendar-Model
$model->content->setContainer($calendar->content->container);
$model->content->created_by = $calendar->content->created_by;
// $model->content->created_at = $model->dtstamp; // set created_at to original created timestamp
$model->content->visibility = ($calendar->public) ? Content::VISIBILITY_PUBLIC : Content::VISIBILITY_PRIVATE;
return $model;
}
public static function getEvents(ExternalCalendar $calendarModel, ICal $ical)
{
$events = [];
if ($calendarModel->event_mode === ExternalCalendar::EVENT_MODE_CURRENT_MONTH) {
$start = new \DateTime('first day of this month');
$start = $start->format('Y-m-d 00:00:00');
$end = new \DateTime('last day of this month');
$end = $end->format('Y-m-d 23:59:59');
$events = $ical->eventsFromRange($start, $end);
} else {
$events = $ical->events();
}
return $events;
}
protected static function getLastModified(Event $event)
{
$last_modified = null;
// first check if lastmodified is set or emtpy... for comparison...
if (!isset($event->lastmodified) || $event->lastmodified == null) {
if (isset($event->last_modified) && $event->last_modified != null) {
$last_modified = CalendarUtils::formatDateTimeToString($event->last_modified);
} else {
$now = new \DateTime('now');
$last_modified = $now->format('Y-m-d H:i:s');
unset($now);
}
} else {
$last_modified = CalendarUtils::formatDateTimeToString($event->lastmodified);
}
return $last_modified;
}
/**
* @param array $events
* @return array
*/
protected static function filterRecurringEvents(array &$events)
{
$recurringEvents = [];
$id_array = [];
foreach ($events as $eventKey => $event) {
if (!in_array($event->uid, $id_array)) {
array_push($id_array, $event->uid);
} else {
array_push($recurringEvents, $event);
unset($events[$eventKey]);
}
}
// now we have filtered the double events
// but we also need the original ones
foreach ($events as $eventKey => $event) {
foreach ($recurringEvents as $key => $val) {
if ($event->uid == $val->uid) {
array_push($recurringEvents, $event);
unset($events[$eventKey]);
} else {
// array_push($recurringEvents, $event);
}
}
}
unset($id_array);
return $recurringEvents;
}
}