-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheverpsblog.php
3576 lines (3496 loc) · 150 KB
/
everpsblog.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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* 2019-2021 Team Ever
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author Team Ever <https://www.team-ever.com/>
* @copyright 2019-2021 Team Ever
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once _PS_MODULE_DIR_ . 'everpsblog/classes/EverPsBlogAuthor.php';
require_once _PS_MODULE_DIR_ . 'everpsblog/classes/EverPsBlogCategory.php';
require_once _PS_MODULE_DIR_ . 'everpsblog/classes/EverPsBlogTag.php';
require_once _PS_MODULE_DIR_ . 'everpsblog/classes/EverPsBlogImage.php';
require_once _PS_MODULE_DIR_ . 'everpsblog/classes/EverPsBlogPost.php';
require_once _PS_MODULE_DIR_ . 'everpsblog/classes/EverPsBlogComment.php';
require_once _PS_MODULE_DIR_ . 'everpsblog/classes/EverPsBlogTaxonomy.php';
require_once _PS_MODULE_DIR_ . 'everpsblog/classes/EverPsBlogSitemap.php';
require_once _PS_MODULE_DIR_ . 'everpsblog/classes/EverPsBlogCleaner.php';
require_once _PS_MODULE_DIR_ . 'everpsblog/classes/EverPsBlogSortOrders.php';
use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever;
use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
use PrestaShop\PrestaShop\Core\Product\ProductListingPresenter;
use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever;
class EverPsBlog extends Module
{
private $html;
private $postErrors = [];
private $postSuccess = [];
public static $route = [];
public function __construct()
{
$this->name = 'everpsblog';
$this->tab = 'front_office_features';
$this->version = '5.6.3';
$this->author = 'Team Ever';
$this->need_instance = 0;
$this->bootstrap = true;
$this->module_folder = _PS_MODULE_DIR_ . 'everpsblog';
parent::__construct();
$this->displayName = $this->l('Ever Blog');
$this->description = $this->l('Simply a blog 😀');
$this->confirmUninstall = $this->l('Do you really want to uninstall this module ?');
$this->ps_versions_compliancy = [
'min' => '1.7',
'max' => _PS_VERSION_,
];
$this->context = Context::getContext();
}
public function install()
{
// Install SQL
include dirname(__FILE__).'/install/install.php';
// Create hooks
include dirname(__FILE__).'/install/hooks-install.php';
// Creating img folders
if (!file_exists(_PS_IMG_DIR_ . 'post')) {
mkdir(_PS_IMG_DIR_ . 'post', 0755, true);
}
// Creating img folders
if (!file_exists(_PS_IMG_DIR_ . 'category')) {
mkdir(_PS_IMG_DIR_ . 'category', 0755, true);
}
// Creating img folders
if (!file_exists(_PS_IMG_DIR_ . 'tag')) {
mkdir(_PS_IMG_DIR_ . 'tag', 0755, true);
}
// Creating img folders
if (!file_exists(_PS_IMG_DIR_ . 'author')) {
mkdir(_PS_IMG_DIR_ . 'author', 0755, true);
}
Configuration::updateValue('EVERBLOG_SHOW_HOME', true);
// Creating root category
$shops = Shop::getShops();
foreach ($shops as $shop) {
$root_category = new EverPsBlogCategory();
$root_category->is_root_category = 1;
$root_category->active = 1;
$root_category->id_shop = (int) $shop['id_shop'];
foreach (Language::getLanguages(false) as $language) {
$root_category->title[$language['id_lang']] = 'Root';
$root_category->content[$language['id_lang']] = 'Root';
$root_category->link_rewrite[$language['id_lang']] = 'root';
}
$root_category->save();
// Unclassed
$unclassed_category = new EverPsBlogCategory();
$unclassed_category->id_parent_category = 0;
$unclassed_category->active = 1;
$unclassed_category->id_shop = (int) $shop['id_shop'];
foreach (Language::getLanguages(false) as $language) {
$unclassed_category->title[$language['id_lang']] = $this->l('Unclassed');
$unclassed_category->content[$language['id_lang']] = '';
$unclassed_category->link_rewrite[$language['id_lang']] = $this->l('Unclassed');
}
$unclassed_category->save();
Configuration::updateValue('EVERBLOG_UNCLASSED_ID', $unclassed_category->id);
}
// Install
return parent::install()
&& $this->installModuleTab(
'AdminEverPsBlog',
'IMPROVE',
$this->l('Blog')
)
&& $this->installModuleTab(
'AdminEverPsBlogPost',
'AdminEverPsBlog',
$this->l('Posts')
)
&& $this->installModuleTab(
'AdminEverPsBlogCategory',
'AdminEverPsBlog',
$this->l('Categories')
)
&& $this->installModuleTab(
'AdminEverPsBlogTag',
'AdminEverPsBlog',
$this->l('Tags')
)
&& $this->installModuleTab(
'AdminEverPsBlogComment',
'AdminEverPsBlog',
$this->l('Comments')
)
&& $this->installModuleTab(
'AdminEverPsBlogAuthor',
'AdminEverPsBlog',
$this->l('Authors')
)
&& Configuration::updateValue('EVERPSBLOG_ROUTE', 'blog')
&& Configuration::updateValue('EVERBLOG_ADMIN_EMAIL', 1)
&& Configuration::updateValue('EVERBLOG_EMPTY_TRASH', 7)
&& Configuration::updateValue('EVERBLOG_ALLOW_COMMENTS', 1)
&& Configuration::updateValue('EVERBLOG_CHECK_COMMENTS', 1)
&& Configuration::updateValue('EVERBLOG_BANNED_USERS', '')
&& Configuration::updateValue('EVERBLOG_BANNED_IP', '')
&& Configuration::updateValue('EVERPSBLOG_PAGINATION', '10')
&& Configuration::updateValue('EVERPSBLOG_HOME_NBR', '4')
&& Configuration::updateValue('EVERPSBLOG_PRODUCT_NBR', '4')
&& Configuration::updateValue('EVERPSBLOG_EXCERPT', '150')
&& Configuration::updateValue('EVERPSBLOG_TITLE_LENGTH', '150')
&& Configuration::updateValue('EVERPSBLOG_BLOG_LAYOUT', 'layouts/layout-right-column.tpl')
&& Configuration::updateValue('EVERPSBLOG_POST_LAYOUT', 'layouts/layout-right-column.tpl')
&& Configuration::updateValue('EVERPSBLOG_CAT_LAYOUT', 'layouts/layout-right-column.tpl')
&& Configuration::updateValue('EVERPSBLOG_AUTHOR_LAYOUT', 'layouts/layout-right-column.tpl')
&& Configuration::updateValue('EVERPSBLOG_TAG_LAYOUT', 'layouts/layout-right-column.tpl')
&& Configuration::updateValue('EVERBLOG_SITEMAP_NUMBER', 5000)
&& $this->checkHooks()
&& $this->checkObligatoryHooks();
}
public function uninstall()
{
include dirname(__FILE__).'/install/uninstall.php';
include dirname(__FILE__).'/install/hooks-uninstall.php';
include dirname(__FILE__).'/install/images-uninstall.php';
Db::getInstance()->delete(
'hook_module',
'id_module = ' . (int) $this->id
);
return parent::uninstall()
&& $this->uninstallModuleTab('AdminEverPsBlog')
&& $this->uninstallModuleTab('AdminEverPsBlogPost')
&& $this->uninstallModuleTab('AdminEverPsBlogCategory')
&& $this->uninstallModuleTab('AdminEverPsBlogTag')
&& $this->uninstallModuleTab('AdminEverPsBlogComment')
&& $this->uninstallModuleTab('AdminEverPsBlogAuthor');
}
private function installModuleTab($tabClass, $parent, $tabName)
{
$tab = new Tab();
$tab->active = 1;
$tab->class_name = $tabClass;
$tab->id_parent = (int) Tab::getIdFromClassName($parent);
$tab->position = Tab::getNewLastPosition($tab->id_parent);
$tab->module = $this->name;
if ($tabClass == 'AdminEverPsBlog') {
$tab->icon = 'icon-team-ever';
}
foreach (Language::getLanguages(false) as $lang) {
$tab->name[(int) $lang['id_lang']] = $tabName;
}
return $tab->add();
}
private function uninstallModuleTab($tabClass)
{
$tab = new Tab((int) Tab::getIdFromClassName($tabClass));
return $tab->delete();
}
/**
* Add link rewrite rule
* @see https://stackoverflow.com/questions/49430883/creating-a-url-rewrite-module-in-prestashop
*/
public function hookModuleRoutes($params)
{
$base_route = Configuration::get('EVERPSBLOG_ROUTE') ? Configuration::get('EVERPSBLOG_ROUTE') : 'blog';
return [
'module-everpsblog-blog' => [
'controller' => 'blog',
'rule' => $base_route,
'keywords' => [
],
'params' => [
'fc' => 'module',
'module' => 'everpsblog',
'controller' => 'blog',
],
],
'module-everpsblog-category' => [
'controller' => 'category',
'rule' => $base_route . '/category{/:id_ever_category}-{:link_rewrite}',
'keywords' => [
'id_ever_category' => ['regexp' => '[0-9]+', 'param' => 'id_ever_category'],
'link_rewrite' => ['regexp' => '[_a-zA-Z0-9-\pL]*'],
],
'params' => [
'fc' => 'module',
'module' => 'everpsblog',
],
],
'module-everpsblog-post' => [
'controller' => 'post',
'rule' => $base_route . '/post{/:id_ever_post}-{:link_rewrite}',
'keywords' => [
'id_ever_post' => ['regexp' => '[0-9]+', 'param' => 'id_ever_post'],
'link_rewrite' => ['regexp' => '[_a-zA-Z0-9-\pL]*'],
],
'params' => [
'fc' => 'module',
'module' => 'everpsblog',
],
],
'module-everpsblog-tag' => [
'controller' => 'tag',
'rule' => $base_route . '/tag{/:id_ever_tag}-{:link_rewrite}',
'keywords' => [
'id_ever_tag' => ['regexp' => '[0-9]+', 'param' => 'id_ever_tag'],
'link_rewrite' => ['regexp' => '[_a-zA-Z0-9-\pL]*'],
],
'params' => [
'fc' => 'module',
'module' => 'everpsblog',
],
],
'module-everpsblog-author' => [
'controller' => 'author',
'rule' => $base_route . '/author{/:id_ever_author}-{:link_rewrite}',
'keywords' => [
'id_ever_author' => ['regexp' => '[0-9]+', 'param' => 'id_ever_author'],
'link_rewrite' => ['regexp' => '[_a-zA-Z0-9-\pL]*'],
],
'params' => [
'fc' => 'module',
'module' => 'everpsblog',
],
],
];
}
public function clearEverblogContent()
{
// Suppression des anciens posts, catégories, et tags
Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'ever_blog_post');
Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'ever_blog_post_lang');
Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'ever_blog_category');
Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'ever_blog_category_lang');
Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'ever_blog_tag');
Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'ever_blog_post_category');
Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'ever_blog_post_tag');
}
public function migrateMagentoToEverblog()
{
// Supprimer le contenu actuel d'Everblog
$this->clearEverblogContent();
// Récupérer les catégories, tags et posts de Magento
$categories = Db::getInstance()->executeS('SELECT * FROM aw_blog_cat');
$tags = Db::getInstance()->executeS('SELECT * FROM aw_blog_tags');
$posts = Db::getInstance()->executeS('SELECT * FROM aw_blog');
// Récupérer tous les IDs de groupes dans PrestaShop
$groups = Db::getInstance()->executeS('SELECT id_group FROM ' . _DB_PREFIX_ . 'group');
$groupIds = array_column($groups, 'id_group');
$allowedGroupsJson = json_encode($groupIds); // Convertir en JSON
// Créer une catégorie par défaut "Non classé"
$defaultCategory = new EverPsBlogCategory();
$defaultCategory->title = [1 => 'Non classé']; // Assuming language ID = 1
$defaultCategory->meta_title = [1 => 'Non classé'];
$defaultCategory->meta_description = [1 => ''];
$defaultCategory->link_rewrite = [1 => Tools::link_rewrite('non-classe')];
$defaultCategory->date_add = date('Y-m-d H:i:s');
$defaultCategory->date_upd = date('Y-m-d H:i:s');
$defaultCategory->id_parent_category = 1;
$defaultCategory->allowed_groups = $allowedGroupsJson;
$defaultCategory->indexable = true;
$defaultCategory->follow = true;
$defaultCategory->active = true;
$defaultCategory->id_shop = 1;
$defaultCategory->save();
// Récupérer l'ID de la catégorie par défaut
$defaultCategoryId = Configuration::get('EVERBLOG_UNCLASSED_ID');
// Insérer les catégories dans Everblog
foreach ($categories as $category) {
$newCategory = new EverPsBlogCategory();
$newCategory->title = [1 => $category['title']]; // Assuming language ID = 1
$newCategory->meta_title = [1 => $category['meta_keywords']];
$newCategory->meta_description = [1 => $category['meta_description']];
$newCategory->link_rewrite = [1 => Tools::link_rewrite($category['title'])];
$newCategory->date_add = date('Y-m-d H:i:s');
$newCategory->date_upd = date('Y-m-d H:i:s');
$newCategory->allowed_groups = $allowedGroupsJson;
$newCategory->active = true;
$newCategory->id_shop = 1;
$newCategory->save();
}
// Insérer les tags dans Everblog
foreach ($tags as $tag) {
$newTag = new EverPsBlogTag();
$newTag->title = [1 => $tag['tag']];
$newTag->meta_title = [1 => $tag['tag']];
$newTag->meta_description = [1 => ''];
$newTag->link_rewrite = [1 => Tools::link_rewrite($tag['tag'])];
$newTag->allowed_groups = $allowedGroupsJson;
$newTag->date_add = date('Y-m-d H:i:s');
$newTag->date_upd = date('Y-m-d H:i:s');
$newTag->indexable = true;
$newTag->follow = true;
$newTag->active = true;
$newTag->id_shop = 1;
$newTag->save();
}
// Insérer les posts dans Everblog
foreach ($posts as $post) {
$post['post_content'] = str_replace('\r\n', '<p></p>', $post['post_content']);
// Nettoyage et remplacement des images dans le contenu
$cleanedContent = $this->replaceAndDownloadImages($post['post_content']);
$cleanedContent = Tools::purifyHTML($cleanedContent);
$cleanedExcerpt = $this->replaceAndDownloadImages($post['short_content']);
// Création du post
$newPost = new EverPsBlogPost();
$newPost->title = [1 => $post['title']];
$newPost->meta_title = [1 => $post['meta_keywords']];
$newPost->meta_description = [1 => $post['meta_description']];
$newPost->link_rewrite = [1 => Tools::link_rewrite($post['title'])];
$newPost->date_add = $post['created_time'] ? $post['created_time'] : date('Y-m-d H:i:s');
$newPost->date_upd = $post['update_time'] ? $post['update_time'] : date('Y-m-d H:i:s');
$newPost->active = ($post['status'] == 1) ? true : false;
$newPost->indexable = ($post['status'] == 1) ? true : false;
$newPost->follow = ($post['status'] == 1) ? true : false;
$newPost->content = [1 => $cleanedContent];
$newPost->post_status = 'published';
$newPost->id_shop = 1;
$newPost->id_default_category = $defaultCategoryId;
$newPost->allowed_groups = $allowedGroupsJson; // Ajouter les groupes autorisés
$newPost->save();
// dump($post['post_content']);
// die();
// Récupérer l'ID du post enregistré
$postId = $newPost->id;
// dump(pSQL($post['post_content'], true));
// die();
// dump($cleanedContent);
// die();
// Mise à jour directe du contenu dans la base de données
// Db::getInstance()->execute('
// UPDATE ' . _DB_PREFIX_ . 'ever_blog_post_lang
// SET content = "' . pSQL($post['post_content'], true) . '", excerpt = "' . pSQL($cleanedExcerpt) . '"
// WHERE id_ever_post = ' . (int)$postId
// );
EverPsBlogTaxonomy::insertTaxonomy($defaultCategoryId, $postId, 'category');
$newPost->save();
// Insérer la catégorie par défaut "Non classé" pour chaque post
// Insérer les autres catégories associées au post
$postCategories = Db::getInstance()->executeS('SELECT * FROM aw_blog_post_cat WHERE post_id = ' . (int)$post['post_id']);
foreach ($postCategories as $postCategory) {
EverPsBlogTaxonomy::insertTaxonomy($postCategory['cat_id'], $postId, 'category');
}
// Insérer les tags associés au post
$postTags = explode(',', $post['tags']);
foreach ($postTags as $tag) {
$existingTag = Db::getInstance()->getRow('SELECT id_ever_tag FROM ' . _DB_PREFIX_ . 'ever_blog_tag_lang WHERE title = "' . pSQL($tag) . '"');
if ($existingTag) {
EverPsBlogTaxonomy::insertTaxonomy($existingTag['id_ever_tag'], $postId, 'tag');
}
}
$newPost->save();
}
}
public function replaceAndDownloadImages($content)
{
// Remplacer {{media url="..."}}
$pattern = '/\{\{media url="wysiwyg\/([^"]+)"\}\}/';
$content = preg_replace_callback($pattern, function($matches) {
$fullUrl = 'https://www.comptoir-de-vie.com/media/wysiwyg/' . $matches[1];
$localPath = $this->downloadImage($fullUrl);
if ($localPath) {
return $localPath; // Replace the {{media url="..."}} with the local path
}
return $fullUrl; // If download fails, return the original full URL
}, $content);
// Remplacer les URLs d'images dans les balises <img>
$dom = new DOMDocument;
if (!empty($content)) {
@$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
}
$xpath = new DOMXPath($dom);
$images = $xpath->query("//img");
foreach ($images as $img) {
$src = $img->getAttribute('src');
if (!empty($src)) {
// Identifier et traiter toutes les URLs valides
if (strpos($src, 'http://www.comptoir-de-vie.com/') !== false) {
// Cible tous les domaines nécessaires
$localPath = $this->downloadImage($src);
if ($localPath) {
$img->setAttribute('src', $localPath);
}
} elseif (strpos($src, 'http://www.comptoir-de-vie.com/') === false && strpos($src, '/comptoir/') !== false) {
// Cible tous les domaines nécessaires
$localPath = $this->downloadImage('http://www.comptoir-de-vie.com/'.$src);
if ($localPath) {
$img->setAttribute('src', $localPath);
}
}
}
}
return $dom->saveHTML();
}
public function downloadImage($url)
{
$imageContent = @file_get_contents($url);
if ($imageContent !== false) {
$imageName = basename(parse_url($url, PHP_URL_PATH));
$localPath = _PS_IMG_DIR_ . 'cms/' . $imageName;
if (!is_dir(_PS_IMG_DIR_ . 'cms/')) {
mkdir(_PS_IMG_DIR_ . 'cms/', 0755, true);
}
file_put_contents($localPath, $imageContent);
return _PS_IMG_ . 'cms/' . $imageName;
}
return false;
}
public function getContent()
{
$this->checkObligatoryHooks();
$this->checkAndFixDatabase();
$this->html = '';
// Process internal linking
if (Tools::isSubmit('submitGenerateBlogSitemap')) {
$this->postValidation();
if (!count($this->postErrors)) {
$this->generateBlogSitemap();
}
}
if (Tools::isSubmit('submitEverPsBlogConf')) {
$this->postValidation();
if (!count($this->postErrors)) {
$this->postProcess();
}
}
if (count($this->postErrors)) {
foreach ($this->postErrors as $error) {
$this->html .= $this->displayError($error);
}
}
// Display confirmations
if (count($this->postSuccess)) {
foreach ($this->postSuccess as $success) {
$this->html .= $this->displayConfirmation($success);
}
}
$ever_blog_token = Tools::encrypt('everpsblog/cron');
$emptytrash = $this->context->link->getModuleLink(
$this->name,
'emptytrash',
[
'token' => $ever_blog_token,
'id_shop' => (int) $this->context->shop->id,
],
true,
(int) $this->context->language->id,
(int) $this->context->shop->id
);
$pending = $this->context->link->getModuleLink(
$this->name,
'pending',
[
'token' => $ever_blog_token,
'id_shop' => (int) $this->context->shop->id,
],
true,
(int) $this->context->language->id,
(int) $this->context->shop->id
);
$planned = $this->context->link->getModuleLink(
$this->name,
'planned',
[
'token' => $ever_blog_token,
'id_shop' => (int) $this->context->shop->id,
],
true,
(int) $this->context->language->id,
(int) $this->context->shop->id
);
$sitemap_link = $this->context->link->getModuleLink(
$this->name,
'sitemaps',
[
'token' => $ever_blog_token,
'id_shop' => (int) $this->context->shop->id,
],
true,
(int) $this->context->language->id,
(int) $this->context->shop->id
);
$default_blog = $this->context->link->getModuleLink(
$this->name,
'blog',
[],
true,
(int) $this->context->language->id,
(int) $this->context->shop->id
);
$this->context->smarty->assign([
'blog_sitemaps' => $this->getSitemapIndexes(),
'image_dir' => $this->_path.'views/img',
'everpsblogcron' => $emptytrash,
'everpsblogcronpending' => $pending,
'everpsblogcronplanned' => $planned,
'everpsblogcronsitemap' => $sitemap_link,
'blog_url' => $default_blog,
]);
if ($this->checkLatestEverModuleVersion()) {
$this->html .= $this->context->smarty->fetch($this->local_path . 'views/templates/admin/upgrade.tpl');
}
$this->html .= $this->context->smarty->fetch($this->local_path . 'views/templates/admin/header.tpl');
$this->html .= $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');
$this->html .= $this->renderForm();
$this->html .= $this->context->smarty->fetch($this->local_path . 'views/templates/admin/footer.tpl');
return $this->html;
}
public function postValidation()
{
if (Tools::isSubmit('submitEverPsBlogConf')) {
if (!Tools::getValue('EVERPSBLOG_ROUTE')
|| !Validate::isLinkRewrite(Tools::getValue('EVERPSBLOG_ROUTE'))
) {
$this->postErrors[] = $this->l('Error : The field "Blog route" is not valid');
}
if (!Tools::getValue('EVERPSBLOG_EXCERPT')
|| !Validate::isInt(Tools::getValue('EVERPSBLOG_EXCERPT'))
) {
$this->postErrors[] = $this->l('Error : The field "Excerpt length" is not valid');
}
if (!Tools::getValue('EVERPSBLOG_TITLE_LENGTH')
|| !Validate::isInt(Tools::getValue('EVERPSBLOG_TITLE_LENGTH'))
) {
$this->postErrors[] = $this->l('Error : The field "Title length" is not valid');
}
if (Tools::getValue('EVERBLOG_SHOW_POST_COUNT')
&& !Validate::isBool(Tools::getValue('EVERBLOG_SHOW_POST_COUNT'))
) {
$this->postErrors[] = $this->l('Error : The field "Show post count" is not valid');
}
if (Tools::getValue('EVERBLOG_TINYMCE')
&& !Validate::isBool(Tools::getValue('EVERBLOG_TINYMCE'))
) {
$this->postErrors[] = $this->l('Error : The field "Extends TinyMCE" is not valid');
}
if (Tools::getValue('EVERBLOG_SHOW_HOME')
&& !Validate::isBool(Tools::getValue('EVERBLOG_SHOW_HOME'))
) {
$this->postErrors[] = $this->l('Error : The field "Show post on homepage" is not valid');
}
if (!Tools::getValue('EVERPSBLOG_PAGINATION')
&& !Validate::isUnsignedInt(Tools::getValue('EVERPSBLOG_PAGINATION'))
) {
$this->postErrors[] = $this->l('Error : The field "Posts per page" is not valid');
}
if (!Tools::getValue('EVERPSBLOG_HOME_NBR')
&& !Validate::isUnsignedInt(Tools::getValue('EVERPSBLOG_HOME_NBR'))
) {
$this->postErrors[] = $this->l('Error : The field "Posts for home" is not valid');
}
if (!Tools::getValue('EVERPSBLOG_PRODUCT_NBR')
&& !Validate::isUnsignedInt(Tools::getValue('EVERPSBLOG_PRODUCT_NBR'))
) {
$this->postErrors[] = $this->l('Error : The field "Posts for product" is not valid');
}
if (!Tools::getValue('EVERBLOG_ADMIN_EMAIL')
|| !Validate::isUnsignedInt(Tools::getValue('EVERBLOG_ADMIN_EMAIL'))
) {
$this->postErrors[] = $this->l('Error : The field "Admin email" is not valid');
}
if (Tools::getValue('EVERBLOG_ALLOW_COMMENTS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_ALLOW_COMMENTS'))
) {
$this->postErrors[] = $this->l('Error : The field "Allow comments" is not valid');
}
if (Tools::getValue('EVERBLOG_CHECK_COMMENTS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_CHECK_COMMENTS'))
) {
$this->postErrors[] = $this->l('Error : The field "Check comments" is not valid');
}
if (Tools::getValue('EVERBLOG_RSS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_RSS'))
) {
$this->postErrors[] = $this->l('Error : The field "Use RSS feed" is not valid');
}
if (Tools::getValue('EVERBLOG_SHOW_AUTHOR')
&& !Validate::isBool(Tools::getValue('EVERBLOG_SHOW_AUTHOR'))
) {
$this->postErrors[] = $this->l('Error : The field "Show author" is not valid');
}
if (Tools::getValue('EVERBLOG_BANNED_USERS')
&& !Validate::isGenericName(Tools::getValue('EVERBLOG_BANNED_USERS'))
) {
$this->postErrors[] = $this->l('Error : The field "Banned users" is not valid');
}
if (Tools::getValue('EVERBLOG_BANNED_IP')
&& !Validate::isGenericName(Tools::getValue('EVERBLOG_BANNED_IP'))
) {
$this->postErrors[] = $this->l('Error : The field "Banned IP" is not valid');
}
if (Tools::getValue('EVERBLOG_ONLY_LOGGED_COMMENT')
&& !Validate::isBool(Tools::getValue('EVERBLOG_ONLY_LOGGED_COMMENT'))
) {
$this->postErrors[] = $this->l('Error : The field "Only logged can comment" is not valid');
}
if (!Tools::getValue('EVERBLOG_EMPTY_TRASH')
&& !Validate::isUnsignedInt(Tools::getValue('EVERBLOG_FANCYBOX'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Fancybox" is not valid'
);
}
if (!Tools::getValue('EVERPSBLOG_TYPE')
&& !Validate::isString(Tools::getValue('EVERPSBLOG_TYPE'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Default blog type" is not valid'
);
}
if (Tools::getValue('EVERBLOG_ANIMATE')
&& !Validate::isBool(Tools::getValue('EVERBLOG_ANIMATE'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Use cool CSS" is not valid'
);
}
if (Tools::getValue('EVERBLOG_RELATED_POST')
&& !Validate::isBool(Tools::getValue('EVERBLOG_RELATED_POST'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Show related posts on product page" is not valid'
);
}
if (Tools::getValue('EVERBLOG_SHOW_FEAT_CAT')
&& !Validate::isBool(Tools::getValue('EVERBLOG_SHOW_FEAT_CAT'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Show featured category image" is not valid'
);
}
if (Tools::getValue('EVERBLOG_SHOW_FEAT_TAG')
&& !Validate::isBool(Tools::getValue('EVERBLOG_SHOW_FEAT_TAG'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Show featured tag image" is not valid'
);
}
if (Tools::getValue('EVERBLOG_ARCHIVE_COLUMNS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_ARCHIVE_COLUMNS'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Show archives on columns" is not valid'
);
}
if (Tools::getValue('EVERBLOG_TAG_COLUMNS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_TAG_COLUMNS'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Show tags on columns" is not valid'
);
}
if (Tools::getValue('EVERBLOG_CATEG_COLUMNS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_CATEG_COLUMNS'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Show categories on columns" is not valid'
);
}
if (Tools::getValue('EVERBLOG_FANCYBOX')
&& !Validate::isBool(Tools::getValue('EVERBLOG_FANCYBOX'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Fancybox" is not valid'
);
}
if (Tools::getValue('EVERBLOG_CAT_FEATURED')
&& !Validate::isUnsignedInt(Tools::getValue('EVERBLOG_CAT_FEATURED'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Featured category" is not valid'
);
}
// Multilingual fields
foreach (Language::getLanguages(false) as $lang) {
if (Tools::getValue('EVERBLOG_TITLE_'.$lang['id_lang'])
&& !Validate::isString(Tools::getValue('EVERBLOG_TITLE_'.$lang['id_lang']))
) {
$this->postErrors[] = $this->l(
'Error : Blog title is invalid'
);
}
if (Tools::getValue('EVERBLOG_META_DESC_'.$lang['id_lang'])
&& !Validate::isCleanHtml(Tools::getValue('EVERBLOG_META_DESC_'.$lang['id_lang']))
) {
$this->postErrors[] = $this->l(
'Error : Blog meta description is invalid'
);
}
if (Tools::getValue('EVERBLOG_TOP_TEXT_'.$lang['id_lang'])
&& !Validate::isCleanHtml(Tools::getValue('EVERBLOG_TOP_TEXT_'.$lang['id_lang']))
) {
$this->postErrors[] = $this->l(
'Error : Blog top text is invalid'
);
}
if (Tools::getValue('EVERBLOG_BOTTOM_'.$lang['id_lang'])
&& !Validate::isCleanHtml(Tools::getValue('EVERBLOG_BOTTOM_'.$lang['id_lang']))
) {
$this->postErrors[] = $this->l(
'Error : Blog bottom text is invalid'
);
}
}
// Layouts
if (Tools::getValue('EVERPSBLOG_BLOG_LAYOUT')
&& !Validate::isString(Tools::getValue('EVERPSBLOG_BLOG_LAYOUT'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Blog layout" is not valid'
);
}
if (Tools::getValue('EVERPSBLOG_POST_LAYOUT')
&& !Validate::isString(Tools::getValue('EVERPSBLOG_POST_LAYOUT'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Post layout" is not valid'
);
}
if (Tools::getValue('EVERPSBLOG_CAT_LAYOUT')
&& !Validate::isString(Tools::getValue('EVERPSBLOG_CAT_LAYOUT'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Category layout" is not valid'
);
}
if (Tools::getValue('EVERPSBLOG_AUTHOR_LAYOUT')
&& !Validate::isString(Tools::getValue('EVERPSBLOG_AUTHOR_LAYOUT'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Author layout" is not valid'
);
}
if (Tools::getValue('EVERPSBLOG_TAG_LAYOUT')
&& !Validate::isString(Tools::getValue('EVERPSBLOG_TAG_LAYOUT'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Tag layout" is not valid'
);
}
if (isset($_FILES['wordpress_xml'])
&& isset($_FILES['wordpress_xml']['tmp_name'])
&& !empty($_FILES['wordpress_xml']['tmp_name'])
) {
if (pathinfo($_FILES['wordpress_xml']['name'], PATHINFO_EXTENSION) != 'xml') {
$this->postErrors[] = $this->l(
'Error : The field "Tag layout" is not valid'
);
} else {
$this->importWordPressFile($_FILES['wordpress_xml']);
}
}
if (Tools::getValue('EVERBLOG_IMPORT_POST_STATE')
&& !Validate::isString(Tools::getValue('EVERBLOG_IMPORT_POST_STATE'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Default post status on import from WordPress xml file" is not valid'
);
}
if (Tools::getValue('EVERBLOG_IMPORT_AUTHORS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_IMPORT_AUTHORS'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Import authors from WordPress xml file" is not valid'
);
}
if (Tools::getValue('EVERBLOG_IMPORT_CATS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_IMPORT_CATS'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Import categories from WordPress xml file" is not valid'
);
}
if (Tools::getValue('EVERBLOG_IMPORT_TAGS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_IMPORT_TAGS'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Import tags from WordPress xml file" is not valid'
);
}
if (Tools::getValue('EVERBLOG_ENABLE_AUTHORS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_ENABLE_AUTHORS'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Enable authors from WordPress xml file" is not valid'
);
}
if (Tools::getValue('EVERBLOG_ENABLE_CATS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_ENABLE_CATS'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Enable categories from WordPress xml file" is not valid'
);
}
if (Tools::getValue('EVERBLOG_ENABLE_TAGS')
&& !Validate::isBool(Tools::getValue('EVERBLOG_ENABLE_TAGS'))
) {
$this->postErrors[] = $this->l(
'Error : The field "Enable tags from WordPress xml file" is not valid'
);
}
}
}
protected function postProcess()
{
$form_values = $this->getConfigFormValues();
// Reset hooks
Configuration::deleteByName('PS_ROUTE_module-everpsblog-blog');
Configuration::deleteByName('PS_ROUTE_module-everpsblog-category');
Configuration::deleteByName('PS_ROUTE_module-everpsblog-post');
Configuration::deleteByName('PS_ROUTE_module-everpsblog-tag');
Configuration::deleteByName('PS_ROUTE_module-everpsblog-author');
Hook::exec('hookModuleRoutes');
// Preparing multilingual datas
$everblog_title = [];
$everblog_meta_desc = [];
$everblog_top_text = [];
$everblog_bottom_text = [];
foreach (Language::getLanguages(false) as $lang) {
$everblog_title[$lang['id_lang']] = (Tools::getValue(
'EVERBLOG_TITLE_'.$lang['id_lang']
)) ? Tools::getValue(
'EVERBLOG_TITLE_'.$lang['id_lang']
) : '';
$everblog_meta_desc[$lang['id_lang']] = (Tools::getValue(
'EVERBLOG_META_DESC_'.$lang['id_lang']
)) ? Tools::getValue(
'EVERBLOG_META_DESC_'.$lang['id_lang']
) : '';
$everblog_top_text[$lang['id_lang']] = (Tools::getValue(
'EVERBLOG_TOP_TEXT_'.$lang['id_lang']
)) ? Tools::getValue(
'EVERBLOG_TOP_TEXT_'.$lang['id_lang']
) : '';
$everblog_bottom_text[$lang['id_lang']] = (Tools::getValue(
'EVERBLOG_BOTTOM_TEXT_'.$lang['id_lang']
)) ? Tools::getValue(
'EVERBLOG_BOTTOM_TEXT_'.$lang['id_lang']
) : '';
}
// Save all datas
foreach (array_keys($form_values) as $key) {
if ($key == 'EVERBLOG_TITLE') {
Configuration::updateValue(
$key,
$everblog_title
);
} elseif ($key == 'EVERBLOG_META_DESC') {
Configuration::updateValue(
$key,
$everblog_meta_desc
);
} elseif ($key == 'EVERBLOG_TOP_TEXT') {
Configuration::updateValue(
$key,
$everblog_top_text,
true
);
} elseif ($key == 'EVERBLOG_BOTTOM_TEXT') {
Configuration::updateValue(
$key,
$everblog_bottom_text,
true
);
} else {
Configuration::updateValue($key, Tools::getValue($key));
}
}
if ((bool) Tools::getValue('EVERBLOG_SHOW_HOME') === true) {
$this->registerHook('displayHome');
} else {
$this->unregisterHook('displayHome');
}
$handle = fopen(
_PS_MODULE_DIR_ . '/' . $this->name . '/views/css/custom.css',
'w+'
);
fclose($handle);
/* Insert new values to the CSS file */
file_put_contents(
_PS_MODULE_DIR_ . '/' . $this->name . '/views/css/custom.css',
Tools::getValue('EVERBLOG_CSS')
);
$this->postSuccess[] = $this->l('All settings have been saved');
}
protected function getConfigFormValues()
{
$custom_css = Tools::file_get_contents(
_PS_MODULE_DIR_ . '/' . $this->name . '/views/css/custom.css'
);
$formValues = [];
$everblog_title = [];
$everblog_meta_desc = [];
$everblog_top_text = [];
$everblog_bottom_text = [];
foreach (Language::getLanguages(false) as $lang) {
$everblog_title[$lang['id_lang']] = (Tools::getValue(
'EVERBLOG_TITLE_'.$lang['id_lang']
)) ? Tools::getValue(
'EVERBLOG_TITLE_'.$lang['id_lang']
) : '';
$everblog_meta_desc[$lang['id_lang']] = (Tools::getValue(
'EVERBLOG_META_DESC_'.$lang['id_lang']
)) ? Tools::getValue(
'EVERBLOG_META_DESC_'.$lang['id_lang']
) : '';
$everblog_top_text[$lang['id_lang']] = (Tools::getValue(
'EVERBLOG_TOP_TEXT_'.$lang['id_lang']
)) ? Tools::getValue(
'EVERBLOG_TOP_TEXT_'.$lang['id_lang']
) : '';
$everblog_bottom_text[$lang['id_lang']] = (Tools::getValue(
'EVERBLOG_BOTTOM_TEXT_'.$lang['id_lang']
)) ? Tools::getValue(
'EVERBLOG_BOTTOM_TEXT_'.$lang['id_lang']
) : '';
}
$formValues[] = [
'EVERPSBLOG_ROUTE' => Configuration::get('EVERPSBLOG_ROUTE'),
'EVERPSBLOG_EXCERPT' => Configuration::get('EVERPSBLOG_EXCERPT'),
'EVERPSBLOG_TITLE_LENGTH' => Configuration::get('EVERPSBLOG_TITLE_LENGTH'),
'EVERBLOG_TINYMCE' => Configuration::get('EVERBLOG_TINYMCE'),
'EVERBLOG_SHOW_POST_COUNT' => Configuration::get('EVERBLOG_SHOW_POST_COUNT'),
'EVERBLOG_SHOW_HOME' => Configuration::get('EVERBLOG_SHOW_HOME'),
'EVERPSBLOG_PAGINATION' => Configuration::get('EVERPSBLOG_PAGINATION'),
'EVERPSBLOG_HOME_NBR' => Configuration::get('EVERPSBLOG_HOME_NBR'),
'EVERPSBLOG_PRODUCT_NBR' => Configuration::get('EVERPSBLOG_PRODUCT_NBR'),