Permalink
Browse files

Add Facebook pages you manage to dropdown list

On the Facebook admin page, show pages for which you are the
administrator in addition to the ones you "like".

Both types of pages are shown in the same pulldown, separated into
optgroups.

Closes #1051
  • Loading branch information...
1 parent 6e2f454 commit c8a6cea10755f527ba1b20723911057daa3d1fbb @cwarden cwarden committed with ginatrapani Oct 11, 2011
@@ -1,10 +1,10 @@
Facebook
========
-ThinkUp's Facebook plugin collects posts and status updates for Facebook users and the Facebook pages those users like.
+ThinkUp's Facebook plugin collects posts and status updates for Facebook user profiles and pages.
-Configure the Facebook Plugin (Admin only)
-------------------------------------------
+Set Up the Facebook Plugin (Admin only)
+---------------------------------------
To use the Facebook plugin, you'l need to `create a Facebook application on facebook.com
<https://developers.facebook.com/apps>`_. Set the Web Site > Site URL as recommended, and the Facebook-provided API
@@ -34,6 +34,15 @@ Add a Facebook page to ThinkUp
------------------------------
ThinkUp's Facebook plugin works with Facebook pages, but it can only connect with regular Facebook user accounts.
-To add a Facebook page, connect a regular Facebook user account to ThinkUp. Make sure that user "likes" the page, and
-then add it to ThinkUp from the Likes dropdown in ThinkUp.
+
+To add a Facebook page, connect a regular Facebook user account to ThinkUp. Then, either create a new page or "like" an
+existing page on Facebook.com. Add it to ThinkUp from the pages dropdown in ThinkUp, which contains both pages you
+manage and pages you "like." Any pages you manage will be listed first.
+
+Help! I don't see Facebook pages I manage in the dropdown list
+--------------------------------------------------------------
+
+If you're only seeing pages you "like" but not pages you manage in the pages dropdown list, delete your Facebook user
+account from ThinkUp, and re-add it. Authorize ThinkUp to get access to your managed pages, and then they will appear
+in the drodown list.
@@ -80,6 +80,11 @@ public function authControl() {
return $this->generateView();
}
+ public function isAccountPage($account_id, $access_token) {
+ $account = FacebookGraphAPIAccessor::apiRequest('/' . $account_id . '?metadata=true', $access_token);
+ return !empty($account) && !empty($account->type) && $account->type == 'page';
+ }
+
protected function setUpFacebookInteractions($options) {
// Create our Facebook Application instance
$facebook = new Facebook(array(
@@ -102,8 +107,8 @@ protected function setUpFacebookInteractions($options) {
SessionCache::put('facebook_auth_csrf', md5(uniqid(rand(), true)));
}
- $params = array('scope'=>
- 'offline_access,read_stream,user_likes,user_location,user_website,read_friendlists,friends_location',
+ $params = array('scope'=>'offline_access,read_stream,user_likes,user_location,user_website,'.
+ 'read_friendlists,friends_location,manage_pages,read_insights',
'state'=>SessionCache::get('facebook_auth_csrf'));
$fbconnect_link = $facebook->getLoginUrl($params);
@@ -113,21 +118,36 @@ protected function setUpFacebookInteractions($options) {
$logger = Logger::getInstance();
$user_pages = array();
+ $user_admin_pages = array();
$instance_dao = DAOFactory::getDAO('InstanceDAO');
$owner_instances = $instance_dao->getByOwnerAndNetwork($this->owner, 'facebook');
$ownerinstance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
foreach ($owner_instances as $instance) {
+ // TODO: figure out if the scope has changed since this instance last got its tokens,
+ // and we need to get re-request permission with the new scope
$tokens = $ownerinstance_dao->getOAuthTokens($instance->id);
$access_token = $tokens['oauth_access_token'];
if ($instance->network == 'facebook') { //not a page
$pages = FacebookGraphAPIAccessor::apiRequest('/'.$instance->network_user_id.'/likes', $access_token);
if (@$pages->data) {
$user_pages[$instance->network_user_id] = $pages->data;
}
+
+ $sub_accounts = FacebookGraphAPIAccessor::apiRequest('/'.$instance->network_user_id.'/accounts',
+ $access_token);
+ if (!empty($sub_accounts->data)) {
+ $user_admin_pages[$instance->network_user_id] = array();
+ foreach ($sub_accounts->data as $act) {
+ if (self::isAccountPage($act->id, $access_token)) {
+ $user_admin_pages[$instance->network_user_id][] = $act;
+ }
+ }
+ }
}
}
$this->addToView('user_pages', $user_pages);
+ $this->addToView('user_admin_pages', $user_admin_pages);
$owner_instance_pages = $instance_dao->getByOwnerAndNetwork($this->owner, 'facebook page');
if (count($owner_instance_pages) > 0) {
@@ -153,8 +173,8 @@ protected function processPageActions($options, Facebook $facebook) {
$config = Config::getInstance();
$site_root_path = $config->getValue('site_root_path');
$redirect_uri = urlencode(sprintf('%s://%s%s%s', !empty($_SERVER['HTTPS']) ? 'https' : 'http',
- empty($_SERVER['SERVER_NAME']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'],
- $site_root_path, 'account/?p=facebook'));
+ empty($_SERVER['SERVER_NAME']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'],
+ $site_root_path, 'account/?p=facebook'));
//Build API request URL
$api_req = 'https://graph.facebook.com/oauth/access_token?client_id='.
@@ -249,6 +249,48 @@ public function testConfiguredPluginWithOneFacebookUserNoLikedPages() {
$this->assertEqual(sizeof($v_mgr->getTemplateDataItem('owner_instances')), 1);
}
+ public function testConfiguredPluginWithOneFacebookUserOneLikedPageOneManagedPage() {
+ self::buildInstanceData();
+ // build some options data
+ $options_arry = $this->buildPluginOptions();
+ $this->simulateLogin('me@example.com', true);
+ $owner_dao = DAOFactory::getDAO('OwnerDAO');
+ $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
+ $controller = new FacebookPluginConfigurationController($owner, 'facebook');
+ $output = $controller->go();
+
+ //The mock API accessor reads the page likes JSON from the testdata/668406218_likes file
+ $v_mgr = $controller->getViewManager();
+ $liked_pages = $v_mgr->getTemplateDataItem('user_pages');
+ $this->assertIsA($liked_pages, 'Array');
+ $this->assertEqual(sizeof($liked_pages), 1);
+ $admin_pages = $v_mgr->getTemplateDataItem('user_admin_pages');
+ $this->assertIsA($liked_pages, 'Array');
+ $this->assertEqual(sizeof($liked_pages), 1);
+ $this->assertPattern("/Pages You Manage/", $output);
+ }
+
+ public function testConfiguredPluginWithOneFacebookUserNoLikedPagesNoManagedPages() {
+ self::buildInstanceData();
+ // build some options data
+ $options_arry = $this->buildPluginOptions();
+ $this->simulateLogin('me2@example.com', true);
+ $owner_dao = DAOFactory::getDAO('OwnerDAO');
+ $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
+ $controller = new FacebookPluginConfigurationController($owner, 'facebook');
+ $output = $controller->go();
+
+ //The mock API accessor reads the page likes JSON from the testdata/668406218_likes file
+ $v_mgr = $controller->getViewManager();
+ $liked_pages = $v_mgr->getTemplateDataItem('user_pages');
+ $this->assertIsA($liked_pages, 'Array');
+ $this->assertEqual(sizeof($liked_pages), 0);
+ $admin_pages = $v_mgr->getTemplateDataItem('user_admin_pages');
+ $this->assertIsA($liked_pages, 'Array');
+ $this->assertEqual(sizeof($liked_pages), 0);
+ $this->assertNoPattern("/Pages You Manage/", $output);
+ }
+
private function buildPluginOptions() {
$namespace = OptionDAO::PLUGIN_OPTIONS . '-2';
$builders = array();
@@ -0,0 +1,11 @@
+{
+ "id": "107982202636989",
+ "name": "Sample Cause",
+ "picture": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/276798_107982202636989_4098521_s.jpg",
+ "link": "https://www.facebook.com/pages/Sample-Cause/107982202636989",
+ "likes": 1,
+ "category": "Cause",
+ "website": "<<not-applicable>>",
+ "can_post": true,
+ "type": "page"
+}
@@ -0,0 +1,13 @@
+{
+ "id": "139411052797633",
+ "name": "pinkup",
+ "link": "https://www.facebook.com/apps/application.php?id=139411052797633",
+ "canvas_name": "pinkhamster",
+ "namespace": "pinkhamster",
+ "icon_url": "https://s-static.ak.facebook.com/rsrc.php/v1/y1/r/DiQbfqEb9qG.gif",
+ "logo_url": "https://s-static.ak.facebook.com/rsrc.php/v1/yd/r/diYVBC9Z4eH.gif",
+ "daily_active_users": "1",
+ "weekly_active_users": "1",
+ "monthly_active_users": "1",
+ "type": "application"
+}
@@ -0,0 +1,16 @@
+{
+ "data": [
+ {
+ "name": "Sample Cause",
+ "access_token": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
+ "category": "Cause",
+ "id": "107982202636989"
+ },
+ {
+ "name": "pinkup",
+ "access_token": "AAABZByypHnsEBAClV6dI32M88r2l02Tt4Tt6py1SUXALuzcPm0shTx03cGti9UmmLPima3m5s6ZCfLWSqv3JAKI8ZAxM36Beo5cFhYQNQZDZD",
+ "category": "Application",
+ "id": "139411052797633"
+ }
+ ]
+}
@@ -0,0 +1,4 @@
+{
+ "data": [
+ ]
+}
@@ -16,7 +16,7 @@
</div>
</div>
- {if count($owner_instances) > 0 }
+{if count($owner_instances) > 0 }
<h2 class="subhead">Facebook User Profiles</h2>
{include file="_usermessage.tpl" field="user_add"}
{foreach from=$owner_instances key=iid item=i name=foo}
@@ -63,28 +63,36 @@
<br />
{/if}
-
-<h2 class="subhead">Add a Facebook Page You "Like"</h2>
+<h2 class="subhead">Add a Facebook Page</h2>
{foreach from=$owner_instances key=iid item=i name=foo}
{assign var='facebook_user_id' value=$i->network_user_id}
- {if $user_pages.$facebook_user_id}
+ {if $user_pages.$facebook_user_id or $user_admin_pages.$facebook_user_id}
<div class="clearfix">
<div class="grid_4 right" style="padding-top:.5em;">
{$i->network_username}&nbsp;likes:
</div>
<form name="addpage" action="index.php?p=facebook">
<div class="grid_8">
- {if $user_pages.$facebook_user_id}
<input type="hidden" name="instance_id" value="{$i->id}">
<input type="hidden" name="p" value="facebook">
<input type="hidden" name ="viewer_id" value="{$i->network_user_id}" />
<input type="hidden" name ="owner_id" value="{$owner->id}" />
<select name="facebook_page_id">
- {foreach from=$user_pages.$facebook_user_id key=page_id item=page name=p}
- <option value="{$page->id}">{if strlen($page->name)>27}{$page->name|substr:0:27}...{else}{$page->name}{/if}</option> <br />
- {/foreach}
+ {if $user_admin_pages.$facebook_user_id}
+ <optgroup label="Pages You Manage">
+ {foreach from=$user_admin_pages.$facebook_user_id key=page_id item=page name=p}
+ <option value="{$page->id}">{if strlen($page->name)>27}{$page->name|substr:0:27}...{else}{$page->name}{/if}</option> <br />
+ {/foreach}
+ </optgroup>
+ {/if}
+ {if $user_pages.$facebook_user_id}
+ <optgroup label="Pages You Like">
+ {foreach from=$user_pages.$facebook_user_id key=page_id item=page name=p}
+ <option value="{$page->id}">{if strlen($page->name)>27}{$page->name|substr:0:27}...{else}{$page->name}{/if}</option> <br />
+ {/foreach}
+ </optgroup>
+ {/if}
</select>
- {/if}
</div>
<div class="grid_7">
<span id="divaddpage{$i->network_username}"><input type="submit" name="action" class="tt-button ui-state-default ui-priority-secondary ui-corner-all
@@ -93,9 +101,10 @@ addPage" id="{$i->network_username}" value="add page" /></span>
</form>
</div>
{else}
- To add a Facebook page to ThinkUp, "like" it on Facebook.com and refresh this page.
+ To add a Facebook page to ThinkUp, create a new page on Facebook.com or "like" an existing one, and refresh this page.
{/if}
{/foreach}
+
{/if}
</div>

0 comments on commit c8a6cea

Please sign in to comment.