Skip to content

Commit 69c2557

Browse files
committed
Improve controllers
1 parent 368707d commit 69c2557

3 files changed

Lines changed: 38 additions & 14 deletions

File tree

classes/Repository/GanalyticsRepository.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,17 @@ public function addNewRow(array $data, $type = \Db::INSERT_IGNORE)
8181
*
8282
* @param array $data
8383
* @param string $where
84+
* @param int $limit
8485
*
8586
* @return bool
8687
*/
87-
public function updateData($data, $where)
88+
public function updateData($data, $where, $limit = 0)
8889
{
8990
return \Db::getInstance()->update(
9091
self::TABLE_NAME,
9192
$data,
92-
$where
93+
$where,
94+
$limit
9395
);
9496
}
9597
}

controllers/admin/AdminGanalyticsAjax.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,30 @@
1717
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
1818
* International Registered Trademark & Property of PrestaShop SA
1919
*/
20+
21+
use PrestaShop\Module\Ps_Googleanalytics\Repository\GanalyticsRepository;
22+
2023
class AdminGanalyticsAjaxController extends ModuleAdminController
2124
{
2225
public $ssl = true;
2326

2427
public function init()
2528
{
26-
$order = new Order((int) Tools::getValue('orderid'));
27-
$context = Context::getContext();
28-
if (Validate::isLoadedObject($order) && (isset($context->employee->id) && $context->employee->id)) {
29-
Db::getInstance()->execute('
30-
UPDATE `' . _DB_PREFIX_ . 'ganalytics` SET sent = 1, date_add = NOW() WHERE id_order = ' . (int) Tools::getValue('orderid')
29+
$orderId = (int) Tools::getValue('orderid');
30+
$order = new Order($orderId);
31+
32+
if (Validate::isLoadedObject($order) && (isset($this->context->employee->id) && $this->context->employee->id)) {
33+
(new GanalyticsRepository())->updateData(
34+
[
35+
'sent' => 1,
36+
'date_add' => 'NOW()',
37+
],
38+
'id_order = ' . $orderId
3139
);
32-
die('OK');
40+
41+
$this->ajaxDie('OK');
3342
}
34-
die('KO');
43+
44+
$this->ajaxDie('KO');
3545
}
3646
}

controllers/front/ajax.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
1818
* International Registered Trademark & Property of PrestaShop SA
1919
*/
20+
21+
use PrestaShop\Module\Ps_Googleanalytics\Repository\GanalyticsRepository;
22+
2023
class ps_GoogleanalyticsAjaxModuleFrontController extends ModuleFrontController
2124
{
2225
public $ssl = true;
@@ -28,13 +31,22 @@ public function initContent()
2831
{
2932
parent::initContent();
3033

31-
$order = new Order((int) Tools::getValue('orderid'));
34+
$orderId = (int) Tools::getValue('orderid');
35+
$order = new Order($orderId);
36+
3237
if (!Validate::isLoadedObject($order) || $order->id_customer != (int) Tools::getValue('customer')) {
33-
die;
38+
$this->ajaxDie('KO');
3439
}
35-
Db::getInstance()->execute(
36-
'UPDATE `' . _DB_PREFIX_ . 'ganalytics` SET sent = 1, date_add = NOW() WHERE id_order = ' . (int) Tools::getValue('orderid') . ' LIMIT 1'
40+
41+
(new GanalyticsRepository())->updateData(
42+
[
43+
'sent' => 1,
44+
'date_add' => 'NOW()',
45+
],
46+
'id_order = ' . $orderId,
47+
1
3748
);
38-
die;
49+
50+
$this->ajaxDie('OK');
3951
}
4052
}

0 commit comments

Comments
 (0)