/**
  * Fetches data from Facebook for the specified type and then updates the DB
  *
  * @param string $type the facebook graph object type to sync
  *
  * @return array
  */
 public function do_facebook_sync($type)
 {
     $response = array("errors" => false, "error_messages" => array());
     $fgoc = Ai1ec_Facebook_Factory::get_facebook_graph_object_collection(array());
     // Set the an instance of the Facebook class
     $facebook = $this->facebook_instance_factory();
     $fgoc->set_facebook($facebook);
     // Set the currently active user.
     if ($this->get_current_facebook_user_from_cache() !== NULL) {
         // If we have a seesion, we use it
         $fgoc->set_facebook_user($this->get_current_facebook_user_from_cache());
     } else {
         // We don't have a session, this is probably the cron. get a new user
         $current_user = Ai1ec_Facebook_Factory::get_facebook_user_instance($facebook);
         // Do login
         $logged_in = $current_user->do_login();
         if ($logged_in) {
             $fgoc->set_facebook_user($current_user);
         } else {
             $this->set_error_message_for_cron($current_user->get_error_message());
         }
     }
     // Set the type of the collection.
     $fgoc->set_type($type);
     try {
         $fgoc->sync_facebook_users();
         $response['message'] = sprintf(__("Fetching data for %s has completed succesfully", AI1EC_PLUGIN_NAME), Ai1ec_Facebook_Graph_Object_Collection::get_type_printable_text($type));
     } catch (WP_FacebookApiException $e) {
         // The first FQL query failed and nothing has changed
         $response['errors'] = TRUE;
         $response['error_messages'][] = $e->getMessage();
     } catch (Ai1ec_Facebook_Friends_Sync_Exception $e) {
         // The first query failed but we have something to return
         $response['errors'] = TRUE;
         $response['error_messages'] = array_merge($response['error_messages'], $e->get_error_messages());
     }
     return $response;
 }
    /**
     * Renders the Facebook Graph Object that user has subscribed to.
     *
     * @param array $types the Type of Facebook Graph Objects to render
     *
     * @param $current_id The id of the currently logged on user which must be excluded from subscribers and multiselects
     *
     * @return string the HTML
     */
    private function render_subscribers(array $types, $current_id)
    {
        $subscribers = $this->render_all_elements($types, FALSE, $current_id);
        $html = '';
        foreach ($types as $type) {
            $text = Ai1ec_Facebook_Graph_Object_Collection::get_type_printable_text($type);
            $html .= <<<HTML
<div class="ai1ec-facebook-items" data-type="{$type}">
\t<h2 class="ai1ec-facebook-header">{$text}</h2>
\t{$subscribers[$type]}
</div>
HTML;
        }
        $keep_events = __("Keep Events", AI1EC_PLUGIN_NAME);
        $remove_events = __("Remove Events", AI1EC_PLUGIN_NAME);
        $body = __("Would you like to remove these events from your calendar, or preserve them?", AI1EC_PLUGIN_NAME);
        $removing = __("Removing the following subscription: ", AI1EC_PLUGIN_NAME);
        $header_text = $removing . '<span id="ai1ec-facebook-user-modal"></span>';
        // Attach the modal for when you unsubscribe.
        $twitter_bootstrap_modal = Ai1ec_Helper_Factory::create_bootstrap_modal_instance($body);
        $twitter_bootstrap_modal->set_id('ai1ec-facebook-modal');
        $twitter_bootstrap_modal->set_delete_button_text($remove_events);
        $twitter_bootstrap_modal->set_keep_button_text($keep_events);
        $twitter_bootstrap_modal->set_header_text($header_text);
        $html .= $twitter_bootstrap_modal->render_as_html();
        return $html;
    }
    /**
     * Renders the Facebook Graph Object that user has subscribed to.
     *
     * @param array $types the Type of Facebook Graph Objects to render
     *
     * @param $current_id The id of the currently logged on user which must be excluded from subscribers and multiselects
     *
     * @return string the HTML
     */
    private function render_subscribers(array $types, $current_id)
    {
        $subscribers = $this->render_all_elements($types, FALSE, $current_id);
        $html = '';
        foreach ($types as $type) {
            $text = Ai1ec_Facebook_Graph_Object_Collection::get_type_printable_text($type);
            $html .= <<<HTML
<div class="ai1ec-facebook-items" data-type="{$type}">
\t<h2 class="ai1ec-facebook-header">{$text}</h2>
\t{$subscribers[$type]}
</div>
HTML;
        }
        $keep_events = __("Keep Events", AI1EC_PLUGIN_NAME);
        $remove_events = __("Remove Events", AI1EC_PLUGIN_NAME);
        $body = __("Would you like to remove these events from your calendar, or preserve them?", AI1EC_PLUGIN_NAME);
        $removing = __("Removing the following subscription: ", AI1EC_PLUGIN_NAME);
        $header_text = $removing . '<span id="ai1ec-facebook-user-modal"></span>';
        // Attach the modal for when you unsubscribe.
        $twitter_bootstrap_modal = new Ai1ec_Bootstrap_Modal($body);
        $twitter_bootstrap_modal->set_id('ai1ec-facebook-modal');
        $twitter_bootstrap_modal->set_delete_button_text($remove_events);
        $twitter_bootstrap_modal->set_keep_button_text($keep_events);
        $twitter_bootstrap_modal->set_header_text($header_text);
        $modal = <<<HTML
<div class="modal hide" id="ai1ec-facebook-modal">
\t<div class="modal-header">
\t\t<button class="close" data-dismiss="modal">×</button>
\t\t<h1><small>{$removing} <span id="ai1ec-facebook-user-modal"></span></small></h1>
\t</div>
\t<div class="modal-body">
\t\t<p>{$body}</p>
\t</div>
\t<div class="modal-footer">
\t\t<a href="#" class="btn remove btn-danger">{$remove_events}</a>
\t\t<a href="#" class="btn keep btn-primary">{$keep_events}</a>
\t</div>
</div>
HTML;
        $html .= $twitter_bootstrap_modal->render_modal_and_return_html();
        return $html;
    }