Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var permissions = ['create_event' , 'manage_pages'];
- var loggedIn;
- document.addEvent('domready',function(){
- FB.Event.subscribe('auth.logout', function(response) {
- loggedIn = false; // get, if the user logs off on facebook to prohibit posts
- });
- FB.Event.subscribe('auth.login', function(response) {
- loggedIn = true; // get, if the user logs off on facebook to prohibit posts
- });
- FB.getLoginStatus(function(response) {
- if (response.session) { // facebook session was found
- checkPerms(response.perms);
- FB.api('/me', function(response) {
- createSessionStatus(response); // set the session status and generate a logou butom
- loggedIn = true;
- });
- } else {
- createLoginElement(); // no session was found so generate a login button
- }
- });
- myFacebookBackend = new FacebookBackend();
- });
- /*
- Function: checkPerms
- Function checks if the user has granted sufficient permissions
- */
- function checkPerms(perms){
- var extendedPermissions = JSON.decode(perms).extended; // get the granted permissions and check if we need more
- permissions.each(function(item){
- if(!extendedPermissions.contains(item))
- facebook_login(); // we need more permissions so call login
- });
- }
- /*
- Function: getPermsAndCheck
- Function gets the granted permissions and then checks wheter they are sufficient
- */
- function getPermsAndCheck(){
- FB.getLoginStatus(function(response) {
- if (response.session) { // facebook session was found
- checkPerms(perms);
- };
- });
- }
- /*
- Function: facebook_logout
- Logs the current user out and creates an login-Element.
- */
- function facebook_logout(){
- FB.logout(function(response) {
- createLoginElement();
- });
- }
- /*
- Function: facebook_login
- Logs the user in to facebook and trys to get oermissions granted.
- */
- function facebook_login(){
- FB.login(function(response){
- if (response.session){
- if (response.perms){
- FB.api('/me', function(response){
- createSessionStatus(response);
- });
- } else {
- createLoginElement(); // logged in but not all permissions
- return;
- }
- } else {
- createLoginElement(); // not logged in
- return;
- }
- },{perms: permissions.toString()});
- }
- /*
- Function: createLoginElement
- Ccreates a link for loggin in to facebook.
- */
- function createLoginElement(){
- var element = $('facebook_session');
- var loginElement = new Element('a', {'onClick' : 'facebook_login()', 'html' : 'Login to facebook'}).inject(element);
- }
- /*
- Function: createSessionStatus
- Creates a overview of the actual sessions and an logout button.
- Parameters:
- response - The facebook API response.
- */
- function createSessionStatus(response){
- var element = $('facebook_session');
- element.empty();
- var loginStatus = new Element('span.text', {'html' : 'Logged in to facebook as ' + response.name + ' // '}).inject(element);
- var logout = new Element('a', {'onClick' : 'facebook_logout()', 'html' : 'Logout'}).inject(loginStatus);
- }
- /**
- * Class FacebookBackend
- */
- var FacebookBackend = new Class({
- Implements: [Options],
- //Properties
- /**
- * Initialize
- */
- initialize: function(options){
- this.setOptions(options);
- },
- // posts the selected event to facebook
- // TODO: correct the timestamps!!
- postToFacebook: function(event, eventId, el){
- FB.api('/events', 'post',{ name: event[0].title, start_time:1272718027, location:"Beirut", page_id : event[0].fbId},
- function(response) {
- console.log(response);
- new Request.JSON(
- {
- url: window.location.href,
- data: 'isAjax=1&action=fillFacebookEventId&id=' + eventId + '&facebookEventId=' + response.id,
- onComplete: function()
- {
- AjaxRequest.hideBox();
- },
- onFailure: function()
- {
- alert('failed to post facebookEventId');
- },
- onSuccess: function(result)
- {
- el.getChildren().set( {src: 'system/modules/calendar/html/fb_on.gif'});
- }
- }).send();
- });
- },
- // gets the selected event as JSON
- getEvent2: function(el, eventId){
- var self = this;
- new Request.JSON(
- {
- url: window.location.href,
- data: 'isAjax=1&action=getEvent&id=' + eventId,
- onRequest: function()
- {
- AjaxRequest.displayBox('Loading data …');
- },
- onFailure: function()
- {
- alert('failed to get event');
- },
- onSuccess: function(event)
- {
- if(loggedIn){ // check if the user is logged in on facebook
- if(!event[0].facebookEid)
- self.postToFacebook(event, eventId, el);
- } else {
- AjaxRequest.hideBox();
- alert('You need to be logged in to facebook. Use the \'Login-Buttont\'in the right top corner to login');
- }
- }
- }).send();
- return false;
- },
- getEvent: function(eventId){
- var self = this;
- var result;
- new Request.JSON({
- async: false,
- url: window.location.href,
- data: 'isAjax=1&action=getEvent&id=' + eventId,
- onRequest: function()
- {
- AjaxRequest.displayBox('Loading data …');
- },
- onFailure: function()
- {
- alert('failed to get event');
- },
- onSuccess: function(event)
- {
- result = event;
- }
- }).send();
- return result;
- },
- publishEventOnFacebook: function(el, eventId){
- var self = this;
- var event = this.getEvent(eventId);
- FB.api('/events', 'post',{ name: event[0].title, start_time:1272718027, location:"Beirut", page_id : event[0].fbId},
- function(response) {
- new Request.JSON(
- {
- url: window.location.href,
- data: 'isAjax=1&action=fillFacebookEventId&id=' + eventId + '&facebookEventId=' + response.id,
- onComplete: function()
- {
- AjaxRequest.hideBox();
- },
- onFailure: function()
- {
- alert('failed to post facebookEventId');
- },
- onSuccess: function(result)
- {
- el.set( {onclick: 'Backend.getScrollOffset(); return myFacebookBackend.deleteEventOnFacebook(this, ' + eventId + ')'})
- el.getChildren().set( {src: 'system/modules/calendar/html/fb_on.gif'});
- }
- }).send();
- });
- return false;
- },
- deleteEventOnFacebook: function(el,eventId){
- var self = this;
- var event = this.getEvent(eventId);
- FB.api('/' + event[0].facebookEid, 'delete', { },
- function(response) {
- new Request.JSON(
- {
- url: window.location.href,
- data: 'isAjax=1&action=deleteFacebookEventId&id=' + eventId,
- onComplete: function()
- {
- AjaxRequest.hideBox();
- },
- onFailure: function()
- {
- alert('failed to post facebookEventId');
- },
- onSuccess: function(result)
- {
- el.set( {onclick: 'Backend.getScrollOffset(); return myFacebookBackend.publishEventOnFacebook(this, ' + eventId + ')'});
- el.getChildren().set( {src: 'system/modules/calendar/html/fb_off.gif'});
- console.log(result);
- }
- }).send();
- });
- return false;
- }
- // TODO: $a = $this->api->api("/{$this->id}", "DELETE" , '');
- // delete event on facebook
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement