Merge branch 'master' of https://github.com/odoo/odoo
[odoo/odoo.git] / addons / google_calendar / static / src / js / calendar_sync.js
1 openerp.google_calendar = function(instance) {
2     var _t = instance.web._t,
3        _lt = instance.web._lt;
4     var QWeb = instance.web.qweb;
5
6     instance.web_calendar.CalendarView.include({
7         view_loading: function(r) {
8             var self = this;
9             this.$el.on('click', 'div.oe_cal_sync_button', function() {
10                 self.sync_calendar(r);
11             });
12             return this._super(r);
13         },
14         sync_calendar: function(res, button) {
15             var self = this;
16             var context = instance.web.pyeval.eval('context');
17             //$('div.oe_cal_sync_button').hide();
18             $('div.oe_cal_sync_button').prop('disabled', true);
19
20             self.rpc('/google_calendar/sync_data', {
21                 arch: res.arch,
22                 fields: res.fields,
23                 model: res.model,
24                 fromurl: window.location.href,
25                 local_context: context
26             }).done(function(o) {
27                 if (o.status === "need_auth") {
28                     alert(_t("You will be redirected to Google to authorize access to your calendar!"));
29                     instance.web.redirect(o.url);
30                 }
31                 else if (o.status === "need_config_from_admin"){
32                     if (!_.isUndefined(o.action) && parseInt(o.action)){
33                         if (confirm(_t("The Google Synchronization needs to be configured before you can use it, do you want to do it now?"))) {
34                             self.do_action(o.action);
35                         }
36                     }
37                     else{
38                         alert(_t("An administrator needs to configure Google Synchronization before you can use it!"));
39                     }
40                 }
41                 else if (o.status === "need_refresh"){
42                     self.$calendar.fullCalendar('refetchEvents');
43                 }
44                 else if (o.status === "need_reset"){
45                     var confirm_text1 = _t("The account you are trying to synchronize (%s) is not the same as the last one used (%s)!");
46                     var confirm_text2 = _t("In order to do this, you first need to disconnect all existing events from the old account.");
47                     var confirm_text3 = _t("Do you want to do this now?");
48                     var text = _.str.sprintf(confirm_text1 + "\n" + confirm_text2 + "\n\n" + confirm_text3, o.info.new_name, o.info.old_name);
49                     if (confirm(text)) {
50                         self.rpc('/google_calendar/remove_references', {
51                             model:res.model,
52                             local_context:context
53                         }).done(function(o) {
54                             if (o.status === "OK") {
55                                 alert(_t("All events have been disconnected from your previous account. You can now restart the synchronization"));
56                             }
57                             else if (o.status === "KO") {
58                                 alert(_t("An error occured while disconnecting events from your previous account. Please retry or contact your administrator."));
59                             }
60                             //else NOP
61                         });
62                     }
63                 }
64             }).always(function(o) { $('div.oe_cal_sync_button').prop('disabled',false); });
65         }
66     });
67     
68     instance.web_calendar.CalendarView.include({
69         extraSideBar: function() {
70             this._super();
71             if (this.dataset.model == "calendar.event") {
72                 this.$el.find('.oe_calendar_filter').prepend(QWeb.render('GoogleCalendar.buttonSynchro'));
73             }
74         }
75     });
76
77 };