Skip to content

Commit 4194fab

Browse files
authored
Merge pull request #1 from Vendic/feature/gdrp-cookies
Add cookie based trigger for Zopim
2 parents a815a48 + 9585635 commit 4194fab

File tree

6 files changed

+216
-52
lines changed

6 files changed

+216
-52
lines changed

Block/View.php

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,18 @@
33
namespace Vendic\Zopim\Block;
44

55
use Magento\Framework\View\Element\Template;
6+
use Vendic\Zopim\Model\Config;
67

78
class View extends \Magento\Framework\View\Element\Template
89
{
10+
public $config;
911

10-
protected function getConfig($path)
12+
public function __construct(
13+
Config $config,
14+
Template\Context $context, array $data = [])
1115
{
12-
return $this->_scopeConfig->getValue(
13-
$path,
14-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
15-
);
16-
}
17-
18-
public function getIsEnabled()
19-
{
20-
return boolval($this->getConfig('vendic_zopim/general/enable'));
21-
}
22-
23-
public function getZopimId()
24-
{
25-
return $this->getConfig('vendic_zopim/general/zopim_id');
26-
}
27-
28-
public function getDelay()
29-
{
30-
$delay = 0;
31-
if(!empty($this->getConfig('vendic_zopim/general/delay'))) {
32-
$delay = $this->getConfig('vendic_zopim/general/delay');
33-
}
34-
35-
return intval($delay);
16+
$this->config = $config;
17+
parent::__construct($context, $data);
3618
}
3719

3820
}

Model/Config.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Vendic\Zopim\Model;
4+
5+
use Magento\Framework\App\Config\ScopeConfigInterface;
6+
7+
class Config
8+
{
9+
/**
10+
* @var ScopeConfigInterface
11+
*/
12+
private $scopeConfig;
13+
14+
public function __construct(
15+
ScopeConfigInterface $scopeConfig
16+
) {
17+
$this->scopeConfig = $scopeConfig;
18+
}
19+
20+
public function getCookiesEnabled()
21+
{
22+
return boolval($this->getConfig('vendic_zopim/cookie/enable'));
23+
}
24+
25+
public function getExpectedCookieName()
26+
{
27+
return $this->getConfig('vendic_zopim/cookie/name');
28+
}
29+
30+
31+
public function getExpectedCookieValue()
32+
{
33+
return $this->getConfig('vendic_zopim/cookie/value');
34+
}
35+
36+
public function getIsEnabled()
37+
{
38+
return boolval($this->getConfig('vendic_zopim/general/enable'));
39+
}
40+
41+
public function getZopimId()
42+
{
43+
return $this->getConfig('vendic_zopim/general/zopim_id');
44+
}
45+
46+
public function getDelay()
47+
{
48+
$delay = 0;
49+
if (!empty($this->getConfig('vendic_zopim/general/delay'))) {
50+
$delay = $this->getConfig('vendic_zopim/general/delay');
51+
}
52+
53+
return intval($delay);
54+
}
55+
56+
protected function getConfig($path)
57+
{
58+
return $this->scopeConfig->getValue(
59+
$path,
60+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
61+
);
62+
}
63+
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "vendic/module-zopim",
33
"description": "Zopim module for Magento 2",
4-
"version": "1.0.1",
4+
"version": "1.1.0",
55
"type": "magento2-module",
66
"license": [
77
"proprietary"
88
],
99
"require": {
10-
"php": "^7.0 || ^7.1",
10+
"php": "^7.0 || ^7.1 || ^7.2",
1111
"magento/framework": "*",
1212
"magento/module-require-js": "*"
1313
},

etc/adminhtml/system.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
<label>Chatbox delay</label>
5050
<validate>validate-number</validate>
5151
<comment>Enter delay in miliseconds. Default value 1000 (1 second)</comment>
52+
<depends>
53+
<field id="enable">1</field>
54+
</depends>
5255
</field>
5356
<field id="zopim_id"
5457
translate="label"
@@ -58,6 +61,53 @@
5861
showInWebsite="1"
5962
showInStore="1">
6063
<label>Zopim ID</label>
64+
<depends>
65+
<field id="enable">1</field>
66+
</depends>
67+
</field>
68+
</group>
69+
<group id="cookie"
70+
translate="label"
71+
type="text"
72+
sortOrder="1"
73+
showInDefault="1"
74+
showInWebsite="1"
75+
showInStore="1">
76+
<label>Cookies (GDPR)</label>
77+
<field id="enable"
78+
translate="label"
79+
type="select"
80+
sortOrder="1"
81+
showInDefault="1"
82+
showInWebsite="1"
83+
showInStore="1">
84+
<label>Enable cookies</label>
85+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
86+
<comment>Trigger Zopim based on a cookie value </comment>
87+
</field>
88+
<field id="name"
89+
translate="label"
90+
type="text"
91+
sortOrder="1"
92+
showInDefault="1"
93+
showInWebsite="1"
94+
showInStore="1">
95+
<label>Expected Cookie Name</label>
96+
<depends>
97+
<field id="enable">1</field>
98+
</depends>
99+
</field>
100+
<field id="value"
101+
translate="label"
102+
type="text"
103+
sortOrder="1"
104+
showInDefault="1"
105+
showInWebsite="1"
106+
showInStore="1">
107+
<label>Expected Cookie value</label>
108+
<depends>
109+
<field id="enable">1</field>
110+
</depends>
61111
</field>
62112
</group>
63113
</section>

etc/config.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
<enable>0</enable>
77
<delay>1200</delay>
88
</general>
9+
<cookie>
10+
<enable>0</enable>
11+
<name>allowedAllCookies</name>
12+
<value>true</value>
13+
</cookie>
914
</vendic_zopim>
1015
</default>
1116
</config>

view/frontend/templates/footer/zopim.phtml

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,97 @@
33
* @var $block \Vendic\Zopim\Block\View
44
*/
55
?>
6-
<?php if($block->getIsEnabled() && !empty($block->getZopimId())) : ?>
7-
8-
<script>
9-
require([
10-
'jquery'
11-
], function () {
12-
jQuery(document).ready(function() {
13-
14-
setTimeout(function (){
15-
16-
var isMobile = /(iphone|ipod|android|blackberry|windows ce|palm|symbian)/i.test(navigator.userAgent);
17-
var curl = window.location.href;
18-
var arr = curl.split("/");
19-
var result = arr[2];
20-
if (isMobile) {
21-
}else{
22-
window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
23-
d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
24-
_.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute("charset","utf-8");
25-
$.src="//v2.zopim.com/?<?php echo $block->getZopimId() ?>";z.t=+new Date;$.
26-
type="text/javascript";e.parentNode.insertBefore($,e)})(document,"script");
27-
}
6+
<?php if ($block->config->getIsEnabled() && !empty($block->config->getZopimId())) : ?>
7+
<?php if ($block->config->getCookiesEnabled()) : ?>
8+
<script>
9+
require([
10+
'jquery',
11+
'jquery/jquery.cookie',
12+
'domReady!'
13+
], function ($) {
14+
$(document).ready(function () {
15+
16+
var cookieName = "<?php echo $block->config->getExpectedCookieName() ?>"
17+
var cookieValue = "<?php echo $block->config->getExpectedCookieValue() ?>"
18+
19+
console.log(cookieName);
20+
console.log(cookieValue);
21+
console.log($.cookie(cookieValue));
22+
23+
if ($.cookie(cookieName) === cookieValue) {
2824

29-
}, <?php echo $block->getDelay() ?>);
25+
setTimeout(function () {
3026

27+
var isMobile = /(iphone|ipod|android|blackberry|windows ce|palm|symbian)/i.test(navigator.userAgent);
28+
var curl = window.location.href;
29+
var arr = curl.split("/");
30+
var result = arr[2];
31+
if (isMobile) {
32+
} else {
33+
window.$zopim || (function (d, s) {
34+
var z = $zopim = function (c) {
35+
z._.push(c)
36+
}, $ = z.s =
37+
d.createElement(s), e = d.getElementsByTagName(s)[0];
38+
z.set = function (o) {
39+
z.set._.push(o)
40+
};
41+
z._ = [];
42+
z.set._ = [];
43+
$.async = !0;
44+
$.setAttribute("charset", "utf-8");
45+
$.src = "//v2.zopim.com/?<?php echo $block->config->getZopimId() ?>";
46+
z.t = +new Date;
47+
$.type = "text/javascript";
48+
e.parentNode.insertBefore($, e)
49+
})(document, "script");
50+
}
51+
52+
}, <?php echo $block->config->getDelay() ?>);
53+
54+
}
55+
56+
});
3157
});
32-
});
33-
</script>
58+
</script>
59+
<?php else: ?>
60+
<script>
61+
require([
62+
'jquery'
63+
], function ($) {
64+
$(document).ready(function () {
65+
66+
setTimeout(function () {
67+
68+
var isMobile = /(iphone|ipod|android|blackberry|windows ce|palm|symbian)/i.test(navigator.userAgent);
69+
var curl = window.location.href;
70+
var arr = curl.split("/");
71+
var result = arr[2];
72+
if (isMobile) {
73+
} else {
74+
window.$zopim || (function (d, s) {
75+
var z = $zopim = function (c) {
76+
z._.push(c)
77+
}, $ = z.s =
78+
d.createElement(s), e = d.getElementsByTagName(s)[0];
79+
z.set = function (o) {
80+
z.set._.push(o)
81+
};
82+
z._ = [];
83+
z.set._ = [];
84+
$.async = !0;
85+
$.setAttribute("charset", "utf-8");
86+
$.src = "//v2.zopim.com/?<?php echo $block->config->getZopimId() ?>";
87+
z.t = +new Date;
88+
$.type = "text/javascript";
89+
e.parentNode.insertBefore($, e)
90+
})(document, "script");
91+
}
3492

93+
}, <?php echo $block->config->getDelay() ?>);
94+
95+
});
96+
});
97+
</script>
98+
<?php endif; ?>
3599
<?php endif; ?>

0 commit comments

Comments
 (0)