File: /home/p/e/t/petsclubcc/shop/modules/pixelsmi/pixelsmi.php
<?php
/**
* 2007-2022 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2022 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class Pixelsmi extends Module
{
protected $config_form = false;
public function __construct()
{
$this->name = 'pixelsmi';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'ShopMyInfluence';
$this->need_instance = 0;
$this->module_key = 'cc13e13a23e3492ac477ad9df3f43bbd';
/*
* Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
*/
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('ShopMyInfluence pixel');
$this->description = $this->l('This module allows you to configure our pixel with a few clicks on your store and start using the unique experience of our Shop My Influence tools.');
$this->confirmUninstall = $this->l('');
$this->ps_versions_compliancy = array(
'min' => '1.7',
'max' => _PS_VERSION_,
);
}
/**
* Don't forget to create update methods if needed:
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
*/
public function install()
{
// Configuration::updateValue('PIXELSMI_LIVE_MODE', false);
// include(dirname(__FILE__).'/sql/install.php');
// TODO: TEST_ME and COMPARE the old version
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('backOfficeHeader') &&
$this->registerHook('displayHeader') &&
$this->registerHook('displayOrderConfirmation');
}
public function uninstall()
{
// old code
// Configuration::deleteByName('PIXEL_SMI_FORM');
// return parent::uninstall();
include dirname(__FILE__) . '/sql/uninstall.php';
Configuration::deleteByName('PIXELSMI_LIVE_MODE');
Configuration::deleteByName('PIXEL_SMI_FORM');
return parent::uninstall();
}
/**
* Load the configuration form
*/
public function getContent()
{
$id_shop_group = (int) Shop::getContextShopGroupID();
$id_shop = (int) $this->context->shop->id;
if ($id_shop_group == 0) {
$id_shop_group = null;
$id_shop = null;
}
$configuration = json_decode(Configuration::get('PIXEL_SMI_FORM', null, $id_shop_group, $id_shop));
// check if there is already values saved to get the old logo
$isNew = '';
//dump($configuration);
if (is_array($configuration) && isset($configuration[0])) {
//dump($configuration);die();
is_null($configuration[0]) || (Tools::getValue('nom_marque') != '') ? $nom_marque = Tools::getValue('nom_marque') : $nom_marque = $configuration[0];
is_null($configuration[1]) || (Tools::getValue('email') != '') ? $email = Tools::getValue('email') : $email = $configuration[1];
is_null($configuration[2]) || (Tools::getValue('telephone') != '') ? $telephone = Tools::getValue('telephone') : $telephone = $configuration[2];
//dump(Tools::getValue('telephone'));
is_null($configuration[4]) || (Tools::getValue('numero_serie') != '') ? $numero_serie = Tools::getValue('numero_serie') : $numero_serie = Tools::getValue('numero_serie');
is_null($configuration[5]) || (Tools::getValue('adresse') != '') ? $adresse = Tools::getValue('adresse') : $adresse = $configuration[5];
is_null($configuration[6]) || (Tools::getValue('code_postal') != '') ? $code_postal = Tools::getValue('code_postal') : $code_postal = $configuration[6];
is_null($configuration[8]) || (Tools::getValue('nom_contact') != '') ? $nom_contact = Tools::getValue('nom_contact') : $nom_contact = $configuration[8];
is_null($configuration[9]) || (Tools::getValue('email_contact') != '') ? $email_contact = Tools::getValue('email_contact') : $email_contact = $configuration[9];
is_null($configuration[3]) || (Tools::getValue('site') != '') ? $site = Tools::getValue('site') : $site = $configuration[3];
is_null($configuration[7]) || is_array(Tools::getValue('pays') ) ? $pays = Tools::getValue('pays')[0] : $pays = $configuration[7];
is_null($configuration[10]) || (Tools::getValue('telephone_contact') != '') ? $telephone_contact = Tools::getValue('telephone_contact') : $telephone_contact = $configuration[10];
is_null($configuration[11]) || (Tools::getValue('commission') != '') ? $commission = Tools::getValue('commission') : $commission = $configuration[11];
is_null($configuration[12]) || (Tools::getValue('categorie') != '') ? $categorie = Tools::getValue('categorie') : $categorie = $configuration[12];
is_null($configuration[13]) || (Tools::getValue('description') != '') ? $description = Tools::getValue('description') : $description = $configuration[13];
is_null($configuration[14]) || (Tools::getValue('logo') != '') ? $logo = Tools::getValue('logo') : $logo = $configuration[14];
is_null($configuration[15]) || (Tools::getValue('pays_livraison') != '') ? $pays_livraison = Tools::getValue('pays_livraison') : $pays_livraison = $configuration[15];
$PixelArray = array(
$nom_marque,
$email,
$telephone,
$site,
$numero_serie,
$adresse,
$code_postal,
$pays,
$nom_contact,
$email_contact,
$telephone_contact,
$commission,
$categorie,
$description,
$logo,
$pays_livraison,
);
}else{
$type = array_fill(0, 2, '');
$country = array_fill(0, 2, '');
$image = null;
$configuration = array_fill(0, 15, '');
$configuration[12] = $type;
$configuration[14] = $image;
$configuration[15] = $country;
$PixelArray = $configuration;
Configuration::updateValue('PIXEL_SMI_FORM', json_encode($configuration), false, $id_shop_group, $id_shop);
}
$configuration = json_decode(Configuration::get('PIXEL_SMI_FORM'), json_encode($PixelArray));
//dump($configuration);die();
$message = '';
if (((bool) Tools::isSubmit('submitPixelModule')) == true) {
if (Validate::isFloat(Tools::getValue('commission')) && (Tools::getValue('commission') <= 100 && Tools::getValue('commission') >= 0)) {
!$configuration ? $isNew == true : $isNew == false;
$data = array(
'brandName' => $nom_marque,
'email' => $email,
'siret' => $numero_serie,
'country' => $pays,
'contactName' => $nom_contact,
'contactEmail' => $email_contact,
'website' => $site,
'phoneNumber' => $telephone_contact,
'commission' => $commission,
'categories' => $categorie,
'description' => $description,
'logo' => $logo,
'deliveriesCountries' => $pays_livraison,
'isNew' => $isNew,
'domainName' => Context::getContext()
->shop
->getBaseURL(true),
);
$url = 'http://us-central1-shopmyinfluens.cloudfunctions.net/globalApi/prestashop/request';
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = Tools::file_get_contents($url, false, $context);
Configuration::updateValue('PIXEL_SMI_FORM', json_encode($PixelArray));
!$configuration ? $message = $this->displayConfirmation($this->l('thank you for entering your information! our team will contact you as soon as possible')) : $message = $this->displayConfirmation($this->l('settings saved successfully!'));
} else {
if (!(Validate::isFloat(Tools::getValue('commission'))) || (Tools::getValue('commission') > 100 || Tools::getValue('commission') < 0) || !(Tools::getValue('commission'))) {
$message = $message . $this->displayError($this->l('Invalid commission !'));
}
}
}
(string) $this->context->language->iso_code == 'en' ? $output = $this->context->smarty->fetch($this->local_path . 'views\templates\admin\sss.tpl') : $output = $this->context->smarty->fetch($this->local_path . 'views\templates\admin\ss.tpl');
$this->context->smarty->assign(array('PIXEL_SMI_FORM' => Configuration::get('PIXEL_SMI_FORM')));
return $message . $output . $this->displayForm();
}
/**
* Display form using Helper
*
* @return string
*/
public function displayForm()
{
$id_shop_group = (int) Shop::getContextShopGroupID();
$id_shop = (int) $this
->context
->shop->id;
$this
->context
->controller
->addJS($this->_path . '/views/js/front.js');
$this
->context
->controller
->addCSS($this->_path . '/views/css/front.css');
$configuration = json_decode(Configuration::get('PIXEL_SMI_FORM', null, $id_shop_group, $id_shop));
if (!is_null($configuration[0])) {
$this
->context
->smarty
->assign('pixel_form_marque', $configuration[0]);
$this
->context
->smarty
->assign('pixel_form_email', $configuration[1]);
$this
->context
->smarty
->assign('pixel_form_tel', $configuration[2]);
$this
->context
->smarty
->assign('pixel_form_site', $configuration[3]);
$this
->context
->smarty
->assign('pixel_form_siret', $configuration[4]);
$this
->context
->smarty
->assign('pixel_form_adresse', $configuration[5]);
$this
->context
->smarty
->assign('pixel_form_code', $configuration[6]);
$this
->context
->smarty
->assign('pixel_form_pays', $configuration[7]);
$this
->context
->smarty
->assign('pixel_form_name_contact', $configuration[8]);
$this
->context
->smarty
->assign('pixel_form_email_contact', $configuration[9]);
$this
->context
->smarty
->assign('pixel_form_telephone_contact', $configuration[10]);
$this
->context
->smarty
->assign('pixel_form_commission', $configuration[11]);
$this
->context
->smarty
->assign('pixel_form_categorie', $configuration[12]);
$this
->context
->smarty
->assign('pixel_form_description', $configuration[13]);
$this
->context
->smarty
->assign('pixel_form_logo', $configuration[14]);
$this
->context
->smarty
->assign('deliveriesCountries', $configuration[15]);
}
/* pour deliveries country */
$countries_file = Tools::file_get_contents($this->local_path . 'translations/countries.json');
// get countries list from the json file
$countries = json_decode($countries_file, true);
$arr = array();
$selectedDeliveriesCountries = $configuration[15];
// check which country translated name to passe, french or english name
// create deliveries countries list to be sent to .tpl file
if ((string) $this
->context
->language->iso_code == 'fr') {
foreach ($countries as $key => $value) {
$isSelected = false;
// check if there already selected countries in config to select them in the .tpl file
if (!is_null($selectedDeliveriesCountries)) {
foreach ($selectedDeliveriesCountries as $key2 => $name) {
if ($name == $value['fr_name']) {
$isSelected = true;
}
}
}
$countObj = json_decode('{}');
$countObj->name = $value['fr_name'];
$countObj->isSelected = $isSelected;
$arr[] = $countObj;
$this
->context
->smarty
->assign('countries', $arr);
}
} else {
foreach ($countries as $key => $value) {
$isSelected = false;
// check if there already selected countries in config to select them in the .tpl file
if (!is_null($selectedDeliveriesCountries)) {
foreach ($selectedDeliveriesCountries as $key => $name) {
if ($name == $value['en_name']) {
$isSelected = true;
}
}
}
$countObj = json_decode('{}');
$countObj->name = $value['en_name'];
$countObj->isSelected = $isSelected;
$arr[] = $countObj;
$this
->context
->smarty
->assign('countries', $arr);
}
}
// get countries list from the json file
$countries_file = Tools::file_get_contents($this->local_path . 'translations/countries.json');
$countries = json_decode($countries_file, true);
$ar = array();
$selectedCountry = $configuration[7];
// check which country translated name to passe, french or english name
// create brand location countries list to be sent to .tpl file
if ((string) $this
->context
->language->iso_code == 'fr') {
foreach ($countries as $key => $value) {
$isSelected = false;
if (!is_null($selectedCountry)) {
if ($value['fr_name'] == $selectedCountry) {
$isSelected = true;
}
}
$countObj = json_decode('{}');
$countObj->name = $value['fr_name'];
$countObj->isSelected = $isSelected;
$ar[] = $countObj;
$this
->context
->smarty
->assign('locationCountries', $ar);
}
} else {
foreach ($countries as $key => $value) {
/* echo($key); */
$isSelected = false;
if (!is_null($selectedCountry)) {
if ($value['en_name'] == $selectedCountry) {
$isSelected = true;
}
}
$countObj = json_decode('{}');
$countObj->name = $value['en_name'];
$countObj->isSelected = $isSelected;
$ar[] = $countObj;
$this
->context
->smarty
->assign('locationCountries', $ar);
}
}
$categories_file = Tools::file_get_contents($this->local_path . 'translations/categories.json');
// get categorieses list from the json file
$categories = json_decode($categories_file, true);
$arrr = array();
$selectedcategories = $configuration[12];
// check which categories translated name to passe, french or english name
// check with category was previously selected
if ((string) $this
->context
->language->iso_code == 'fr') {
foreach ($categories as $key => $value) {
/* echo($key); */
$isSelected = false;
if (!is_null($selectedcategories)) {
foreach ($selectedcategories as $key => $name) {
if ($name == $value['fr_name']) {
$isSelected = true;
}
}
}
$countObj = json_decode('{}');
$countObj->name = $value['fr_name'];
$countObj->isSelected = $isSelected;
$arrr[] = $countObj;
$this
->context
->smarty
->assign('categories', $arrr);
}
} else {
foreach ($categories as $key => $value) {
/* echo($key); */
$isSelected = false;
if (!is_null($selectedcategories)) {
foreach ($selectedcategories as $key => $name) {
if ($name == $value['en_name']) {
$isSelected = true;
}
}
}
$countObj = json_decode('{}');
$countObj->name = $value['en_name'];
$countObj->isSelected = $isSelected;
$arrr[] = $countObj;
$this
->context
->smarty
->assign('categories', $arrr);
}
}
return $this->display(__FILE__, 'views/templates/admin/configure.tpl');
}
/**
* Create the form that will be displayed in the configuration of your module.
*/
protected function renderForm()
{
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitPixelsmiModule';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
. '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
}
/**
* Create the structure of your form.
*/
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'switch',
'label' => $this->l('Live mode'),
'name' => 'PIXELSMI_LIVE_MODE',
'is_bool' => true,
'desc' => $this->l('Use this module in live mode'),
'values' => array(
array(
'id' => 'active_on',
'value' => true,
'label' => $this->l('Enabled'),
),
array(
'id' => 'active_off',
'value' => false,
'label' => $this->l('Disabled'),
),
),
),
array(
'col' => 3,
'type' => 'text',
'prefix' => '<i class="icon icon-envelope"></i>',
'desc' => $this->l('Enter a valid email address'),
'name' => 'PIXELSMI_ACCOUNT_EMAIL',
'label' => $this->l('Email'),
),
array(
'type' => 'password',
'name' => 'PIXELSMI_ACCOUNT_PASSWORD',
'label' => $this->l('Password'),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
/**
* Set values for the inputs.
*/
protected function getConfigFormValues()
{
return array(
'PIXELSMI_LIVE_MODE' => Configuration::get('PIXELSMI_LIVE_MODE', true),
'PIXELSMI_ACCOUNT_EMAIL' => Configuration::get('PIXELSMI_ACCOUNT_EMAIL', 'contact@prestashop.com'),
'PIXELSMI_ACCOUNT_PASSWORD' => Configuration::get('PIXELSMI_ACCOUNT_PASSWORD', null),
);
}
/**
* Save form data.
*/
protected function postProcess()
{
$form_values = $this->getConfigFormValues();
foreach (array_keys($form_values) as $key) {
Configuration::updateValue($key, Tools::getValue($key));
}
}
/**
* Add the CSS & JavaScript files you want to be loaded in the BO.
*/
public function hookBackOfficeHeader()
{
if (Tools::getValue('module_name') == $this->name) {
$this->context->controller->addJS($this->_path . 'views/js/back.js');
$this->context->controller->addCSS($this->_path . 'views/css/back.css');
}
}
/**
* Add the CSS & JavaScript files you want to be added on the FO.
*/
public function hookHeader()
{
$this->context->controller->addJS($this->_path . '/views/js/front.js');
$this->context->controller->addCSS($this->_path . '/views/css/front.css');
}
public function hookDisplayHeader()
{
$this->context->controller->registerJavascript('cdn', 'https://firebasestorage.googleapis.com/v0/b/shopmyinfluens.appspot.com/o/Pixel%2FclickObserver.js?alt=media&token=aea9abc8-a7a7-4351-a78e-d9621d55abd6', array('media' => 'all', 'priority' => 10, 'inline' => true, 'server' => 'remote'));
}
public function hookDisplayOrderConfirmation($params)
{
//get total transaction amount
$total = (float)$params['order']->getTotalProductsWithoutTaxes();
//convert to main shop currency
$total = Tools::convertPrice($total);
//get order
$order = $params['order'];
//get order id
$order_id = $order->id;
//get products
$products = $order->getProducts();
//products count
$product_count = 0;
//get products details
$products_details = array();
foreach ($products as $item) {
$products_details[] = array(
"name" => $item['product_name'],
"product_id" => $item['product_id'],
"price" => $item['total_price_tax_incl'],
"quantity" => $item['product_quantity']
);
$product_count = $product_count + $item['product_quantity'];
}
//convert products details array to json object
$productsJsonData = json_encode($products_details);
$id_shop_group = (int)Shop::getContextShopGroupID();
$id_shop = (int)$this
->context
->shop->id;
if ($id_shop_group == 0) {
$id_shop_group = null;
$id_shop = null;
}
$configuration = json_decode(Configuration::get('PIXEL_SMI_FORM', null, $id_shop_group, $id_shop));
$commission = $configuration[11];
Media::addJsDef(
array(
'pixelsmi' => array(
'afprice' => $total,
'commission_amount' => $configuration[11],
'custom_field2' => $order_id,
'custom_field3' => $product_count,
'custom_field4' => $productsJsonData,
)
)
);
$this->context->controller->registerJavascript('modules-pixelsmi', 'modules/' . $this->name . '/views/js/scriptcommission.js');
}
}