/**
  * Performs ical updates on given events.
  *
  * @param array ical and event properties to update. See ical_sync::get_updates().
  * @return array List of event ids.
  */
 private function _perform_updates($updates)
 {
     $event_ids = array();
     $num_created = 0;
     $num_updated = 0;
     foreach ($updates as $update) {
         // local event -> update event
         if (isset($update["local_event"])) {
             // let edit_event() do all the magic
             if (parent::edit_event($update["remote_event"] + (array) $update["local_event"])) {
                 $event_id = $update["local_event"]["id"];
                 array_push($event_ids, $event_id);
                 $num_updated++;
                 self::debug_log("Updated event \"{$event_id}\".");
             } else {
                 self::debug_log("Could not perform event update: " . print_r($update, true));
             }
         } else {
             $event_id = parent::new_event($update["remote_event"]);
             if ($event_id) {
                 array_push($event_ids, $event_id);
                 $num_created++;
                 self::debug_log("Created event \"{$event_id}\".");
             } else {
                 self::debug_log("Could not perform event creation: " . print_r($update, true));
             }
         }
     }
     self::debug_log("Created {$num_created} new events, updated {$num_updated} event.");
     return $event_ids;
 }
 /**
  * Update the event entry with the given data and sync with caldav server.
  *
  * @param array Hash array with event properties
  * @param array Internal use only, filled with non-modified event if this is second try after a calendar sync was enforced first.
  * @see calendar_driver::edit_event()
  */
 public function edit_event($event, $old_event = null)
 {
     if ($event['_savemode'] == 'new') {
         $event['uid'] = $this->cal->generate_uid();
         return $this->new_event($event);
     } else {
         $event = $this->_get_id($event);
         $sync_enforced = $old_event != null;
         $event_id = (int) $event['id'];
         $cal_id = $event['calendar'];
         if ($old_event == null) {
             $old_event = parent::get_master($event);
             if ($old_event['categories'] && is_string($old_event['categories'])) {
                 $old_event['categories'] = explode(',', $old_event['categories']);
             }
             $old_event = $this->_save_preprocess($old_event);
         }
         if (parent::edit_event($event)) {
             // Get updates event and push to caldav.
             $event = parent::get_master(array('id' => $event_id));
             if ($event['categories'] && is_string($event['categories'])) {
                 $event['categories'] = explode(',', $event['categories']);
             }
             $event = $this->_save_preprocess($event);
             $sync_client = $this->sync_clients[$cal_id];
             $props_id = $event['current']['recurrence_id'] ? (int) $event['current']['recurrence_id'] : $event_id;
             $props = $this->_get_caldav_props($props_id, self::OBJ_TYPE_VEVENT);
             if (is_array($props)) {
                 $success = $sync_client->update_event($event, $props);
             } else {
                 $success = false;
             }
             if ($success === true) {
                 self::debug_log("Successfully updated event \"{$event_id}\".");
                 // Trigger calendar sync to update ctags and etags.
                 $this->_sync_calendar($cal_id);
                 return true;
             } else {
                 if (!$success && !$sync_enforced) {
                     self::debug_log("Event \"{$event_id}\", tag \"" . $props["tag"] . "\" not up to date, will update calendar first ...");
                     $this->_sync_calendar($cal_id, true);
                     return $this->edit_event($event, $old_event);
                     // Re-try after re-sync
                 } else {
                     self::debug_log("Unkown error while updating caldav event, undo updating local event \"{$event_id}\"!");
                     parent::edit_event($old_event);
                     return false;
                 }
             }
         }
     }
     return false;
 }