Skip to content

Commit 46af28f

Browse files
Merge branch 'dev' into master
2 parents 4258d1c + 354d754 commit 46af28f

2 files changed

Lines changed: 134 additions & 29 deletions

File tree

ps_googleanalytics.php

Lines changed: 96 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* 2007-2017 PrestaShop
3+
* 2007-2018 PrestaShop
44
*
55
* NOTICE OF LICENSE
66
*
@@ -19,7 +19,7 @@
1919
* needs please refer to http://www.prestashop.com for more information.
2020
*
2121
* @author PrestaShop SA <contact@prestashop.com>
22-
* @copyright 2007-2017 PrestaShop SA
22+
* @copyright 2007-2018 PrestaShop SA
2323
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
2424
* International Registered Trademark & Property of PrestaShop SA
2525
*/
@@ -40,7 +40,7 @@ public function __construct()
4040
{
4141
$this->name = 'ps_googleanalytics';
4242
$this->tab = 'analytics_stats';
43-
$this->version = '3.0.4';
43+
$this->version = '3.1.0';
4444
$this->ps_versions_compliancy = array('min' => '1.7.0.0', 'max' => _PS_VERSION_);
4545
$this->author = 'PrestaShop';
4646
$this->module_key = 'fd2aaefea84ac1bb512e6f1878d990b8';
@@ -91,7 +91,7 @@ public function uninstall()
9191
*/
9292
protected function createTables()
9393
{
94-
return (bool)Db::getInstance()->execute('
94+
if ((bool)Db::getInstance()->execute('
9595
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ganalytics` (
9696
`id_google_analytics` int(11) NOT NULL AUTO_INCREMENT,
9797
`id_order` int(11) NOT NULL,
@@ -103,17 +103,35 @@ protected function createTables()
103103
KEY `id_order` (`id_order`),
104104
KEY `sent` (`sent`)
105105
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 AUTO_INCREMENT=1
106-
');
106+
') && (bool)Db::getInstance()->execute('
107+
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ganalytics_data` (
108+
`id_cart` int(11) NOT NULL,
109+
`id_shop` int(11) NOT NULL,
110+
`data` TEXT DEFAULT NULL,
111+
PRIMARY KEY (`id_cart`)
112+
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8
113+
'))
114+
{
115+
return true;
116+
}
117+
118+
return false;
107119
}
108120

109121
/**
110122
* deletes tables
111123
*/
112124
protected function deleteTables()
113125
{
114-
return (bool)Db::getInstance()->execute('
126+
if ((bool)Db::getInstance()->execute('
115127
DROP TABLE IF EXISTS `'._DB_PREFIX_.'ganalytics`
116-
');
128+
') && (bool)Db::getInstance()->execute('
129+
DROP TABLE IF EXISTS `'._DB_PREFIX_.'ganalytics_data`
130+
')) {
131+
return true;
132+
}
133+
134+
return false;
117135
}
118136

119137
public function displayForm()
@@ -185,6 +203,24 @@ public function displayForm()
185203
'label' => $this->l('Disabled')
186204
))
187205
),
206+
array(
207+
'type' => 'switch',
208+
'label' => $this->l('Anonymize IP'),
209+
'name' => 'GA_ANONYMIZE_ENABLED',
210+
'hint' => $this->l('Use this option to anonymize the visitor’s IP to comply with data privacy laws in some countries'),
211+
'values' => array(
212+
array(
213+
'id' => 'ga_anonymize_enabled',
214+
'value' => 1,
215+
'label' => $this->l('Enabled')
216+
),
217+
array(
218+
'id' => 'ga_anonymize_disabled',
219+
'value' => 0,
220+
'label' => $this->l('Disabled')
221+
),
222+
),
223+
),
188224
),
189225
'submit' => array(
190226
'title' => $this->l('Save'),
@@ -215,6 +251,7 @@ public function displayForm()
215251
$helper->fields_value['GA_ACCOUNT_ID'] = Configuration::get('GA_ACCOUNT_ID');
216252
$helper->fields_value['GA_USERID_ENABLED'] = Configuration::get('GA_USERID_ENABLED');
217253
$helper->fields_value['GA_CROSSDOMAIN_ENABLED'] = Configuration::get('GA_CROSSDOMAIN_ENABLED');
254+
$helper->fields_value['GA_ANONYMIZE_ENABLED'] = Configuration::get('GA_ANONYMIZE_ENABLED');
218255

219256
return $helper->generateForm($fields_form);
220257
}
@@ -237,19 +274,25 @@ public function getContent()
237274
Configuration::updateValue('GA_USERID_ENABLED', (bool)$ga_userid_enabled);
238275
$output .= $this->displayConfirmation($this->trans('Settings for User ID updated successfully', array(), 'Modules.GAnalytics.Admin'));
239276
}
277+
240278
$ga_crossdomain_enabled = Tools::getValue('GA_CROSSDOMAIN_ENABLED');
241279
if (null !== $ga_crossdomain_enabled) {
242280
Configuration::updateValue('GA_CROSSDOMAIN_ENABLED', (bool)$ga_crossdomain_enabled);
243281
$output .= $this->displayConfirmation($this->trans('Settings for User ID updated successfully', array(), 'Modules.GAnalytics.Admin'));
244282
}
283+
284+
$ga_anonymize_enabled = Tools::getValue('GA_ANONYMIZE_ENABLED');
285+
if (null !== $ga_anonymize_enabled) {
286+
Configuration::updateValue('GA_ANONYMIZE_ENABLED', (bool)$ga_anonymize_enabled);
287+
$output .= $this->displayConfirmation($this->trans('Settings for Anonymize IP updated successfully', array(), 'Modules.GAnalytics.Admin'));
288+
}
245289
}
246290

247291
$output .= $this->displayForm();
248292

249293
return $this->display(__FILE__, './views/templates/admin/configuration.tpl').$output;
250294
}
251295

252-
253296
public function hookdisplayHeader($params, $back_office = false)
254297
{
255298
if (Configuration::get('GA_ACCOUNT_ID')) {
@@ -350,22 +393,19 @@ public function hookdisplayFooter()
350393
{
351394
$ga_scripts = '';
352395
$this->js_state = 0;
353-
if (isset($this->context->cookie->ga_cart)) {
396+
$gacarts = $this->_manageData("", "R");
397+
if (count($gacarts)>0) {
354398
$this->filterable = 0;
355-
356-
$gacarts = json_decode($this->context->cookie->ga_cart, true);
357-
358-
if (is_array($gacarts)) {
359-
foreach ($gacarts as $gacart) {
360-
if ($gacart['quantity'] > 0) {
361-
$ga_scripts .= 'MBG.addToCart('.json_encode($gacart).');';
362-
} elseif ($gacart['quantity'] < 0) {
363-
$gacart['quantity'] = abs($gacart['quantity']);
364-
$ga_scripts .= 'MBG.removeFromCart('.json_encode($gacart).');';
365-
}
399+
400+
foreach ($gacarts as $gacart) {
401+
if ($gacart['quantity'] > 0) {
402+
$ga_scripts .= 'MBG.addToCart('.json_encode($gacart).');';
403+
} elseif ($gacart['quantity'] < 0) {
404+
$gacart['quantity'] = abs($gacart['quantity']);
405+
$ga_scripts .= 'MBG.removeFromCart('.json_encode($gacart).');';
366406
}
367407
}
368-
unset($this->context->cookie->ga_cart);
408+
$gacarts = $this->_manageData("", "D");
369409
}
370410

371411
$controller_name = Tools::getValue('controller');
@@ -383,7 +423,6 @@ public function hookdisplayFooter()
383423
$ga_scripts .= 'MBG.addCheckout(\''.(int)$step.'\');';
384424
}
385425

386-
387426
$confirmation_hook_id = (int)Hook::getIdByName('displayOrderConfirmation');
388427
if (isset(Hook::$executed_hooks[$confirmation_hook_id])) {
389428
$this->eligible = 1;
@@ -671,6 +710,38 @@ protected function _runJs($js_code, $backoffice = 0)
671710
}
672711
}
673712

713+
/**
714+
* Manage data
715+
* @param string $action "R" read data from DB, "W" write data, "A" append data, D" delete data
716+
* @return array dans le cas du R, sinon true
717+
*/
718+
protected function _manageData($data, $action)
719+
{
720+
if ($action == 'R') {
721+
$dataretour = Db::getInstance()->getValue('SELECT data FROM `'._DB_PREFIX_.'ganalytics_data` WHERE id_cart = \''.(int)$this->context->cart->id.'\' AND id_shop = \''.(int)$this->context->shop->id.'\'');
722+
if ($dataretour === false)
723+
return array();
724+
else
725+
return json_decode($dataretour,true);
726+
}
727+
if ($action == 'W') {
728+
return Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'ganalytics_data` (id_cart, id_shop, data) VALUES(\''.(int)$this->context->cart->id.'\',\''.(int)$this->context->shop->id.'\',\''.json_encode($data).'\') ON DUPLICATE KEY UPDATE data =\''.json_encode($data).'\' ;');
729+
}
730+
if ($action == 'A') {
731+
$dataretour = Db::getInstance()->getValue('SELECT data FROM `'._DB_PREFIX_.'ganalytics_data` WHERE id_cart = \''.(int)$this->context->cart->id.'\' AND id_shop = \''.(int)$this->context->shop->id.'\'');
732+
if ($dataretour === false)
733+
$datanew = array($data);
734+
else {
735+
$datanew = json_decode($dataretour,true);
736+
$datanew[] = $data;
737+
}
738+
return Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'ganalytics_data` (id_cart, id_shop, data) VALUES(\''.(int)$this->context->cart->id.'\',\''.(int)$this->context->shop->id.'\',\''.json_encode($datanew).'\') ON DUPLICATE KEY UPDATE data =\''.serialize($datanew).'\' ;');
739+
}
740+
if ($action == 'D') {
741+
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'ganalytics_data` WHERE id_cart = \''.(int)$this->context->cart->id.'\' AND id_shop = \''.(int)$this->context->shop->id.'\'');
742+
}
743+
}
744+
674745
/**
675746
* Hook admin order to send transactions and refunds details
676747
*/
@@ -808,11 +879,7 @@ public function hookactionCartSave()
808879
$id_product = Tools::getValue('id_product');
809880
}
810881

811-
if (isset($this->context->cookie->ga_cart)) {
812-
$gacart = json_decode($this->context->cookie->ga_cart, true);
813-
} else {
814-
$gacart = array();
815-
}
882+
$gacart = $this->_manageData("", "R");
816883

817884
if ($cart['removeAction'] == 'delete') {
818885
$ga_products['quantity'] = -1;
@@ -829,15 +896,15 @@ public function hookactionCartSave()
829896
}
830897

831898
$gacart[$id_product] = $ga_products;
832-
$this->context->cookie->ga_cart = json_encode($gacart);
899+
$this->_manageData($gacart, 'W');
833900
}
834901
}
835902

836903
public function hookactionCarrierProcess($params)
837904
{
838905
if (isset($params['cart']->id_carrier)) {
839906
$carrier_name = Db::getInstance()->getValue('SELECT name FROM `'._DB_PREFIX_.'carrier` WHERE id_carrier = '.(int)$params['cart']->id_carrier);
840-
$this->context->cookie->ga_cart .= 'MBG.addCheckoutOption(2,\''.$carrier_name.'\');';
907+
$this->_manageData('MBG.addCheckoutOption(2,\''.$carrier_name.'\');', 'A');
841908
}
842909
}
843910

upgrade/install-3.1.0.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* 2007-2018 PrestaShop
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Academic Free License (AFL 3.0)
8+
* that is bundled with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* http://opensource.org/licenses/afl-3.0.php
11+
* If you did not receive a copy of the license and are unable to
12+
* obtain it through the world-wide-web, please send an email
13+
* to license@prestashop.com so we can send you a copy immediately.
14+
*
15+
* DISCLAIMER
16+
*
17+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18+
* versions in the future. If you wish to customize PrestaShop for your
19+
* needs please refer to http://www.prestashop.com for more information.
20+
*
21+
* @author PrestaShop SA <contact@prestashop.com>
22+
* @copyright 2007-2018 PrestaShop SA
23+
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
24+
* International Registered Trademark & Property of PrestaShop SA
25+
*/
26+
if (!defined('_PS_VERSION_'))
27+
exit;
28+
function upgrade_module_3_1_0($object)
29+
{
30+
Configuration::updateValue('GANALYTICS', '3.1.0');
31+
return Db::getInstance()->execute('
32+
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ganalytics_data` (
33+
`id_cart` int(11) NOT NULL,
34+
`id_shop` int(11) NOT NULL,
35+
`data` TEXT DEFAULT NULL,
36+
PRIMARY KEY (`id_cart`)
37+
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8');
38+
}

0 commit comments

Comments
 (0)