/**
  * Unpack the results of a fql multy-query for venues updating the events accordingly
  *
  * @param array $events The events array
  *
  * @param array $venues the result of the fql multi query
  *
  * @param array $indexes an array that stores the index in $events of the events eid
  *
  * @return array the events arrey with the added venues
  */
 protected function merge_events_and_venues(array $events, array $venues, array $indexes)
 {
     foreach ($venues as $venue) {
         $eid = $venue['name'];
         $index = $indexes[$eid];
         $events[$index]['venue'] = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($venue['fql_result_set'][0], 'location');
     }
     return $events;
 }
 /**
  * Convert a Facebook event into a Ai1ec Event.
  *
  * @param array $facebook_event The event to convert
  *
  * @param int $user_timezone the timezone of the user which is used to convert time
  *
  * @param int $post_id the id of the wp_post if we are updating
  *
  * @return Ai1ec_Event|array an Ai1ec object if we are performing an insert or an array if we ar updating
  */
 private function convert_facebook_event_to_ai1ec_event(array $facebook_event, $user_timezone, $post_id = NULL)
 {
     global $ai1ec_events_helper;
     // Create a new calendar event.
     $event = new Ai1ec_Event();
     // Set start time and end time after converting it to GMT
     if (isset($facebook_event['start_time'])) {
         $event->start = Ai1ec_Facebook_Event::convert_facebook_time_to_gmt_timestamp($facebook_event['start_time'], $user_timezone);
     }
     if (isset($facebook_event['end_time']) && $facebook_event['end_time'] !== NULL) {
         $event->end = Ai1ec_Facebook_Event::convert_facebook_time_to_gmt_timestamp($facebook_event['end_time'], $user_timezone);
     }
     // Check if the coordinates are set.
     $coordinates_set = isset($facebook_event['venue']['longitude']) && isset($facebook_event['venue']['latitude']);
     // Save location details, defaulting to empty strings if unset.
     $event->venue = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event, 'location');
     $event->address = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event['venue'], 'street');
     $event->city = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event['venue'], 'city');
     $event->province = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event['venue'], 'state');
     $event->postal_code = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event['venue'], 'zip');
     $event->country = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event['venue'], 'country');
     // If location is not a proper Facebook venue, address information will
     // not be provided by the above fields. Instead, "location" stores the
     // address, likely with commas separating components. If there are no commas
     // then we keep it as the venue name.
     if (!$event->address) {
         if (false !== strpos($event->venue, ',')) {
             $event->address = $event->venue;
             $event->venue = '';
         }
     } else {
         // In Ai1ec, "address" property always stores the combination of all
         // address details, so concatenate them now. (City, province, postal code,
         // country are used only for data collection & their indexing potential.)
         $address = array($event->address, $event->city, $event->province, $event->postal_code, $event->country);
         $event->address = implode(', ', array_filter($address));
     }
     // We show the map if we have a valid address or we have coordinates. This is arbitrary.
     $event->show_map = $coordinates_set || $event->address;
     $data = Ai1ec_Facebook_Factory::get_facebook_graph_object($facebook_event['facebook_user'])->get_category_and_tag();
     if (!$data['map_display_enabled']) {
         $event->show_map = false;
     }
     $event->show_coordinates = $coordinates_set ? 1 : 0;
     if ($coordinates_set) {
         $event->longitude = $facebook_event['venue']['longitude'];
         $event->latitude = $facebook_event['venue']['latitude'];
     }
     // We save the user and eid.
     $event->facebook_user = $facebook_event['facebook_user'];
     $event->facebook_eid = $facebook_event['eid'];
     $event->facebook_status = Ai1ecFacebookConnectorPlugin::FB_IMPORTED_EVENT;
     // We create the post array. This will create / update the wp_post.
     $post = array();
     $post['post_content'] = $facebook_event['description'];
     // Prepend profile picture, left-floated, to post body.
     if ($facebook_event['has_profile_pic']) {
         list(, , , $attr) = getimagesize($facebook_event['pic_big']);
         $post['post_content'] = '<div class="ai1ec-event-avatar alignleft"><img src="' . esc_attr($facebook_event['pic_big']) . '" ' . $attr . ' /></div>' . $post['post_content'];
     }
     $post['post_title'] = $facebook_event['name'];
     $post['post_status'] = 'publish';
     $post['post_type'] = AI1EC_POST_TYPE;
     // Set categories, tags, and comment status.
     $fgo = Ai1ec_Facebook_Factory::get_facebook_graph_object($facebook_event['facebook_user']);
     $data = $fgo->get_category_and_tag();
     $event->categories = $data['category'];
     $event->tags = $data['tag'];
     $post['comment_status'] = $data['comments_enabled'] ? 'open' : 'closed';
     // If we are saving a new event, fill out event object and return it alone.
     if ($post_id === NULL) {
         $event->post = $post;
         return $event;
     } else {
         $post['ID'] = $post_id;
         $event->post_id = $post_id;
         $return_array = array("post" => $post, "event" => $event);
         return $return_array;
     }
 }