247 lines
9.0 KiB
PHP
247 lines
9.0 KiB
PHP
<?php
|
|
/**
|
|
* 2022 ECSoft
|
|
*
|
|
* 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 dev.ecsoft@gmail.com so we can send you a copy immediately.
|
|
*
|
|
* @author ECSoft <dev.ecsoft@gmail.com>
|
|
* @copyright 2022 ECSoft
|
|
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
|
* International Registered Trademark & Property of ECSoft
|
|
*/
|
|
require_once dirname(__FILE__) . '/../../classes/EcsGtmProInstaller.php';
|
|
require_once dirname(__FILE__) . '/../../classes/EcsGtmProTools.php';
|
|
|
|
class AdminEcsGtmProHooksController extends ModuleAdminController
|
|
{
|
|
public function __construct()
|
|
{
|
|
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
|
|
header("Cache-Control: post-check=0, pre-check=0", false);
|
|
header("Pragma: no-cache");
|
|
$this->bootstrap = true;
|
|
$this->display = 'view';
|
|
parent::__construct();
|
|
}
|
|
|
|
public function ajaxProcessRegenerateOrderLog()
|
|
{
|
|
$count = $this->module->createMissingGtmOrderLogs();
|
|
die(EcsGtmProTools::jsonEncode(array(
|
|
'success' => 1,
|
|
'count' => $count
|
|
)));
|
|
}
|
|
|
|
public function ajaxProcessInstallHooks()
|
|
{
|
|
$installer = new EcsGtmProInstaller($this->module);
|
|
if ($installer->installCustomHooks()) {
|
|
die(EcsGtmProTools::jsonEncode(array(
|
|
'success' => 1,
|
|
'errors' => $installer->getCustomHooksErrors()
|
|
)));
|
|
}
|
|
die(EcsGtmProTools::jsonEncode(array(
|
|
'success' => 0,
|
|
'errors' => $installer->getCustomHooksErrors()
|
|
)));
|
|
}
|
|
|
|
public function ajaxProcessSaveConfig()
|
|
{
|
|
$data = Tools::getValue('data');
|
|
$modelClass = Tools::getValue('model');
|
|
if (!in_array($modelClass, $this->module->getAllowedSubmits())) {
|
|
http_response_code(404);
|
|
die(EcsGtmProTools::jsonEncode(array(
|
|
'error' => $this->l('Model class not found')
|
|
)));
|
|
}
|
|
$model = new $modelClass($this->module);
|
|
$model->populate();
|
|
if ($model->validate()) {
|
|
die(EcsGtmProTools::jsonEncode(array(
|
|
'success' => (int)$model->saveToConfig(),
|
|
'model' => $model->getAttributes()
|
|
)));
|
|
} else {
|
|
http_response_code(422);
|
|
die(EcsGtmProTools::jsonEncode($model->getErrors()));
|
|
}
|
|
}
|
|
|
|
public function ajaxProcessLoadConfig()
|
|
{
|
|
$modelClass = Tools::getValue('model');
|
|
$model = new $modelClass($this->module);
|
|
$model->loadFromConfig();
|
|
die(EcsGtmProTools::jsonEncode(array(
|
|
'model' => $model->getAttributes(),
|
|
'config' => $model->attributesConfig(),
|
|
), JSON_NUMERIC_CHECK));
|
|
}
|
|
|
|
protected function getSQL($where = array(), $count = false)
|
|
{
|
|
$table = EcsGtmProOrder::$definition['table'];
|
|
$select = array(
|
|
'o.id_order',
|
|
'o.reference',
|
|
'o.total_paid_tax_incl',
|
|
'o.payment',
|
|
'o.current_state',
|
|
'a.sent',
|
|
'a.resent',
|
|
'a.refund',
|
|
'IF(a.datalayer = "", "", 1) AS dlstatus',
|
|
'osl.`name` AS `osname`',
|
|
'os.`color` AS `oscolor`',
|
|
'o.date_add',
|
|
'a.date_upd AS gtm_date_update'
|
|
);
|
|
$where = array_merge($where, array('1'));
|
|
$orderBy = 'o.id_order';
|
|
$orderWay = 'DESC';
|
|
$join = array(
|
|
'RIGHT JOIN `' . _DB_PREFIX_ . 'orders` o ON (o.id_order = a.id_order)',
|
|
'LEFT JOIN `' . _DB_PREFIX_ . 'order_state` os ON (os.`id_order_state` = o.`current_state`)',
|
|
'LEFT JOIN `' . _DB_PREFIX_ . 'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = ' . (int)Context::getContext()->language->id . ')'
|
|
);
|
|
if (Shop::isFeatureActive()) {
|
|
$join[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'shop` sh ON (sh.`id_shop` = o.`id_shop`)';
|
|
$select[] = ' sh.name as shop_name,';
|
|
}
|
|
|
|
if ($count) {
|
|
return 'SELECT COUNT(1) AS total FROM ' . _DB_PREFIX_ . $table . ' a ' .
|
|
PHP_EOL . implode(PHP_EOL, $join) .
|
|
PHP_EOL . ' WHERE ' . implode(' AND ', $where) .
|
|
PHP_EOL . ' ORDER BY ' . $orderBy . ' ' . $orderWay;
|
|
}
|
|
|
|
$sql = 'SELECT ' . implode(', ', $select) . ' FROM ' . _DB_PREFIX_ . $table . ' a ' .
|
|
PHP_EOL . implode(PHP_EOL, $join) .
|
|
PHP_EOL . ' WHERE ' . implode(' AND ', $where) .
|
|
PHP_EOL . ' ORDER BY ' . $orderBy . ' ' . $orderWay;
|
|
|
|
return $sql;
|
|
}
|
|
|
|
public function ajaxProcessLoadOrders()
|
|
{
|
|
$page = Tools::getValue('page', 1);
|
|
$limit = 20;
|
|
$offset = ((int)$page - 1) * $limit;
|
|
|
|
$total = Db::getInstance()->getValue($this->getSQL(array(), true));
|
|
|
|
$sql = $this->getSQL() . PHP_EOL . ' LIMIT ' . (int)$limit . ' OFFSET ' . (int)$offset;
|
|
|
|
$rows = Db::getInstance()->executeS($sql);
|
|
|
|
foreach ($rows as &$row) {
|
|
$row['total_paid_tax_incl'] = Tools::displayPrice($row['total_paid_tax_incl']);
|
|
}
|
|
|
|
die(EcsGtmProTools::jsonEncode(array(
|
|
'sql' => $sql,
|
|
'rows' => $rows,
|
|
'total' => $total,
|
|
'pages' => ceil($total / $limit)
|
|
), JSON_NUMERIC_CHECK));
|
|
}
|
|
|
|
public function ajaxProcessViewOrder()
|
|
{
|
|
$id = Tools::getValue('id');
|
|
$row = Db::getInstance()->getRow('SELECT * FROM `' . _DB_PREFIX_ . EcsGtmProOrder::TABLE_NAME . '` WHERE id_order = ' . (int)$id);
|
|
if ($row) {
|
|
$gtmOrder = new EcsGtmProOrder($row['id']);
|
|
$buttons = array();
|
|
if (empty($gtmOrder->datalayer)) {
|
|
$buttons[] = 'generate';
|
|
} else {
|
|
if (!$gtmOrder->resent) {
|
|
$buttons[] = 'resend';
|
|
}
|
|
}
|
|
$gtmOrder->datalayer = str_replace(array("\n", PHP_EOL), '', $gtmOrder->datalayer);
|
|
|
|
if (preg_match('{(function\(.*?send\(\)\;\s?\})}is', $gtmOrder->datalayer, $m)) {
|
|
$gtmOrder->datalayer = preg_replace('{(function\(.*?send\(\)\;\s?\})}is', '"' . addslashes($m[1]) . '"', $gtmOrder->datalayer);
|
|
}
|
|
} else {
|
|
$gtmOrder = new EcsGtmProOrder();
|
|
$buttons = array('generate');
|
|
}
|
|
$order = Db::getInstance()->getRow($this->getSQL(['o.id_order = ' . (int)$id]));
|
|
$order['total_paid_tax_incl'] = Tools::displayPrice($order['total_paid_tax_incl']);
|
|
die(EcsGtmProTools::jsonEncode(array(
|
|
'model' => $gtmOrder,
|
|
'order' => $order,
|
|
'buttons' => $buttons,
|
|
'orderLink' => Context::getContext()->link->getAdminLink('AdminOrders', true, array(), array('id_order' => $gtmOrder->id_order, 'orderId' => $gtmOrder->id_order, 'vieworder' => 1))
|
|
), JSON_NUMERIC_CHECK));
|
|
}
|
|
|
|
public function ajaxProcessRegenerateDataLayer()
|
|
{
|
|
$id = Tools::getValue('id');
|
|
$id_order = Tools::getValue('id_order');
|
|
$dataLayer = '';
|
|
$orderLog = new EcsGtmProOrder($id);
|
|
if (Validate::isLoadedObject($orderLog) && empty($orderLog->datalayer)) {
|
|
$order = new Order($id_order);
|
|
if (Validate::isLoadedObject($order)) {
|
|
$this->module->dlManager->orderConfirmation($order->id_cart);
|
|
$dataLayer = $this->module->dlManager->getDataLayer()->toJson();
|
|
$orderLog->datalayer = $dataLayer;
|
|
$orderLog->save();
|
|
}
|
|
}
|
|
|
|
die(EcsGtmProTools::jsonEncode(array(
|
|
'dl' => $dataLayer,
|
|
'dlContent' => $this->module->render('gtm_push.tpl', array(
|
|
'dataLayer' => $dataLayer
|
|
)),
|
|
'id' => $id
|
|
)));
|
|
}
|
|
|
|
public function ajaxProcessReSendDataLayer()
|
|
{
|
|
$id = Tools::getValue('id');
|
|
$id_order = Tools::getValue('id_order');
|
|
$dataLayer = '';
|
|
$orderLog = new EcsGtmProOrder($id);
|
|
if (Validate::isLoadedObject($orderLog)) {
|
|
$order = new Order($id_order);
|
|
if (Validate::isLoadedObject($order)) {
|
|
$this->module->dlManager->orderConfirmation($order->id_cart, true);
|
|
$this->module->dlManager->getDataLayer()->event = "order_resend";
|
|
$dataLayer = $this->module->dlManager->getDataLayer()->toJson();
|
|
$orderLog->datalayer = $dataLayer;
|
|
$orderLog->save();
|
|
}
|
|
}
|
|
|
|
die(EcsGtmProTools::jsonEncode(array(
|
|
'dl' => $dataLayer,
|
|
'dlContent' => $this->module->render('gtm_push.tpl', array(
|
|
'dataLayer' => $dataLayer
|
|
)),
|
|
'id' => $id
|
|
)));
|
|
}
|
|
}
|