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
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
test_linkedin: function() {
|
|
|
|
var self = this;
|
|
|
|
return this.test_api_key().pipe(function() {
|
|
|
|
if (self.linkedin_added)
|
|
|
|
return self.linkedin_def.promise();
|
|
|
|
var tag = document.createElement('script');
|
|
|
|
tag.type = 'text/javascript';
|
|
|
|
tag.src = "http://platform.linkedin.com/in.js";
|
|
|
|
tag.innerHTML = 'api_key : ' + self.api_key + '\nauthorize : true';
|
|
|
|
document.getElementsByTagName('head')[0].appendChild(tag);
|
2012-08-02 08:47:45 +00:00
|
|
|
self.linkedin_added = true;
|
2012-08-01 13:27:33 +00:00
|
|
|
$(tag).load(function() {
|
2012-08-03 08:45:45 +00:00
|
|
|
IN.Event.on(IN, "auth", function() {
|
|
|
|
self.auth_def.resolve();
|
|
|
|
});
|
2012-08-01 13:27:33 +00:00
|
|
|
self.linkedin_def.resolve();
|
|
|
|
});
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
return new instance.web.Model("ir.config_parameter").call("get_param", ["web.linkedin.apikey"]).pipe(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() {
|
|
|
|
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;
|
2012-08-02 08:47:45 +00:00
|
|
|
this.display_dm.add(instance.web_linkedin.tester.test_linkedin()).then(function() {
|
2012-08-03 14:04:13 +00:00
|
|
|
var pop = new instance.web_linkedin.LinkedinPopup(self, self.get("value"));
|
|
|
|
pop.open();
|
|
|
|
pop.on("selected", this, function(entity) {
|
|
|
|
self.selected_entity(entity);
|
|
|
|
});
|
2012-08-07 15:34:35 +00:00
|
|
|
}, _.bind(this.linkedin_disabled, this));
|
|
|
|
},
|
|
|
|
linkedin_disabled: function() {
|
2012-08-23 09:53:14 +00:00
|
|
|
instance.web.dialog($(QWeb.render("LinkedIn.DisabledWarning")), {
|
|
|
|
title: _t("LinkedIn is not enabled"),
|
|
|
|
buttons: [
|
|
|
|
{text: _t("Ok"), click: function() { $(this).dialog("close"); }}
|
|
|
|
]
|
|
|
|
});
|
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;
|
|
|
|
this.create_on_change(entity).then(function(to_change) {
|
|
|
|
self.view.on_processed_onchange({value:to_change});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
create_on_change: function(entity) {
|
2012-08-06 14:09:25 +00:00
|
|
|
var self = this;
|
2012-08-06 10:07:30 +00:00
|
|
|
var to_change = {};
|
2012-08-06 14:09:25 +00:00
|
|
|
var defs = [];
|
2012-08-03 14:41:50 +00:00
|
|
|
if (entity.__type === "company") {
|
2012-08-06 14:54:35 +00:00
|
|
|
to_change.is_company = true;
|
2012-08-06 10:07:30 +00:00
|
|
|
to_change.name = entity.name;
|
2012-08-22 08:27:01 +00:00
|
|
|
to_change.image = false;
|
2012-08-06 14:09:25 +00:00
|
|
|
if (entity.logoUrl) {
|
|
|
|
defs.push(self.rpc('/web_linkedin/binary/url2binary',
|
|
|
|
{'url': entity.logoUrl}).pipe(function(data){
|
2012-08-22 08:27:01 +00:00
|
|
|
to_change.image = data;
|
2012-08-06 14:09:25 +00:00
|
|
|
}));
|
|
|
|
}
|
2012-08-06 15:03:04 +00:00
|
|
|
to_change.website = entity.websiteUrl;
|
|
|
|
to_change.phone = false;
|
|
|
|
_.each(entity.locations.values || [], function(el) {
|
|
|
|
to_change.phone = el.contactInfo.phone1;
|
|
|
|
});
|
2012-08-06 15:28:09 +00:00
|
|
|
var children_def = $.Deferred();
|
|
|
|
IN.API.PeopleSearch().fields(commonPeopleFields).params({
|
|
|
|
"company-name" : entity.name,
|
|
|
|
"current-company": true,
|
|
|
|
"count": 25,
|
|
|
|
}).result(function(result) {
|
|
|
|
children_def.resolve(result);
|
|
|
|
});
|
|
|
|
defs.push(children_def.pipe(function(result) {
|
2012-08-22 09:17:59 +00:00
|
|
|
result = _.reject(result.people.values || [], function(el) {
|
|
|
|
return ! el.formattedName;
|
|
|
|
});
|
|
|
|
var defs = _.map(result, function(el) {
|
2012-08-06 15:28:09 +00:00
|
|
|
el.__type = "people";
|
|
|
|
return self.create_on_change(el);
|
|
|
|
});
|
|
|
|
return $.when.apply($, defs).pipe(function() {
|
|
|
|
var p_to_change = _.toArray(arguments);
|
|
|
|
to_change.child_ids = p_to_change;
|
|
|
|
});
|
|
|
|
}));
|
2012-08-06 14:33:43 +00:00
|
|
|
/* TODO
|
2012-08-06 14:19:38 +00:00
|
|
|
to_change.linkedinUrl = _.str.sprintf("http://www.linkedin.com/company/%d", entity.id);
|
|
|
|
*/
|
|
|
|
} else { // people
|
2012-08-06 14:54:35 +00:00
|
|
|
to_change.is_company = false;
|
|
|
|
to_change.name = entity.formattedName;
|
2012-08-22 08:27:01 +00:00
|
|
|
to_change.image = false;
|
2012-08-06 14:09:25 +00:00
|
|
|
if (entity.pictureUrl) {
|
|
|
|
defs.push(self.rpc('/web_linkedin/binary/url2binary',
|
|
|
|
{'url': entity.pictureUrl}).pipe(function(data){
|
2012-08-22 08:27:01 +00:00
|
|
|
to_change.image = data;
|
2012-08-06 14:09:25 +00:00
|
|
|
}));
|
|
|
|
}
|
2012-08-06 14:54:35 +00:00
|
|
|
to_change.mobile = false;
|
|
|
|
to_change.phone = false;
|
2012-08-22 09:17:59 +00:00
|
|
|
_.each((entity.phoneNumbers || {}).values || [], function(el) {
|
2012-08-06 14:54:35 +00:00
|
|
|
if (el.phoneType === "mobile") {
|
|
|
|
to_change.mobile = el.phoneNumber;
|
|
|
|
} else {
|
|
|
|
to_change.phone = el.phoneNumber;
|
|
|
|
}
|
|
|
|
});
|
2012-08-22 09:17:59 +00:00
|
|
|
var positions = (entity.positions || {}).values || [];
|
2012-08-22 09:10:13 +00:00
|
|
|
to_change.function = positions.length > 0 ? positions[0].title : false;
|
2012-08-06 14:33:43 +00:00
|
|
|
/* TODO
|
2012-08-06 14:19:38 +00:00
|
|
|
to_change.linkedinUrl = entity.publicProfileUrl;
|
|
|
|
*/
|
2012-08-03 14:41:50 +00:00
|
|
|
}
|
2012-08-06 15:28:09 +00:00
|
|
|
return $.when.apply($, defs).pipe(function() {
|
|
|
|
return to_change;
|
2012-08-06 14:09:25 +00:00
|
|
|
});
|
2012-08-03 14:04:13 +00:00
|
|
|
},
|
2012-08-01 13:27:33 +00:00
|
|
|
});
|
2012-08-06 10:07:30 +00:00
|
|
|
|
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
|
|
|
|
2012-08-06 15:28:09 +00:00
|
|
|
var commonPeopleFields = ["id", "picture-url", "public-profile-url",
|
|
|
|
"formatted-name", "location", "phone-numbers", "im-accounts",
|
2012-08-22 08:27:01 +00:00
|
|
|
"main-address", "headline", "positions"];
|
2012-08-06 15:28:09 +00:00
|
|
|
|
2012-08-03 08:45:45 +00:00
|
|
|
instance.web_linkedin.LinkedinPopup = instance.web.Dialog.extend({
|
|
|
|
template: "Linkedin.popup",
|
|
|
|
init: function(parent, text) {
|
2012-08-06 10:07:30 +00:00
|
|
|
this._super(parent, {title:_t("LinkedIn search")});
|
2012-08-03 08:45:45 +00:00
|
|
|
this.text = text;
|
2012-08-22 09:10:13 +00:00
|
|
|
this.limit = 5;
|
2012-08-03 08:45:45 +00:00
|
|
|
},
|
|
|
|
start: function() {
|
|
|
|
this._super();
|
|
|
|
var self = this;
|
|
|
|
this.on("authentified", this, this.authentified);
|
|
|
|
instance.web_linkedin.tester.test_authentication().then(function() {
|
|
|
|
self.trigger("authentified");
|
|
|
|
});
|
|
|
|
},
|
|
|
|
authentified: function() {
|
2012-08-03 10:08:34 +00:00
|
|
|
var self = this;
|
|
|
|
cdef = $.Deferred();
|
|
|
|
pdef = $.Deferred();
|
2012-08-06 14:54:35 +00:00
|
|
|
IN.API.Raw(_.str.sprintf(
|
2012-08-06 15:03:04 +00:00
|
|
|
"company-search:(companies:" +
|
|
|
|
"(id,name,logo-url,description,industry,website-url,locations))?keywords=%s&count=%d",
|
2012-08-06 14:54:35 +00:00
|
|
|
encodeURI(this.text), this.limit)).result(function (result) {
|
2012-08-03 10:08:34 +00:00
|
|
|
cdef.resolve(result);
|
|
|
|
});
|
2012-08-22 09:10:13 +00:00
|
|
|
var def = cdef.pipe(function(companies) {
|
|
|
|
var lst = companies.companies.values || [];
|
|
|
|
lst = _.first(lst, self.limit);
|
|
|
|
lst = _.map(lst, function(el) {
|
|
|
|
el.__type = "company";
|
|
|
|
return el;
|
|
|
|
});
|
|
|
|
console.debug("Linkedin companies found:", lst.length, lst);
|
|
|
|
return self.display_result(lst, self.$(".oe_linkedin_pop_c"));
|
|
|
|
});
|
2012-08-06 15:28:09 +00:00
|
|
|
IN.API.PeopleSearch().fields(commonPeopleFields).
|
2012-08-05 13:54:20 +00:00
|
|
|
params({"keywords": this.text, "count": this.limit}).result(function(result) {
|
2012-08-03 10:08:34 +00:00
|
|
|
pdef.resolve(result);
|
|
|
|
});
|
2012-08-22 09:10:13 +00:00
|
|
|
var def2 = pdef.pipe(function(people) {
|
2012-08-05 14:03:32 +00:00
|
|
|
var plst = people.people.values || [];
|
2012-08-22 09:10:13 +00:00
|
|
|
plst = _.first(plst, self.limit);
|
|
|
|
plst = _.map(plst, function(el) {
|
2012-08-03 10:08:34 +00:00
|
|
|
el.__type = "people";
|
|
|
|
return el;
|
|
|
|
});
|
2012-08-22 09:10:13 +00:00
|
|
|
console.debug("Linkedin people found:", plst.length, plst);
|
|
|
|
return self.display_result(plst, self.$(".oe_linkedin_pop_p"));
|
2012-08-03 08:45:45 +00:00
|
|
|
});
|
2012-08-22 09:10:13 +00:00
|
|
|
return $.when(def, def2);
|
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 i = 0;
|
|
|
|
var $row;
|
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);
|
2012-08-03 12:34:48 +00:00
|
|
|
if (i % 5 === 0) {
|
|
|
|
$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);
|
|
|
|
pc.$element.css("display", "table-cell");
|
2012-08-03 14:04:13 +00:00
|
|
|
pc.$element.css("width", "20%");
|
|
|
|
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
|
|
|
i++;
|
|
|
|
});
|
2012-08-22 09:10:13 +00:00
|
|
|
if (result.length === 0) {
|
|
|
|
$elem.text(_t("No results found"));
|
2012-08-07 09:02:38 +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;
|
|
|
|
this.$element.click(function() {
|
|
|
|
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);
|
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:
|