-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathps_attributtify.php
More file actions
209 lines (172 loc) · 7.25 KB
/
ps_attributtify.php
File metadata and controls
209 lines (172 loc) · 7.25 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Ps_Attributtify - Simplified product combination creation for PrestaShop 8.x
*
* Injects a combination builder panel into the Combinations tab of the new
* Symfony-based product page by overriding the combination form theme Twig
* template (documented PS8 approach: views/PrestaShop/... override path).
*
* @author levskiy0 (https://github.com/levskiy0/ps_attributtify)
* @copyright 2026 levskiy0
* @license MIT
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class Ps_Attributtify extends Module
{
const CONF_CUSTOM_TYPES = 'ATTRIBUTTIFY_CUSTOM_TYPES';
public function __construct()
{
$this->name = 'ps_attributtify';
$this->tab = 'administration';
$this->version = '1.4.3';
$this->author = 'levskiy0';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '8.0.0',
'max' => _PS_VERSION_,
];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Attributtify');
$this->description = $this->l('Combination builder inside the Combinations tab — define rules, set prices, generate.');
$this->confirmUninstall = $this->l('Remove Attributtify? All saved rule configurations will be deleted.');
}
public function install(): bool
{
return parent::install()
&& $this->registerHook('displayBackOfficeHeader')
&& $this->installTab('AdminPsAttributtifyAjax', 'Attributtify Ajax');
}
public function uninstall(): bool
{
/*
Db::getInstance()->execute(
'DELETE FROM `' . _DB_PREFIX_ . 'configuration`'
. ' WHERE `name` LIKE "ATTRIBUTTIFY_PRODUCT_%"'
. ' OR `name` = "' . pSQL(self::CONF_CUSTOM_TYPES) . '"'
);
Db::getInstance()->execute(
'DELETE FROM `' . _DB_PREFIX_ . 'configuration_lang`'
. ' WHERE `id_configuration` NOT IN (SELECT `id_configuration` FROM `' . _DB_PREFIX_ . 'configuration`)'
);
*/
$this->uninstallTab('AdminPsAttributtifyAjax');
return parent::uninstall();
}
// ─── Module settings (Configure button) ──────────────────────────────────
public function getContent(): string
{
$output = '';
if (Tools::isSubmit('addCustomType')) {
$output .= $this->processAddType();
} elseif (Tools::isSubmit('deleteCustomType')) {
$output .= $this->processDeleteType();
}
return $output . $this->renderSettingsPage();
}
protected function processAddType(): string
{
$id = trim((string) Tools::getValue('type_id'));
$name = trim((string) Tools::getValue('type_name'));
if (!preg_match('/^[a-z][a-z0-9_]{0,49}$/', $id)) {
return $this->displayError(
$this->l('Invalid type ID. Use lowercase letters, digits and underscores (start with a letter, max 50 chars).')
);
}
if ($name === '' || mb_strlen($name) > 100) {
return $this->displayError($this->l('Display name is required (max 100 characters).'));
}
$types = $this->loadCustomTypes();
foreach ($types as $t) {
if ($t['id'] === $id) {
return $this->displayError($this->l('A custom type with this ID already exists.'));
}
}
$types[] = ['id' => $id, 'name' => $name];
$this->saveCustomTypes($types);
return $this->displayConfirmation($this->l('Custom type added.'));
}
protected function processDeleteType(): string
{
$delId = (string) Tools::getValue('type_id');
$types = $this->loadCustomTypes();
$types = array_values(array_filter($types, static function (array $t) use ($delId): bool {
return $t['id'] !== $delId;
}));
$this->saveCustomTypes($types);
return $this->displayConfirmation($this->l('Custom type removed.'));
}
protected function renderSettingsPage(): string
{
$actionUrl = $this->context->link->getAdminLink('AdminModules', true, [], [
'configure' => $this->name,
]);
$this->context->smarty->assign([
'types' => $this->loadCustomTypes(),
'action_url' => $actionUrl,
]);
return $this->context->smarty->fetch(_PS_MODULE_DIR_ . $this->name . '/views/templates/admin/settings.tpl');
}
// ─── Custom types persistence ─────────────────────────────────────────────
protected function loadCustomTypes(): array
{
$json = Configuration::get(self::CONF_CUSTOM_TYPES);
if (empty($json)) {
return [];
}
$data = json_decode($json, true);
return is_array($data) ? $data : [];
}
protected function saveCustomTypes(array $types): void
{
Configuration::updateValue(self::CONF_CUSTOM_TYPES, json_encode(array_values($types)));
}
// ─── Hooks ────────────────────────────────────────────────────────────────
/**
* Load CSS/JS on the product edit page and expose the AJAX URL to JS.
*/
public function hookDisplayBackOfficeHeader(): void
{
$uri = $_SERVER['REQUEST_URI'] ?? '';
$controller = Tools::getValue('controller');
$isNewProductPage = strpos($uri, '/sell/catalog/products') !== false;
$isLegacyProductPage = $controller === 'AdminProducts'
&& (Tools::getValue('addproduct') || Tools::getValue('updateproduct') || Tools::getValue('id_product'));
if (!$isNewProductPage && !$isLegacyProductPage) {
return;
}
$this->context->controller->addCSS($this->_path . 'views/css/select2.css?v=' . $this->version);
$this->context->controller->addCSS($this->_path . 'views/css/attributtify.css?v=' . $this->version);
$this->context->controller->addJS($this->_path . 'views/js/attributtify.js?v=' . $this->version);
Media::addJsDef([
'attributtifyAjaxUrl' => $this->context->link->getAdminLink('AdminPsAttributtifyAjax'),
]);
}
// ─── Tab helpers ──────────────────────────────────────────────────────────
private function installTab(string $className, string $tabName): bool
{
if (Tab::getIdFromClassName($className)) {
return true;
}
$tab = new Tab();
$tab->active = 1;
$tab->class_name = $className;
$tab->id_parent = -1;
$tab->module = $this->name;
$tab->name = [];
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = $tabName;
}
return (bool) $tab->add();
}
private function uninstallTab(string $className): void
{
$id = Tab::getIdFromClassName($className);
if (!$id) {
return;
}
(new Tab($id))->delete();
}
}