[IMP] im_livechat : add rule to display (or not) the support button.
authorJérome Maes <jem@openerp.com>
Thu, 18 Sep 2014 08:22:46 +0000 (10:22 +0200)
committerJérome Maes <jem@openerp.com>
Fri, 21 Nov 2014 14:21:17 +0000 (15:21 +0100)
addons/im_livechat/im_livechat.py
addons/im_livechat/im_livechat_demo.xml
addons/im_livechat/security/ir.model.access.csv
addons/im_livechat/static/src/js/im_livechat.js
addons/im_livechat/views/im_livechat.xml
addons/im_livechat/views/im_livechat_view.xml

index cfc9a42..0e9d978 100644 (file)
@@ -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")
index f199179..3da9b39 100644 (file)
             <field name="users" eval="[(4, ref('base.user_demo'))]"/>
         </record>
 
+        <record id="channel_rule" model="im_livechat.channel.rule">
+            <field name="regex_url">/im_livechat/</field>
+            <field name="action">auto_popup</field>
+            <field name="auto_popup_timer">3</field>
+            <field name="channel_id" ref="channel_website" />
+        </record>
+
     </data>
 </openerp>
index cb6097e..f571df7 100644 (file)
@@ -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
index 802340d..188248b 100644 (file)
     };
 
     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, {
                 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 = [];
                 }));
             });
             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);
             }
         }
     });
index d77af57..1b3811b 100644 (file)
                         defaultMessage: "<t t-esc="defaultMessage"/>" || '',
                         auto: window.oe_im_livechat_auto || false,
                         defaultUsername: "<t t-esc="username"/>" || undefined,
-                    });
+                    },
+                    <t t-raw="rule"/>
+                );
             })();
         </template>
 
index bae7b59..4b9fc67 100644 (file)
                         <field name="web_page" readonly="1" class="oe_tag" widget="url"/>
                         <p>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.</p>
                     </div>
+
+                    <group string="Channel Rules">
+                        <field name="rule_ids" nolabel="1"/>
+                    </group>
                 </sheet>
                 </form>
             </field>
         </record>
 
 
+
+        <!--
+            Views for im_livechat_channel_rule
+        -->
+        <record id="im_livechat_channel_rule_tree_view" model="ir.ui.view">
+            <field name="name">im.livechat.channel.rule.tree</field>
+            <field name="model">im_livechat.channel.rule</field>
+            <field name="arch" type="xml">
+                <tree string="Rules">
+                    <field name="regex_url"/>
+                    <field name="action"/>
+                    <field name="country_ids"/>
+                </tree>
+            </field>
+        </record>
+
+        <record id="im_livechat_channel_rule_form_view" model="ir.ui.view">
+            <field name="name">im_livechat.channel.rule.form</field>
+            <field name="model">im_livechat.channel.rule</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Channel Rule">
+                    <sheet>
+                        <group>
+                            <field name="action"/>
+                            <field name="regex_url" />
+                            <field name="auto_popup_timer" />
+                        </group>
+                        <notebook>
+                            <page string="Blocked Countries">
+                                <field name="country_ids" />
+                            </page>
+                        </notebook>
+                    </sheet>
+                </form>
+            </field>
+        </record>
+
+
         <!-- Menu items -->
         <menuitem name="Live Chat" parent="mail.mail_feeds_main" id="im_livechat" groups="group_im_livechat"/>
 
 
         <menuitem name="Channels" parent="im_livechat" id="support_channels" action="action_support_channels" groups="group_im_livechat"/>
 
+
     </data>
 </openerp>