Skip to content

Commit ecaa7a9

Browse files
Compatibilité 1.7
1 parent cb450b4 commit ecaa7a9

1 file changed

Lines changed: 69 additions & 81 deletions

File tree

ps_googleanalytics.php

Lines changed: 69 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* 2007-2015 PrestaShop
3+
* 2007-2017 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-2015 PrestaShop SA
22+
* @copyright 2007-2017 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,34 +40,57 @@ public function __construct()
4040
$this->name = 'ps_googleanalytics';
4141
$this->tab = 'analytics_stats';
4242
$this->version = '3.0.0';
43+
$this->ps_versions_compliancy = array('min' => '1.7.1.0', 'max' => _PS_VERSION_);
4344
$this->author = 'PrestaShop';
4445
$this->module_key = 'fd2aaefea84ac1bb512e6f1878d990b8';
4546
$this->bootstrap = true;
4647

4748
parent::__construct();
4849

49-
$this->displayName = $this->l('Google Analytics');
50-
$this->description = $this->l('Gain clear insights into important metrics about your customers, using Google Analytics');
51-
$this->ps_versions_compliancy = array('min' => '1.7.1.0', 'max' => _PS_VERSION_);
52-
$this->confirmUninstall = $this->l('Are you sure you want to uninstall Google Analytics? You will lose all the data related to this module.');
50+
$this->displayName = $this->trans('Google Analytics', array(), 'Modules.GAnalytics.Admin');
51+
$this->description = $this->trans('Gain clear insights into important metrics about your customers, using Google Analytics', array(), 'Modules.GAnalytics.Admin');
52+
53+
$this->confirmUninstall = $this->trans('Are you sure you want to uninstall Google Analytics? You will lose all the data related to this module.', array(), 'Modules.GAnalytics.Admin');
5354

5455
}
55-
5656
public function install()
5757
{
58+
if (Shop::isFeatureActive())
59+
Shop::setContext(Shop::CONTEXT_ALL);
60+
61+
if (parent::install() &&
62+
$this->registerHook('displayHeader') &&
63+
$this->registerHook('displayAdminOrder') &&
64+
$this->registerHook('displayFooter') &&
65+
$this->registerHook('displayHome') &&
66+
$this->registerHook('displayFooterProduct') &&
67+
$this->registerHook('displayOrderConfirmation') &&
68+
$this->registerHook('actionProductCancel') &&
69+
$this->registerHook('actionCartSave') &&
70+
$this->registerHook('displayBackOfficeHeader') &&
71+
$this->registerHook('actionCarrierProcess')
72+
) {
73+
return $this->createTables();
74+
}
5875

59-
Shop::setContext(Shop::CONTEXT_ALL);
76+
return false;
77+
}
6078

61-
if (!parent::install() || !$this->installTab() || !$this->registerHook('displayHeader') || !$this->registerHook('displayAdminOrder')
62-
|| !$this->registerHook('displayFooter') || !$this->registerHook('displayHome')
63-
|| !$this->registerHook('displayFooterProduct') || !$this->registerHook('displayOrderConfirmation')
64-
|| !$this->registerHook('actionProductCancel') || !$this->registerHook('actionCartSave')
65-
|| !$this->registerHook('displayBackOfficeHeader')) || !$this->registerHook('actionCarrierProcess'))
66-
return false;
79+
public function uninstall()
80+
{
81+
if (parent::uninstall()) {
82+
return $this->deleteTables();
83+
}
6784

68-
Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'ganalytics`');
85+
return false;
86+
}
6987

70-
if (!Db::getInstance()->Execute('
88+
/**
89+
* Creates tables
90+
*/
91+
protected function createTables()
92+
{
93+
return (bool)Db::getInstance()->execute('
7194
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ganalytics` (
7295
`id_google_analytics` int(11) NOT NULL AUTO_INCREMENT,
7396
`id_order` int(11) NOT NULL,
@@ -78,46 +101,19 @@ public function install()
78101
PRIMARY KEY (`id_google_analytics`),
79102
KEY `id_order` (`id_order`),
80103
KEY `sent` (`sent`)
81-
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 AUTO_INCREMENT=1'))
82-
return $this->uninstall();
83-
84-
return true;
85-
}
86-
87-
public function uninstall()
88-
{
89-
if (!$this->uninstallTab() || !parent::uninstall())
90-
return false;
91-
92-
return Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'ganalytics`');
93-
}
94-
95-
public function installTab()
96-
{
97-
98-
$tab = new Tab();
99-
$tab->active = 0;
100-
$tab->class_name = 'AdminGanalyticsAjax';
101-
$tab->name = array();
102-
foreach (Language::getLanguages(true) as $lang)
103-
$tab->name[$lang['id_lang']] = 'Google Analytics Ajax';
104-
$tab->id_parent = -1; //(int)Tab::getIdFromClassName('AdminAdmin');
105-
$tab->module = $this->name;
106-
return $tab->add();
104+
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 AUTO_INCREMENT=1
105+
');
107106
}
108107

109-
public function uninstallTab()
110-
{
111-
112-
$id_tab = (int)Tab::getIdFromClassName('AdminGanalyticsAjax');
113-
if ($id_tab)
114-
{
115-
$tab = new Tab($id_tab);
116-
return $tab->delete();
117-
}
118-
119-
return true;
120-
}
108+
/**
109+
* deletes tables
110+
*/
111+
protected function deleteTables()
112+
{
113+
return (bool)Db::getInstance()->execute('
114+
DROP TABLE IF EXISTS `'._DB_PREFIX_.'ganalytics`
115+
');
116+
}
121117

122118
public function displayForm()
123119
{
@@ -213,19 +209,19 @@ public function getContent()
213209
{
214210
Configuration::updateValue('GA_ACCOUNT_ID', $ga_account_id);
215211
Configuration::updateValue('GANALYTICS_CONFIGURATION_OK', true);
216-
$output .= $this->displayConfirmation($this->l('Account ID updated successfully'));
212+
$output .= $this->displayConfirmation($this->trans('Account ID updated successfully', array(), 'Modules.GAnalytics.Admin'));
217213
}
218214
$ga_userid_enabled = Tools::getValue('GA_USERID_ENABLED');
219215
if (null !== $ga_userid_enabled)
220216
{
221217
Configuration::updateValue('GA_USERID_ENABLED', (bool)$ga_userid_enabled);
222-
$output .= $this->displayConfirmation($this->l('Settings for User ID updated successfully'));
218+
$output .= $this->displayConfirmation($this->trans('Settings for User ID updated successfully', array(), 'Modules.GAnalytics.Admin'));
223219
}
224220
}
225221

226222
$output .= $this->displayForm();
227223

228-
return $this->fetch('module:ps_googleanalytics/views/templates/admin/configuration.tpl').$output;
224+
return $this->display(__FILE__, './views/templates/admin/configuration.tpl').$output;
229225
}
230226

231227
protected function _getGoogleAnalyticsTag($back_office = false)
@@ -340,11 +336,12 @@ public function hookdisplayFooter()
340336
$ga_scripts .= 'MBG.removeFromCart('.Tools::jsonEncode($gacart).');';
341337
}
342338
}
343-
unset($this->context->cookie->ga_cart);
339+
unset($this->context->cookie->ga_cart);
344340
}
345341

346342
$controller_name = Tools::getValue('controller');
347-
$products = $this->wrapProducts($this->context->smarty->getTemplateVars('products'), array(), true);
343+
$listing = $this->context->smarty->getTemplateVars('listing');
344+
$products = $this->wrapProducts($listing['products'], array(), true);
348345

349346
if ($controller_name == 'order' || $controller_name == 'orderopc')
350347
{
@@ -388,42 +385,30 @@ public function hookdisplayHome()
388385
$ga_scripts = '';
389386

390387
// Home featured products
391-
if ($this->isModuleEnabled('homefeatured'))
388+
if ($this->isModuleEnabled('ps_featuredproducts'))
392389
{
393390
$category = new Category($this->context->shop->getCategory(), $this->context->language->id);
394391
$home_featured_products = $this->wrapProducts($category->getProducts((int)Context::getContext()->language->id, 1,
395392
(Configuration::get('HOME_FEATURED_NBR') ? (int)Configuration::get('HOME_FEATURED_NBR') : 8), 'position'), array(), true);
396393
$ga_scripts .= $this->addProductImpression($home_featured_products).$this->addProductClick($home_featured_products);
397394
}
398395

399-
// New products
400-
if ($this->isModuleEnabled('blocknewproducts') && (Configuration::get('PS_NB_DAYS_NEW_PRODUCT')
401-
|| Configuration::get('PS_BLOCK_NEWPRODUCTS_DISPLAY')))
402-
{
403-
$new_products = Product::getNewProducts((int)$this->context->language->id, 0, (int)Configuration::get('NEW_PRODUCTS_NBR'));
404-
$new_products_list = $this->wrapProducts($new_products, array(), true);
405-
$ga_scripts .= $this->addProductImpression($new_products_list).$this->addProductClick($new_products_list);
406-
}
407-
408-
// Best Sellers
409-
if ($this->isModuleEnabled('blockbestsellers') && (!Configuration::get('PS_CATALOG_MODE')
410-
|| Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')))
411-
{
412-
$ga_homebestsell_product_list = $this->wrapProducts(ProductSale::getBestSalesLight((int)$this->context->language->id, 0, 8), array(), true);
413-
$ga_scripts .= $this->addProductImpression($ga_homebestsell_product_list).$this->addProductClick($ga_homebestsell_product_list);
414-
}
415-
416396
$this->js_state = 1;
417397
return $this->_runJs($this->filter($ga_scripts));
418398
}
419399

420400
/**
421401
* hook home to display generate the product list associated to home featured, news products and best sellers Modules
422402
*/
423-
public function isModuleEnabled($name)
403+
public function isModuleEnabled($module_name)
424404
{
425-
$module = Module::getInstanceByName($name);
426-
return $module->isRegisteredInHook('home');
405+
406+
if (($module = Module::getInstanceByName($module_name)) !== false &&
407+
Module::isInstalled($module_name) &&
408+
$module->active ) {
409+
return $module->registerHook('displayHome');
410+
}
411+
427412
}
428413

429414
/**
@@ -503,7 +488,7 @@ public function wrapProduct($product, $extras, $index = 0, $full = false)
503488
'quantity' => $product_qty,
504489
'list' => Tools::getValue('controller'),
505490
'url' => isset($product['link']) ? urlencode($product['link']) : '',
506-
'price' => number_format($product['price'], 2, '.', '')
491+
'price' => $product['price']
507492
);
508493
}
509494
else
@@ -591,8 +576,10 @@ public function addProductFromCheckout($products)
591576
public function hookdisplayFooterProduct($params)
592577
{
593578
$controller_name = Tools::getValue('controller');
579+
594580
if ($controller_name == 'product')
595581
{
582+
596583
// Add product view
597584
$ga_product = $this->wrapProduct((array)$params['product'], null, 0, true);
598585
$js = 'MBG.addProductDetailView('.Tools::jsonEncode($ga_product).');';
@@ -814,4 +801,5 @@ protected function _debugLog($function, $log)
814801
fwrite($fh, print_r($log, true)."\n\n");
815802
fclose($fh);
816803
}
804+
817805
}

0 commit comments

Comments
 (0)