forked from PrestaShop/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Link.php
1593 lines (1404 loc) · 59 KB
/
Link.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
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use PrestaShop\PrestaShop\Core\Exception\CoreException;
use PrestaShop\PrestaShop\Core\Feature\TokenInUrls;
use PrestaShopBundle\Routing\Converter\LegacyUrlConverter;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class LinkCore
{
/** @var bool Rewriting activation */
protected $allow;
protected $url;
public static $cache = ['page' => []];
public $protocol_link;
public $protocol_content;
protected $ssl_enable;
protected $urlShopId = null;
protected static $category_disable_rewrite = null;
/**
* Constructor (initialization only).
*
* @param string|null $protocolLink
* @param string|null $protocolContent
*/
public function __construct($protocolLink = null, $protocolContent = null)
{
$this->allow = (bool) Configuration::get('PS_REWRITING_SETTINGS');
$this->url = $_SERVER['SCRIPT_NAME'];
$this->protocol_link = $protocolLink;
$this->protocol_content = $protocolContent;
if (!defined('_PS_BASE_URL_')) {
define('_PS_BASE_URL_', Tools::getShopDomain(true));
}
if (!defined('_PS_BASE_URL_SSL_')) {
define('_PS_BASE_URL_SSL_', Tools::getShopDomainSsl(true));
}
if (Link::$category_disable_rewrite === null) {
Link::$category_disable_rewrite = [
Configuration::get('PS_HOME_CATEGORY'),
Configuration::get('PS_ROOT_CATEGORY'),
];
}
$this->ssl_enable = Configuration::get('PS_SSL_ENABLED');
}
/**
* Create a link to delete a product.
*
* @param Product|array|int $product ID of the product OR a Product object
* @param int $idPicture ID of the picture to delete
*
* @return string
*/
public function getProductDeletePictureLink($product, $idPicture)
{
$url = $this->getProductLink($product);
return $url . ((strpos($url, '?')) ? '&' : '?') . 'deletePicture=' . $idPicture;
}
/**
* Return a product object from various product format.
*
* @param Product|array|int $product
* @param int $idLang
* @param int $idShop
*
* @return Product
*
* @throws PrestaShopException
*/
public function getProductObject($product, $idLang, $idShop)
{
if (!is_object($product)) {
if (is_array($product) && isset($product['id_product'])) {
$product = new Product($product['id_product'], false, $idLang, $idShop);
} elseif ((int) $product) {
$product = new Product((int) $product, false, $idLang, $idShop);
} else {
throw new PrestaShopException('Invalid product vars');
}
}
return $product;
}
/**
* Create a link to a product.
*
* @param ProductCore|array|int $product Product object (can be an ID product, but deprecated)
* @param string|null $alias
* @param string|null $category
* @param string|null $ean13
* @param int|null $idLang
* @param int|null $idShop (since 1.5.0) ID shop need to be used when we generate a product link for a product in a cart
* @param int|null $idProductAttribute ID product attribute
* @param bool $force_routes
* @param bool $relativeProtocol
* @param bool $withIdInAnchor
* @param array $extraParams
* @param bool $addAnchor
*
* @return string
*
* @throws PrestaShopException
*/
public function getProductLink(
$product,
$alias = null,
$category = null,
$ean13 = null,
$idLang = null,
$idShop = null,
$idProductAttribute = null,
$force_routes = false,
$relativeProtocol = false,
$withIdInAnchor = false,
$extraParams = [],
bool $addAnchor = true
) {
$dispatcher = Dispatcher::getInstance();
if (!$idLang) {
$idLang = Context::getContext()->language->id;
}
$url = $this->getBaseLink($idShop, null, $relativeProtocol) . $this->getLangLink($idLang, null, $idShop);
// Set available keywords
$params = [];
if (!is_object($product)) {
if (is_array($product) && isset($product['id_product'])) {
$params['id'] = $product['id_product'];
} elseif ((int) $product) {
$params['id'] = $product;
} else {
throw new PrestaShopException('Invalid product vars');
}
} else {
$params['id'] = $product->id;
}
// Attribute equal to 0 or empty is useless, so we force it to null so that it won't be inserted in query parameters
if (empty($idProductAttribute)) {
$idProductAttribute = null;
}
$params['id_product_attribute'] = $idProductAttribute;
if (!$alias) {
$product = $this->getProductObject($product, $idLang, $idShop);
}
$params['rewrite'] = (!$alias) ? $product->getFieldByLang('link_rewrite') : $alias;
if (!$ean13) {
$product = $this->getProductObject($product, $idLang, $idShop);
}
$params['ean13'] = (!$ean13) ? $product->ean13 : $ean13;
if ($dispatcher->hasKeyword('product_rule', $idLang, 'meta_keywords', $idShop)) {
$product = $this->getProductObject($product, $idLang, $idShop);
$params['meta_keywords'] = Tools::str2url($product->getFieldByLang('meta_keywords'));
}
if ($dispatcher->hasKeyword('product_rule', $idLang, 'meta_title', $idShop)) {
$product = $this->getProductObject($product, $idLang, $idShop);
$params['meta_title'] = Tools::str2url($product->getFieldByLang('meta_title'));
}
if ($dispatcher->hasKeyword('product_rule', $idLang, 'manufacturer', $idShop)) {
$product = $this->getProductObject($product, $idLang, $idShop);
$params['manufacturer'] = Tools::str2url($product->isFullyLoaded ? $product->manufacturer_name : Manufacturer::getNameById($product->id_manufacturer));
}
if ($dispatcher->hasKeyword('product_rule', $idLang, 'supplier', $idShop)) {
$product = $this->getProductObject($product, $idLang, $idShop);
$params['supplier'] = Tools::str2url($product->isFullyLoaded ? $product->supplier_name : Supplier::getNameById($product->id_supplier));
}
if ($dispatcher->hasKeyword('product_rule', $idLang, 'price', $idShop)) {
$product = $this->getProductObject($product, $idLang, $idShop);
$params['price'] = $product->isFullyLoaded ? $product->price : Product::getPriceStatic($product->id, false, null, 6, null, false, true, 1, false, null, null, null, $product->specificPrice);
}
if ($dispatcher->hasKeyword('product_rule', $idLang, 'tags', $idShop)) {
$product = $this->getProductObject($product, $idLang, $idShop);
$params['tags'] = Tools::str2url($product->getTags($idLang));
}
if ($dispatcher->hasKeyword('product_rule', $idLang, 'category', $idShop)) {
if (!$category) {
$product = $this->getProductObject($product, $idLang, $idShop);
}
$params['category'] = (!$category) ? $product->category : $category;
}
if ($dispatcher->hasKeyword('product_rule', $idLang, 'reference', $idShop)) {
$product = $this->getProductObject($product, $idLang, $idShop);
$params['reference'] = Tools::str2url($product->reference);
}
if ($dispatcher->hasKeyword('product_rule', $idLang, 'categories', $idShop)) {
$product = $this->getProductObject($product, $idLang, $idShop);
$params['category'] = (!$category) ? $product->category : $category;
$cats = [];
foreach ($product->getParentCategories($idLang) as $cat) {
if (!in_array($cat['id_category'], Link::$category_disable_rewrite)) {
// remove root and home category from the URL
$cats[] = $cat['link_rewrite'];
}
}
$params['categories'] = implode('/', $cats);
}
if ($idProductAttribute) {
$product = $this->getProductObject($product, $idLang, $idShop);
}
$anchor = $addAnchor && $idProductAttribute ? $product->getAnchor((int) $idProductAttribute, (bool) $withIdInAnchor) : '';
return $url . $dispatcher->createUrl('product_rule', $idLang, array_merge($params, $extraParams), $force_routes, $anchor, $idShop);
}
/**
* Get the URL to remove the Product from the Cart.
*
* @param int $idProduct
* @param int $idProductAttribute
* @param int|null $idCustomization
*
* @return string
*/
public function getRemoveFromCartURL(
$idProduct,
$idProductAttribute,
$idCustomization = null
) {
$params = [
'delete' => 1,
'id_product' => $idProduct,
'id_product_attribute' => $idProductAttribute,
];
if ($idCustomization) {
$params['id_customization'] = $idCustomization;
}
return $this->getPageLink(
'cart',
true,
null,
$params,
false
);
}
/**
* Get URL to add one Product to Cart.
*
* @param int $idProduct
* @param int $idProductAttribute
* @param int|null $idCustomization
*
* @return string
*/
public function getUpQuantityCartURL(
$idProduct,
$idProductAttribute,
$idCustomization = null
) {
return $this->getUpdateQuantityCartURL($idProduct, $idProductAttribute, $idCustomization, 'up');
}
/**
* Get URL to remove one Product to Cart.
*
* @param int $idProduct
* @param int $idProductAttribute
* @param int|null $idCustomization
*
* @return string
*/
public function getDownQuantityCartURL(
$idProduct,
$idProductAttribute,
$idCustomization = null
) {
return $this->getUpdateQuantityCartURL($idProduct, $idProductAttribute, $idCustomization, 'down');
}
/**
* Get URL to update quantity of Product in Cart.
*
* @param int $idProduct
* @param int $idProductAttribute
* @param int|null $idCustomization
* @param string|null $op
*
* @return string
*/
public function getUpdateQuantityCartURL(
$idProduct,
$idProductAttribute,
$idCustomization = null,
$op = null
) {
$params = [
'update' => 1,
'id_product' => $idProduct,
'id_product_attribute' => $idProductAttribute,
'token' => Tools::getToken(false),
];
if (null !== $op) {
$params['op'] = $op;
}
if ($idCustomization) {
$params['id_customization'] = $idCustomization;
}
return $this->getPageLink(
'cart',
true,
null,
$params,
false
);
}
/**
* Get add to Cart URL.
*
* @param int $idProduct
* @param int $idProductAttribute
*
* @return string
*/
public function getAddToCartURL($idProduct, $idProductAttribute)
{
$params = [
'add' => 1,
'id_product' => $idProduct,
'id_product_attribute' => $idProductAttribute,
];
return $this->getPageLink(
'cart',
true,
null,
$params,
false
);
}
/**
* Return a category object from various category format.
*
* @param Category|array|int $category
* @param int $idLang
*
* @return Category
*
* @throws PrestaShopException
*/
public function getCategoryObject($category, $idLang)
{
if (!is_object($category)) {
if (isset($category['id_category'])) {
$category = new Category($category['id_category'], $idLang);
} elseif ((int) $category) {
$category = new Category((int) $category, $idLang);
} else {
throw new PrestaShopException('Invalid category vars');
}
}
return $category;
}
/**
* Create a link to a category.
*
* @param CategoryCore|array|int|string $category Category object (can be an ID category, but deprecated)
* @param string|null $alias
* @param int|null $idLang
* @param string|null $selectedFilters Url parameter to autocheck filters of the module blocklayered
* @param int|null $idShop
* @param bool $relativeProtocol
*
* @return string
*/
public function getCategoryLink(
$category,
$alias = null,
$idLang = null,
$selectedFilters = null,
$idShop = null,
$relativeProtocol = false
) {
$dispatcher = Dispatcher::getInstance();
if (!$idLang) {
$idLang = Context::getContext()->language->id;
}
$url = $this->getBaseLink($idShop, null, $relativeProtocol) . $this->getLangLink($idLang, null, $idShop);
// Set available keywords
$params = [];
if (Validate::isLoadedObject($category)) {
$params['id'] = $category->id;
} elseif (is_array($category) && isset($category['id_category'])) {
$params['id'] = $category['id_category'];
} elseif (is_int($category) || (is_string($category) && ctype_digit($category))) {
$params['id'] = (int) $category;
} else {
throw new InvalidArgumentException('Invalid category parameter');
}
if ((int) $params['id'] === 0) {
Tools::displayAsDeprecated('Generating URL with id 0 is deprecated');
}
$rule = 'category_rule';
if (!$alias) {
$category = $this->getCategoryObject($category, $idLang);
}
$params['rewrite'] = (!$alias) ? $category->link_rewrite : $alias;
if ($dispatcher->hasKeyword($rule, $idLang, 'meta_keywords', $idShop)) {
$category = $this->getCategoryObject($category, $idLang);
$params['meta_keywords'] = Tools::str2url($category->getFieldByLang('meta_keywords'));
}
if ($dispatcher->hasKeyword($rule, $idLang, 'meta_title', $idShop)) {
$category = $this->getCategoryObject($category, $idLang);
$params['meta_title'] = Tools::str2url($category->getFieldByLang('meta_title'));
}
return $url . Dispatcher::getInstance()->createUrl($rule, $idLang, $params, $this->allow, '', $idShop);
}
/**
* Create a link to a CMS category.
*
* @param CMSCategory|int $cmsCategory CMSCategory object (can be an ID category, but deprecated)
* @param string|null $alias
* @param int|null $idLang
* @param int|null $idShop
* @param bool $relativeProtocol
*
* @return string
*/
public function getCMSCategoryLink(
$cmsCategory,
$alias = null,
$idLang = null,
$idShop = null,
$relativeProtocol = false
) {
if (!$idLang) {
$idLang = Context::getContext()->language->id;
}
$url = $this->getBaseLink($idShop, null, $relativeProtocol) . $this->getLangLink($idLang, null, $idShop);
$dispatcher = Dispatcher::getInstance();
if (!is_object($cmsCategory)) {
if ($alias !== null && !$dispatcher->hasKeyword('cms_category_rule', $idLang, 'meta_keywords', $idShop) && !$dispatcher->hasKeyword('cms_category_rule', $idLang, 'meta_title', $idShop)) {
return $url . $dispatcher->createUrl('cms_category_rule', $idLang, ['id' => (int) $cmsCategory, 'rewrite' => (string) $alias], $this->allow, '', $idShop);
}
$cmsCategory = new CMSCategory($cmsCategory, $idLang);
}
if (is_array($cmsCategory->link_rewrite) && isset($cmsCategory->link_rewrite[(int) $idLang])) {
$cmsCategory->link_rewrite = $cmsCategory->link_rewrite[(int) $idLang];
}
if (is_array($cmsCategory->meta_keywords) && isset($cmsCategory->meta_keywords[(int) $idLang])) {
$cmsCategory->meta_keywords = $cmsCategory->meta_keywords[(int) $idLang];
}
if (is_array($cmsCategory->meta_title) && isset($cmsCategory->meta_title[(int) $idLang])) {
$cmsCategory->meta_title = $cmsCategory->meta_title[(int) $idLang];
}
// Set available keywords
$params = [];
$params['id'] = $cmsCategory->id;
$params['rewrite'] = (!$alias) ? $cmsCategory->link_rewrite : $alias;
$params['meta_keywords'] = Tools::str2url($cmsCategory->meta_keywords);
$params['meta_title'] = Tools::str2url($cmsCategory->meta_title);
return $url . $dispatcher->createUrl('cms_category_rule', $idLang, $params, $this->allow, '', $idShop);
}
/**
* Create a link to a CMS page.
*
* @param CMS|int $cms CMS object
* @param string|null $alias
* @param bool|null $ssl
* @param int|null $idLang
* @param int|null $idShop
* @param bool $relativeProtocol
*
* @return string
*/
public function getCMSLink(
$cms,
$alias = null,
$ssl = null,
$idLang = null,
$idShop = null,
$relativeProtocol = false
) {
if (!$idLang) {
$idLang = Context::getContext()->language->id;
}
$url = $this->getBaseLink($idShop, $ssl, $relativeProtocol) . $this->getLangLink($idLang, null, $idShop);
$dispatcher = Dispatcher::getInstance();
if (!is_object($cms)) {
if ($alias !== null && !$dispatcher->hasKeyword('cms_rule', $idLang, 'meta_keywords', $idShop) && !$dispatcher->hasKeyword('cms_rule', $idLang, 'meta_title', $idShop)) {
return $url . $dispatcher->createUrl('cms_rule', $idLang, ['id' => (int) $cms, 'rewrite' => (string) $alias], $this->allow, '', $idShop);
}
$cms = new CMS($cms, $idLang);
}
// Set available keywords
$params = [];
$params['id'] = $cms->id;
$params['rewrite'] = (!$alias) ? (is_array($cms->link_rewrite) ? $cms->link_rewrite[(int) $idLang] : $cms->link_rewrite) : $alias;
$params['meta_keywords'] = '';
if (isset($cms->meta_keywords) && !empty($cms->meta_keywords)) {
$params['meta_keywords'] = is_array($cms->meta_keywords) ? Tools::str2url($cms->meta_keywords[(int) $idLang]) : Tools::str2url($cms->meta_keywords);
}
$params['meta_title'] = '';
if (isset($cms->meta_title) && !empty($cms->meta_title)) {
$params['meta_title'] = is_array($cms->meta_title) ? Tools::str2url($cms->meta_title[(int) $idLang]) : Tools::str2url($cms->meta_title);
}
return $url . $dispatcher->createUrl('cms_rule', $idLang, $params, $this->allow, '', $idShop);
}
/**
* Create a link to a supplier.
*
* @param Supplier|int $supplier Supplier object (can be an ID supplier, but deprecated)
* @param string|null $alias
* @param int|null $idLang
* @param int|null $idShop
* @param bool $relativeProtocol
*
* @return string
*/
public function getSupplierLink(
$supplier,
$alias = null,
$idLang = null,
$idShop = null,
$relativeProtocol = false
) {
if (!$idLang) {
$idLang = Context::getContext()->language->id;
}
$url = $this->getBaseLink($idShop, null, $relativeProtocol) . $this->getLangLink($idLang, null, $idShop);
$dispatcher = Dispatcher::getInstance();
if (!is_object($supplier)) {
if ($alias !== null
&& !$dispatcher->hasKeyword('supplier_rule', $idLang, 'meta_keywords', $idShop)
&& !$dispatcher->hasKeyword('supplier_rule', $idLang, 'meta_title', $idShop)
) {
return $url . $dispatcher->createUrl(
'supplier_rule',
$idLang,
['id' => (int) $supplier, 'rewrite' => (string) $alias],
$this->allow,
'',
$idShop
);
}
$supplier = new Supplier($supplier, $idLang);
}
// Set available keywords
$params = [];
$params['id'] = $supplier->id;
$params['rewrite'] = (!$alias) ? $supplier->link_rewrite : $alias;
$params['meta_keywords'] = Tools::str2url($supplier->meta_keywords);
$params['meta_title'] = Tools::str2url($supplier->meta_title);
return $url . $dispatcher->createUrl('supplier_rule', $idLang, $params, $this->allow, '', $idShop);
}
/**
* Create a link to a manufacturer.
*
* @param Manufacturer|int $manufacturer Manufacturer object (can be an ID supplier, but deprecated)
* @param string|null $alias
* @param int|null $idLang
* @param int|null $idShop
* @param bool $relativeProtocol
*
* @return string
*/
public function getManufacturerLink(
$manufacturer,
$alias = null,
$idLang = null,
$idShop = null,
$relativeProtocol = false
) {
if (!$idLang) {
$idLang = Context::getContext()->language->id;
}
$url = $this->getBaseLink($idShop, null, $relativeProtocol) . $this->getLangLink($idLang, null, $idShop);
$dispatcher = Dispatcher::getInstance();
if (!is_object($manufacturer)) {
if ($alias !== null && !$dispatcher->hasKeyword('manufacturer_rule', $idLang, 'meta_keywords', $idShop) && !$dispatcher->hasKeyword('manufacturer_rule', $idLang, 'meta_title', $idShop)) {
return $url . $dispatcher->createUrl('manufacturer_rule', $idLang, ['id' => (int) $manufacturer, 'rewrite' => (string) $alias], $this->allow, '', $idShop);
}
$manufacturer = new Manufacturer($manufacturer, $idLang);
}
// Set available keywords
$params = [];
$params['id'] = $manufacturer->id;
$params['rewrite'] = (!$alias) ? $manufacturer->link_rewrite : $alias;
$params['meta_keywords'] = Tools::str2url($manufacturer->meta_keywords);
$params['meta_title'] = Tools::str2url($manufacturer->meta_title);
return $url . $dispatcher->createUrl('manufacturer_rule', $idLang, $params, $this->allow, '', $idShop);
}
/**
* Create a link to a module.
*
* @since 1.5.0
*
* @param string $module Module name
* @param string $controller
* @param array $params
* @param bool|null $ssl
* @param int|null $idLang
* @param int|null $idShop
* @param bool $relativeProtocol
*
* @return string
*/
public function getModuleLink(
$module,
$controller = 'default',
array $params = [],
$ssl = null,
$idLang = null,
$idShop = null,
$relativeProtocol = false
) {
if (!$idLang) {
$idLang = Context::getContext()->language->id;
}
$url = $this->getBaseLink($idShop, $ssl, $relativeProtocol) . $this->getLangLink($idLang, null, $idShop);
// Set available keywords
$params['module'] = $module;
$params['controller'] = $controller ? $controller : 'default';
// If the module has its own route ... just use it !
if (Dispatcher::getInstance()->hasRoute('module-' . $module . '-' . $controller, $idLang, $idShop)) {
return $this->getPageLink('module-' . $module . '-' . $controller, $ssl, $idLang, $params, false, $idShop);
} else {
return $url . Dispatcher::getInstance()->createUrl('module', $idLang, $params, $this->allow, '', $idShop);
}
}
/**
* Use controller name to create a link.
*
* Warning on fallback to Symfony Router, this exceptions can be thrown
* - RouteNotFoundException If the named route doesn't exist
* - MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
* - InvalidParameterException When a parameter value for a placeholder is not correct because it does not match the requirement
*
* @param string $controller
* @param bool $withToken include or not the token in the url
* @param array $sfRouteParams (Since 1.7.0.0) Optional parameters to use into New architecture specific cases. If these specific cases should redirect to legacy URLs, then this parameter is used to complete GET query string
* @param array $params (Since 1.7.0.3) Optional
*
* @return string url
*/
public function getAdminLink($controller, $withToken = true, $sfRouteParams = [], $params = [])
{
// Cannot generate admin link from front
if (!defined('_PS_ADMIN_DIR_')) {
return '';
}
if (!is_array($sfRouteParams)) {
$sfRouteParams = [];
}
if ($withToken && !TokenInUrls::isDisabled()) {
$params['token'] = Tools::getAdminTokenLite($controller);
}
// Even if URL rewriting is not enabled, the page handled by Symfony must work !
// For that, we add an 'index.php' in the URL before the route
$sfContainer = SymfonyContainer::getInstance();
$sfRouter = null;
$legacyUrlConverter = null;
if (null !== $sfContainer) {
/** @var UrlGeneratorInterface $sfRouter */
$sfRouter = $sfContainer->get('router');
/** @var LegacyUrlConverter $legacyUrlConverter */
$legacyUrlConverter = $sfContainer->get('prestashop.bundle.routing.converter.legacy_url_converter');
}
if (!empty($sfRouteParams['route']) && null !== $sfRouter) {
$sfRoute = $sfRouteParams['route'];
unset($sfRouteParams['route']);
return $sfRouter->generate($sfRoute, $sfRouteParams, UrlGeneratorInterface::ABSOLUTE_URL);
}
$routeName = '';
switch ($controller) {
case 'AdminTranslations':
// In case of email body translations we want to get a link to legacy controller,
// in other cases - it's the migrated controller
if (isset($params['selected-emails']) && $params['selected-emails'] === 'body') {
break;
}
// In case of modules translations get a link to legacy controller
if (isset($params['type']) && $params['type'] === 'modules' && isset($params['module'])) {
break;
}
// When params are empty or only token exists we want to use default translations route.
if (empty($params) || 1 === count($params) && isset($params['token'])) {
$routeName = 'admin_international_translations_show_settings';
}
break;
case 'AdminEmployees':
if (!isset($params['action'])) {
break;
}
if ('toggleMenu' === $params['action']) {
// Linking legacy toggle menu action to migrated action.
$routeName = 'admin_employees_toggle_navigation';
} elseif ('formLanguage' === $params['action']) {
// Linking legacy change form language action to migrated action.
$routeName = 'admin_employees_change_form_language';
}
break;
}
if (!empty($routeName) && null !== $sfRouter) {
$sfRoute = array_key_exists('route', $sfRouteParams) ? $sfRouteParams['route'] : $routeName;
return $sfRouter->generate($sfRoute, $sfRouteParams, UrlGeneratorInterface::ABSOLUTE_URL);
}
if (empty($routeName) && null !== $legacyUrlConverter) {
try {
$conversionParameters = array_merge(['controller' => $controller], $sfRouteParams, $params);
unset($conversionParameters['token']);
return $legacyUrlConverter->convertByParameters($conversionParameters);
} catch (CoreException $e) {
// The url could not be converted so we fallback on legacy url
}
}
$idLang = Context::getContext()->language->id;
return $this->getAdminBaseLink() . basename(_PS_ADMIN_DIR_) . '/' . Dispatcher::getInstance()->createUrl($controller, $idLang, $params);
}
/**
* Warning on fallback to Symfony Router, this exceptions can be thrown
* - RouteNotFoundException If the named route doesn't exist
* - MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
* - InvalidParameterException When a parameter value for a placeholder is not correct because it does not match the requirement
*
* @param array $tab
*
* @return string
*/
public function getTabLink(array $tab)
{
if (!empty($tab['route_name'])) {
$sfContainer = SymfonyContainer::getInstance();
if (null !== $sfContainer) {
/** @var UrlGeneratorInterface $sfRouter */
$sfRouter = $sfContainer->get('router');
return $sfRouter->generate($tab['route_name']);
}
}
return $this->getAdminLink($tab['class_name']);
}
/**
* Used when you explicitly want to create a LEGACY admin link, this should be deprecated
* in 9.x
*
* @param string $controller
* @param bool $withToken
* @param array $params
*
* @return string
*/
public function getLegacyAdminLink($controller, $withToken = true, $params = [])
{
$idLang = Context::getContext()->language->id;
if ($withToken && !TokenInUrls::isDisabled()) {
$params['token'] = Tools::getAdminTokenLite($controller);
}
return $this->getAdminBaseLink() . basename(_PS_ADMIN_DIR_) . '/' . Dispatcher::getInstance()->createUrl($controller, $idLang, $params);
}
/**
* @param int|null $idShop
* @param bool|null $ssl
* @param bool $relativeProtocol
*
* @return string
*/
public function getAdminBaseLink($idShop = null, $ssl = null, $relativeProtocol = false)
{
if (null === $ssl) {
$ssl = Configuration::get('PS_SSL_ENABLED');
}
if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE')) {
if (null === $idShop) {
$idShop = $this->getMatchingUrlShopId();
}
// Use the matching shop if present, or fallback on the default one
if (null !== $idShop) {
$shop = new Shop($idShop);
} else {
$shop = new Shop((int) Configuration::get('PS_SHOP_DEFAULT'));
}
} else {
$shop = Context::getContext()->shop;
}
if ($relativeProtocol) {
$base = '//' . ($ssl && $this->ssl_enable ? $shop->domain_ssl : $shop->domain);
} else {
$base = (($ssl && $this->ssl_enable) ? 'https://' . $shop->domain_ssl : 'http://' . $shop->domain);
}
return $base . $shop->getBaseURI();
}
/**
* Search for a shop whose domain matches the current url.
*
* @return int|null
*/
public function getMatchingUrlShopId()
{
if (null === $this->urlShopId) {
$host = Tools::getHttpHost();
$request_uri = rawurldecode($_SERVER['REQUEST_URI']);
$sql = 'SELECT s.id_shop, CONCAT(su.physical_uri, su.virtual_uri) AS uri, su.domain, su.main
FROM ' . _DB_PREFIX_ . 'shop_url su
LEFT JOIN ' . _DB_PREFIX_ . 'shop s ON (s.id_shop = su.id_shop)
WHERE (su.domain = \'' . pSQL($host) . '\' OR su.domain_ssl = \'' . pSQL($host) . '\')
AND s.active = 1
AND s.deleted = 0
ORDER BY LENGTH(CONCAT(su.physical_uri, su.virtual_uri)) DESC';
try {
$result = Db::getInstance()->executeS($sql);
} catch (PrestaShopDatabaseException $e) {
return null;
}
foreach ($result as $row) {
// A shop matching current URL was found
if (preg_match('#^' . preg_quote($row['uri'], '#') . '#i', $request_uri)) {
$this->urlShopId = $row['id_shop'];
break;
}
}
}
return $this->urlShopId;
}
/**
* Returns a link to a product image for display
* Note: image filesystem stores product images in subdirectories of img/p/.
*
* @param string $name Rewrite link of the image
* @param string|int $idImage numeric ID of product image or a name of default image like "fr-default"
* @param string|null $type Image thumbnail name (small_default, medium_default, large_default, etc.)
* @param string $extension What image extension should the link point to
*
* @return string
*/
public function getImageLink($name, $idImage, $type = null, string $extension = 'jpg')
{
$type = ($type ? '-' . $type : '');
$idImage = (string) $idImage;
// Default image like "fr-default"
if (strpos($idImage, 'default') !== false) {
$theme = ((Shop::isFeatureActive() && file_exists(_PS_PRODUCT_IMG_DIR_ . $idImage . $type . '-' . Context::getContext()->shop->theme_name . '.jpg')) ? '-' . Context::getContext()->shop->theme_name : '');
$uriPath = _THEME_PROD_DIR_ . $idImage . $type . $theme . '.' . $extension;
// Regular image with numeric ID
} else {
// We will still process the old way of requesting images in a form of productID-imageID, but notify developers
if (strpos($idImage, '-')) {
$idImage = explode('-', $idImage)[1];
if (_PS_MODE_DEV_) {
trigger_error(
'Passing image identifier in the old format is deprecated, use only image ID. This fallback will be removed in next major.',
E_USER_DEPRECATED
);
}
}
$theme = ((Shop::isFeatureActive() && file_exists(_PS_PRODUCT_IMG_DIR_ . Image::getImgFolderStatic($idImage) . $idImage . $type . '-' . (int) Context::getContext()->shop->theme_name . '.jpg')) ? '-' . Context::getContext()->shop->theme_name : '');
// If friendly URLs are enabled
if ($this->allow) {
$uriPath = __PS_BASE_URI__ . $idImage . $type . $theme . '/' . $name . '.' . $extension;
// If friendly URLs are disabled
} else {
$uriPath = _THEME_PROD_DIR_ . Image::getImgFolderStatic($idImage) . $idImage . $type . $theme . '.' . $extension;
}
}
return $this->getMediaLink($uriPath);
}
/**
* Returns a link to a supplier image for display.
*
* @param int $idSupplier
* @param string|null $type Image thumbnail name (small_default, medium_default, large_default, etc.)
* @param string $extension What image extension should the link point to
*
* @return string
*/
public function getSupplierImageLink($idSupplier, $type = null, string $extension = 'jpg')
{
$idSupplier = (int) $idSupplier;