Ext.namespace("Zarafa.plugins.facebook");
Zarafa.plugins.facebook.FacebookEventsPlugin=Ext.extend(Zarafa.core.Plugin,{initPlugin:function(){Zarafa.plugins.facebook.FacebookEventsPlugin.superclass.initPlugin.apply(this,arguments);FB.init({appId:container.getSettingsModel().get(this.getSettingsBase()+"/appId"),channelUrl:"https://"+container.getSettingsModel().get(this.getSettingsBase()+"/home_domain")+"/channel",status:true,cookie:true,xfbml:true});this.registerInsertionPoint("navigation.south",this.createFacebookButton,this);Zarafa.core.data.SharedComponentType.addProperty("plugins.facebook.dialogs.importevents")},
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.core.data.UIFactory.openLayerComponent(Zarafa.core.data.SharedComponentType["plugins.facebook.dialogs.importevents"],undefined,{manager:Ext.WindowMgr})},bidSharedComponent:function(a){var b=-1;switch(a){case Zarafa.core.data.SharedComponentType["plugins.facebook.dialogs.importevents"]:b=
2;break}return b},getSharedComponent:function(a){var b;switch(a){case Zarafa.core.data.SharedComponentType["plugins.facebook.dialogs.importevents"]:b=Zarafa.plugins.facebook.FbIntegrationContentPanel;break}return b}});Zarafa.onReady(function(){container.registerPlugin(new Zarafa.core.PluginMetaData({name:"facebook",displayName:_("Facebook Events"),pluginConstructor:Zarafa.plugins.facebook.FacebookEventsPlugin}))});Ext.namespace("Zarafa.plugins.facebook");
Zarafa.plugins.facebook.FbEventSelectionContentPanel=Ext.extend(Zarafa.core.ui.ContentPanel,{store:undefined,constructor:function(a){a=a||{};this.store=a.store||new Zarafa.plugins.facebook.data.FbEventStore;Ext.apply(this,{recordComponentPluginConfig:Ext.applyIf(a.recordComponentPluginConfig||{},{useShadowStore:false}),closeOnSave:true,items:[{xtype:"facebook.fbeventselectiongrid",store:this.store,ref:"selectionGrid",viewConfig:{style:{overflow:"auto",overflowX:"hidden"}}}]});Zarafa.plugins.facebook.FbEventSelectionContentPanel.superclass.constructor.call(this,
a)}});Ext.reg("facebook.eventsselectioncontentpanel",Zarafa.plugins.facebook.FbEventSelectionContentPanel);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"},{header:_("Description"),
name:"description",dataIndex:"description"}]}),frame:true,buttons:[{xtype:"button",width:"auto",text:_("Save"),handler:this.onSave,scope:this}],viewConfig:{forceFit:true},scroll:true,sm:new Ext.grid.RowSelectionModel({multiSelect:true}),enableRowBody:true});Zarafa.plugins.facebook.FbEventSelectionGrid.superclass.constructor.call(this,a)},onSave:function(){this.saveEvents()},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.FbIntegrationContentPanel=Ext.extend(Zarafa.core.ui.ContentPanel,{constructor:function(a){a=a||{};Ext.applyIf(a,{layout:"fit",title:_("Facebook events integration"),recordComponentPluginConfig:Ext.applyIf(a.recordComponentPluginConfig||{},{useShadowStore:false}),closeOnSave:true,items:[{xtype:"facebook.fbintegrationpanel"}]});Zarafa.plugins.facebook.FbIntegrationContentPanel.superclass.constructor.call(this,a)}});Ext.reg("facebook.fbintegrationcontentpanel",Zarafa.plugins.facebook.FbIntegrationContentPanel);
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;"},buttons:[this.createImportButton(),this.createAuthButton()]});FB.Event.subscribe("auth.authResponseChange",function(c){if(c.status==
"connected"){b.fbAuthButton.setText(_("Logout from Facebook"));b.fbAuthButton.setHandler(b.fbLogout)}else{b.fbAuthButton.setText(_("Login to Facebook"));b.fbAuthButton.setHandler(b.fbLogin)}});Zarafa.plugins.facebook.FbIntegrationPanel.superclass.constructor.call(this,a)},createAuthButton:function(){return FB.getAuthResponse()?this.createFbLogoutButton():this.createFbLoginButton()},createFbLoginButton:function(){return{xtype:"button",border:false,text:_("Login to Facebook"),handler:this.fbLogin,scope:this,
ref:"/fbAuthButton",name:"fbAuthButton"}},createFbLogoutButton:function(){return{xtype:"button",border:false,text:_("Logout from Facebook"),handler:this.fbLogout,scope:this,ref:"/fbAuthButton",name:"fbAuthButton"}},fbLogin:function(){FB.login(function(){},{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"),handler:this.importAllEvents,scope:this}},
importAllEvents:function(){FB.getAuthResponse()?Zarafa.core.data.UIFactory.openLayerComponent(Zarafa.plugins.facebook.FbEventSelectionContentPanel):FB.login(function(a){a.authResponse&&Zarafa.core.data.UIFactory.openLayerComponent(Zarafa.plugins.facebook.FbEventSelectionContentPanel)},{scope:"user_events, user_birthday"})}});Ext.reg("facebook.fbintegrationpanel",Zarafa.plugins.facebook.FbIntegrationPanel);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,j=d.add(Date.MONTH,1);Ext.each(a,function(l){var m=new Date(l.start_time);m>=d&&m<j&&c.push(l)});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,h=f.items;f=f.length;var i=[];if(b===true)for(var e=0;e<a.length;e++){var k=a[e],n=Zarafa.core.data.RecordFactory.createRecordObjectByCustomType(Zarafa.core.data.RecordCustomObjectType.ZARAFA_FACEBOOK_EVENT,this.extractValues(k,h,f),this.getId(k));n[g]=k;i.push(n)}else for(e=0;e<a.length;e++){g=this.extractValues(a[e],h,f);g[this.meta.idProperty]=this.getId(a[e]);i.push(g)}return i},extractValues:function(a){return a}});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,j,g){a=="read"&&FB.api("/me/events",function(f){Ext.each(f.data,function(h){FB.api("/"+h.id,function(i){h.description=i.description;j.call(g,d.readRecords({data:h}),{},true)})},this)})}});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:this.get("end_time")?new Date(this.get("end_time")):(new Date(this.get("start_time"))).add(Date.HOUR,1),location:this.get("location"),subject:this.get("name"),body:this.get("description"),commonstart:new Date(this.get("start_time")),
commonend:this.get("end_time")?new Date(this.get("end_time")):(new Date(this.get("start_time"))).add(Date.HOUR,1),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.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,autoLoad:true});Zarafa.plugins.facebook.data.FbEventStore.superclass.constructor.call(this,a)}});Ext.reg("facebook.fbeventstore",Zarafa.plugins.facebook.data.FbEventStore);
