Ext.namespace("Zarafa.plugins.facebook.data");Zarafa.plugins.facebook.data.FbEventStore=Ext.extend(Ext.data.Store,{constructor:function(a){a=a||{};Ext.applyIf(a,{reader:new Zarafa.plugins.facebook.data.FbEventDataReader({id:"id",idProperty:"id",dynamicRecord:false}),writer:new Zarafa.core.data.JsonWriter,proxy:new Zarafa.plugins.facebook.data.FbEventProxy});Zarafa.plugins.facebook.data.FbEventStore.superclass.constructor.call(this,a)}});Ext.reg("facebook.fbeventstore",Zarafa.plugins.facebook.data.FbEventStore);
Ext.namespace("Zarafa.plugins.facebook.data");Zarafa.plugins.facebook.data.FbEventRecordFields=[{name:"location",type:"string",defaultValue:""},{name:"name",type:"string",defaultValue:""},{name:"rsvp_status",type:"string",defaultValue:""},{name:"start_time",type:"date",dateFormat:"timestamp",defaultValue:null},{name:"end_time",type:"date",dateFormat:"timestamp",defaultValue:null},{name:"description",type:"string",defaultValue:""}];Zarafa.core.data.RecordCustomObjectType.addProperty("ZARAFA_FACEBOOK_EVENT");
Zarafa.core.data.RecordFactory.addFieldToCustomType(Zarafa.core.data.RecordCustomObjectType.ZARAFA_FACEBOOK_EVENT,Zarafa.plugins.facebook.data.FbEventRecordFields);
Zarafa.plugins.facebook.data.FbEventRecord=Ext.extend(Zarafa.core.data.IPMRecord,{baseIdProperties:["id"],convertToAppointmentRecord:function(a){return Zarafa.core.data.RecordFactory.createRecordObjectByMessageClass("IPM.Appointment",{startdate:new Date(this.get("start_time")),duedate:new Date(this.get("end_time")),location:this.get("location"),subject:this.get("name"),commonstart:new Date(this.get("start_time")),commonend:new Date(this.get("end_time")),parent_entryid:a.get("entryid"),store_entryid:a.get("store_entryid")})}});
Zarafa.core.data.RecordFactory.setBaseClassToCustomType(Zarafa.core.data.RecordCustomObjectType.ZARAFA_FACEBOOK_EVENT,Zarafa.plugins.facebook.data.FbEventRecord);Ext.namespace("Zarafa.plugins.facebook.data");
Zarafa.plugins.facebook.data.FbEventProxy=Ext.extend(Ext.data.DataProxy,{constructor:function(a){var b={};a=a||{};b[Ext.data.Api.actions.read]=true;b[Ext.data.Api.actions.create]=true;Ext.apply(a,{api:b});Zarafa.plugins.facebook.data.FbEventProxy.superclass.constructor.call(this,a)},doRequest:function(a,b,c,d,h,g){a=="read"&&FB.api("/me/events",function(f){h.call(g,d.readRecords(f),{},true)})}});Ext.namespace("Zarafa.plugins.facebook.data");
Zarafa.plugins.facebook.data.FbEventDataReader=Ext.extend(Ext.data.DataReader,{customObjectType:Zarafa.core.data.RecordCustomObjectType.ZARAFA_FACEBOOK_EVENT,constructor:function(a,b){a=Ext.applyIf(a||{},{id:"id",idProperty:"id"});Ext.isDefined(b)||(b=Zarafa.core.data.RecordFactory.getRecordClassByCustomType(a.customObjectType||this.customObjectType));Zarafa.plugins.facebook.data.FbEventDataReader.superclass.constructor.call(this,a,b)},readRecords:function(a){var b=true,c,d=a.data.length;if(this.meta.totalProperty){c=
parseInt(this.getTotal(a),10);isNaN(c)||(d=c)}if(this.meta.successProperty){c=this.getSuccess(a);if(c===false||c==="false")b=false}a=this.extractData(a.data,true);return{success:b,records:a,totalRecords:d}},getId:function(a){return a.id},extractData:function(a,b){var c=[],d=new Date,h=d.add(Date.MONTH,1);Ext.each(a,function(k){var l=new Date(k.start_time);l>d&&l<h&&c.push(k)});a=c;var g=this instanceof Ext.data.JsonReader?"json":"node";if(this.isData(a)&&!(this instanceof Ext.data.XmlReader))a=[a];
var f=this.recordType.prototype.fields,m=f.items;f=f.length;var i=[];if(b===true)for(var e=0;e<a.length;e++){var j=a[e],n=Zarafa.core.data.RecordFactory.createRecordObjectByCustomType(Zarafa.core.data.RecordCustomObjectType.ZARAFA_FACEBOOK_EVENT,this.extractValues(j,m,f),this.getId(j));n[g]=j;i.push(n)}else for(e=0;e<a.length;e++){g=this.extractValues(a[e],m,f);g[this.meta.idProperty]=this.getId(a[e]);i.push(g)}return i},extractValues:function(a){return a}});Ext.namespace("Zarafa.plugins.facebook");
Zarafa.plugins.facebook.FbIntegrationPanel=Ext.extend(Ext.form.FormPanel,{constructor:function(a){a=a||{};var b=this;Ext.apply(a,{xtype:"facebook.FbIntegrationPanel",layout:{type:"form",align:"stretch"},anchor:"100%",bodyStyle:"background-color: inherit;",defaults:{border:true,bodyStyle:"background-color: inherit; padding: 3px 0px 3px 0px; border-style: none none solid none;"},items:[this.createFbLoginButton(),this.createImportButton()]});FB.Event.subscribe("auth.authResponseChange",function(){b.dialog.close()});
Zarafa.plugins.facebook.FbIntegrationPanel.superclass.constructor.call(this,a)},createFbLoginButton:function(){return FB.getAuthResponse()?{xtype:"button",width:100,border:false,text:_("Logout from Facebook"),anchor:"100%",handler:this.fbLogout,scope:this,ref:"/fbLoginButton",name:"fbLoginButton",allowBlank:false}:{xtype:"button",width:100,border:false,text:_("Login to Facebook"),anchor:"100%",handler:this.fbLogin,scope:this,ref:"/fbLoginButton",name:"fbLoginButton",allowBlank:false}},fbLogin:function(){var a=
this;FB.login(function(b){b.authResponse&&a.dialog.close()},{scope:"user_events, user_birthday"})},fbLogout:function(){FB.logout()},createImportButton:function(){return{xtype:"button",width:250,border:false,text:_("List events for selected period from Facebook"),anchor:"100%",handler:this.importAllEvents,scope:this,allowBlank:false}},importAllEvents:function(){var a=this;FB.getAuthResponse()?Zarafa.plugins.facebook.FbEventSelectionDialog.create():FB.login(function(b){if(b.authResponse){Zarafa.plugins.facebook.FbEventSelectionDialog.create();
a.dialog.close()}},{scope:"user_events, user_birthday"})}});Zarafa.core.ui.Dialog.register(Zarafa.plugins.facebook.FbIntegrationPanel,"facebook.FbIntegrationPanel");Ext.namespace("Zarafa.plugins.facebook");
Zarafa.plugins.facebook.FbIntegrationDialog=Ext.extend(Zarafa.core.ui.Dialog,{constructor:function(a){a=a||{};Ext.applyIf(a,{layout:"",defaultTitle:_("Facebook events integration"),alwaysUseDefaultTitle:true,useShadowStore:false,closeOnSave:true,items:[{xtype:"facebook.FbIntegrationPanel"}]});Zarafa.plugins.facebook.FbIntegrationDialog.superclass.constructor.call(this,a)}});Zarafa.core.ui.Dialog.register(Zarafa.plugins.facebook.FbIntegrationDialog,"facebook.FbIntegrationDialog");Ext.namespace("Zarafa.plugins.facebook");
Zarafa.plugins.facebook.FbEventSelectionGrid=Ext.extend(Ext.grid.GridPanel,{constructor:function(a){a=a||{};Ext.apply(a,{colModel:new Ext.grid.ColumnModel({defaults:{width:120,sortable:false},columns:[{header:_("Name"),name:"name",dataIndex:"name"},{header:_("Status"),name:"rsvp_status",dataIndex:"rsvp_status"},{header:_("Start Time"),name:"start_time",dataIndex:"start_time"},{header:_("End Time"),name:"end_time",dataIndex:"end_time"},{header:_("Location"),name:"location",dataIndex:"location"}]}),
frame:true,viewConfig:{forceFit:true},sm:new Ext.grid.RowSelectionModel({multiSelect:true}),enableRowBody:true});Zarafa.plugins.facebook.FbEventSelectionGrid.superclass.constructor.call(this,a)},saveEvents:function(){var a=new Zarafa.calendar.AppointmentStore,b=container.getHierarchyStore().getDefaultFolder("calendar"),c=this.selModel.getSelections();Ext.each(c,function(d){d=d.convertToAppointmentRecord(b);a.add(d);a.save()},this);this.dialog.close()}});Ext.reg("facebook.FbEventSelectionGrid",Zarafa.plugins.facebook.FbEventSelectionGrid);
Ext.namespace("Zarafa.plugins.facebook");
Zarafa.plugins.facebook.FbEventSelectionDialog=Ext.extend(Zarafa.core.ui.Dialog,{store:undefined,constructor:function(a){a=a||{};this.store=a.store||new Zarafa.plugins.facebook.data.FbEventStore;this.store.load();Ext.apply(this,{useShadowStore:false,closeOnSave:true,items:[{xtype:"button",width:300,text:_("Save"),handler:this.onSave,scope:this},{xtype:"facebook.FbEventSelectionGrid",store:this.store,height:this.height,ref:"selectionGrid"}]});Zarafa.plugins.facebook.FbEventSelectionDialog.superclass.constructor.call(this,a)},
onSave:function(){this.selectionGrid.saveEvents()}});Zarafa.core.ui.Dialog.register(Zarafa.plugins.facebook.FbEventSelectionDialog,"facebook.eventsSelectionDialog");Ext.namespace("Zarafa.plugins.facebook");
Zarafa.plugins.facebook.FacebookEventsPlugin=Ext.extend(Zarafa.core.Plugin,{constructor:function(a){a=a||{};Zarafa.plugins.facebook.FacebookEventsPlugin.superclass.constructor.call(this,a);this.init();this.fbinit()},fbinit:function(){FB.init({appId:"223761867729127",channelUrl:"//zarafa.com.test/channel",status:true,cookie:true,xfbml:true})},init:function(){this.registerInsertionPoint("navigation.south",this.createFacebookButton,this)},createFacebookButton:function(){return{xtype:"button",text:_("Import Facebook events"),
iconCls:"icon_facebook_button",navigationContext:container.getContextByName("calendar"),handler:this.onImportFacebookEvents,scope:this}},onImportFacebookEvents:function(){Zarafa.plugins.facebook.FbIntegrationDialog.create()}});Zarafa.onReady(function(){container.getSettingsModel().get("zarafa/v1/plugins/facebook/enable")===true&&container.registerPlugin(new Zarafa.plugins.facebook.FacebookEventsPlugin)});
