Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chatsalesorder.php
1<?php
2
4
10
11Loader::includeModule('landing');
12Loader::includeModule('salescenter');
13
14Loc::loadMessages(__FILE__);
15Loc::loadMessages(Manager::getDocRoot() . '/bitrix/components/bitrix/landing.demo/data/page/store-chats-dark/catalog/.description.php');
16
18{
22 private const ONLY_CODES = [
23 'store-chats-dark',
24 'store-chats-light',
25 'store-chats',
26 ];
27
28 private const PAGE_CODE = 'store-chats-dark/order';
29
30 private static function getExistsPageId(int $siteId, string $code): ?int
31 {
32 $res = Landing::getList([
33 'select' => [
34 'ID'
35 ],
36 'filter' => [
37 'SITE_ID' => $siteId,
38 '=TPL_CODE' => $code,
39 '=PUBLIC' => 'Y',
40 ],
41 ]);
42 if ($page = $res->fetch())
43 {
44 return $page['ID'];
45 }
46 return null;
47 }
48 private static function getLandingHooks(string $pageId): ?array
49 {
50 $res = Internals\HookDataTable::getList([
51 'select' => [
52 'ID', 'HOOK', 'CODE', 'VALUE', 'PUBLIC'
53 ],
54 'filter' => [
55 '=ENTITY_TYPE' => 'L',
56 'ENTITY_ID' => $pageId,
57 ],
58 ]);
59 if ($rows = $res->fetchAll())
60 {
61 return $rows;
62 }
63 return null;
64 }
65
66 private static function getLandingBlocks(string $pageId): ?array
67 {
68 $res = Internals\BlockTable::getList([
69 'select' => [
70 'ID', 'CODE', 'CONTENT'
71 ],
72 'filter' => [
73 'LID' => $pageId,
74 ],
75 ]);
76 if ($rows = $res->fetchAll())
77 {
78 return $rows;
79 }
80 return null;
81 }
82
83 private static function getNewLandingBlocksData(): array
84 {
85 return [
86 'store.salescenter.order.details' => [
87 'COMPONENT_PARAMS' => [
88 'TEMPLATE_MODE' => 'graymode',
89 ],
90 ],
91 '61.1.phone_w_btn_rght' => [
92 'COMPONENT_PARAMS' => [
93 'TEMPLATE_MODE' => 'graymode',
94 'BUTTON_CLASSES' => 'btn g-rounded-50 g-btn-type-outline g-btn-px-l g-btn-size-md g-btn-darkgray text-uppercase',
95 ],
96 'CLASSES' => [
97 'landing-block g-pt-20 g-pb-0 g-bg-transparent u-block-border-none',
98 ],
99 ],
100 '26.separator' => [
101 'CONTENT' => '<section class="landing-block g-bg-transparent g-pt-20 g-pb-10" style="">'
102 . PHP_EOL . '<hr class="landing-block-line g-brd-gray-dark-v2 my-0" style="" />'
103 . PHP_EOL . '</section>',
104 ],
105 ];
106 }
107
108 private static function prepareLandingBlocks($currentBlocksData, $newBlocksData): void
109 {
110 $newBlocksDataFiltered = array_filter($newBlocksData, static function($newBlockData) {
111 return isset($newBlockData['CONTENT']) || isset($newBlockData['COMPONENT_PARAMS']) || isset($newBlockData['CLASSES']);
112 });
113
114 $currentBlocksDataProcessed = array_map(static function($currentBlockData) use ($newBlocksDataFiltered) {
115 foreach ($newBlocksDataFiltered as $codeBlock => $newBlockData)
116 {
117 if ($codeBlock === $currentBlockData['CODE'])
118 {
119 if (isset($newBlockData['CONTENT']))
120 {
121 $currentBlockData['CONTENT'] = $newBlockData['CONTENT'];
122 }
123 if (isset($newBlockData['COMPONENT_PARAMS']))
124 {
125 foreach ($newBlockData['COMPONENT_PARAMS'] as $nameParam => $valueParam)
126 {
127 $newParamsString = PHP_EOL . '"' . $nameParam . '" => "' . $valueParam . '",';
128 $pattern = '/["\']' . $nameParam . '["\'][\s=>]*[^,]*[,?]/';
129 if (preg_match($pattern, $currentBlockData['CONTENT']) === 1)
130 {
131 $currentBlockData['CONTENT'] = preg_replace($pattern, $newParamsString, $currentBlockData['CONTENT']);
132 }
133 else
134 {
135 $pattern = '/(["\'].*["\']\s*=>\s*[^,]*[,?])/';
136 $currentBlockData['CONTENT'] = preg_replace($pattern, '${1}' . $newParamsString, $currentBlockData['CONTENT'], 1);
137 }
138 }
139 }
140 if (isset($newBlockData['CLASSES']))
141 {
142 $pos = strpos($newBlockData['CLASSES'][0], ' ');
143 $string = substr($newBlockData['CLASSES'][0], 0, $pos + 1);
144 $pattern = '/class="'. $string . '.*"/';
145 $replacement = 'class="' . $newBlockData['CLASSES'][0] . '"';
146 $currentBlockData['CONTENT'] = preg_replace($pattern, $replacement, $currentBlockData['CONTENT']);
147 }
148 return $currentBlockData;
149 }
150 }
151 return $currentBlockData;
152 }, $currentBlocksData);
153
154 $preparedBlocksData = array_filter($currentBlocksDataProcessed);
155
156 //update
157 foreach ($preparedBlocksData as $preparedBlockData)
158 {
159 Internals\BlockTable::update(
160 $preparedBlockData['ID'],
161 ['CONTENT' => $preparedBlockData['CONTENT']]
162 );
163 }
164 }
165
166 private static function getNewLandingHooks(): array
167 {
168 return [
169 [
170 'HOOK' => 'BACKGROUND',
171 'CODE' => 'USE',
172 'VALUE' => 'Y',
173 ],
174 [
175 'HOOK' => 'BACKGROUND',
176 'CODE' => 'COLOR',
177 'VALUE' => '#1c1c22',
178 ],
179 [
180 'HOOK' => 'BACKGROUND',
181 'CODE' => 'PICTURE',
182 'VALUE' => 'https://cdn.bitrix24.site/bitrix/images/landing/bg/store-chat-gray.jpg',
183 ],
184 [
185 'HOOK' => 'BACKGROUND',
186 'CODE' => 'POSITION',
187 'VALUE' => 'no_repeat',
188 ],
189 [
190 'HOOK' => 'CSSBLOCK',
191 'CODE' => 'USE',
192 'VALUE' => 'Y',
193 ],
194 [
195 'HOOK' => 'CSSBLOCK',
196 'CODE' => 'CODE',
197 'VALUE' => '.landing-viewtype--mobile .landing-public-mode {outline: none;}',
198 ],
199 ];
200 }
201
202 private static function prepareLandingHooks($currentHooks, $newHooks, $pageId): void
203 {
204 $isUpdateBgHooks = true;
205 foreach ($currentHooks as $currentHookData)
206 {
207 if (
208 $currentHookData['HOOK'] === 'BACKGROUND'
209 && $currentHookData['CODE'] === 'USE'
210 && $currentHookData['VALUE'] === 'Y'
211 )
212 {
213 $isUpdateBgHooks = false;
214 }
215 }
216
217 $updateHooksData = [];
218 $createHooksData = [];
219 foreach ($newHooks as $hookData)
220 {
221 if ($hookData['HOOK'] === 'BACKGROUND' && $isUpdateBgHooks === false)
222 {
223 continue;
224 }
225
226 $isExistPublicHook = false;
227 $isExistUnPublicHook = false;
228 foreach ($currentHooks as $currentHookData)
229 {
230 if (
231 $hookData['HOOK'] === $currentHookData['HOOK']
232 && $hookData['CODE'] === $currentHookData['CODE']
233 )
234 {
235 if ($hookData['VALUE'] !== $currentHookData['VALUE'])
236 {
237 $updateHooksData[] = [
238 'ID' => $currentHookData['ID'],
239 'VALUE' => $hookData['VALUE'],
240 ];
241 }
242 $isPublicHook = $currentHookData['PUBLIC'];
243 if ($isPublicHook === 'Y')
244 {
245 $isExistPublicHook = true;
246 }
247 if ($isPublicHook === 'N')
248 {
249 $isExistUnPublicHook = true;
250 }
251 }
252 }
253 $isNeedCreateHook = false;
254 if (!$isExistPublicHook)
255 {
256 $isNeedCreateHook = true;
257 $isPublic = 'Y';
258 }
259 if (!$isExistUnPublicHook)
260 {
261 $isNeedCreateHook = true;
262 $isPublic = 'N';
263 }
264 if ($isNeedCreateHook === true && isset($isPublic))
265 {
266 $createHooksData[] = [
267 'ENTITY_ID' => $pageId,
268 'ENTITY_TYPE' => 'L',
269 'HOOK' => $hookData['HOOK'],
270 'CODE' => $hookData['CODE'],
271 'VALUE' => $hookData['VALUE'],
272 'PUBLIC' => $isPublic,
273 ];
274 }
275 }
276
277 //update
278 foreach ($updateHooksData as $updateHookData)
279 {
280 Internals\HookDataTable::update(
281 $updateHookData['ID'],
282 ['VALUE' => $updateHookData['VALUE']]
283 );
284 }
285 //create
286 foreach ($createHooksData as $createHookData)
287 {
288 Internals\HookDataTable::add([
289 'ENTITY_ID' => $createHookData['ENTITY_ID'],
290 'ENTITY_TYPE' => $createHookData['ENTITY_TYPE'],
291 'HOOK' => $createHookData['HOOK'],
292 'CODE' => $createHookData['CODE'],
293 'VALUE' => $createHookData['VALUE'],
294 'PUBLIC' => $createHookData['PUBLIC'],
295 ]);
296 }
297 }
298
304 public static function update(int $siteId): bool
305 {
306 $site = self::getId($siteId);
307
308 if (!$site || !in_array($site['TPL_CODE'], self::ONLY_CODES, true))
309 {
310 return true;
311 }
312
313 $orderPageId = self::getExistsPageId($siteId, self::PAGE_CODE);
314 if ($orderPageId)
315 {
316 $currentHooks = self::getLandingHooks($orderPageId);
317 $newHooks = self::getNewLandingHooks();
318 self::prepareLandingHooks($currentHooks, $newHooks, $orderPageId);
319
320 $currentBlocks = self::getLandingBlocks($orderPageId);
321 $newBlocksData = self::getNewLandingBlocksData();
322 self::prepareLandingBlocks($currentBlocks, $newBlocksData);
323 }
324
325 return true;
326 }
327}
static getId(int $siteId)
Definition update.php:18
static loadMessages($file)
Definition loc.php:64
static getList(array $parameters=array())