2013-05-09 14:10:01 +00:00
|
|
|
openerp.google_docs = function (instance, m) {
|
|
|
|
var _t = instance.web._t,
|
|
|
|
QWeb = instance.web.qweb;
|
|
|
|
|
2012-10-17 07:10:08 +00:00
|
|
|
instance.web.Sidebar.include({
|
2013-05-09 14:10:01 +00:00
|
|
|
start: function () {
|
2012-08-30 16:15:31 +00:00
|
|
|
var self = this;
|
2013-05-09 14:10:01 +00:00
|
|
|
var ids
|
2012-10-17 06:49:06 +00:00
|
|
|
this._super.apply(this, arguments);
|
2012-10-19 10:40:20 +00:00
|
|
|
var view = self.getParent();
|
2013-03-08 13:30:39 +00:00
|
|
|
var result;
|
2013-05-09 14:10:01 +00:00
|
|
|
if (view.fields_view.type == "form") {
|
|
|
|
ids = []
|
|
|
|
view.on("load_record", self, function (r) {
|
|
|
|
ids = [r.id]
|
|
|
|
self.add_gdoc_items(view, r.id)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add_gdoc_items: function (view, res_id) {
|
|
|
|
var self = this;
|
|
|
|
var gdoc_item = _.indexOf(_.pluck(self.items.other, 'classname'), 'oe_share_gdoc');
|
|
|
|
if (gdoc_item !== -1) {
|
|
|
|
self.items.other.splice(gdoc_item, 1);
|
|
|
|
}
|
|
|
|
if (res_id) {
|
|
|
|
view.sidebar_eval_context().done(function (context) {
|
2013-05-10 06:35:01 +00:00
|
|
|
var ds = new instance.web.DataSet(this, 'google.docs.config', context);
|
2013-05-09 14:10:01 +00:00
|
|
|
ds.call('get_google_docs_config', [view.dataset.model, res_id, context]).done(function (r) {
|
|
|
|
if (!_.isEmpty(r)) {
|
|
|
|
_.each(r, function (res) {
|
|
|
|
var g_item = _.indexOf(_.pluck(self.items.other, 'label'), res.name);
|
|
|
|
if (g_item !== -1) {
|
|
|
|
self.items.other.splice(g_item, 1);
|
|
|
|
}
|
|
|
|
self.add_items('other', [{
|
|
|
|
label: res.name,
|
|
|
|
config_id: res.id,
|
|
|
|
res_id: res_id,
|
|
|
|
res_model: view.dataset.model,
|
|
|
|
callback: self.on_google_doc,
|
|
|
|
classname: 'oe_share_gdoc'
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2013-05-10 06:35:01 +00:00
|
|
|
|
|
|
|
fetch: function (model, fields, domain, ctx) {
|
2013-05-09 14:10:01 +00:00
|
|
|
return new instance.web.Model(model).query(fields).filter(domain).context(ctx).all()
|
|
|
|
},
|
2013-05-10 06:35:01 +00:00
|
|
|
|
2013-05-09 14:10:01 +00:00
|
|
|
on_google_doc: function (doc_item) {
|
2013-05-10 06:35:01 +00:00
|
|
|
var self = this;
|
|
|
|
self.config = doc_item;
|
|
|
|
var loaded = self.fetch('google.docs.config', ['gdocs_resource_id', 'google_client_id'], [['id', '=', doc_item.config_id]])
|
|
|
|
.then(function (configs) {
|
|
|
|
var ds = new instance.web.DataSet(self, 'google.docs.config');
|
2013-05-09 14:10:01 +00:00
|
|
|
ds.call('get_google_doc_name', [[doc_item.config_id], doc_item.res_id]).done(function (r) {
|
|
|
|
if (!_.isEmpty(r)) {
|
2013-05-10 06:35:01 +00:00
|
|
|
self.OAUTHURL = 'https://accounts.google.com/o/oauth2/auth?';
|
|
|
|
self.VALIDURL = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=';
|
|
|
|
self.SCOPES = 'https://www.googleapis.com/auth/drive';
|
|
|
|
self.CLIENT_ID = configs[0].google_client_id;
|
|
|
|
self.GDOC_NAME = r[doc_item.config_id]['name'];
|
|
|
|
self.GDOCS_TEMPLATE_ID = configs[0].gdocs_resource_id;
|
|
|
|
self.GDOC_URL = r[doc_item.config_id]['url'];
|
|
|
|
self.handleClientLoad();
|
2013-05-09 14:10:01 +00:00
|
|
|
}
|
2013-05-10 06:35:01 +00:00
|
|
|
});
|
2013-05-09 14:10:01 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handleClientLoad: function () {
|
|
|
|
var self = this;
|
|
|
|
window.setTimeout(function () {
|
|
|
|
self.checkAuth(self)
|
|
|
|
}, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
checkAuth: function (self) {
|
|
|
|
gapi.auth.authorize({
|
|
|
|
'client_id': self.CLIENT_ID,
|
|
|
|
'scope': self.SCOPES,
|
|
|
|
'immediate': true
|
|
|
|
}, function (authResult) {
|
|
|
|
self.handleAuthResult(self, authResult)
|
|
|
|
});
|
|
|
|
},
|
2013-03-08 13:30:39 +00:00
|
|
|
|
2013-05-09 14:10:01 +00:00
|
|
|
handleAuthResult: function (self, authResult) {
|
2013-05-10 06:35:01 +00:00
|
|
|
|
2013-05-09 14:10:01 +00:00
|
|
|
if (authResult && !authResult.error) {
|
|
|
|
self.clientLoad(self);
|
|
|
|
} else {
|
|
|
|
gapi.auth.authorize({
|
|
|
|
'client_id': self.CLIENT_ID,
|
|
|
|
'scope': self.SCOPES,
|
|
|
|
'immediate': false
|
2013-05-10 06:35:01 +00:00
|
|
|
},function (authResult) {
|
|
|
|
self.handleAuthResult(self, authResult)
|
|
|
|
});
|
2013-05-09 14:10:01 +00:00
|
|
|
}
|
|
|
|
},
|
2013-03-08 13:30:39 +00:00
|
|
|
|
2013-05-09 14:10:01 +00:00
|
|
|
clientLoad: function (self) {
|
|
|
|
gapi.client.load('drive', 'v2', function () {
|
2013-05-10 06:35:01 +00:00
|
|
|
if (self.GDOC_URL == false) {
|
|
|
|
self.copyFile(self.config, self.GDOCS_TEMPLATE_ID, self.GDOC_NAME);
|
|
|
|
} else {
|
|
|
|
window.open(self.GDOC_URL, '_blank');
|
|
|
|
}
|
2013-05-09 14:10:01 +00:00
|
|
|
});
|
|
|
|
},
|
2013-03-11 11:26:49 +00:00
|
|
|
|
2013-05-09 14:10:01 +00:00
|
|
|
copyFile: function (config, originFileId, copyTitle) {
|
|
|
|
var body = {
|
|
|
|
'title': copyTitle
|
|
|
|
};
|
|
|
|
var request = gapi.client.drive.files.copy({
|
|
|
|
'fileId': originFileId,
|
|
|
|
'resource': body
|
|
|
|
});
|
|
|
|
request.execute(function (resp) {
|
|
|
|
console.log('Copy ID: ' + resp.id);
|
|
|
|
var get_new_file = gapi.client.drive.files.get({
|
|
|
|
'fileId': resp.id
|
|
|
|
});
|
|
|
|
get_new_file.execute(function (file) {
|
2013-05-10 06:35:01 +00:00
|
|
|
var ds = new instance.web.DataSet(self, 'ir.attachment');
|
|
|
|
vals = {
|
|
|
|
'res_model': config.res_model,
|
|
|
|
'res_id': config.res_id,
|
|
|
|
'type': 'url',
|
|
|
|
'name': copyTitle,
|
|
|
|
'url': file.alternateLink
|
|
|
|
}
|
2013-05-09 14:10:01 +00:00
|
|
|
ds.call('create', [vals]).done(function (r) {
|
2013-05-10 06:35:01 +00:00
|
|
|
window.open(file.alternateLink, '_blank');
|
2013-05-09 14:10:01 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2012-10-03 11:22:27 +00:00
|
|
|
});
|
2013-05-09 14:10:01 +00:00
|
|
|
};
|