From 6dcad6d0ec48d5c7e1e4cfee9c94e3bddbc1ba5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Maes?= Date: Thu, 18 Sep 2014 10:22:46 +0200 Subject: [PATCH] [IMP] im_livechat : add rule to display (or not) the support button. --- addons/im_livechat/im_livechat.py | 52 ++++++++++++++++++- addons/im_livechat/im_livechat_demo.xml | 7 +++ .../im_livechat/security/ir.model.access.csv | 3 ++ .../im_livechat/static/src/js/im_livechat.js | 22 ++++---- addons/im_livechat/views/im_livechat.xml | 4 +- addons/im_livechat/views/im_livechat_view.xml | 44 ++++++++++++++++ 6 files changed, 119 insertions(+), 13 deletions(-) diff --git a/addons/im_livechat/im_livechat.py b/addons/im_livechat/im_livechat.py index cfc9a428873..0e9d978b2e0 100644 --- a/addons/im_livechat/im_livechat.py +++ b/addons/im_livechat/im_livechat.py @@ -25,6 +25,9 @@ import json import openerp.addons.im_chat.im_chat import datetime +import re +import json + from openerp.osv import osv, fields from openerp import tools from openerp import http @@ -123,6 +126,7 @@ class im_livechat_channel(osv.Model): "Use this field anywhere a small image is required."), 'session_ids' : fields.one2many('im_chat.session', 'channel_id', 'Sessions'), 'nbr_session' : fields.function(_compute_nbr_session, type='integer', string='Number of session', store=False), + 'rule_ids': fields.one2many('im_livechat.channel.rule','channel_id','Rules'), } def _default_user_ids(self, cr, uid, context=None): @@ -184,6 +188,36 @@ class im_livechat_channel(osv.Model): self.write(cr, uid, ids, {'user_ids': [(3, uid)]}) return True + + +class im_livechat_channel_rule(osv.Model): + _name = 'im_livechat.channel.rule' + + _columns = { + 'regex_url' : fields.char('URL Regex', help="Regular expression identifying the web page on which the rules will be applied."), + 'action' : fields.selection([('display_button', 'Display the button'),('auto_popup','Auto popup'), ('hide_button', 'Hide the button')], 'Action', size=32, required=True, + help="Select 'Display the button' to simply display the chat button on the pages."\ + " Select 'Auto popup' for to display the button, and automatically open the conversation window."\ + " Select 'Hide the button' to hide the chat button on the pages."), + 'auto_popup_timer' : fields.integer('Auto popup timer', help="Delay (in seconds) to automatically open the converssation window. Note : the selected action must be 'Auto popup', otherwise this parameter will not be take into account."), + 'channel_id': fields.many2one('im_livechat.channel', 'Channel', help="The channel of the rule"), + 'country_ids': fields.many2many('res.country', 'im_livechat_channel_blocked_country_rel', 'channel_id', 'country_id', 'Blocked Country', help="For this country list, the channel will not be available on the web page matched with the Regular Expression. This feature requires GeoIP installed on your server."), + } + + _defaults = { + 'auto_popup_timer': 0, + 'action' : 'display_button', + } + + def match_rule(self, cr, uid, channel_id, word, context=None): + """ determine if a rule of the given channel match with the given word """ + rule_ids = self.search(cr, uid, [('channel_id', '=', channel_id)], context=context) + for rule in self.browse(cr, uid, rule_ids, context=context): + if re.search(rule.regex_url, word): + return rule + return False + + class im_chat_session(osv.Model): _inherit = 'im_chat.session' @@ -259,6 +293,20 @@ class LiveChatController(http.Controller): info["dbname"] = dbname info["channel"] = channel_id info["username"] = kwargs.get("username", "Visitor") + url = request.httprequest.headers['Referer'] or request.httprequest.base_url + rule = registry.get('im_livechat.channel.rule').match_rule(cr, uid, channel_id, url, context=context) + if rule: + if request.session.geoip and request.session.geoip.get('country_name', "") in [c.code for c in rule.country_ids]: + # don't return the initialization script, since its blocked in the country + return + if rule.action == 'hide_button': + return + rule_data = { + 'action' : rule.action, + 'auto_popup_timer' : rule.auto_popup_timer, + 'regex_url' : rule.regex_url, + } + info['rule'] = json.dumps(rule and rule_data or False) return request.render('im_livechat.loader', info) @http.route('/im_livechat/get_session', type="json", auth="none") @@ -266,8 +314,8 @@ class LiveChatController(http.Controller): cr, uid, context, db = request.cr, request.uid or openerp.SUPERUSER_ID, request.context, request.db reg = openerp.modules.registry.RegistryManager.get(db) # if geoip, add the country name to the anonymous name - if hasattr(request, 'geoip'): - anonymous_name = anonymous_name + " ("+request.geoip.get('country_name', "")+")" + if request.session.geoip: + anonymous_name = anonymous_name + " ("+request.session.geoip.get('country_name', "")+")" return reg.get("im_livechat.channel").get_channel_session(cr, uid, channel_id, anonymous_name, context=context) @http.route('/im_livechat/available', type='json', auth="none") diff --git a/addons/im_livechat/im_livechat_demo.xml b/addons/im_livechat/im_livechat_demo.xml index f199179fb50..3da9b3998de 100644 --- a/addons/im_livechat/im_livechat_demo.xml +++ b/addons/im_livechat/im_livechat_demo.xml @@ -11,5 +11,12 @@ + + /im_livechat/ + auto_popup + 3 + + + diff --git a/addons/im_livechat/security/ir.model.access.csv b/addons/im_livechat/security/ir.model.access.csv index cb6097e549a..f571df74e69 100644 --- a/addons/im_livechat/security/ir.model.access.csv +++ b/addons/im_livechat/security/ir.model.access.csv @@ -3,3 +3,6 @@ access_ls_chann1,im_livechat.channel,model_im_livechat_channel,,1,0,0,0 access_ls_chann2,im_livechat.channel,model_im_livechat_channel,group_im_livechat,1,1,1,0 access_ls_chann3,im_livechat.channel,model_im_livechat_channel,group_im_livechat_manager,1,1,1,1 access_livechat_support_report,im_livechat.report,model_im_livechat_report,group_im_livechat_manager,1,1,1,1 +access_ls_chann_rule1,im_livechat.channel.rule,model_im_livechat_channel_rule,,1,0,0,0 +access_ls_chann_rule2,im_livechat.channel.rule,model_im_livechat_channel_rule,group_im_livechat,1,1,1,0 +access_ls_chann_rule3,im_livechat.channel.rule,model_im_livechat_channel_rule,group_im_livechat_manager,1,1,1,1 diff --git a/addons/im_livechat/static/src/js/im_livechat.js b/addons/im_livechat/static/src/js/im_livechat.js index 802340dcecf..188248b7d90 100644 --- a/addons/im_livechat/static/src/js/im_livechat.js +++ b/addons/im_livechat/static/src/js/im_livechat.js @@ -126,7 +126,7 @@ }; im_livechat.LiveSupport = openerp.Widget.extend({ - init: function(server_url, db, channel, options) { + init: function(server_url, db, channel, options, rule) { this._super(); options = options || {}; _.defaults(options, { @@ -136,9 +136,9 @@ defaultUsername: _t("Visitor"), }); openerp.session = new openerp.Session(null, server_url, { use_cors: false }); - this.load_template(db, channel, options); + this.load_template(db, channel, options, rule); }, - load_template: function(db, channel, options){ + load_template: function(db, channel, options, rule){ var self = this; // load the qweb templates var defs = []; @@ -149,27 +149,29 @@ })); }); return $.when.apply($, defs).then(function() { - self.setup(db, channel, options); + self.setup(db, channel, options, rule); }); }, - setup: function(db, channel, options){ + setup: function(db, channel, options, rule){ var self = this; var session = openerp.get_cookie(im_livechat.COOKIE_NAME); if(session){ - self.build_button(channel, options, JSON.parse(session)); + self.build_button(channel, options, JSON.parse(session), rule); }else{ openerp.session.rpc("/im_livechat/available", {db: db, channel: channel}).then(function(activated) { if(activated){ - self.build_button(channel, options); + self.build_button(channel, options, false, rule); } }); } }, - build_button: function(channel, options, session){ + build_button: function(channel, options, session, rule){ var button = new im_livechat.ChatButton(null, channel, options, session); button.appendTo($("body")); - if (options.auto){ - button.click(); + if (rule.action === 'auto_popup'){ + setTimeout(function() { + button.click(); + }, rule.auto_popup_timer*1000); } } }); diff --git a/addons/im_livechat/views/im_livechat.xml b/addons/im_livechat/views/im_livechat.xml index d77af57cfed..1b3811bad90 100644 --- a/addons/im_livechat/views/im_livechat.xml +++ b/addons/im_livechat/views/im_livechat.xml @@ -132,7 +132,9 @@ defaultMessage: "" || '', auto: window.oe_im_livechat_auto || false, defaultUsername: "" || undefined, - }); + }, + + ); })(); diff --git a/addons/im_livechat/views/im_livechat_view.xml b/addons/im_livechat/views/im_livechat_view.xml index bae7b59f5b5..4b9fc67794d 100644 --- a/addons/im_livechat/views/im_livechat_view.xml +++ b/addons/im_livechat/views/im_livechat_view.xml @@ -216,12 +216,55 @@

For website built with Odoo CMS, please install the website_livechat module. Then go to Settings > Website Settings and select the Live Chat Channel you want to add on your website.

+ + + +
+ + + + im.livechat.channel.rule.tree + im_livechat.channel.rule + + + + + + + + + + + im_livechat.channel.rule.form + im_livechat.channel.rule + form + +
+ + + + + + + + + + + + +
+
+
+ + @@ -229,5 +272,6 @@ +