diff --git a/plugins/calendar/calendar.php b/plugins/calendar/calendar.php --- a/plugins/calendar/calendar.php +++ b/plugins/calendar/calendar.php @@ -2990,17 +2990,7 @@ // preserve my participant status for regular updates if (empty($status)) { - $emails = $this->get_user_emails(); - foreach ($event['attendees'] as $i => $attendee) { - if ($attendee['email'] && in_array(strtolower($attendee['email']), $emails)) { - foreach ($existing['attendees'] as $j => $_attendee) { - if ($attendee['email'] == $_attendee['email']) { - $event['attendees'][$i] = $existing['attendees'][$j]; - break; - } - } - } - } + $this->lib->merge_attendees($event, $existing); } // set status=CANCELLED on CANCEL messages diff --git a/plugins/libcalendaring/libcalendaring.php b/plugins/libcalendaring/libcalendaring.php --- a/plugins/libcalendaring/libcalendaring.php +++ b/plugins/libcalendaring/libcalendaring.php @@ -1551,6 +1551,54 @@ $this->rc->output->command('plugin.expand_attendee_callback', $result); } + /** + * Merge attendees of the old and new event version + * with keeping current user and his delegatees status + * + * @param array &$new New object data + * @param array $old Old object data + */ + public function merge_attendees(&$new, $old) + { + $emails = $this->get_user_emails(); + $delegates = array(); + $attendees = array(); + + // keep attendee status of the current user + foreach ((array) $new['attendees'] as $i => $attendee) { + if (empty($attendee['email'])) { + continue; + } + + $attendees[] = $email = strtolower($attendee['email']); + + if (in_array($email, $emails)) { + foreach ($old['attendees'] as $_attendee) { + if ($attendee['email'] == $_attendee['email']) { + $new['attendees'][$i] = $_attendee; + if ($_attendee['status'] == 'DELEGATED' && ($email = $_attendee['delegated-to'])) { + $delegates[] = strtolower($email); + } + + break; + } + } + } + } + + // make sure delegated attendee is not lost + foreach ($delegates as $delegatee) { + if (!in_array($delegatee, $attendees)) { + foreach ((array) $old['attendees'] as $attendee) { + if ($attendee['email'] && ($email = strtolower($attendee['email'])) && $email == $delegatee) { + $new['attendees'][] = $attendee; + break; + } + } + } + } + } + /********* Static utility functions *********/ diff --git a/plugins/tasklist/tasklist.php b/plugins/tasklist/tasklist.php --- a/plugins/tasklist/tasklist.php +++ b/plugins/tasklist/tasklist.php @@ -2074,17 +2074,7 @@ // preserve my participant status for regular updates if (empty($status)) { - $emails = $this->lib->get_user_emails(); - foreach ($task['attendees'] as $i => $attendee) { - if ($attendee['email'] && in_array(strtolower($attendee['email']), $emails)) { - foreach ($existing['attendees'] as $j => $_attendee) { - if ($attendee['email'] == $_attendee['email']) { - $task['attendees'][$i] = $existing['attendees'][$j]; - break; - } - } - } - } + $this->lib->merge_attendees($task, $existing); } // set status=CANCELLED on CANCEL messages