9
9
namespace Magefan \ProductLabel \Model \Parser ;
10
10
11
11
use Magefan \ProductLabel \Model \GetLabels ;
12
+ use Magefan \ProductLabel \Model \Config ;
12
13
13
14
class Html
14
15
{
15
16
const COMMENT_PREFIX = '<!--mf_product_label_comment_ ' ;
16
17
const COMMENT_PREFIX_GALLERY = '<!--mf_product_label_gallery_comment_ ' ;
17
18
const COMMENT_SUFFIX = '--> ' ;
18
19
20
+
19
21
/**
20
22
* @var GetLabels
21
23
*/
22
24
protected $ getLabels ;
25
+ protected $ mapProductToCustomPosition = [];
23
26
24
27
public function __construct (
25
28
GetLabels $ getLabels
@@ -35,8 +38,8 @@ public function __construct(
35
38
public function execute (string $ output ): string
36
39
{
37
40
$ isOutputIsJson = $ this ->json_validate ($ output );
38
-
39
41
$ productIds = $ this ->getProductIds ($ output );
42
+
40
43
$ currentPageProductId = $ this ->getCurrentPageProductId ($ output );
41
44
$ productIdsForProductPage = [];
42
45
@@ -50,6 +53,9 @@ public function execute(string $output): string
50
53
foreach ($ replaceMap as $ productId => $ replace ) {
51
54
$ replace = $ isOutputIsJson ? trim (json_encode ($ replace ),'" ' ) : $ replace ;
52
55
56
+ // should be above regular replace
57
+ $ this ->replaceForCustomPosition ($ output , $ replace , $ productId );
58
+
53
59
$ output = ($ currentPageProductId && $ currentPageProductId == $ productId )
54
60
? str_replace (self ::COMMENT_PREFIX_GALLERY . $ productId . self ::COMMENT_SUFFIX , $ replace , $ output )
55
61
: str_replace (self ::COMMENT_PREFIX . $ productId . self ::COMMENT_SUFFIX , $ replace , $ output );
@@ -58,6 +64,36 @@ public function execute(string $output): string
58
64
return $ output ;
59
65
}
60
66
67
+ /**
68
+ * @param string $output
69
+ * @param string $replace
70
+ * @param $productId
71
+ * @return void
72
+ */
73
+ private function replaceForCustomPosition (string &$ output , string &$ replace , $ productId )
74
+ {
75
+ $ customPositions = $ this ->mapProductToCustomPosition [$ productId ] ?? [];
76
+
77
+ if (strpos ($ replace , Config::SPLITTERS_FOR_CUSTOM_POSITIONS ) !== false ) {
78
+ if ($ customPositions ) {
79
+ $ customPositionsLabels = explode (Config::SPLITTERS_FOR_CUSTOM_POSITIONS , $ replace );
80
+ $ replace = $ customPositionsLabels [0 ];
81
+ unset($ customPositionsLabels [0 ]);
82
+
83
+ foreach ($ customPositionsLabels as $ label ) {
84
+ foreach ($ customPositions as $ customPosition ) {
85
+ if (strpos ($ label , $ customPosition ) !== false ) {
86
+ $ output = str_replace (self ::COMMENT_PREFIX . $ productId . '____ ' . $ customPosition . self ::COMMENT_SUFFIX , $ label , $ output );
87
+ }
88
+ }
89
+ }
90
+ } else {
91
+ // leave only labels with regular positions
92
+ $ replace = explode (Config::SPLITTERS_FOR_CUSTOM_POSITIONS , $ replace );
93
+ $ replace = $ replace [0 ];
94
+ }
95
+ }
96
+ }
61
97
62
98
/**
63
99
* @param string $html
@@ -68,7 +104,6 @@ private function getCurrentPageProductId(string $html): int
68
104
$ pattern = '/ ' . self ::COMMENT_PREFIX_GALLERY . '(.*?) ' . self ::COMMENT_SUFFIX . '/ ' ;
69
105
preg_match_all ($ pattern , $ html , $ matches );
70
106
71
-
72
107
foreach ($ matches [1 ] as $ commentData ) {
73
108
$ productId = (int )$ commentData ;
74
109
@@ -91,9 +126,18 @@ private function getProductIds(string $html): array
91
126
$ productIds = [];
92
127
93
128
foreach ($ matches [1 ] as $ commentData ) {
94
- $ productId = (int )$ commentData ; //for now commentData=productId
129
+ /* $commentData = '3____product_list' | '3'*/
130
+
131
+ if (is_numeric ($ commentData )) {
132
+ $ productId = (int )$ commentData ;
133
+ } else {
134
+ [$ productId , $ customPositionName ] = explode ('____ ' , $ commentData );
135
+ $ productId = (int )$ productId ;
136
+ $ this ->mapProductToCustomPosition [$ productId ][$ customPositionName ] = $ customPositionName ;
137
+ }
138
+
95
139
if ($ productId ) {
96
- $ productIds [] = $ productId ;
140
+ $ productIds [$ productId ] = $ productId ;
97
141
}
98
142
}
99
143
0 commit comments