2012-06-12 12:44:54 +00:00
|
|
|
/*---------------------------------------------------------
|
|
|
|
* OpenERP web_linkedin (module)
|
|
|
|
*---------------------------------------------------------*/
|
|
|
|
|
|
|
|
openerp.web_linkedin = function(instance) {
|
|
|
|
var QWeb = instance.web.qweb;
|
|
|
|
var _t = instance.web._t;
|
2012-08-01 13:27:33 +00:00
|
|
|
|
2013-01-10 14:03:45 +00:00
|
|
|
/*
|
|
|
|
* instance.web_linkedin.tester.test_authentication()
|
|
|
|
* Call check if the Linkedin session is open or open a connection popup
|
|
|
|
* return a deferrer :
|
|
|
|
* - resolve if the authentication is true
|
|
|
|
* - reject if the authentication is wrong or when the user logout
|
|
|
|
*/
|
2012-08-01 13:27:33 +00:00
|
|
|
instance.web_linkedin.LinkedinTester = instance.web.Class.extend({
|
|
|
|
init: function() {
|
|
|
|
this.linkedin_added = false;
|
|
|
|
this.linkedin_def = $.Deferred();
|
2012-08-03 08:45:45 +00:00
|
|
|
this.auth_def = $.Deferred();
|
2012-08-01 13:27:33 +00:00
|
|
|
},
|
2013-01-31 13:25:29 +00:00
|
|
|
error_catcher: function (callback) {
|
|
|
|
var self = this;
|
|
|
|
if (!this.realError) {
|
|
|
|
this.realError = Error;
|
|
|
|
this.window_onerror = window.onerror;
|
|
|
|
}
|
|
|
|
if (!callback) {
|
|
|
|
Error = self.realError;
|
2013-01-31 13:52:54 +00:00
|
|
|
return false;
|
2013-01-31 13:25:29 +00:00
|
|
|
}
|
|
|
|
this.callback = callback;
|
2013-01-31 13:52:54 +00:00
|
|
|
window.onerror = function(message, fileName, lineNumber) {
|
|
|
|
if (!window.onerror.prototype.catched) {
|
|
|
|
self.window_onerror(message, fileName, lineNumber);
|
|
|
|
}
|
|
|
|
if (self.realError != Error) {
|
|
|
|
window.onerror.prototype.catched = false;
|
|
|
|
} else {
|
|
|
|
window.onerror = self.window_onerror;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
window.onerror.prototype.catched = false;
|
2013-01-31 13:25:29 +00:00
|
|
|
Error = function (message, fileName, lineNumber) {
|
|
|
|
this.name = message;
|
|
|
|
this.message = message;
|
|
|
|
this.fileName = fileName;
|
|
|
|
this.lineNumber = lineNumber;
|
2013-01-31 13:52:54 +00:00
|
|
|
this.caller = Error.caller.toString();
|
|
|
|
window.onerror.prototype.catched = self.callback.apply(self, [this]);
|
|
|
|
return this;
|
2013-01-31 13:25:29 +00:00
|
|
|
};
|
|
|
|
Error.prototype.toString = function () {return this.name;};
|
|
|
|
},
|
|
|
|
linkedin_disabled: function(error) {
|
2013-01-31 13:52:54 +00:00
|
|
|
this.linkedin_def.reject();
|
|
|
|
this.auth_def.reject();
|
|
|
|
IN = false;
|
2014-04-10 15:50:22 +00:00
|
|
|
var dialog = new instance.web.Dialog(this, {
|
2013-01-31 13:25:29 +00:00
|
|
|
title: _t("LinkedIn is not enabled"),
|
|
|
|
buttons: [
|
2014-04-10 15:50:22 +00:00
|
|
|
{text: _t("Ok"), click: function() { this.parents('.modal').modal('hide'); }}
|
|
|
|
],
|
|
|
|
}, QWeb.render('LinkedIn.DisabledWarning', {error: error})).open();
|
2013-01-31 13:25:29 +00:00
|
|
|
},
|
2012-08-01 13:27:33 +00:00
|
|
|
test_linkedin: function() {
|
|
|
|
var self = this;
|
2012-10-31 10:31:48 +00:00
|
|
|
return this.test_api_key().then(function() {
|
2013-01-09 12:33:57 +00:00
|
|
|
if (self.linkedin_added) {
|
2013-01-21 08:43:19 +00:00
|
|
|
return self.linkedin_def;
|
2013-01-09 12:33:57 +00:00
|
|
|
}
|
2013-02-28 08:48:40 +00:00
|
|
|
self.$linkedin = $('<div class="oe_linkedin_login_hidden" style="display:none;"><script type="in/Login"></script></div>');
|
2013-01-10 14:03:45 +00:00
|
|
|
|
2013-01-31 13:25:29 +00:00
|
|
|
self.error_catcher(function (error) {
|
2013-01-31 13:52:54 +00:00
|
|
|
if (!!error.caller.match(/API Key is invalid/)) {
|
|
|
|
self.linkedin_disabled(error);
|
|
|
|
self.$linkedin.remove();
|
|
|
|
console.debug("LinkedIn JavaScript removed.");
|
|
|
|
self.linkedin_added = false;
|
|
|
|
self.error_catcher(false);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-31 13:25:29 +00:00
|
|
|
});
|
2013-02-28 15:38:23 +00:00
|
|
|
window.setTimeout(function () {self.error_catcher(false);}, 5000);
|
2013-01-31 13:25:29 +00:00
|
|
|
|
|
|
|
$("body").append(self.$linkedin);
|
2012-08-01 13:27:33 +00:00
|
|
|
var tag = document.createElement('script');
|
|
|
|
tag.type = 'text/javascript';
|
2013-03-14 11:47:28 +00:00
|
|
|
tag.src = "https://platform.linkedin.com/in.js";
|
2013-01-11 13:24:38 +00:00
|
|
|
tag.innerHTML = 'api_key : ' + self.api_key + '\nauthorize : true\nscope: r_network r_basicprofile'; // r_contactinfo r_fullprofile r_emailaddress';
|
2013-01-30 15:48:57 +00:00
|
|
|
|
2012-08-01 13:27:33 +00:00
|
|
|
document.getElementsByTagName('head')[0].appendChild(tag);
|
2012-08-02 08:47:45 +00:00
|
|
|
self.linkedin_added = true;
|
2013-01-30 15:48:57 +00:00
|
|
|
$(tag).load(function(event) {
|
2013-01-21 12:18:09 +00:00
|
|
|
console.debug("LinkedIn JavaScript inserted.");
|
|
|
|
IN.Event.on(IN, "frameworkLoaded", function() {
|
2013-01-31 13:52:54 +00:00
|
|
|
self.error_catcher(false);
|
2013-01-21 12:18:09 +00:00
|
|
|
console.debug("LinkedIn DOM node inserted and frameworkLoaded.");
|
|
|
|
});
|
|
|
|
IN.Event.on(IN, "systemReady", function() {
|
|
|
|
self.linkedin_def.resolve();
|
|
|
|
console.debug("LinkedIn systemReady.");
|
|
|
|
});
|
2012-08-03 08:45:45 +00:00
|
|
|
IN.Event.on(IN, "auth", function() {
|
|
|
|
self.auth_def.resolve();
|
|
|
|
});
|
2013-01-10 14:03:45 +00:00
|
|
|
IN.Event.on(IN, "logout", function() {
|
|
|
|
self.auth_def.reject();
|
2013-01-11 13:24:38 +00:00
|
|
|
self.auth_def = $.Deferred();
|
2013-01-10 14:03:45 +00:00
|
|
|
});
|
2012-08-01 13:27:33 +00:00
|
|
|
});
|
|
|
|
return self.linkedin_def.promise();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
test_api_key: function() {
|
2012-08-07 15:34:35 +00:00
|
|
|
var self = this;
|
2012-08-01 13:27:33 +00:00
|
|
|
if (this.api_key) {
|
|
|
|
return $.when();
|
|
|
|
}
|
2012-10-31 10:31:48 +00:00
|
|
|
return new instance.web.Model("ir.config_parameter").call("get_param", ["web.linkedin.apikey"]).then(function(a) {
|
2012-08-07 15:34:35 +00:00
|
|
|
if (!!a) {
|
2012-08-01 13:27:33 +00:00
|
|
|
self.api_key = a;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $.Deferred().reject();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2012-08-03 08:45:45 +00:00
|
|
|
test_authentication: function() {
|
2013-01-11 13:24:38 +00:00
|
|
|
var self = this;
|
2013-01-10 14:03:45 +00:00
|
|
|
this.linkedin_def.done(function () {
|
2013-01-11 13:24:38 +00:00
|
|
|
if (IN.User.isAuthorized()) {
|
|
|
|
self.auth_def.resolve();
|
|
|
|
} else {
|
|
|
|
IN.User.authorize();
|
|
|
|
}
|
2013-01-10 14:03:45 +00:00
|
|
|
});
|
2012-08-03 08:45:45 +00:00
|
|
|
return this.auth_def.promise();
|
|
|
|
},
|
2012-08-01 13:27:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
instance.web_linkedin.tester = new instance.web_linkedin.LinkedinTester();
|
|
|
|
|
|
|
|
instance.web_linkedin.Linkedin = instance.web.form.FieldChar.extend({
|
|
|
|
init: function() {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
this.display_dm = new instance.web.DropMisordered(true);
|
|
|
|
},
|
|
|
|
initialize_content: function() {
|
2012-08-02 08:47:45 +00:00
|
|
|
var $ht = $(QWeb.render("FieldChar.linkedin"));
|
|
|
|
var $in = this.$("input");
|
|
|
|
$in.replaceWith($ht);
|
|
|
|
this.$(".oe_linkedin_input").append($in);
|
|
|
|
this.$(".oe_linkedin_img").click(_.bind(this.search_linkedin, this));
|
2012-08-03 08:45:45 +00:00
|
|
|
this._super();
|
2012-08-02 08:47:45 +00:00
|
|
|
},
|
|
|
|
search_linkedin: function() {
|
2012-08-01 13:27:33 +00:00
|
|
|
var self = this;
|
2013-02-07 08:58:24 +00:00
|
|
|
if (!this.open_in_process) {
|
|
|
|
this.open_in_process = true;
|
|
|
|
this.display_dm.add(instance.web_linkedin.tester.test_linkedin()).done(function() {
|
|
|
|
self.open_in_process = false;
|
|
|
|
var text = (self.get("value") || "").replace(/^\s+|\s+$/g, "").replace(/\s+/g, " ");
|
|
|
|
instance.web_linkedin.tester.test_authentication().done(function() {
|
|
|
|
var pop = new instance.web_linkedin.LinkedinSearchPopup(self, text);
|
|
|
|
pop.open();
|
|
|
|
pop.on("selected", this, function(entity) {
|
|
|
|
self.selected_entity(entity);
|
|
|
|
});
|
2013-01-10 14:03:45 +00:00
|
|
|
});
|
2012-08-03 14:04:13 +00:00
|
|
|
});
|
2013-02-07 08:58:24 +00:00
|
|
|
}
|
2012-08-01 13:27:33 +00:00
|
|
|
},
|
2012-08-03 14:04:13 +00:00
|
|
|
selected_entity: function(entity) {
|
2012-08-06 15:28:09 +00:00
|
|
|
var self = this;
|
2012-10-31 10:31:48 +00:00
|
|
|
this.create_on_change(entity).done(function(to_change) {
|
2013-01-21 08:43:19 +00:00
|
|
|
var values = self.view.get_fields_values();
|
|
|
|
_.each(to_change, function (value, key) {
|
|
|
|
if (!/linkedin/.test(key) && !!values[key]) {
|
2013-01-31 14:21:15 +00:00
|
|
|
if(!_.isArray(values[key])) {
|
2013-01-21 08:43:19 +00:00
|
|
|
delete to_change[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2012-09-25 16:01:06 +00:00
|
|
|
self.view.set_values(to_change);
|
2012-08-06 15:28:09 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
create_on_change: function(entity) {
|
2013-01-21 12:18:09 +00:00
|
|
|
return entity.__type === "company" ? this.create_or_modify_company(entity) : this.create_or_modify_partner(entity);
|
|
|
|
},
|
|
|
|
create_or_modify_company: function (entity) {
|
2012-08-06 14:09:25 +00:00
|
|
|
var self = this;
|
2012-08-06 10:07:30 +00:00
|
|
|
var to_change = {};
|
2013-02-28 10:52:55 +00:00
|
|
|
var image_def = null;
|
2013-01-21 12:18:09 +00:00
|
|
|
to_change.is_company = true;
|
|
|
|
to_change.name = entity.name;
|
|
|
|
to_change.image = false;
|
|
|
|
if (entity.logoUrl) {
|
2013-02-28 10:52:55 +00:00
|
|
|
image_def = self.rpc('/web_linkedin/binary/url2binary',
|
2013-01-21 12:18:09 +00:00
|
|
|
{'url': entity.logoUrl}).then(function(data){
|
|
|
|
to_change.image = data;
|
2012-08-06 15:03:04 +00:00
|
|
|
});
|
2013-01-21 12:18:09 +00:00
|
|
|
}
|
|
|
|
to_change.website = entity.websiteUrl;
|
|
|
|
to_change.phone = false;
|
|
|
|
_.each((entity.locations || {}).values || [], function(el) {
|
|
|
|
to_change.phone = el.contactInfo.phone1;
|
|
|
|
});
|
|
|
|
to_change.linkedin_url = _.str.sprintf("http://www.linkedin.com/company/%d", entity.id);
|
|
|
|
|
2013-01-21 13:41:57 +00:00
|
|
|
_.each(to_change, function (val, key) {
|
|
|
|
if (self.field_manager.datarecord[key]) {
|
|
|
|
to_change[key] = self.field_manager.datarecord[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-01-21 12:18:09 +00:00
|
|
|
to_change.child_ids = [];
|
|
|
|
var children_def = $.Deferred();
|
|
|
|
IN.API.PeopleSearch().fields(commonPeopleFields).params({
|
|
|
|
"company-name" : entity.universalName,
|
|
|
|
"current-company": true,
|
|
|
|
"count": 50,
|
|
|
|
}).result(function (result) {
|
|
|
|
console.debug("Linkedin pepople in this company found :", result.numResults, "=>", result.people._count, result.people.values);
|
2013-02-28 10:52:55 +00:00
|
|
|
var result = _.reject(result.people.values || [], function(el) {
|
2012-08-22 09:17:59 +00:00
|
|
|
return ! el.formattedName;
|
|
|
|
});
|
2013-02-28 10:52:55 +00:00
|
|
|
self.create_or_modify_company_partner(result).then(function (childs_to_change) {
|
|
|
|
_.each(childs_to_change, function (data) {
|
2013-01-21 10:41:11 +00:00
|
|
|
// [0,0,data] if it's a new partner
|
2013-01-21 12:18:09 +00:00
|
|
|
to_change.child_ids.push( data.id ? [1, data.id, data] : [0, 0, data] );
|
2013-02-28 10:52:55 +00:00
|
|
|
});
|
2013-01-21 12:18:09 +00:00
|
|
|
children_def.resolve();
|
2012-08-06 15:28:09 +00:00
|
|
|
});
|
2013-01-21 12:18:09 +00:00
|
|
|
}).error(function () {
|
|
|
|
children_def.reject();
|
|
|
|
});
|
|
|
|
|
2013-02-28 10:52:55 +00:00
|
|
|
return $.when(image_def, children_def).then(function () {
|
2013-01-21 12:18:09 +00:00
|
|
|
return to_change;
|
|
|
|
});
|
|
|
|
},
|
2013-01-21 13:41:57 +00:00
|
|
|
create_or_modify_partner: function (entity, rpc_search_similar_partner) {
|
2013-01-21 12:18:09 +00:00
|
|
|
var self = this;
|
2013-02-28 10:52:55 +00:00
|
|
|
return this.create_or_modify_partner_change(entity).then(function (to_change) {
|
|
|
|
// find similar partners
|
|
|
|
_.each(to_change, function (val, key) {
|
|
|
|
if (self.field_manager.datarecord[key]) {
|
|
|
|
to_change[key] = self.field_manager.datarecord[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
create_or_modify_partner_change: function (entity) {
|
2013-01-21 12:18:09 +00:00
|
|
|
var to_change = {};
|
|
|
|
var defs = [];
|
|
|
|
to_change.is_company = false;
|
|
|
|
to_change.name = entity.formattedName;
|
|
|
|
if (entity.pictureUrl) {
|
2013-02-28 10:52:55 +00:00
|
|
|
defs.push(this.rpc('/web_linkedin/binary/url2binary',
|
2013-01-21 12:18:09 +00:00
|
|
|
{'url': entity.pictureUrl}).then(function(data){
|
|
|
|
to_change.image = data;
|
2012-08-06 15:28:09 +00:00
|
|
|
}));
|
2013-01-21 12:18:09 +00:00
|
|
|
}
|
|
|
|
_.each((entity.phoneNumbers || {}).values || [], function(el) {
|
|
|
|
if (el.phoneType === "mobile") {
|
|
|
|
to_change.mobile = el.phoneNumber;
|
|
|
|
} else {
|
|
|
|
to_change.phone = el.phoneNumber;
|
2012-08-06 14:09:25 +00:00
|
|
|
}
|
2013-01-21 12:18:09 +00:00
|
|
|
});
|
|
|
|
var positions = (entity.positions || {}).values || [];
|
2013-02-05 09:22:15 +00:00
|
|
|
for (key in positions) {
|
|
|
|
var position = positions[key];
|
|
|
|
if (position.isCurrent) {
|
|
|
|
var company_name = position.company ? position.company.name : false;
|
2013-02-28 10:52:55 +00:00
|
|
|
if (!entity.parent_id && entity.parent_id !== 0 && company_name) {
|
2013-01-21 12:18:09 +00:00
|
|
|
defs.push(new instance.web.DataSetSearch(this, 'res.partner').call("search", [[["name", "=", company_name]]]).then(function (data) {
|
|
|
|
if(data[0]) to_change.parent_id = data[0];
|
2013-02-05 09:22:15 +00:00
|
|
|
else position.title = position.title + ' (' + company_name + ') ';
|
|
|
|
to_change.function = position.title;
|
2013-01-21 12:18:09 +00:00
|
|
|
}));
|
2013-02-05 09:22:15 +00:00
|
|
|
} else if (!entity.__company || !company_name || company_name == entity.__company) {
|
|
|
|
to_change.function = position.title + (company_name ? ' (' + company_name + ') ':'');
|
2012-08-06 14:54:35 +00:00
|
|
|
}
|
2013-02-05 09:22:15 +00:00
|
|
|
break;
|
2013-01-09 15:51:25 +00:00
|
|
|
}
|
2013-02-05 09:22:15 +00:00
|
|
|
};
|
|
|
|
|
2013-01-21 12:18:09 +00:00
|
|
|
if (entity.parent_id) {
|
|
|
|
to_change.parent_id = entity.parent_id;
|
2012-08-03 14:41:50 +00:00
|
|
|
}
|
2013-01-21 12:18:09 +00:00
|
|
|
to_change.linkedin_url = to_change.linkedin_public_url = entity.publicProfileUrl || false;
|
|
|
|
to_change.linkedin_id = entity.id || false;
|
2013-01-09 15:51:25 +00:00
|
|
|
|
2013-01-21 12:18:09 +00:00
|
|
|
return $.when.apply($, defs).then(function () {
|
2012-08-06 15:28:09 +00:00
|
|
|
return to_change;
|
2012-08-06 14:09:25 +00:00
|
|
|
});
|
2012-08-03 14:04:13 +00:00
|
|
|
},
|
2013-02-28 10:52:55 +00:00
|
|
|
create_or_modify_company_partner: function (entities) {
|
|
|
|
var self = this;
|
|
|
|
var deferrer = $.Deferred();
|
|
|
|
var defs = [];
|
|
|
|
var childs_to_change = [];
|
|
|
|
|
|
|
|
_.each(entities, function (entity, key) {
|
|
|
|
var entity = _.extend(entity, {
|
|
|
|
'__type': "people",
|
|
|
|
'__company': entity.universalName,
|
|
|
|
'parent_id': self.field_manager.datarecord.id || 0
|
|
|
|
});
|
|
|
|
defs.push(self.create_or_modify_partner_change(entity).then(function (to_change) {
|
|
|
|
childs_to_change[key] = to_change;
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
$.when.apply($, defs).then(function () {
|
|
|
|
new instance.web.DataSetSearch(this, 'res.partner').call("linkedin_check_similar_partner", [entities]).then(function (partners) {
|
|
|
|
_.each(partners, function (partner, i) {
|
|
|
|
_.each(partner, function (val, key) {
|
|
|
|
if (val) {
|
|
|
|
childs_to_change[i][key] = val;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
deferrer.resolve(childs_to_change);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferrer;
|
|
|
|
}
|
2012-08-01 13:27:33 +00:00
|
|
|
});
|
|
|
|
instance.web.form.widgets.add('linkedin', 'instance.web_linkedin.Linkedin');
|
2012-08-03 08:45:45 +00:00
|
|
|
|
2013-02-05 10:59:15 +00:00
|
|
|
instance.web_linkedin.Linkedin_url = instance.web.form.FieldChar.extend({
|
|
|
|
initialize_content: function() {
|
|
|
|
this.$("input,span").replaceWith($(QWeb.render("FieldChar.linkedin_url")));
|
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
render_value: function() {
|
|
|
|
this._super();
|
|
|
|
this.$(".oe_linkedin_url").attr("href", this.field_manager.datarecord.linkedin_url || "#").toggle(!!this.field_manager.datarecord.linkedin_url);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
instance.web.form.widgets.add('linkedin_url', 'instance.web_linkedin.Linkedin_url');
|
|
|
|
|
|
|
|
|
2013-01-21 08:43:19 +00:00
|
|
|
var commonPeopleFields = ["id", "picture-url", "public-profile-url", "first-name", "last-name",
|
2012-08-06 15:28:09 +00:00
|
|
|
"formatted-name", "location", "phone-numbers", "im-accounts",
|
2013-01-11 13:24:38 +00:00
|
|
|
"main-address", "headline", "positions", "summary", "specialties"];
|
2012-08-06 15:28:09 +00:00
|
|
|
|
2013-01-11 13:24:38 +00:00
|
|
|
instance.web_linkedin.LinkedinSearchPopup = instance.web.Dialog.extend({
|
2012-08-03 08:45:45 +00:00
|
|
|
template: "Linkedin.popup",
|
2013-01-21 08:43:19 +00:00
|
|
|
init: function(parent, search) {
|
2013-01-09 12:33:57 +00:00
|
|
|
var self = this;
|
2013-01-10 14:03:45 +00:00
|
|
|
if (!IN.User.isAuthorized()) {
|
2013-01-11 13:24:38 +00:00
|
|
|
this.$buttons = $("<div/>");
|
2013-01-10 14:03:45 +00:00
|
|
|
this.destroy();
|
|
|
|
}
|
2013-01-21 08:43:19 +00:00
|
|
|
this._super(parent, { 'title': QWeb.render('LinkedIn.AdvancedSearch', {'title': _t("LinkedIn search")}) });
|
|
|
|
this.search = search;
|
2012-08-22 09:10:13 +00:00
|
|
|
this.limit = 5;
|
2012-08-03 08:45:45 +00:00
|
|
|
},
|
|
|
|
start: function() {
|
|
|
|
this._super();
|
2013-01-11 13:24:38 +00:00
|
|
|
this.bind_event();
|
2013-01-21 08:43:19 +00:00
|
|
|
this.display_account();
|
2013-01-11 13:24:38 +00:00
|
|
|
this.do_search();
|
|
|
|
},
|
|
|
|
bind_event: function() {
|
2012-08-03 08:45:45 +00:00
|
|
|
var self = this;
|
2014-04-10 15:50:22 +00:00
|
|
|
this.$el.parents('.modal').on("click", ".oe_linkedin_logout", function () {
|
2013-01-09 15:51:25 +00:00
|
|
|
IN.User.logout();
|
2013-01-10 14:03:45 +00:00
|
|
|
self.destroy();
|
2013-01-09 15:51:25 +00:00
|
|
|
});
|
2014-04-10 15:50:22 +00:00
|
|
|
this.$search = this.$el.parents('.modal').find(".oe_linkedin_advanced_search" );
|
2013-01-21 08:43:19 +00:00
|
|
|
this.$url = this.$search.find("input[name='search']" );
|
2013-01-11 13:24:38 +00:00
|
|
|
this.$button = this.$search.find("button");
|
|
|
|
|
|
|
|
this.$button.on("click", function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
self.do_search(self.$url.val() || '');
|
2012-08-03 08:45:45 +00:00
|
|
|
});
|
2013-01-11 13:24:38 +00:00
|
|
|
this.$url
|
|
|
|
.on("click mousedown mouseup", function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
}).on("keydown", function (e) {
|
|
|
|
if(e.keyCode == 13) {
|
|
|
|
$(e.target).blur();
|
|
|
|
self.$button.click();
|
|
|
|
}
|
|
|
|
});
|
2012-08-03 08:45:45 +00:00
|
|
|
},
|
2013-01-21 08:43:19 +00:00
|
|
|
display_account: function() {
|
2012-08-03 10:08:34 +00:00
|
|
|
var self = this;
|
2013-01-09 15:51:25 +00:00
|
|
|
IN.API.Profile("me")
|
|
|
|
.fields(["firstName", "lastName"])
|
|
|
|
.result(function (result) {
|
2014-04-10 15:50:22 +00:00
|
|
|
$(QWeb.render('LinkedIn.loginInformation', result.values[0])).appendTo(self.$el.parents('.modal').find(".oe_dialog_custom_buttons"));
|
2013-01-09 15:51:25 +00:00
|
|
|
})
|
2013-01-10 14:03:45 +00:00
|
|
|
},
|
2013-01-11 13:24:38 +00:00
|
|
|
do_search: function(url) {
|
|
|
|
if (!IN.User || !IN.User.isAuthorized()) {
|
|
|
|
this.destroy();
|
|
|
|
}
|
2013-01-10 14:03:45 +00:00
|
|
|
var self = this;
|
|
|
|
var deferrers = [];
|
2013-01-11 13:24:38 +00:00
|
|
|
this.$(".oe_linkedin_pop_c, .oe_linkedin_pop_p").empty();
|
|
|
|
|
|
|
|
if (url && url.length) {
|
|
|
|
var deferrer_c = $.Deferred();
|
|
|
|
var deferrer_p = $.Deferred();
|
|
|
|
deferrers.push(deferrer_c, deferrer_p);
|
|
|
|
|
|
|
|
var url = url.replace(/\/+$/, '');
|
|
|
|
var uid = url.replace(/(.*linkedin\.com\/[a-z]+\/)|(^.*\/company\/)|(\&.*$)/gi, '');
|
|
|
|
|
|
|
|
IN.API.Raw(_.str.sprintf(
|
2013-01-21 08:43:19 +00:00
|
|
|
"companies/universal-name=%s:(id,name,logo-url,description,industry,website-url,locations,universal-name)",
|
2013-01-11 13:24:38 +00:00
|
|
|
encodeURIComponent(uid.toLowerCase()))).result(function (result) {
|
|
|
|
self.do_result_companies({'companies': {'values': [result]}});
|
|
|
|
deferrer_c.resolve();
|
|
|
|
}).error(function (error) {
|
|
|
|
self.do_result_companies({});
|
|
|
|
deferrer_c.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
var url_public = "http://www.linkedin.com/pub/"+uid;
|
|
|
|
IN.API.Profile("url="+ encodeURI(url_public).replace(/%2F/g, '/'))
|
|
|
|
.fields(commonPeopleFields)
|
|
|
|
.result(function(result) {
|
|
|
|
self.do_result_people({'people': result});
|
|
|
|
deferrer_p.resolve();
|
|
|
|
}).error(function (error) {
|
|
|
|
self.do_warn( _t("LinkedIn error"), _t("LinkedIn is temporary down for the searches by url."));
|
|
|
|
self.do_result_people({});
|
|
|
|
deferrer_p.resolve();
|
|
|
|
});
|
|
|
|
|
2013-01-21 08:43:19 +00:00
|
|
|
this.search = url;
|
2013-01-11 13:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var deferrer_c_k = $.Deferred();
|
|
|
|
var deferrer_p_k = $.Deferred();
|
|
|
|
deferrers.push(deferrer_c_k, deferrer_p_k);
|
2012-08-06 14:54:35 +00:00
|
|
|
IN.API.Raw(_.str.sprintf(
|
2012-08-06 15:03:04 +00:00
|
|
|
"company-search:(companies:" +
|
2013-01-21 08:43:19 +00:00
|
|
|
"(id,name,logo-url,description,industry,website-url,locations,universal-name))?keywords=%s&count=%d",
|
|
|
|
encodeURI(this.search), this.limit)).result(function (result) {
|
2013-01-10 14:03:45 +00:00
|
|
|
self.do_result_companies(result);
|
2013-01-11 13:24:38 +00:00
|
|
|
deferrer_c_k.resolve();
|
2012-08-03 10:08:34 +00:00
|
|
|
});
|
2013-01-21 08:43:19 +00:00
|
|
|
IN.API.PeopleSearch().fields(commonPeopleFields).params({"keywords": this.search, "count": this.limit}).result(function(result) {
|
2013-01-10 14:03:45 +00:00
|
|
|
self.do_result_people(result);
|
2013-01-11 13:24:38 +00:00
|
|
|
deferrer_p_k.resolve();
|
2012-08-22 09:10:13 +00:00
|
|
|
});
|
2013-01-11 13:24:38 +00:00
|
|
|
|
2013-01-10 14:03:45 +00:00
|
|
|
return $.when.apply($, deferrers);
|
|
|
|
},
|
|
|
|
do_result_companies: function(companies) {
|
2013-01-11 13:24:38 +00:00
|
|
|
var lst = (companies.companies || {}).values || [];
|
2013-01-10 14:03:45 +00:00
|
|
|
lst = _.first(lst, this.limit);
|
|
|
|
lst = _.map(lst, function(el) {
|
|
|
|
el.__type = "company";
|
|
|
|
return el;
|
2012-08-03 10:08:34 +00:00
|
|
|
});
|
2013-01-11 13:24:38 +00:00
|
|
|
console.debug("Linkedin companies found:", (companies.companies || {})._total, '=>', lst.length, lst);
|
2013-01-10 14:03:45 +00:00
|
|
|
return this.display_result(lst, this.$(".oe_linkedin_pop_c"));
|
|
|
|
},
|
|
|
|
do_result_people: function(people) {
|
2013-01-11 13:24:38 +00:00
|
|
|
var plst = (people.people || {}).values || [];
|
2013-01-10 14:03:45 +00:00
|
|
|
plst = _.first(plst, this.limit);
|
|
|
|
plst = _.map(plst, function(el) {
|
|
|
|
el.__type = "people";
|
|
|
|
return el;
|
2012-08-03 08:45:45 +00:00
|
|
|
});
|
2013-01-10 14:03:45 +00:00
|
|
|
console.debug("Linkedin people found:", people.numResults, '=>', plst.length, plst);
|
|
|
|
return this.display_result(plst, this.$(".oe_linkedin_pop_p"));
|
2012-08-03 08:45:45 +00:00
|
|
|
},
|
2012-08-22 09:10:13 +00:00
|
|
|
display_result: function(result, $elem) {
|
2012-08-03 12:34:48 +00:00
|
|
|
var self = this;
|
|
|
|
var $row;
|
2013-01-10 14:03:45 +00:00
|
|
|
$elem.find(".oe_no_result").remove();
|
2012-08-22 09:10:13 +00:00
|
|
|
_.each(result, function(el) {
|
2012-08-03 14:41:50 +00:00
|
|
|
var pc = new instance.web_linkedin.EntityWidget(self, el);
|
2013-01-10 14:03:45 +00:00
|
|
|
if (!$elem.find("div").size() || $elem.find(" > div:last > div").size() >= 5) {
|
2012-08-03 12:34:48 +00:00
|
|
|
$row = $("<div style='display: table-row;width:100%'/>");
|
2012-08-22 09:10:13 +00:00
|
|
|
$row.appendTo($elem);
|
2012-08-03 12:34:48 +00:00
|
|
|
}
|
|
|
|
pc.appendTo($row);
|
2012-08-24 18:27:43 +00:00
|
|
|
pc.$el.css("display", "table-cell");
|
|
|
|
pc.$el.css("width", "20%");
|
2012-08-03 14:04:13 +00:00
|
|
|
pc.on("selected", self, function(data) {
|
|
|
|
self.trigger("selected", data);
|
2012-08-06 10:07:30 +00:00
|
|
|
self.destroy();
|
2012-08-03 14:04:13 +00:00
|
|
|
});
|
2012-08-03 12:34:48 +00:00
|
|
|
});
|
2013-01-10 14:03:45 +00:00
|
|
|
if (!$elem.find("div").size()) {
|
|
|
|
$elem.append($('<div class="oe_no_result">').text(_t("No results found")));
|
2012-08-07 09:02:38 +00:00
|
|
|
}
|
2012-08-03 12:34:48 +00:00
|
|
|
},
|
2013-01-10 14:03:45 +00:00
|
|
|
|
2012-08-03 12:34:48 +00:00
|
|
|
});
|
|
|
|
|
2012-08-03 14:41:50 +00:00
|
|
|
instance.web_linkedin.EntityWidget = instance.web.Widget.extend({
|
2012-08-07 15:34:35 +00:00
|
|
|
template: "Linkedin.EntityWidget",
|
2012-08-03 12:34:48 +00:00
|
|
|
init: function(parent, data) {
|
|
|
|
this._super(parent);
|
|
|
|
this.data = data;
|
|
|
|
},
|
|
|
|
start: function() {
|
2012-08-03 14:04:13 +00:00
|
|
|
var self = this;
|
2012-08-24 18:27:43 +00:00
|
|
|
this.$el.click(function() {
|
2012-08-03 14:04:13 +00:00
|
|
|
self.trigger("selected", self.data);
|
|
|
|
});
|
2012-08-03 12:34:48 +00:00
|
|
|
if (this.data.__type === "company") {
|
|
|
|
this.$("h3").text(this.data.name);
|
2012-08-05 13:54:20 +00:00
|
|
|
self.$("img").attr("src", this.data.logoUrl);
|
2013-01-11 13:24:38 +00:00
|
|
|
self.$(".oe_linkedin_entity_headline").text(this.data.industry);
|
2012-08-03 12:34:48 +00:00
|
|
|
} else { // people
|
2012-08-06 14:54:35 +00:00
|
|
|
this.$("h3").text(this.data.formattedName);
|
2012-08-05 13:54:20 +00:00
|
|
|
self.$("img").attr("src", this.data.pictureUrl);
|
2012-08-22 08:27:01 +00:00
|
|
|
self.$(".oe_linkedin_entity_headline").text(this.data.headline);
|
2012-08-03 12:34:48 +00:00
|
|
|
}
|
|
|
|
},
|
2012-08-03 08:45:45 +00:00
|
|
|
});
|
2012-06-12 12:44:54 +00:00
|
|
|
};
|
|
|
|
// vim:et fdc=0 fdl=0:
|