Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var permissions = ['create_event' , 'manage_pages'];
  2. var loggedIn;
  3.  
  4. document.addEvent('domready',function(){
  5.         FB.Event.subscribe('auth.logout', function(response) {
  6.             loggedIn = false; // get, if the user logs off on facebook to prohibit posts
  7.         });
  8.         FB.Event.subscribe('auth.login', function(response) {
  9.             loggedIn = true; // get, if the user logs off on facebook to prohibit posts
  10.         });
  11.        
  12.         FB.getLoginStatus(function(response) {
  13.             if (response.session) { // facebook session was found
  14.                 checkPerms(response.perms);
  15.                 FB.api('/me', function(response) {
  16.                     createSessionStatus(response); // set the session status and generate a logou butom
  17.                     loggedIn = true;
  18.                 });
  19.             } else {
  20.                 createLoginElement(); // no session was found so generate a login button
  21.             }
  22.         });
  23.        
  24.         myFacebookBackend = new FacebookBackend();
  25.     });
  26.  
  27. /*
  28.     Function: checkPerms
  29.    
  30.     Function checks if the user has granted sufficient permissions
  31.    
  32. */
  33. function checkPerms(perms){
  34.     var extendedPermissions = JSON.decode(perms).extended; // get the granted permissions and check if we need more
  35.     permissions.each(function(item){
  36.         if(!extendedPermissions.contains(item))
  37.             facebook_login(); // we need more permissions so call login
  38.     });
  39. }
  40.  
  41. /*
  42.     Function: getPermsAndCheck
  43.    
  44.     Function gets the granted permissions and then checks wheter they are sufficient
  45.    
  46. */
  47. function getPermsAndCheck(){
  48.         FB.getLoginStatus(function(response) {
  49.             if (response.session) { // facebook session was found
  50.                 checkPerms(perms);
  51.             };
  52.         });
  53. }
  54.  
  55. /*
  56.    Function: facebook_logout
  57.  
  58.    Logs the current user out and creates an login-Element.
  59.  
  60. */
  61. function facebook_logout(){
  62.     FB.logout(function(response) {
  63.         createLoginElement();
  64.     });
  65. }
  66.  
  67.  
  68. /*
  69.    Function: facebook_login
  70.  
  71.    Logs the user in to facebook and trys to get oermissions granted.
  72.  
  73. */
  74. function facebook_login(){
  75.     FB.login(function(response){
  76.         if (response.session){
  77.             if (response.perms){
  78.                 FB.api('/me', function(response){
  79.                     createSessionStatus(response);
  80.                 });
  81.             } else {
  82.                 createLoginElement(); // logged in but not all permissions
  83.                 return;
  84.             }
  85.         } else {
  86.             createLoginElement(); // not logged in
  87.             return;
  88.         }
  89.     },{perms: permissions.toString()});
  90. }
  91.  
  92.  
  93. /*
  94.    Function: createLoginElement
  95.  
  96.    Ccreates a link for loggin in to facebook.
  97.  
  98. */
  99. function createLoginElement(){
  100.     var element = $('facebook_session');
  101.     var loginElement = new Element('a', {'onClick' : 'facebook_login()', 'html' : 'Login to facebook'}).inject(element);
  102. }
  103.  
  104.  
  105. /*
  106.    Function: createSessionStatus
  107.  
  108.    Creates a overview of the actual sessions and an logout button.
  109.  
  110.    Parameters:
  111.  
  112.       response - The facebook API response.
  113. */
  114. function createSessionStatus(response){
  115.     var element = $('facebook_session');
  116.     element.empty();
  117.     var loginStatus = new Element('span.text', {'html' : 'Logged in to facebook as ' + response.name + ' // '}).inject(element);
  118.     var logout = new Element('a', {'onClick' : 'facebook_logout()', 'html' : 'Logout'}).inject(loginStatus);
  119. }
  120.  
  121. /**
  122.  * Class FacebookBackend
  123.  */
  124.  
  125. var FacebookBackend = new Class({
  126.     Implements: [Options],
  127.    
  128.     //Properties
  129.    
  130.     /**
  131.      * Initialize
  132.      */
  133.     initialize: function(options){
  134.         this.setOptions(options);
  135.     },
  136.    
  137.     // posts the selected event to facebook
  138.     // TODO: correct the timestamps!!
  139.     postToFacebook: function(event, eventId, el){
  140.         FB.api('/events', 'post',{ name: event[0].title, start_time:1272718027, location:"Beirut", page_id : event[0].fbId},
  141.                 function(response) {
  142.                     console.log(response);
  143.                     new Request.JSON(
  144.                     {
  145.                         url: window.location.href,
  146.                         data: 'isAjax=1&action=fillFacebookEventId&id=' + eventId + '&facebookEventId=' + response.id,
  147.                         onComplete: function()
  148.                         {
  149.                             AjaxRequest.hideBox();
  150.                         },
  151.                         onFailure: function()
  152.                         {
  153.                             alert('failed to post facebookEventId');
  154.                         },
  155.                         onSuccess: function(result)
  156.                         {
  157.                             el.getChildren().set( {src: 'system/modules/calendar/html/fb_on.gif'});
  158.                         }
  159.                     }).send();
  160.                 });
  161.     },
  162.    
  163.     // gets the selected event as JSON
  164.     getEvent2: function(el, eventId){
  165.         var self = this;
  166.         new Request.JSON(
  167.         {
  168.             url: window.location.href,
  169.             data: 'isAjax=1&action=getEvent&id=' + eventId,
  170.             onRequest: function()
  171.             {
  172.                 AjaxRequest.displayBox('Loading data …');
  173.             },
  174.             onFailure: function()
  175.             {
  176.                 alert('failed to get event');
  177.             },
  178.             onSuccess: function(event)
  179.             {
  180.                 if(loggedIn){ // check if the user is logged in on facebook
  181.                     if(!event[0].facebookEid)
  182.                         self.postToFacebook(event, eventId, el);   
  183.                 } else {
  184.                     AjaxRequest.hideBox();
  185.                     alert('You need to be logged in to facebook. Use the \'Login-Buttont\'in the right top corner to login');
  186.                 }
  187.             }
  188.         }).send();
  189.         return false;
  190.     },
  191.  
  192.     getEvent: function(eventId){
  193.         var self = this;
  194.         var result;
  195.         new Request.JSON({
  196.             async: false,
  197.             url: window.location.href,
  198.             data: 'isAjax=1&action=getEvent&id=' + eventId,
  199.             onRequest: function()
  200.             {
  201.                 AjaxRequest.displayBox('Loading data …');
  202.             },
  203.             onFailure: function()
  204.             {
  205.                 alert('failed to get event');
  206.             },
  207.             onSuccess: function(event)
  208.             {
  209.                 result = event;
  210.             }
  211.         }).send();
  212.         return result;
  213.     },
  214.    
  215.     publishEventOnFacebook: function(el, eventId){
  216.         var self = this;
  217.         var event = this.getEvent(eventId);
  218.             FB.api('/events', 'post',{ name: event[0].title, start_time:1272718027, location:"Beirut", page_id : event[0].fbId},
  219.                 function(response) {
  220.                     new Request.JSON(
  221.                     {
  222.                         url: window.location.href,
  223.                         data: 'isAjax=1&action=fillFacebookEventId&id=' + eventId + '&facebookEventId=' + response.id,
  224.                         onComplete: function()
  225.                         {
  226.                             AjaxRequest.hideBox();
  227.                         },
  228.                         onFailure: function()
  229.                         {
  230.                             alert('failed to post facebookEventId');
  231.                         },
  232.                         onSuccess: function(result)
  233.                         {
  234.                             el.set( {onclick: 'Backend.getScrollOffset(); return myFacebookBackend.deleteEventOnFacebook(this, ' + eventId + ')'})
  235.                             el.getChildren().set( {src: 'system/modules/calendar/html/fb_on.gif'});
  236.                         }
  237.                     }).send();
  238.             });
  239.         return false;
  240.     },
  241.    
  242.     deleteEventOnFacebook: function(el,eventId){
  243.         var self = this;
  244.         var event = this.getEvent(eventId);
  245.             FB.api('/' + event[0].facebookEid, 'delete', { },
  246.                 function(response) {
  247.                     new Request.JSON(
  248.                     {
  249.                         url: window.location.href,
  250.                         data: 'isAjax=1&action=deleteFacebookEventId&id=' + eventId,
  251.                         onComplete: function()
  252.                         {
  253.                             AjaxRequest.hideBox();
  254.                         },
  255.                         onFailure: function()
  256.                         {
  257.                             alert('failed to post facebookEventId');
  258.                         },
  259.                         onSuccess: function(result)
  260.                         {
  261.                             el.set( {onclick: 'Backend.getScrollOffset(); return myFacebookBackend.publishEventOnFacebook(this, ' + eventId + ')'});
  262.                             el.getChildren().set( {src: 'system/modules/calendar/html/fb_off.gif'});
  263.                             console.log(result);
  264.                         }
  265.                     }).send();
  266.             });
  267.         return false;
  268.     }
  269.    
  270.     // TODO: $a = $this->api->api("/{$this->id}", "DELETE" , '');
  271.     // delete event on facebook
  272. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement