Skip to content

Commit daf0997

Browse files
Add better support for syncing recurring caldav events
1 parent dd092d4 commit daf0997

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

application/controllers/Caldav.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,13 @@ public static function sync(string $provider_id): void
132132

133133
$sync_future_days = $provider['settings']['sync_future_days'];
134134

135-
$start_date_time = date('Y-m-d H:i:s', strtotime('-' . $sync_past_days . ' days'));
135+
$start_date_time_object = new DateTime('-' . $sync_past_days . ' days');
136+
$start_date_time_object->setTime(0, 0);
137+
$start_date_time = $start_date_time_object->format('Y-m-d H:i:s');
136138

137-
$end_date_time = date('Y-m-d H:i:s', strtotime('+' . $sync_future_days . ' days'));
139+
$end_date_time_object = new DateTime('+' . $sync_future_days . ' days');
140+
$end_date_time_object->setTime(23, 59, 59);
141+
$end_date_time = $end_date_time_object->format('Y-m-d H:i:s');
138142

139143
$where = [
140144
'start_datetime >=' => $start_date_time,
@@ -253,6 +257,24 @@ public static function sync(string $provider_id): void
253257
continue;
254258
}
255259

260+
$matching_unavailability = $CI->unavailabilities_model
261+
->query()
262+
->where([
263+
'start_datetime' => $caldav_event['start_datetime'],
264+
'end_datetime' => $caldav_event['end_datetime'],
265+
'notes' => $caldav_event['summary'] . ' ' . $caldav_event['description'],
266+
'id_users_provider' => $provider_id,
267+
])
268+
->get()
269+
->row_array();
270+
271+
if ($matching_unavailability) {
272+
// Update the ID of the matching unavailability record.
273+
$matching_unavailability['id_caldav_calendar'] = $caldav_event['id'];
274+
$CI->unavailabilities_model->save($matching_unavailability);
275+
continue;
276+
}
277+
256278
// Record doesn't exist in the Easy!Appointments, so add the event now.
257279

258280
$local_event = [

application/libraries/Caldav_sync.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ private function convert_caldav_event_to_array_event(VEvent $vevent, DateTimeZon
524524
$event_id = (string) $vevent->UID;
525525

526526
if ($is_recurring_event) {
527-
$event_id .= '-RECURRENCE-' . random_string();
527+
$event_id .= '-RECURRENCE-' . $caldav_start_date_time;
528528
}
529529

530530
// Return the converted event

application/models/Appointments_model.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ public function delete_caldav_recurring_events(string $start_date_time, string $
352352
$this->db
353353
->where('start_datetime >=', $start_date_time)
354354
->where('end_datetime <=', $end_date_time)
355-
->like('id_caldav_calendar', '%RECURRENCE%')
355+
->where('is_unavailability', true)
356+
->like('id_caldav_calendar', 'RECURRENCE')
356357
->delete('appointments');
357358
}
358359

0 commit comments

Comments
 (0)