Refactored Facebook Widget stuff

This commit is contained in:
Albert Santoni 2015-10-30 14:38:34 -04:00
parent 323b54d239
commit 702ee97c7b
6 changed files with 207 additions and 3 deletions

View file

@ -96,6 +96,12 @@ class Config {
$CC_CONFIG['soundcloud-client-id'] = $globalAirtimeConfigValues['soundcloud']['soundcloud_client_id'];
$CC_CONFIG['soundcloud-client-secret'] = $globalAirtimeConfigValues['soundcloud']['soundcloud_client_secret'];
$CC_CONFIG['soundcloud-redirect-uri'] = $globalAirtimeConfigValues['soundcloud']['soundcloud_redirect_uri'];
if (isset($globalAirtimeConfigValues['facebook']['facebook_app_id'])) {
$CC_CONFIG['facebook-app-id'] = $globalAirtimeConfigValues['facebook']['facebook_app_id'];
$CC_CONFIG['facebook-app-url'] = $globalAirtimeConfigValues['facebook']['facebook_app_url'];
$CC_CONFIG['facebook-app-api-key'] = $globalAirtimeConfigValues['facebook']['facebook_app_api_key'];
}
if(isset($values['demo']['demo'])){
$CC_CONFIG['demo'] = $values['demo']['demo'];

View file

@ -41,6 +41,12 @@ $pages = array(
'module' => 'default',
'controller' => 'embeddablewidgets',
'action' => 'schedule',
),
array(
'label' => _('Facebook'),
'module' => 'default',
'controller' => 'embeddablewidgets',
'action' => 'facebook',
)
)
),

View file

@ -26,8 +26,8 @@ class EmbeddableWidgetsController extends Zend_Controller_Action
$this->view->player_form = $form;
} else {
$this->view->player_error_msg = _("To configure and use the embeddable player you must:<br><br>
1. Enable at least one MP3, AAC, or OGG stream under System -> Streams<br>
2. Enable the Public Airtime API under System -> Preferences");
1. Enable at least one MP3, AAC, or OGG stream under Settings -> Streams<br>
2. Enable the Public Airtime API under Settings -> Preferences");
}
}
@ -39,8 +39,83 @@ class EmbeddableWidgetsController extends Zend_Controller_Action
if (!$apiEnabled) {
$this->view->weekly_schedule_error_msg = _("To use the embeddable weekly schedule widget you must:<br><br>
Enable the Public Airtime API under System -> Preferences");
Enable the Public Airtime API under Settings -> Preferences");
}
}
public function facebookAction()
{
Zend_Layout::getMvcInstance()->assign('parent_page', 'Widgets');
$apiEnabled = Application_Model_Preference::GetAllow3rdPartyApi();
if (!$apiEnabled) {
$this->view->facebook_error_msg = _("To add the Radio Tab to your Facebook Page, you must first:<br><br>
Enable the Public Airtime API under Settings -> Preferences");
}
$CC_CONFIG = Config::getConfig();
$baseUrl = Application_Common_OsPath::getBaseDir();
$facebookAppId = $CC_CONFIG['facebook-app-id'];
$this->view->headScript()->appendScript("var FACEBOOK_APP_ID = " . json_encode($facebookAppId) . ";");
$this->view->headScript()->appendFile($baseUrl . 'js/airtime/common/facebook.js?' . $CC_CONFIG['airtime_version'], 'text/javascript');
}
/** Airtime makes an AJAX POST here after it successfully adds a tab to your Facebook page. */
public function facebookTabSuccessAction()
{
// disable the view and the layout
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
//TODO: Get list of page IDs (deserialize)
$request = $this->getRequest();
if (!$request->isPost()) {
return;
}
$values = $request->getPost();
$facebookPageIds = json_decode($values["pages"]);
$CC_CONFIG = Config::getConfig();
$facebookMicroserviceUrl = $CC_CONFIG['facebook-app-url'];
$facebookMicroserviceApiKey = $CC_CONFIG['facebook-app-api-key'];
//Post the page tab ID and station subdomain to the social microservice so that mapping can be saved
//in a database.
foreach ($facebookPageIds as $facebookPageId)
{
$postfields = array();
$postfields["facebookPageId"] = $facebookPageId;
$postfields["stationId"] = $CC_CONFIG['stationId'];
$query_string = "";
foreach ($postfields as $k => $v) $query_string .= "$k=".urlencode($v)."&";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $facebookMicroserviceUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_USERPWD, ":$facebookMicroserviceApiKey");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$jsondata = curl_exec($ch);
if (curl_error($ch)) {
throw new Exception("Failed to reach server in " . __FUNCTION__ . ": "
. curl_errno($ch) . ' - ' . curl_error($ch) . ' - ' . curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
}
curl_close($ch);
}
//$arr = json_decode($jsondata, true); # Decode JSON String
}
}

View file

@ -219,6 +219,9 @@ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
}
?>
<div id="fb-root"></div>
</body>
</html>

View file

@ -0,0 +1,25 @@
<div id="weekly-schedule-widget" class="prefpanel ui-widget simple-formblock clearfix padded-strong ">
<?php $baseUrl = Application_Common_OsPath::getBaseDir(); ?>
<h2 style="float:left"><?php echo _("Facebook Radio Player") ?></h2>
<div style="clear:both"></div>
<div id="weekly-schedule-widget-error">
<?php echo $this->facebook_error_msg; ?>
</div>
<div><p>Add your <?php echo(SAAS_PRODUCT_BRANDING_NAME); ?> station to your Facebook Page.</p>
<button id="facebook-login">Add to Facebook Page</button>
</div>
<div>
<h3>Preview:</h3>
<div>
<iframe width=800 height=800 src="<?php echo Application_Common_HTTPHelper::getStationUrl(); ?>/?facebook=1"></iframe>
</div>
</div>
</div>

View file

@ -0,0 +1,89 @@
$(document).ready(function() {
$("#facebook-login").click(function() {
AIRTIME.facebook.promptForFacebookPage();
});
});
window.fbAsyncInit = function() {
FB.init({
appId : FACEBOOK_APP_ID,
xfbml : true,
version : 'v2.4'
});
};
var AIRTIME = (function(AIRTIME) {
//Module initialization
if (AIRTIME.facebook === undefined) {
AIRTIME.facebook = {};
}
var mod = AIRTIME.facebook;
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
mod.promptForFacebookPage = function() {
FB.login(function (response) {
if (response.authResponse) {
mod.getPagesOwnedByUser(response.authResponse.userID, response.authResponse.accessToken);
mod.addPageTab();
} else {
console.log('Authorization failed.');
}
}, {scope: 'manage_pages'});
}
mod.getPagesOwnedByUser = function(userId, accessToken) {
FB.api('/' + userId + '/accounts', function (response) {
console.log(response);
}, {access_token: accessToken});
}
mod.addPageTab = function() {
FB.ui(
{ method: 'pagetab' },
function (resp) {
console.log("response:");
console.log(resp);
var pageIdList = [];
var tabs = resp["tabs_added"];
if ((tabs != undefined) && (Object.keys(tabs).length > 0)) {
for (var pageId in tabs) {
pageIdList.push(pageId);
}
//POST these back to Airtime, which will then proxy it over to our social app. (multiple requests from Airtime)
$.post('facebook-tab-success', { "pages" : JSON.stringify(pageIdList) }, function() {
alert("Successfully added to your Facebook page!");
}).done(function() {
}).fail(function() {
alert("Sorry, an error occurred and we were unable to add the widget to your Facebook page.");
});
}
},
{
app_id: FACEBOOK_APP_ID,
//redirect_uri: 'https://localhost'
}
);
}
return AIRTIME;
}(AIRTIME || {}));