/** * 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; }
/** * Returns the upper part of the tab, with any errors * * @param Ai1ec_Facebook_Current_User $user The user currently logged in to Facebook * * @return string the HTML */ private function render_user_html(Ai1ec_Facebook_Current_User $user) { // Create the variables and then create the HTML. $submit_logout_value = esc_attr__('Sign out of Facebook', AI1EC_PLUGIN_NAME); $submit_subscribe_your_events = esc_attr__('Subscribe to your events', AI1EC_PLUGIN_NAME); $alerts = $this->generate_html_for_alerts(); $submit_your_events = $user->get_subscribed() ? '' : '<button type="submit" id="ai1ec_facebook_subscribe_yours" class="btn btn-primary" name="ai1ec_facebook_subscribe_yours"><i class="icon-plus"></i> ' . $submit_subscribe_your_events . '</button>'; $user = $this->render_user_pic_and_icons($user); $question_mark = $this->render_question_mark_for_facebook(); $html = <<<HTML <div class="row-fluid"> \t<div class="span12"> \t\t<div id="alerts">{$alerts}</div> \t\t<div id="ai1ec-facebook"> \t\t{$user} \t\t</div> \t\t<div class="ai1ec_submit_wrapper btn-toolbar"> \t\t\t<div class="btn-group"> \t\t\t\t{$submit_your_events} \t\t\t\t<button type="submit" id="ai1ec_logout_facebook" class="btn" \t\t\t\t\tname="ai1ec_logout_facebook"> \t\t\t\t\t<i class="icon-signout"></i> {$submit_logout_value} \t\t\t\t</button> \t\t\t</div> \t\t\t<div class="btn-group"> \t\t\t\t{$question_mark} \t\t\t</div> \t\t</div> \t</div> </div> HTML; return $html; }
/** * Returns the upper part of the tab, with any errors * * @param Ai1ec_Facebook_Current_User $user The user currently logged in to Facebook * * @return string the HTML */ private function render_user_html(Ai1ec_Facebook_Current_User $user) { // Create the variables and then create the HTML. $submit_logout_value = esc_attr__('Log out from Facebook', AI1EC_PLUGIN_NAME); $submit_subscribe_your_events = esc_attr__('Subscribe to your events', AI1EC_PLUGIN_NAME); $alerts = $this->generate_html_for_alerts(); $submit_your_events = $user->get_subscribed() ? "<input type='submit' id='ai1ec_facebook_subscribe_yours' name='ai1ec_facebook_subscribe_yours' class='button-primary hide' value='{$submit_subscribe_your_events}'>" : "<input type='submit' id='ai1ec_facebook_subscribe_yours' name='ai1ec_facebook_subscribe_yours' class='button-primary' value='{$submit_subscribe_your_events}'>"; $user = $this->render_user_pic_and_icons($user); $question_mark = $this->render_question_mark_for_facebook(); $html = <<<HTML <div class="row-fluid"> \t<div class="span12"> \t\t<div id="alerts">{$alerts}</div> \t\t<div id="ai1ec-facebook"> \t\t{$user} \t\t</div> \t\t<div class="ai1ec_submit_wrapper"> \t\t\t{$submit_your_events} \t\t\t<input type="submit" id="ai1ec_logout_facebook" name="ai1ec_logout_facebook" class="button-secondary" value="{$submit_logout_value}"> \t\t\t{$question_mark} \t\t</div> \t</div> </div> HTML; return $html; }
/** * Populate events variable from an ai1ec event and the posted data * * @param $post array * The posted data * * @param $event Ai1ec_Event * The event which has been saved. * */ public function populate_event_from_post_and_ai1ec_event(array $post, Ai1ec_Event $event, Ai1ec_Facebook_Current_User $current_user) { // We must set the id, that means that we would be updating later on. First we check if the // eid is set and that the event was not imported from facebook if (isset($event->facebook_eid) && $event->facebook_status !== Ai1ecFacebookConnectorPlugin::FB_IMPORTED_EVENT) { // Then we check that the eid is not 0 $this->id = (int) $event->facebook_eid === 0 ? NULL : $event->facebook_eid; } // Starting from september 2012 Facebook doesn't accept timestamps anymore. No need for conversions. if (isset($_POST['ai1ec_all_day_event'])) { $this->start_time = date('Y-m-d\\TH:i:sP', $event->start); $this->end_time = date('Y-m-d\\TH:i:sP', $event->end - 60); } else { $this->start_time = date('Y-m-d\\TH:i:sP', $event->start); $this->end_time = date('Y-m-d\\TH:i:sP', $event->end); } $this->name = $post['post_title']; $this->description = $post['content']; $this->city = $post['ai1ec_city']; $this->country = $post['ai1ec_country']; $this->state = $post['ai1ec_province']; $this->zip = $post['ai1ec_postal_code']; // Clone the address. We must clone it as we are handling a refernce and we don't want to modify this. $street = $post['ai1ec_address'] . ""; $this->street = $this->get_street_from_ai1ec_event_street($street, $post['ai1ec_postal_code'], $post['ai1ec_country'], $post['ai1ec_city']); $this->location = $post['ai1ec_venue']; if (!empty($event->facebook_user)) { $this->access_token = $this->get_facebook_page_access_token_from_id($event->facebook_user); } // let's check if the radio were present if (isset($post[Ai1ecFacebookConnectorPlugin::EXPORT_PAGE_RADIO_NAME])) { // get the id $id = (string) $post[Ai1ecFacebookConnectorPlugin::EXPORT_PAGE_RADIO_NAME]; if ((string) $current_user->get_id() !== $id) { try { $this->access_token = $this->get_facebook_page_access_token_from_id($id); } catch (Exception $e) { // no token found, do nothing for now and keep the normal flow } } } }