-
Notifications
You must be signed in to change notification settings - Fork 0
/
customhtmlpages.php
670 lines (545 loc) · 18.2 KB
/
customhtmlpages.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
<?php
if (!defined('_TB_VERSION_')) {
exit;
}
include_once(dirname(__FILE__) . '/CustomHTMLPageModel.php');
/**
* Module for Custom HTML Pages
*/
class CustomHTMLPages extends Module
{
protected $hooksList = [];
public function __construct()
{
$this->name = 'customhtmlpages';
$this->className = 'CustomHTMLPages';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'Michael Rouse';
$this->tb_min_version = '1.0.0';
$this->tb_versions_compliancy = '> 1.0.0';
$this->need_instance = 0;
$this->table_name = 'customhtmlpages';
$this->table_lang = $this->table_name . '_lang';
$this->table_related = $this->table_name .'_relatedPages';
$this->bootstrap = true;
$this->ps_versions_compliancy = ['min' => '1.6', 'max' => '1.6.99.99'];
// List of hooks
$this->hooksList = [
'displayBackOfficeHeader',
'moduleRoutes',
'displaySitemapPages', /* You must had {hook h='displaySitemapPages'} to your sitemap.tpl file if you want this to work */
];
parent::__construct();
$this->displayName = $this->l('Custom HTML Pages');
$this->description = $this->l('Add custom HTML Pages to your store');
}
/**
* Registers all of the routes for all of the pages
*/
public function hookModuleRoutes($params)
{
$pages = $this->getAllHTMLPages();
$routes = [];
$classPages = $this->convertToClasses($pages);
foreach ($classPages as $i => $page)
{
$result = $this->getRouteForPage($page);
$routes[$this->generateRouteKey($page)] = $result;
}
return $routes;
}
/**
* Add CSS/JS to the back office
*/
public function hookDisplayBackOfficeHeader()
{
$this->context->controller->addCSS($this->_path . 'css/customhtmlpages.css', 'all');
$this->context->controller->addJS($this->_path . 'js/customhtmlpages.js');
$allPages = $this->getAllHTMLPages(true);
Media::addJsDef([
'allCustomHTMLPages' => $allPages
]);
}
/**
* Return Pages for the sitemap
*/
public function hookDisplaySitemapPages()
{
$allPages = $this->getAllHTMLPages();
$tree = $this->convertToTree($allPages);
$this->context->smarty->assign([
'tree' => $tree,
'templatePath' => _PS_MODULE_DIR_.'/'.$this->name.'/views/templates/front/sitemap.tpl',
]);
return $this->display(__FILE__, 'views/templates/front/sitemap.tpl');
}
/**********************************
* Database Interface *
**********************************/
/**
* Returns all of the HTML pages
*/
public function getAllHTMLPages($getInactive = false)
{
try
{
$language = $this->context->language->id;
$shop = $this->context->shop->id;
$qry = (new DbQuery())
->select('t1.`id_page`, t1.`name`, t1.`id_parent`, t1.`url`, t1.`breadcrumb_parameters`, t1.`style`, t1.`id_products`, t1.`id_categories`, t1.`active`, t2.`meta_title`, t2.`meta_description`, t2.`meta_keywords`, t2.`content`')
->from($this->table_name, 't1')
->leftJoin($this->table_lang, 't2', 't2.`id_page`= t1.`id_page` AND t2.`id_shop`=t1.`id_shop` AND t2.`id_lang`='. $language)
->orderBy('t1.`id_page`')
->where('t1.`id_shop`='.$shop);
if (!$getInactive)
$qry->where('t1.`active`=1');
$result = Db::getInstance()->ExecuteS($qry);
if (!is_array($result))
return [];
return $result;
}
catch (Exception $e)
{
Logger::addLog("CustomHTMLPages getAllHTMLPages Exception: {$e->getMessage()}");
return [];
}
}
/**
* Returns a single Custom HTML Page
*/
public function getHTMLPage($pageId)
{
try
{
if ($pageId == 'new')
return $this->getDefaultValues();
$language = $this->context->language->id;
$shop = $this->context->shop->id;
$qry = (new DbQuery())
->select('t1.`id_page`, t1.`name`, t1.`id_parent`, t1.`url`, t1.`breadcrumb_parameters`, t1.`style`, t1.`id_products`, t1.`id_categories`, t1.`active`, t2.`meta_title`, t2.`meta_description`, t2.`meta_keywords`, t2.`content`, t2.`id_lang`')
->from($this->table_name, 't1')
->leftJoin($this->table_lang, 't2', 't1.`id_page` = t2.`id_page` AND t2.`id_shop`=t1.`id_shop` AND t2.`id_lang`='. $language)
->where('t1.`id_shop`='.$shop)
->where('t1.`id_page`='.$pageId);
$result = Db::getInstance()->ExecuteS($qry);
if (!$result)
return null;
return $this->transformResult($result[0]);
}
catch (Exception $e)
{
Logger::addLog("CustomHTMLPages getHTMLPage Exception: {$e->getMessage()}");
return null;
}
}
/**
* Returns the status of a single page
*/
public function getHTMLPageStatus($pageId)
{
$page = $this->getHTMLPage($pageId);
if (is_null($page)) {
return 1;
}
return $page['active'];
}
/**
* Returns all of the pages that are related to a certain page
*/
private function getRelatedPagesForPage($pageId)
{
try
{
$language = $this->context->language->id;
$shop = $this->context->shop->id;
$qry = (new DbQuery())
->select('id_related')
->from($this->table_related)
->where('`id_parent`='.$pageId);
$result = Db::getInstance()->ExecuteS($qry);
if (!$result)
return [];
$final = [];
foreach ($result as $res)
{
array_push($final, $res['id_related']);
}
return $final;
}
catch (Exception $e)
{
Logger::addLog("CustomHTMLPages getRelatedPagesForPage Exception: {$e->getMessage()}");
return null;
}
}
/**
* Opposite of getRelatedPages, returns pages that this page is marked to be related to
*/
private function getPagesThisPageIsRelatedTo($pageId)
{
try
{
$language = $this->context->language->id;
$shop = $this->context->shop->id;
$qry = (new DbQuery())
->select('id_parent')
->from($this->table_related)
->where('`id_related`='.$pageId);
$result = Db::getInstance()->ExecuteS($qry);
if (!$result)
return [];
$final = [];
foreach ($result as $res)
{
array_push($final, $res['id_parent']);
}
return $final;
}
catch (Exception $e)
{
Logger::addLog("CustomHTMLPages getPagesThisPageIsRelatedTo Exception: {$e->getMessage()}");
return null;
}
}
/****************************************
* Helper/Utility Functions *
****************************************/
/**
* Returns a single page as a class (for use in the page.php file)
*/
public function getHTMLPageAsAClass($pageId)
{
try
{
$pages = $this->getAllHTMLPages();
$allPages = $this->convertToClasses($pages);
$shopURL = $this->getShopURL($this->context);
$found = null;
foreach ($allPages as $p) {
$p->link = $shopURL.$p->url;
if ($p->id == $pageId)
$found = $p;
}
return $found;
}
catch (Exception $e)
{
Logger::addLog("CustomHTMLPages getHTMLPage Exception: {$e->getMessage()}");
return null;
}
}
/**
* Returns the route
*/
public function getRouteForPage($page)
{
$rule = $page->url;
$route = [
'controller' => 'page',
'rule' => $rule.'{e:/}',
'keywords' => [
'e' => ['regexp' => '']
],
'params' => [
'fc' => 'module',
'module' => $this->name,
'id_page' => $page->id,
'url' => $rule,
]
];
return $route;
}
/**
* Returns the route rule for a page (recurses through parents)
* For displaying on the table page
*/
public function getFullURLForPage(&$page, &$allPages)
{
if (is_null($page))
return '';
$prefix = '';
if (array_key_exists('id_parent', $page) && !is_null($page['id_parent']))
{
$parent = null;
foreach ($allPages as $i => $p)
{
if ($p['id_page'] == $page['id_parent'])
{
$parent = $p;
break;
}
}
if (!is_null($parent))
{
$prefix = $this->getFullURLForPage($parent, $allPages).'/';
}
}
$url = $prefix.$page['url'];
return $url;
}
/**
* Generates a name for a route
*/
private function generateRouteKey($page)
{
return 'customhtmlpages-page'.$page->id;
}
/**
* Transforms the result into an object for editing on the edit page
*/
private function transformResult($page)
{
$lang = $this->context->language->id;
$tmp = [
'id' => $page['id_page'],
'id_page' => $page['id_page'],
'name' => $page['name'],
'url' => $page['url'],
'breadcrumb_parameters' => $page['breadcrumb_parameters'],
'id_parent' => $page['id_parent'],
'style' => $page['style'],
'active' => $page['active'],
'id_products' => $this->explode($page['id_products']),
'id_categories' => $this->explode($page['id_categories']),
'id_relatedTo' => $this->getPagesThisPageIsRelatedTo($page['id_page']),
'meta_title' => $page['meta_title'],
'meta_title_lang' => [
$lang => $page['meta_title']
],
'meta_description' => $page['meta_description'],
'meta_description_lang' => [
$lang => $page['meta_description']
],
'meta_keywords' => $page['meta_keywords'],
'meta_keywords_lang' => [
$lang => $page['meta_keywords']
],
'content' => $page['content'],
'content_lang' => [
$lang => $page['content']
]
];
return $tmp;
}
/**
* Default values for a new page
*/
private function getDefaultValues()
{
return [
'id_page' => 'new',
'id' => 'new',
'name' => '',
'url' => '',
'style' => '',
'active' => 1,
'meta_title' => '',
'meta_description' => '',
'meta_keywords' => '',
'id_products' => [],
'id_categories' => [],
'id_relatedTo' => [],
];
}
private function explode($data)
{
return (is_null($data) || empty($data)) ? [] : explode(',', $data);
}
/**
* Converts everything to a class
*/
public function convertToClasses(&$pages)
{
$classesById = [];
$idsToIndex = [];
foreach ($pages as $page)
{
$id = $page['id_page'];
$classesById[$id] = new CustomHTMLPageModel($page, $this->context);
}
// Populate children
foreach ($pages as $page)
{
$pageClass = $classesById[$page['id_page']];
// Add this page as a child
if (array_key_exists('id_parent', $page) && !is_null($page['id_parent']) && $page['id_parent'] != 0)
{
$parent = $classesById[$page['id_parent']];
$parent->addChild($pageClass);
}
// Mark related pages
$relatedPages = $this->getRelatedPagesForPage($page['id_page']);
foreach ($relatedPages as $relatedPage)
{
if (array_key_exists($relatedPage, $classesById))
{
$relatedPageClass = $classesById[$relatedPage];
$pageClass->addRelated($relatedPageClass);
}
}
}
return $classesById;
}
/**
* Converts the pages to a tree for the sitemap
*/
public function convertToTree($pages)
{
if (!is_array($pages) || count($pages) == 0)
return;
$classes = (gettype($pages[0]) == 'array') ? $this->convertToClasses($pages) : $pages;
$roots = [];
foreach ($classes as $page) {
$page->computeFullURL();
if ($page->parent == null) {
array_push($roots, $page);
}
}
$url = $this->getShopURL($this->context);
// Clear all parent links
foreach ($classes as $page) {
$page->url = $url.'/'.$page->url;
$page->parent = null;
}
return $roots;
}
/**
* Gets the shop URL
*/
private function getShopURL($context)
{
$url = 'http://'.$context->shop->domain;
if (isset($context->shop->domain_ssl) && !empty($context->shop->domain_ssl))
$url = 'https://'.$context->shop->domain_ssl;
return $url.'/';
}
/**************************
* Install/Uninstall *
**************************/
public function install()
{
if ( ! parent::install()
|| ! $this->_createTabs()
|| ! $this->_createDatabases()
) {
return false;
}
foreach ($this->hooksList as $hook) {
if ( ! $this->registerHook($hook)) {
return false;
}
}
return true;
}
public function uninstall()
{
if ( ! parent::uninstall()
|| ! $this->_eraseDatabases()
|| ! $this->_eraseTabs()
) {
return false;
}
return true;
}
/**
* Create tabs on the admin page
*/
private function _createTabs()
{
/* This is the main tab, all others will be children of this */
$allLangs = Language::getLanguages();
$idTab = $this->_createSingleTab(0, 'Admin'.$this->className, $this->displayName, $allLangs);
return true;
}
/**
* Creates a single tab
*/
private function _createSingleTab($idParent, $class, $name, $allLangs)
{
$tab = new Tab();
$tab->active = 1;
foreach ($allLangs as $language) {
$tab->name[$language['id_lang']] = $name;
}
$tab->class_name = $class;
$tab->module = $this->name;
$tab->id_parent = $idParent;
if ($tab->add()) {
return $tab->id;
}
return false;
}
/**
* Get rid of all installed back office tabs
*/
private function _eraseTabs()
{
$idTabm = (int)Tab::getIdFromClassName('Admin'.ucfirst($this->name));
if ($idTabm) {
$tabm = new Tab($idTabm);
$tabm->delete();
}
foreach ($this->_tabs as $class => $name) {
$idTab = (int)Tab::getIdFromClassName($class);
if ($idTab) {
$tab = new Tab($idTab);
$tab->delete();
}
}
return true;
}
/**
* Create Database Tables
*/
private function _createDatabases()
{
$sql = 'CREATE TABLE `'._DB_PREFIX_.$this->table_name.'` (
`id_page` INT( 12 ) AUTO_INCREMENT,
`id_lang` INT( 12 ) NOT NULL,
`id_shop` INT( 12 ) NOT NULL DEFAULT 1,
`name` VARCHAR( 255 ) NOT NULL,
`id_parent` INT ( 12 ) DEFAULT NULL,
`url` VARCHAR( 128 ) NOT NULL,
`breadcrumb_parameters` LONGTEXT NOT NULL,
`style` LONGTEXT DEFAULT NULL,
`id_products` LONGTEXT DEFAULT NULL,
`id_categories` LONGTEXT DEFAULT NULL,
`active` TINYINT(1) NOT NULL DEFAULT 1,
PRIMARY KEY ( `id_page` )
) ENGINE =' ._MYSQL_ENGINE_;
$sql2 = 'CREATE TABLE `'._DB_PREFIX_.$this->table_lang.'` (
`id_page` INT( 12 ) AUTO_INCREMENT,
`id_lang` INT( 12 ) NOT NULL,
`id_shop` INT( 12 ) NOT NULL DEFAULT 1,
`meta_title` VARCHAR( 128 ) NOT NULL,
`meta_description` VARCHAR( 255 ) DEFAULT NULL,
`meta_keywords` VARCHAR( 255 ) DEFAULT NULL,
`content` LONGTEXT,
PRIMARY KEY ( `id_page`, `id_lang` )
) ENGINE =' ._MYSQL_ENGINE_;
$sql3 = 'CREATE TABLE `'._DB_PREFIX_.$this->table_related.'` (
`id_parent` INT( 12 ) NOT NULL,
`id_related` INT( 12 ) NOT NULL,
PRIMARY KEY ( `id_parent`, `id_related` )
) ENGINE =' ._MYSQL_ENGINE_;
if (!Db::getInstance()->Execute($sql) || !Db::getInstance()->Execute($sql2) || !Db::getInstance()->Execute($sql3))
{
return false;
}
return true;
}
/**
* Remove Database Tables
*/
private function _eraseDatabases()
{
if ( ! Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.$this->table_name.'`') ||
! Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.$this->table_lang.'`') ||
! Db::getInstance()->Execute('DROP TABLE`'._DB_PREFIX_.$this->table_related.'`'))
{
return false;
}
return true;
}
}