1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
TaskUpdateBlock.php
См. документацию.
1<?php
2declare(strict_types=1);
3
4namespace Bitrix\Landing\Copilot\Generation\Step;
5
6use Bitrix\Landing\Copilot\Data;
7use Bitrix\Landing\Hook\Page\Fonts;
8use Bitrix\Landing\Landing;
9use Bitrix\Landing\PublicAction\Block;
10use Bitrix\Landing\Rights;
11
13{
20
26 protected int $landingId;
27
33 public function execute(): bool
34 {
35 parent::execute();
36
38 $landingInstance = $this->siteData->getLandingInstance();
39 if ($landingInstance !== null)
40 {
41 $this->landingInstance = $landingInstance;
42 $this->landingId = $this->landingInstance->getId();
43 $this->updateBlocks();
44 }
46
47 return true;
48 }
49
55 protected function updateBlocks(): void
56 {
57 foreach ($this->siteData->getBlocks() as $block)
58 {
59 $this->updateBlock($block);
60 }
61 }
62
70 private function updateBlock(Data\Block $blockData): void
71 {
72 $blockId = $blockData->getId();
73
74 if (!$blockId || $blockId <= 0)
75 {
76 return;
77 }
78
79 $blockInstance = $this->landingInstance->getBlockById($blockId);
80 $blockNodes = $blockData->getNodes();
81 if ($blockInstance && $blockNodes)
82 {
83 $nodesArray = [];
84 foreach ($blockNodes as $blockNode)
85 {
86 $code = $blockNode->getCode();
87 $nodesArray[$code] = $blockNode->getValues();
88 }
89 $blockInstance->updateNodes($nodesArray);
90 $blockInstance->save();
91
92 $this->updateBlockStyles($blockInstance, $blockData);
93 }
94 }
95
96 private function updateBlockStyles(\Bitrix\Landing\Block $blockInstance, Data\Block $blockData): void
97 {
98 $styles = [];
99
100 $styles = $this->updateBackgroundStyles($styles, $blockData, $blockInstance);
101 $styles = $this->updateNodeStyles($styles, $blockData, $blockInstance);
102
103 $this->applyStylesToBlock($blockData, $styles);
104 }
105
106 private function updateBackgroundStyles(
107 array $styles,
108 Data\Block $blockData,
109 \Bitrix\Landing\Block $blockInstance
110 ): array
111 {
112 $updatedStyles = $styles;
113 $blockStyles = $blockData->getStyles();
114
115 $styleManifest = $blockInstance->getManifest()['style'] ?? [];
116 $typesWithBackground = [
117 'background',
118 'block-default',
119 'block-default-background',
120 'block-default-background-height-vh',
121 'block-border',
122 ];
123
124 if (
125 isset($styleManifest['block']['type'])
126 && !empty(
127 array_intersect(
128 $typesWithBackground,
129 $styleManifest['block']['type']
130 )
131 )
132 )
133 {
134 $wrapperSelector = '#block' . $blockInstance->getId();
135 $blockContent = $blockInstance->getContent();
136 $wrapperSelectorClasses = explode(' ', Data\Block\Operator::extractWrapperClasses($blockContent));
137
138 if (
139 isset($blockStyles['background']) && $blockStyles['background'] !== ''
140 && !in_array('g-bg-image', $wrapperSelectorClasses, true)
141 )
142 {
143 $preparedSelectorClasses = [];
144 $bgStyleClass = 'g-bg';
145 foreach ($wrapperSelectorClasses as $class)
146 {
147 if (
148 $class === $bgStyleClass
149 || (!str_starts_with($class, 'g-bg-') && !str_starts_with($class, 'g-theme-'))
150 )
151 {
152 $preparedSelectorClasses[] = $class;
153 }
154 }
155 if (!in_array($bgStyleClass, $preparedSelectorClasses, true))
156 {
157 $preparedSelectorClasses[] = $bgStyleClass;
158 }
159 $updatedStyles[$wrapperSelector] = [
160 'style' => ['--bg' => $blockStyles['background']],
161 'classList' => $preparedSelectorClasses,
162 ];
163 }
164 }
165
166 return $updatedStyles;
167 }
168
169 private function updateNodeStyles(
170 array $styles,
171 Data\Block $blockData,
172 \Bitrix\Landing\Block $blockInstance
173 ): array
174 {
175 $blockStyles = $blockData->getStyles();
176
177 $styleManifest = $blockInstance->getManifest()['style'] ?? [];
178 $nodesWithProperty = [
179 'color' => [],
180 'fontFamily' => [],
181 ];
182
183 if (isset($styleManifest['nodes']))
184 {
185 foreach ($styleManifest['nodes'] as $codeNode => $styleNode)
186 {
187 $nodesWithProperty = $this->processStyleNode(
188 $styleNode,
189 $blockStyles,
190 $codeNode,
191 $nodesWithProperty
192 );
193 }
194 }
195
196 $updatedStyles = $this->applyColorStyles($styles, $blockData, $nodesWithProperty['color'], $blockInstance);
197
198 return $this->applyFontFamilyStyles(
199 $updatedStyles,
200 $blockData,
201 $nodesWithProperty['fontFamily'],
202 $blockInstance
203 );
204 }
205
206 private function processStyleNode(
207 array $styleNode,
208 array $blockStyles,
209 string $codeNode,
210 array $nodesWithProperty,
211 ): array
212 {
213 $updatedNodesWithProperty = $nodesWithProperty;
214
215 if (is_string($styleNode['type']))
216 {
217 $styleNode['type'] = [$styleNode['type']];
218 }
219
220 if (!is_array($styleNode['type']))
221 {
222 return $updatedNodesWithProperty;
223 }
224
225 if (
226 (isset($blockStyles['textsColor']) && $blockStyles['textsColor'] !== '')
227 || (isset($blockStyles['headersColor']) && $blockStyles['headersColor'] !== '')
228 )
229 {
230 $typesWithColor = ['color', 'typo', 'typo-link', 'button'];
231 if (!empty(array_intersect($typesWithColor, $styleNode['type'])))
232 {
233 $updatedNodesWithProperty['color'][] = $codeNode;
234 }
235 }
236
237 if (
238 (isset($blockStyles['textsFontName']) && $blockStyles['textsFontName'] !== '')
239 || (isset($blockStyles['headersFontName']) && $blockStyles['headersFontName'] !== '')
240 )
241 {
242 $typesWithFontFamily = ['color', 'typo', 'typo-link', 'button'];
243 if (!empty(array_intersect($typesWithFontFamily, $styleNode['type'])))
244 {
245 $updatedNodesWithProperty['fontFamily'][] = $codeNode;
246 }
247 }
248
249 return $updatedNodesWithProperty;
250 }
251
252 private function applyColorStyles(
253 array $styles,
254 Data\Block $blockData,
255 array $nodesWithPropertyColor,
256 \Bitrix\Landing\Block $blockInstance
257 ): array
258 {
259 $updatedStyles = $styles;
260 if (empty($nodesWithPropertyColor))
261 {
262 return $updatedStyles;
263 }
264
265 $blockStyles = $blockData->getStyles();
266
267 $styleNodesSelectorClasses = [];
268
269 $blockContent = $blockInstance->getContent();
270 foreach ($nodesWithPropertyColor as $codeNode)
271 {
272 $styleNodesSelectorClasses = $this->prepareNodeSelectorClasses(
273 $styleNodesSelectorClasses,
274 $codeNode,
275 $blockContent,
276 'g-color',
277 ['g-color-'],
278 [],
279 );
280 $updatedStyles[$codeNode]['style']['--color'] = $blockStyles['textsColor'];
281 $updatedStyles[$codeNode]['classList'] = $styleNodesSelectorClasses[$codeNode];
282 }
283
284 return $updatedStyles;
285 }
286
287 private function applyFontFamilyStyles(
288 array $styles,
289 Data\Block $blockData,
290 array $nodesWithPropertyFontFamily,
291 \Bitrix\Landing\Block $blockInstance
292 ): array
293 {
294 $updatedStyles = $styles;
295 if (empty($nodesWithPropertyFontFamily))
296 {
297 return $updatedStyles;
298 }
299
300 $blockStyles = $blockData->getStyles();
301
302 $styleNodesSelectorClasses = [];
303 $blockContent = $blockInstance->getContent();
304 foreach ($nodesWithPropertyFontFamily as $codeNode)
305 {
306 $styleNodesSelectorClasses = $this->prepareNodeSelectorClasses(
307 $styleNodesSelectorClasses,
308 $codeNode,
309 $blockContent,
310 'g-font-' . strtolower(str_replace(' ', '-', $blockStyles['textsFontName'])),
311 ['g-font-'],
312 ['g-font-size-', 'g-font-weight-', 'g-font-style-']
313 );
314 $updatedStyles[$codeNode]['affect'][] = 'font-family';
315 $updatedStyles[$codeNode]['classList'] = $styleNodesSelectorClasses[$codeNode];
316 }
317
318 $content = Fonts::generateFontTags($blockStyles['textsFontName']);
319 $landingId = $blockInstance->getLandingId();
320 \Bitrix\Landing\PublicAction\Landing::updateHead($landingId, $content);
321
322 return $updatedStyles;
323 }
324
325 private function prepareNodeSelectorClasses(
326 array $styleNodesSelectorClasses,
327 string $codeNode,
328 string $blockContent,
329 string $classToAdd,
330 array $setPrefixToRemove,
331 array $setPrefixNotRemove
332 ): array
333 {
334 $preparedStyleNodesSelectorClasses = $styleNodesSelectorClasses;
335
336 if (!isset($preparedStyleNodesSelectorClasses[$codeNode]))
337 {
338 $preparedStyleNodesSelectorClasses[$codeNode] = explode(
339 ' ',
340 Data\Block\Operator::extractNodeClasses(
341 $codeNode,
342 $blockContent
343 )
344 );
345 }
346 $preparedSelectorClasses = [];
347
348 foreach ($preparedStyleNodesSelectorClasses[$codeNode] as $class)
349 {
350 if (
351 is_string($class)
352 && (
353 $class === $classToAdd
354 || !$this->canRemoveClass($class, $setPrefixToRemove, $setPrefixNotRemove)
355 )
356 )
357 {
358 $preparedSelectorClasses[] = $class;
359 }
360 }
361 if (!in_array($classToAdd, $preparedSelectorClasses, true))
362 {
363 $preparedSelectorClasses[] = $classToAdd;
364 }
365
366 $preparedStyleNodesSelectorClasses[$codeNode] = $preparedSelectorClasses;
367
368 return $preparedStyleNodesSelectorClasses;
369 }
370
371 private function applyStylesToBlock(Data\Block $blockData, array $styles): void
372 {
373 $blockId = $blockData->getId();
374
375 if (!$blockId)
376 {
377 return;
378 }
379
380 $blockInstance = $this->landingInstance->getBlockById($blockId);
381 if ($blockInstance)
382 {
383 Block::updateStyles($this->landingId, $blockInstance->getId(), $styles, true);
384 }
385 }
386
387 protected function canRemoveClass(string $class, array $prefixes, array $prefixesNotRemove): bool
388 {
389 foreach ($prefixes as $prefix)
390 {
391 if (!str_starts_with($class, $prefix))
392 {
393 continue;
394 }
395
396 foreach ($prefixesNotRemove as $prefixeNotRemove)
397 {
398 if (str_starts_with($class, $prefixeNotRemove))
399 {
400 return false;
401 }
402 }
403
404 return true;
405 }
406
407 return false;
408 }
409}
canRemoveClass(string $class, array $prefixes, array $prefixesNotRemove)
Определения TaskUpdateBlock.php:387
static setGlobalOn()
Определения rights.php:116
static setGlobalOff()
Определения rights.php:107
$content
Определения commerceml.php:144
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
Определения agent.php:3
Определения aliases.php:105