/**
  * Handle saving / updating multiple events;
  *
  * @param array $events the Facebook events to save.
  *
  * @param int $timestamp the timestamp used to query events.
  *
  * @return array the result of the operation.
  */
 private function save_events(array $events, $timestamp)
 {
     $return = array('type' => '', 'errors' => FALSE, 'events_inserted' => 0, 'events_updated' => 0, 'events_deleted' => 0);
     // We set the type that it's either the nice name of the tape or the name of the user if we have only one id.
     if (count($this->_ids) > 1) {
         $return['type'] = $this->get_type_printable_text($this->_type);
     } elseif (count($this->_ids) === 1) {
         $fgo = Ai1ec_Facebook_Factory::get_facebook_graph_object($this->_ids[0]);
         $return['type'] = $fgo->get_user_name();
     }
     // Get the id of the event which are already in the db.
     $events_in_the_db = $this->get_events_from_user_events_table_for_collection($timestamp);
     // Flip the events. We are only interested in knowing if an event we fetched from facebook was already in the db. If we flip we can do the check with isset().
     $events_in_the_db = array_flip($events_in_the_db);
     // Delete th events so that later we can check if the one we just get are referenced.
     $this->delete_events_from_user_events_table_for_collection($timestamp);
     foreach ($events as $event) {
         // Check if the event is already present in the db.
         $post_id = $this->get_post_id_from_eid($event['eid']);
         // Save data in the user_events table
         $_curr_evt_start = Ai1ec_Facebook_Event::convert_facebook_time_to_gmt_timestamp($event['start_time'], $this->_facebook_user->get_timezone());
         if ($_curr_evt_start <= 0) {
             $return['errors'] = TRUE;
             continue;
             // ignore event with unknown start time
         }
         $table_user_update_ok = $this->handle_saving_in_user_events_table($event['facebook_user'], $event['eid'], $_curr_evt_start);
         // Unset the event if it was present from the event which were already in the db.
         if (isset($events_in_the_db[$event['eid']])) {
             unset($events_in_the_db[$event['eid']]);
         }
         // If something goes wrong signal the errors.
         if (!$table_user_update_ok) {
             $return['errors'] = TRUE;
         }
         try {
             // If the post id wasn't found, create a new event.
             if ($post_id === NULL) {
                 $this->save_event($event, $this->_facebook_user->get_timezone());
                 $return['events_inserted']++;
                 // Otherwise update it.
             } else {
                 $this->update_event($event, $this->_facebook_user->get_timezone(), $post_id);
                 $return['events_updated']++;
             }
         } catch (Exception $e) {
             // There is nothing we can do. Something must be wrong with the db. Maybe it's just related to a certain event while other went well.
             $return['errors'] = TRUE;
         }
     }
     // If not every event has been unset, we should delete them.
     if (count($events_in_the_db)) {
         $deleted = $this->delete_events_not_referenced_by_facebook_users(array_keys($events_in_the_db));
         $return['events_deleted'] = $deleted;
     }
     return $return;
 }