/**
  * Creates a multy-query to get events for groups
  *
  * @param array $grouped_eids an array where the keys are the id of the groups and the values are an array of events id
  *
  * @param int $timestamp the timestamp that needs to be passed for checking only events that start after it
  *
  * @return array the FQL mutly-query array.
  */
 private function generate_multi_query_for_event_details(array $grouped_eids, $timestamp)
 {
     $fql = array();
     foreach ($grouped_eids as $id => $eids) {
         $imploded_eids = implode(',', $eids);
         $time = Ai1ec_Facebook_Event::get_facebook_actual_time($timestamp);
         $fql[$id] = "SELECT\n\t\t\t\t\teid,\n\t\t\t\t\tname,\n\t\t\t\t\tdescription,\n\t\t\t\t\tstart_time,\n\t\t\t\t\tend_time,\n\t\t\t\t\tvenue,\n\t\t\t\t\tlocation,\n\t\t\t\t\tupdate_time,\n\t\t\t\t\thas_profile_pic,\n\t\t\t\t\tpic_big\n\t\t\t\tFROM\n\t\t\t\t\tevent\n\t\t\t\tWHERE\n\t\t\t\t\teid IN ({$imploded_eids}) AND start_time > {$time}";
     }
     return $fql;
 }
 /**
  * Generate the fql query to get the details of user events
  *
  * @param array $users
  *
  * @param int $timestamp
  *
  * @return array an array of fql queries.
  */
 private function generate_fql_multiquery_to_get_events_details(array $users, $timestamp)
 {
     $fql = array();
     // When we make a query we must convert
     $time = Ai1ec_Facebook_Event::get_facebook_actual_time($timestamp);
     foreach ($users as $id) {
         $fql[$id] = "SELECT\n\t\t\t\t\teid,\n\t\t\t\t\tname,\n\t\t\t\t\tdescription,\n\t\t\t\t\tstart_time,\n\t\t\t\t\tend_time,\n\t\t\t\t\tvenue,\n\t\t\t\t\tlocation,\n\t\t\t\t\tupdate_time,\n\t\t\t\t\ttimezone,\n\t\t\t\t\thas_profile_pic,\n\t\t\t\t\tpic_big\n\t\t\t\tFROM\n\t\t\t\t\tevent\n\t\t\t\tWHERE\n\t\t\t\t\teid IN (SELECT eid FROM event_member WHERE uid = {$id}) AND start_time > {$time}";
     }
     return $fql;
 }
 /**
  * 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;
     }
 }