-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvcore.php
571 lines (495 loc) · 20 KB
/
tvcore.php
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
<?php
/**
* Core PrestaShop module - Cornelius
* @author tivuno.com <[email protected]>
* @copyright 2018 - 2025 © tivuno.com
* @license https://tivuno.com/el/blog/nea-tis-epicheirisis/apli-adeia
*/
// PrestaShop validator - Start
if (!defined('_PS_VERSION_')) {
exit;
}
// PrestaShop validator - Finish
if (!defined('_PS_ADMIN_DIR_ONLY_')) {
// if _PS_ADMIN_DIR_ is not defined, define.
define('_PS_ADMIN_DIR_ONLY_', 'ptolemeo'); // Configuration::get('PS_ADMIN_DIR_ONLY'));
}
require_once __DIR__ . '/models/TvcoreFile.php';
require_once __DIR__ . '/models/file_types/TvcoreJson.php';
class Tvcore extends Module
{
protected static array $templates = [
'admin_header' => '/themes/default/template/header.tpl',
];
private $languages;
public function __construct()
{
$this->name = 'tvcore';
$this->tab = 'administration';
$this->version = '1.0.5';
$this->author = 'tivuno.com';
$this->ps_versions_compliancy = [
'min' => '8.0.0',
'max' => _PS_VERSION_,
];
$this->displayName = $this->l('Core PrestaShop module - Cornelius');
$this->description = $this->l('It adds useful hooks, functions and libraries to PrestaShop');
$this->bootstrap = true;
parent::__construct();
$this->languages = Language::getLanguages();
}
public static function debugValue($string)
{
echo '<pre>' . $string . '</pre>';
}
public static function unregisterHooks(string $module_dir): bool
{
$module_obj = Module::getInstanceByName($module_dir);
if (Validate::isLoadedObject($module_obj)) {
$hooks = require_once _PS_MODULE_DIR_ . $module_dir . '/sql/hooks.php';
if (is_array($hooks)) {
foreach ($hooks as $hook) {
$module_obj->unregisterHook($hook);
}
}
}
return true;
}
public static function installNewAdminTemplates($module_name): bool
{
$parent_dir = TvcoreFile::getTemplateParentDir($module_name);
$new_admin_templates = require_once _PS_MODULE_DIR_ . $module_name . '/sql/new_admin_templates.php';
if (is_array($new_admin_templates) && sizeof($new_admin_templates)) {
foreach ($new_admin_templates as $dir_path => $templates) {
TvcoreFile::mkdir($parent_dir['admin'] . $dir_path, $module_name);
foreach ($templates as $template) {
TvcoreFile::copy(
$parent_dir['module_admin'] . $dir_path,
$parent_dir['admin'] . $dir_path,
$template
);
}
}
}
return true;
}
public static function installSettings(string $module_dir): bool
{
$settings = require_once _PS_MODULE_DIR_ . $module_dir . '/sql/settings.php';
foreach ($settings as $key => $value) {
if (isset($value['default_value'])) {
Configuration::updateValue($key, $value['default_value']);
}
}
return true;
}
public static function uninstallNewAdminTemplates($module_name): bool
{
$parent_dir = TvcoreFile::getTemplateParentDir($module_name);
$new_admin_templates = require_once _PS_MODULE_DIR_ . $module_name . '/sql/new_admin_templates.php';
if (is_array($new_admin_templates) && sizeof($new_admin_templates)) {
foreach ($new_admin_templates as $dir_path => $dir_path) {
Tools::deleteDirectory($parent_dir['admin'] . $dir_path);
}
}
return true;
}
public static function uninstallSettings(string $module_dir): bool
{
$settings = require_once _PS_MODULE_DIR_ . $module_dir . '/sql/settings.php';
foreach ($settings as $key => $key) {
Configuration::deleteByName($key);
}
return true;
}
public static function installTables(string $module_dir): bool
{
$file = _PS_MODULE_DIR_ . $module_dir . '/sql/tables.php';
if (is_file($file)) {
$moduleTables = include_once $file;
foreach ($moduleTables as $table_key => $table_key) {
if (!Db::getInstance()->execute($moduleTables[$table_key])) {
return false;
}
}
self::installTablesOverride($module_dir);
// Some tables which belong to a module are useful only when another one is installed
self::installTablesAdditional($module_dir);
}
return true;
}
public static function installTablesOverride(string $module_dir): bool
{
$file = _PS_MODULE_DIR_ . $module_dir . '/sql/table_overrides.php';
if (is_file($file)) {
$sql = include_once $file;
foreach ($sql as $s) {
$results = Db::getInstance()->executeS($s['check']);
// If the columns do not exist, add them
if (!sizeof($results)) {
if (!Db::getInstance()->execute($s['add'])) {
return false;
}
}
}
}
return true;
}
public static function installTablesAdditional(string $module_dir): bool
{
$modules_tables_additional = self::getTablesAdditional($module_dir);
if (sizeof($modules_tables_additional)) {
foreach ($modules_tables_additional as $module_table) {
$query = str_replace(['_DB_PREFIX_', '_MYSQL_ENGINE_'], [_DB_PREFIX_, _MYSQL_ENGINE_], $module_table);
if (!Db::getInstance()->execute($query)) {
return false;
}
}
}
return true;
}
protected static function getTablesAdditional(string $module_dir): array
{
$result = [];
$additional_tables_directory = _PS_MODULE_DIR_ . $module_dir . '/sql/additional';
if (is_dir($additional_tables_directory)) {
require_once _PS_MODULE_DIR_ . 'tvcore/models/TvcoreFile.php';
$additional_tables_files = TvcoreFile::getDirectoryFiles($additional_tables_directory, ['json']);
foreach ($additional_tables_files as $module_key => $tables_path) {
if (Module::isEnabled($module_key)) {
$result = array_merge($result, TvcoreJson::getDataFromLocalFile($tables_path));
}
}
}
return $result;
}
public static function uninstallTables(string $module_dir): bool
{
$file = _PS_MODULE_DIR_ . $module_dir . '/sql/tables.php';
if (is_file($file)) {
$moduleTables = include_once $file;
foreach ($moduleTables as $table_key => $table_key) {
if (!Db::getInstance()->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . $table_key . '`')) {
return false;
}
}
self::uninstallTablesOverride($module_dir);
// Some tables which belong to a module are useful only when another one is installed
self::uninstallTablesAdditional($module_dir);
}
return true;
}
public static function uninstallTablesOverride(string $module_dir): bool
{
$file = _PS_MODULE_DIR_ . $module_dir . '/sql/table_overrides.php';
if (is_file($file)) {
$sql = include_once $file;
foreach ($sql as $s) {
$results = Db::getInstance()->executeS($s['check']);
// If the columns do exist, drop them
if (sizeof($results)) {
if (!Db::getInstance()->execute($s['drop'])) {
return false;
}
}
}
}
return true;
}
public static function uninstallTablesAdditional(string $module_dir): bool
{
$modules_tables_additional = self::getTablesAdditional($module_dir);
if (sizeof($modules_tables_additional)) {
foreach ($modules_tables_additional as $table_key => $table_key) {
if (!Db::getInstance()->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . $table_key . '`')) {
return false;
}
}
}
return true;
}
/**
* @param string $module_dir
* @return true
*/
public static function importData(string $module_dir): bool
{
require_once _PS_MODULE_DIR_ . 'tvcore/models/TvcoreString.php';
$file = _PS_MODULE_DIR_ . $module_dir . '/sql/data.php';
if (is_file($file)) {
$languages = Language::getLanguages(false);
$tables = require_once $file;
foreach ($tables as $table) {
$model_path = _PS_MODULE_DIR_ . $module_dir . '/models/' . $table['class'] . '.php';
require_once $model_path;
$class = new $table['class']();
$function = $table['function'];
$column = $table['column'];
foreach ($table['data'] as $datum) {
// We should check if the entity exists already
$id = $class::{$function}($datum[$column]);
if (!$id) {
foreach ($datum as $row_key => $row_value) {
if ($row_key != 'lang') {
$function_name = TvcoreString::setCamelFromSnake('set_' . $row_key);
$class->{$function_name}($row_value);
} else {
// The field usually contains more than one field
$language_columns = $row_value;
foreach ($language_columns as $language_column_key => $language_column_value) {
$function_name = TvcoreString::setCamelFromSnake('set_' . $language_column_key);
$language_column_data = [];
foreach ($languages as $language) {
if (isset($language_column_value[$language['iso_code']])) {
$language_column_data[$language['id_lang']] = $language_column_value[$language['iso_code']];
} else {
$language_column_data[$language['id_lang']] = $language_column_value['en'];
}
}
$class->{$function_name}($language_column_data);
}
}
}
$class->add();
}
}
}
}
return true;
}
public static function installTabs(string $module_dir): bool
{
$file = _PS_MODULE_DIR_ . $module_dir . '/sql/tabs.php';
if (is_file($file)) {
$moduleTabs = include_once $file;
$newTab = new Tab();
foreach ($moduleTabs as $moduleTab) {
$id_tab = $newTab::getIdFromClassName($moduleTab['class_name']);
if (!$id_tab) {
$newTab->class_name = pSQL($moduleTab['class_name']);
$newTab->module = pSQL($module_dir);
$newTab->id_parent = 0;
if (isset($moduleTab['id_parent'])) {
$newTab->id_parent = (int) $moduleTab['id_parent'];
} elseif (isset($moduleTab['parent_class'])) {
$newTab->id_parent = (int) Tab::getIdFromClassName($moduleTab['parent_class']);
}
if (isset($moduleTab['level'])) {
$newTab->level = (int) $moduleTab['level'];
}
foreach (Language::getLanguages(false) as $language) {
if (isset($moduleTab['name'][$language['iso_code']])) {
$newTab->name[$language['id_lang']] = pSQL($moduleTab['name'][$language['iso_code']]);
}
}
$newTab->add();
}
}
}
return true;
}
public static function uninstallTabs(string $module_dir): bool
{
$file = _PS_MODULE_DIR_ . $module_dir . '/sql/tabs.php';
if (is_file($file)) {
$moduleTabs = include_once $file;
foreach ($moduleTabs as $moduleTab) {
$id_tab = Tab::getIdFromClassName($moduleTab['class_name']);
if ($id_tab) {
$tab = new Tab($id_tab);
$tab->delete();
}
}
}
return true;
}
public static function debug(array $array): void
{
echo '<pre>' . print_r($array, true) . '</pre>';
exit(rand());
}
public function install(): bool
{
return parent::install() && self::registerHooks($this->name);
}
public static function registerHooks(string $module_dir): bool
{
$module_obj = Module::getInstanceByName($module_dir);
if (Validate::isLoadedObject($module_obj)) {
$hooks = require_once _PS_MODULE_DIR_ . $module_dir . '/sql/hooks.php';
if (is_array($hooks)) {
foreach ($hooks as $hook) {
$module_obj->registerHook($hook);
}
}
}
return true;
}
public function enable($force_all = false): bool
{
return parent::enable($force_all)
&& self::registerHooks($this->name)
&& self::installModifiedTemplates();
}
protected static function installModifiedTemplates(): bool
{
return self::installAdminHeaderTemplate();
}
protected static function installAdminHeaderTemplate(): bool
{
$handle = $result = @file(self::$templates['admin_header']);
$i = 0;
foreach ($handle as $line) {
if (str_contains($line, '{* begin HEADER *}')) {
$result[$i] = " {hook h='displayAdminAfterBodyOpeningTag'}" . PHP_EOL . $line;
break;
}
++$i;
}
file_put_contents(self::$templates['admin_header'], implode('', $result));
@chmod(self::$templates['admin_header'], 0644);
return true;
}
public function disable($force_all = false): bool
{
return parent::disable($force_all)
&& self::uninstallModifiedTemplates();
}
protected static function uninstallModifiedTemplates(): bool
{
return self::uninstallAdminHeaderTemplate();
}
protected static function uninstallAdminHeaderTemplate(): bool
{
$handle = $result = @file(self::$templates['admin_header']);
$i = 0;
foreach ($handle as $line) {
if (str_contains($line, 'displayAdminAfterBodyOpeningTag')) {
unset($result[$i]);
break;
}
++$i;
}
file_put_contents(self::$templates['admin_header'], implode('', $result));
@chmod(self::$templates['admin_header'], 0644);
return true;
}
public function hookDisplayHeader(): void
{
$this->context->controller->registerStylesheet(
'modules-tvcore-fontawesome',
'modules/' . $this->name . '/libraries/fontawesome/css/all.min.css',
['media' => 'all', 'priority' => 150]
);
$this->context->controller->registerStylesheet(
'modules-tvcore-main',
'modules/' . $this->name . '/views/css/front/main.css',
['media' => 'all', 'priority' => 150]
);
$this->context->controller->registerStylesheet(
'modules-tvcore-minimum',
'modules/' . $this->name . '/views/css/front/minimum.css',
['media' => 'all', 'priority' => 150]
);
}
public function hookDisplayAdminAfterBodyOpeningTag(): string
{
return $this->fetch('module:' . $this->name . '/views/templates/hooks/displayAdminAfterBodyOpeningTag.tpl');
}
public function hookDisplayAfterBodyOpeningTag(): string
{
return $this->fetch('module:' . $this->name . '/views/templates/hooks/displayAfterBodyOpeningTag.tpl');
}
public function getSettingsWithValues(array $settings): array
{
$values = [];
foreach ($settings as $setting) {
$validation_method = $setting['validation'];
if (isset($setting['language'])) {
foreach (Language::getLanguages(false, false, true) as $language_id) {
$tmp_value = Configuration::get($setting['key'], $language_id);
if (Validate::$validation_method($tmp_value)) {
$values[$setting['key']][$language_id] = $tmp_value;
}
}
} else {
$tmp_value = Configuration::get($setting['key']);
if (Validate::$validation_method($tmp_value)) {
$values[$setting['key']] = $tmp_value;
}
}
}
return $values;
}
public function getContent(): string
{
$output = '';
// this part is executed only when the form is submitted
if (Tools::isSubmit('submit' . $this->name)) {
// retrieve the value set by the user
$configValue = (string) Tools::getValue('tvimport_prod_link');
if (empty($configValue) || !Validate::isUrl($configValue)) {
$output = $this->displayError($this->l('Invalid Configuration value'));
} else {
Configuration::updateValue('tvimport_prod_link', $configValue);
$output = $this->displayConfirmation($this->l('Settings updated'));
}
}
$token = Tools::hash('tvcore/cron');
$this->context->smarty->assign([
'add_index_cron' => $this->context->link->getModuleLink(
'tvcore',
'addindex',
[
'token' => $token,
'module_name' => 'YOUR_MODULE_DIR',
],
),
'cron_links' => Hook::exec(
'displayConfigurationTvcoreGetCronLinks',
[
'token' => $token,
],
null,
true,
false,
false,
null,
true
),
]);
return $output . $this->displayForm() . $this->display(__FILE__, 'views/templates/admin/configuration.tpl');
}
public function displayForm(): string
{
$form = [
'form' => [
'input' => [
[
'type' => 'text',
'label' => $this->l('Import module link'),
'name' => 'tvimport_prod_link',
'size' => 20,
'required' => true,
],
],
'submit' => [
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right',
],
],
];
$helper = new HelperForm();
// Module, token and currentIndex
$helper->table = $this->table;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&' . http_build_query(['configure' => $this->name]);
$helper->submit_action = 'submit' . $this->name;
// Default language
$helper->default_form_language = (int) Configuration::get('PS_LANG_DEFAULT');
// Load current value into the form
$helper->fields_value['tvimport_prod_link'] = Tools::getValue('tvimport_prod_link', Configuration::get('tvimport_prod_link'));
return $helper->generateForm([$form]);
}
}