-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannelengine.php
More file actions
125 lines (108 loc) · 4.17 KB
/
Copy pathchannelengine.php
File metadata and controls
125 lines (108 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
use Sofija\Channelengine\Bootstrap;
use Sofija\Channelengine\BussinesLogicServices\Interfaces\ServiceInterface\ProductSyncServiceInterface;
use Sofija\Channelengine\Utility\ChannelEngineInstaller;
use Sofija\Channelengine\Utility\ServiceRegistry;
require_once __DIR__ . '/vendor/autoload.php';
// Ensure that the script is being run within PrestaShop's environment
if (!defined('_PS_VERSION_')) {
exit;
}
/**
* Main module class for the ChannelEngine PrestaShop module.
* This class handles the installation, uninstallation, and configuration
* of the ChannelEngine module within PrestaShop.
*/
class ChannelEngine extends Module
{
/**
* Class constructor.
* Initializes the module properties, such as name, version, author,
* and compatibility with PrestaShop versions. It also enables Bootstrap styling.
* @throws Exception
*/
public function __construct()
{
$this->name = 'channelengine';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'Sofija';
$this->need_instance = 0;
$this->ps_versions_compliancy = ['min' => '8.1', 'max' => '8.1.7'];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('channelEngine');
$this->description = $this->l('Sofija channelEngine');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
Bootstrap::init();
}
/**
* Installs the module.
*
* @return bool
*/
public function install(): bool
{
$installer = new ChannelEngineInstaller($this);
return parent::install() && $installer->install();
}
/**
* Uninstalls the module.
*
* @return bool
* @throws PrestaShopException
*/
public function uninstall(): bool
{
$installer = new ChannelEngineInstaller($this);
return parent::uninstall() && $installer->uninstall();
}
/**
* Redirects the user to the module's configuration page.
* This method is called when the user clicks on the "Configure" button in the module list.
*/
public function getContent(): void
{
$link = $this->context->link->getAdminLink('AdminChannelEngine');
Tools::redirectAdmin($link);
}
/**
* Hook that adds CSS and JS to the back-office header.
* This hook ensures that the required styles and scripts for the module are loaded
* when the admin page is displayed.
*/
public function hookDisplayBackOfficeHeader(): void
{
if (Tools::getValue('controller') == 'AdminChannelEngine') {
$this->context->controller->addCSS($this->_path . 'views/css/admin.css');
$this->context->controller->addCSS($this->_path . 'views/css/login.css');
$this->context->controller->addJS($this->_path . 'views/js/sync.js');
$this->context->controller->addCSS($this->_path . 'views/css/sync.css');
}
}
/**
* Hook that is triggered when a product is updated in PrestaShop.
*
* This method logs the hook trigger, retrieves the product ID, and attempts to synchronize the product
* with ChannelEngine. In case of an error during the synchronization, it logs the error message.
*
* @param array $params Parameters of the hook, including the product ID being updated.
* @throws Exception If there is an issue during product synchronization.
*/
public function hookActionProductUpdate(array $params): void
{
PrestaShopLogger::addLog('hookActionProductUpdate triggered for product ID: ' . $params['id_product'],
1);
try {
$productId = $params['id_product'];
$productSyncService = ServiceRegistry::getInstance()->get(ProductSyncServiceInterface::class);
$productSyncService->syncProductById($productId);
PrestaShopLogger::addLog('Synchronization successful for product ID: ' . $productId, 1);
} catch (Exception $e) {
PrestaShopLogger::addLog(
'Error during sync in hookActionProductUpdate for product ID: ' . $productId . ' - ' .
$e->getMessage(), 3
);
}
}
}