Skip to content

Instantly share code, notes, and snippets.

@joshsmith
Created January 27, 2011 03:43
Show Gist options
  • Save joshsmith/798023 to your computer and use it in GitHub Desktop.
Save joshsmith/798023 to your computer and use it in GitHub Desktop.
<?php
require_once(str_replace('//','/',dirname(__FILE__).'/') .'../models/UsersFacebookStore.php');
require_once(str_replace('//','/',dirname(__FILE__).'/') .'../controllers/UsersGoal.php');
require_once(str_replace('//','/',dirname(__FILE__).'/') .'../controllers/User.class.php');
require_once(str_replace('//','/',dirname(__FILE__).'/') .'../helpers/URL.php');
/**
* @owner Josh Smith <[email protected]>
*
* @package Controllers
*/
class UsersFacebook
{
public $usersFacebookStore;
public function __construct()
{
$this->usersFacebookStore = new UsersFacebookStore();
}
/**
* Adds an access token
* @param int $uid User's id
* @param int $facebook_uid Facebook uid
* @param string $token Access token
*
* @return bool True if add succeeded, false otherwise
*/
public function addAccessToken($uid, $facebook_uid, $token)
{
if($this->usersFacebookStore->addAccessToken($uid, $facebook_uid, $token)) {
return true;
}
}
/**
* Gets a user's Facebook info
* @param int $uid uid
*
* @return array Array of info containing uid, Facebook uid, and access_token
*/
public function getFacebookInfo($uid)
{
$array = $this->usersFacebookStore->getFacebookInfo($uid);
return $array;
}
/**
* Publicly accessible method for posting to a user's wall
* @param int $uid User's ID
* @param string $type Type of activity/name of function (e.g. addedGoal)
* @param int $activity_id Activity ID (e.g. goal_id for goal)
*
* @return string REST response from Graph API
*/
public function postToWall($uid, $type, $activity_id)
{
$fbArray = $this->getFacebookInfo($uid);
$token = $fbArray['access_token'];
$facebookUid = $fbArray['facebook_uid'];
// Check that the token's not null
if(is_null($token)) {
return false;
}
$url = 'https://graph.facebook.com/' . $facebookUid . '/feed?access_token=' . $token;
$response = $this->$type($uid, $url, $activity_id);
return $response;
}
/**
* Posts 'added goal' to FB Wall
* @param int $uid User's ID
* @param string $url Base URL for Graph
* @param int $goal_id Goal ID
*
* @return string REST Response from Graph API
*/
protected function addedGoal($uid, $url, $goal_id)
{
// Get the goal
$usersGoal = new UsersGoal();
$goalArray = $usersGoal->getGoal($goal_id);
// Generate the link
$URL = new URL();
$slug = $URL->generateSlug($goalArray['title']);
$link = 'http://www.goals.com/support/' . $goal_id . '/' . $slug;
// Get the user's first name
$user = new User();
$userArray = $user->getUserArray($uid);
$first_name = $userArray[0]['first_name'];
// Set the photo URL, link text, description, and action link
$photoUrl = 'http://static.goalscdn.com/img/goalswhiteback.png';
$linkText = $first_name . ' added a goal: ' . $goalArray['title'];
$description = 'Click "Support" below to show your support.';
$actionsJSON = '[{"name": "Support", "link": "' . $link . '"}]';
// Set the parameters
$params = 'picture=' . $photoUrl . '&link=' . $link . '&name=' . $linkText;
$params .= '&description=' . $description . '&actions=' . $actionsJSON;
// Execute cURL
$response = $this->doCurl($url, $params);
return $response;
}
/**
* Posts 'made progress' to FB Wall
* @param int $uid User's ID
* @param string $url Base URL for Graph
* @param int $goal_id Goal ID
*
* @return string REST Response from Graph API
*/
protected function madeProgress($uid, $url, $goal_id)
{
// Get the goal
$usersGoal = new UsersGoal();
$goalArray = $usersGoal->getGoal($goal_id);
// Generate the link
$URL = new URL();
$slug = $URL->generateSlug($goalArray['title']);
$link = 'http://www.goals.com/support/' . $goal_id . '/' . $slug;
// Get the user's first name
$user = new User();
$userArray = $user->getUserArray($uid);
$first_name = $userArray[0]['first_name'];
// Set the photo URL, link text, description, and action link
$photoUrl = 'http://static.goalscdn.com/img/goalswhiteback.png';
$linkText = $first_name . ' added a goal: ' . $goalArray['title'];
$description = 'Click "Support" below to show your support.';
$actionsJSON = '[{"name": "Support", "link": "' . $link . '"}]';
// Set the parameters
$params = 'picture=' . $photoUrl . '&link=' . $link . '&name=' . $linkText;
$params .= '&description=' . $description . '&actions=' . $actionsJSON;
// Execute cURL
$response = $this->doCurl($url, $params);
return $response;
}
/**
* Executes cURL for getting REST response from Graph
* @param string $url Base url
* @param string $params Params for Graph
*
* @return string $response REST Response from Graph
*/
protected function doCurl($url, $params)
{
// Get the curl session object
$curl = curl_init($url);
// Tell curl to use HTTP POST
curl_setopt ($curl, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($curl, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment