コード例 #1
0
 /**
  *  Parse the JSON returned from foursquare and store them in the database
  *  @param JSON $checkins
  */
 private function parseResults($checkins)
 {
     $post_dao = DAOFactory::getDAO('PostDAO');
     $place_dao = DAOFactory::getDAO('PlaceDAO');
     $link_dao = DAOFactory::getDAO('LinkDAO');
     $number_stored = 0;
     // Check we actually got a set of checkins
     if (isset($checkins->response->checkins->items) && sizeof($checkins->response->checkins->items) > 0) {
         // Make a query out to the API for this users details, like name etc.
         $user = $this->api_accessor->apiRequest('users/self', $this->access_token);
         // For each checkin store it in the database
         foreach ($checkins->response->checkins->items as $item) {
             // The post ID, is the checkin ID foursquare provides
             $post['post_id'] = $item->id;
             // The post text is the text they enter when checking in
             $post['post_text'] = isset($item->shout) ? $item->shout : " ";
             // The author username is the users foursquare email address
             $post['author_username'] = $user->response->user->contact->email;
             // The author full name is the name they gave foursquare
             $post['author_fullname'] = $user->response->user->firstName . " " . $user->response->user->lastName;
             // The avatar is the one they have set on foursquare
             $post["author_avatar"] = $user->response->user->photo->prefix . "100x100" . $user->response->user->photo->suffix;
             // The author user id is there foursquare user ID
             $post['author_user_id'] = $user->response->user->id;
             // The date they checked in
             $post['pub_date'] = date('Y-m-d H:i:s', $item->createdAt);
             // Source of the checkin
             $post['source'] = $item->source->name;
             // Check if the checkin was marked as private
             if (isset($item->private)) {
                 $post['is_protected'] = true;
             } else {
                 $post['is_protected'] = false;
             }
             // Set the network to foursquare
             $post['network'] = 'foursquare';
             // Set place to the name of the place their checking into
             $post['place'] = $item->venue->name;
             // There are a few parameters that may or may not be set for location so let the method figure it out
             $post['location'] = $this->getLocation($item);
             //$post['location'] = $item->location->postalCode;
             // Place ID is an ID foursquare provides
             $post['place_id'] = $item->venue->id;
             // Set geo to the lat,lng that foursquare provides
             $post['geo'] = $item->venue->location->lat . "," . $item->venue->location->lng;
             // These parameters cant be null but we don't need them for our plugin so set them to any empty string
             $post['reply_count_cache'] = '';
             $post['favlike_count_cache'] = '';
             $post['retweet_count_cache'] = '';
             $post['author_follower_count'] = '';
             // Store the checkin details in the database
             $done = $post_dao->addPost($post);
             if ($done != null) {
                 $number_stored++;
             }
             // Check if any photos are attached to this checkin
             if ($item->photos->count > 0 && $done != null) {
                 foreach ($item->photos->items as $photo) {
                     $photo_store = new Link(array('url' => $photo->url, 'expanded_url' => $photo->url, 'title' => ' ', 'description' => ' ', 'image_src' => $photo->url, 'caption' => ' ', 'clicks' => 0, 'post_key' => $done, 'error' => 'none'));
                     // Insert the photo into the database
                     $link_dao->insert($photo_store);
                     // Delete the current photo info ready for the next one
                     $photo_store = null;
                 }
             }
             // If there are any comments on this checkin capture them
             if ($item->comments->count > 0) {
                 // Make a query out for the comments
                 $comments = $this->api_accessor->apiRequest('checkins/' . $item->id, $this->access_token);
                 foreach ($comments->response->checkin->comments->items as $comment) {
                     // The post ID, is the comment ID foursquare provides
                     $comment_store['post_id'] = $comment->id;
                     // The post text is the comment they made
                     $comment_store['post_text'] = $comment->text;
                     // The author username is the users foursquare email address (which we need to query for)
                     $name = $this->api_accessor->apiRequest('users/' . $comment->user->id, $this->access_token);
                     $user_name = $name->response->user->contact->email;
                     $comment_store['author_username'] = isset($user_name) ? $user_name : 'email address withheld';
                     // The author full name is the name they gave foursquare
                     $comment_store['author_fullname'] = $comment->user->firstName . " " . $comment->user->lastName;
                     // The avatar is the one they have set on foursquare
                     $comment_store["author_avatar"] = $comment->user->photo->prefix . "100x100" . $comment->user->photo->suffix;
                     // The author user id is there foursquare user ID
                     $comment_store['author_user_id'] = $comment->user->id;
                     // The date they posted the comment
                     $comment_store['pub_date'] = date('Y-m-d H:i:s', $comment->createdAt);
                     // Source of the comment
                     $comment_store['source'] = "";
                     // Comments can not be private
                     $comment_store['is_protected'] = false;
                     // Set the network to foursquare
                     $comment_store['network'] = 'foursquare';
                     // Set place to the name of the place the comment is about
                     $comment_store['place'] = $comments->response->checkin->venue->name;
                     // A few parameters may or may not be set for location so let the method do the work
                     $comment_store['location'] = $this->getLocation($item);
                     // Place ID is an ID foursquare provides
                     $comment_store['place_id'] = $comments->response->checkin->venue->id;
                     // Set geo to the lat,lng that foursquare provides
                     $comment_store['geo'] = $item->venue->location->lat . "," . $item->venue->location->lng;
                     // The ID of the author of the checkin
                     $comment_store['in_reply_to_user_id'] = $user->response->user->id;
                     // The ID of the checkin this is a reply to
                     $comment_store['in_reply_to_post_id'] = $item->id;
                     // The number of replies this checkin has
                     $comment_store['reply_count_cache'] = $item->comments->count;
                     // These parameters cant be null but we don't need them so set them to any empty string
                     $comment_store['reply_count_cache'] = '';
                     $comment_store['favlike_count_cache'] = '';
                     $comment_store['retweet_count_cache'] = '';
                     $comment_store['author_follower_count'] = '';
                     self::fetchUser($comment_store['author_user_id'], 'comment');
                     // Now store the comment in the database
                     $post_dao->addPost($comment_store);
                     $comment = null;
                 }
             }
             // Store the details about this place in the place table if it doesn't already exist
             // See if this place is already in the database
             $place_test = $place_dao->getPlaceByID($item->venue->id);
             // If it isn't already in the database
             if ($place_test == null) {
                 // Insert it
                 $places['id'] = $item->venue->id;
                 $places['place_type'] = $item->venue->categories[0]->name;
                 $places['name'] = $item->venue->name;
                 $places['full_name'] = $item->venue->name;
                 $places['icon'] = $item->venue->categories[0]->icon->prefix . '64' . $item->venue->categories[0]->icon->suffix;
                 $places['lat_lng'] = 'POINT(' . $item->venue->location->lat . " " . $item->venue->location->lng . ')';
                 $places['map_image'] = $this->generateMap($item);
                 $place_dao->insertGenericPlace($places, 'foursquare');
             }
             // Blank out the details ready for the next checkin
             $post = null;
             $places = null;
         }
     } else {
         $this->logger->logInfo("No checkins found " . Utils::varDumpToString($checkins));
     }
 }
 /**
  * Add user auth link or process incoming auth requests.
  * @param array $options Plugin options array
  */
 protected function setUpFoursquareInteractions(array $options)
 {
     // Get the client ID and secret
     $client_id = $options['foursquare_client_id']->option_value;
     $client_secret = $options['foursquare_client_secret']->option_value;
     // Set up the redirect URL
     // Get a new configuration instance
     $config = Config::getInstance();
     // Get the root path of our install
     $site_root_path = $config->getValue('site_root_path');
     // If the server supports ssl add an s to our URL path
     $ssl = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != '' ? 's' : '';
     // Generate the redirect URL
     $redirect_uri = urlencode('http' . $ssl . '://' . $_SERVER['SERVER_NAME'] . $site_root_path . 'account/?p=foursquare');
     // Create the OAuth link based on foursquares instructions here: https://developer.foursquare.com/overview/auth
     $oauth_link = "https://foursquare.com/oauth2/authenticate?client_id=" . $client_id . "&response_type=code";
     $oauth_link .= "&redirect_uri=" . $redirect_uri;
     // Add the link for the user to click to the page
     $this->addToView('oauth_link', $oauth_link);
     // If we are here because they have been redirect back by foursquare with a OAuth token
     if (isset($_GET['code'])) {
         // Get the code foursquare provided from the URL
         $code = $_GET['code'];
         // Create a new crawler, as this class contains the method for retriving our tokens
         $crawler = new FoursquareCrawler(null, null);
         // Get the OAuth Tokens
         $tokens = $crawler->getOAuthTokens($client_id, $client_secret, $redirect_uri, $code);
         // If foursquare return an error
         if (isset($tokens->error)) {
             // Tell the user something went wrong
             $this->addErrorMessage("Oops! Something went wrong while obtaining OAuth tokens. foursquare says \"" . $tokens->error . ".\" Please double-check your settings and try again.", 'authorization');
         } else {
             // If we got some OAuth tokens back, check they are valid
             $foursquare_api_accessor = new FoursquareAPIAccessor();
             // Make a query for the users details on foursquare
             $foursquare_user = $foursquare_api_accessor->apiRequest('users/self', $tokens->access_token);
             // If foursquare returned an error after that request
             if (isset($foursquare_user->error) || !isset($foursquare_user->response->user->id) || !isset($foursquare_user->response->user->contact->email)) {
                 $this->addErrorMessage("Oops! Something went wrong querying the foursquare API.\n                         foursquare says \"" . Utils::varDumpToString($foursquare_user) . ".\" Please double-check your settings and try again.", 'authorization');
             } else {
                 // Everything went fine so store the details in the database
                 // Set the user ID and username based on details returned by foursquare
                 $foursquare_user_id = $foursquare_user->response->user->id;
                 $foursquare_username = $foursquare_user->response->user->contact->email;
                 // Save the tokens in the database
                 $this->saveAccessTokens($foursquare_user_id, $foursquare_username, $tokens->access_token);
             }
         }
     }
     // Create a new instance DAO
     $instance_dao = DAOFactory::getDAO('InstanceDAO');
     // Get the owner of this instance
     $owner_instances = $instance_dao->getByOwnerAndNetwork($this->owner, 'foursquare');
     // Add all owners of foursquare instances to the view
     $this->addToView('owner_instances', $owner_instances);
 }