Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
styleimg.php
1<?php
2namespace Bitrix\Landing\Node;
3
9
13class StyleImg extends Node
14{
15 public const STYLES_WITH_IMAGE = [
16 'background',
17 'block-default',
18 'block-border',
19 ];
20
21 protected const STYLES_VARIABLES_WITH_FILES = [
22 '1x' => [
23 '--bg-url' => "url('#url#')",
24 ],
25 '2x' => [
26 '--bg-url-2x' => "url('#url#')",
27 ],
28 ];
29 protected const STYLES_URL_MARKER = '#url#';
30 protected const STYLES_URL_REGEXP = '/url\‍([\'"]?([^\'")]+)[\'")]?\‍)/i';
31 protected const STYLES_NODE_CLASS = 'g-bg-image';
32
37 public static function getHandlerJS()
38 {
39 return 'BX.Landing.Block.Node.StyleImg';
40 }
41
49 public static function saveNode(Block $block, $selector, array $data): void
50 {
51 if (!self::isCorrectNodeType($block, $selector))
52 {
53 Img::saveNode($block, $selector, $data);
54 }
55 else
56 {
57 $resultList = Node\Style::getNodesBySelector($block, $selector);
58 $files = null;
59
60 foreach ($data as $pos => $value)
61 {
62 $id = isset($value['id']) ? (int)$value['id'] : 0;
63 $id2x = isset($value['id2x']) ? (int)$value['id2x'] : 0;
64
65 // check permissions to this file ids
66 if ($id || $id2x)
67 {
68 if ($files === null)
69 {
70 $files = File::getFilesFromBlock($block->getId());
71 }
72 if (!in_array($id, $files))
73 {
74 $id = 0;
75 }
76 if (!in_array($id2x, $files))
77 {
78 $id2x = 0;
79 }
80 }
81
82 if (isset($resultList[$pos]))
83 {
84 // update in content
85 if ($resultList[$pos]->getTagName() !== 'IMG')
86 {
87 $styles = DOM\StyleInliner::getStyle($resultList[$pos]);
88 if (isset($styles['--bg-url']))
89 {
90 $styles['background-image'] = '';
91 }
92 if (!isset($styles['background']))
93 {
94 if ($id)
95 {
96 $stylesChanged = false;
97 $resultList[$pos]->setAttribute('data-fileid', $id);
98 foreach (self::STYLES_VARIABLES_WITH_FILES['1x'] as $var => $pattern)
99 {
100 if (isset($styles[$var]))
101 {
102 $fileArray = \CFile::GetFileArray($id);
103 $styles[$var] = str_replace(
104 self::STYLES_URL_MARKER,
105 $fileArray['SRC'],
106 $pattern
107 );
108 $stylesChanged = true;
109 }
110 }
111
112 if ($id2x)
113 {
114 $resultList[$pos]->setAttribute('data-fileid2x', $id2x);
115 foreach (self::STYLES_VARIABLES_WITH_FILES['2x'] as $var => $pattern)
116 {
117 if (isset($styles[$var]))
118 {
119 $fileArray = \CFile::GetFileArray($id2x);
120 $styles[$var] = str_replace(
121 self::STYLES_URL_MARKER,
122 $fileArray['SRC'],
123 $pattern
124 );
125 $stylesChanged = true;
126 }
127 }
128 }
129
130 if (!$stylesChanged)
131 {
132 $classList = $resultList[$pos]->getAttribute('class');
133 if (!stripos($classList, self::STYLES_NODE_CLASS))
134 {
135 $classList .= ' ' . self::STYLES_NODE_CLASS;
136 }
137 $resultList[$pos]->setAttribute('class', $classList);
138 $fileArray1x = \CFile::GetFileArray($id);
139 $src1x = $fileArray1x['SRC'];
140 $styles['--bg-url'] = "url('{$src1x}');";
141 if ($id2x <= 0 && $id > 0)
142 {
143 $id2x = $id;
144 }
145 $fileArray2x = \CFile::GetFileArray($id2x);
146 $src2x = $fileArray2x['SRC'];
147 $styles['--bg-url-2x'] = "url('{$src2x}');";
148 $stylesChanged = true;
149 }
150
151 if ($stylesChanged)
152 {
153 DOM\StyleInliner::setStyle($resultList[$pos], $styles);
154 }
155 }
156 else
157 {
158 foreach (['fileid', 'fileid2x'] as $dataCode)
159 {
160 $attribute = 'data-' . $dataCode;
161 $oldId = $resultList[$pos]->getAttribute($attribute);
162 if ($oldId > 0)
163 {
165 $block->getId(),
166 $oldId
167 );
168 }
169 }
170 }
171 }
172 }
173 }
174 }
175 }
176 }
177
184 public static function getNode(Block $block, $selector): array
185 {
186 if (!self::isCorrectNodeType($block, $selector))
187 {
188 $data = Img::getNode($block, $selector);
189 }
190 else
191 {
192 $data = [];
193 $resultList = Node\Style::getNodesBySelector($block, $selector);
194
195 foreach ($resultList as $pos => $res)
196 {
197 $data[$pos] = [
198 'src' => '',
199 'src2x' => '',
200 'id' => null,
201 'id2x' => null,
202 'alt' => '',
203 'isLazy' => 'N',
204 ];
205
206 if ($res->getTagName() !== 'IMG')
207 {
208 $isLazy =
209 $res->getAttribute('data-lazy-styleimg')
210 && $res->getAttribute('data-lazy-styleimg') === 'Y'
211 ;
212 $nodeData =
213 $isLazy
215 : self::getNodeData($res)
216 ;
217 if ($nodeData)
218 {
219 $files = File::getFilesFromBlock($block->getId());
220 if (!in_array($nodeData['id'], $files))
221 {
222 continue;
223 }
224 if ($nodeData['id2x'] && !in_array($nodeData['id'], $files))
225 {
226 unset($nodeData['id2x'], $nodeData['src2x'], $nodeData['lazyOrigSrc2x']);
227 }
228
229 $data[$pos] = $nodeData;
230 }
231 }
232 }
233 }
234
235 return $data;
236 }
237
238 protected static function getNodeData(DOM\Node $node): ?array
239 {
240 $data = null;
241
242 $styles = DOM\StyleInliner::getStyle($node);
243 if (
244 (!isset($styles['background']) || $styles['background'] === '')
245 && (!isset($styles['background-image']) || $styles['background-image'] === '')
246 )
247 {
248 $fileId = (int)$node->getAttribute('data-fileid');
249 if ($fileId)
250 {
251 $data = [];
252 $data['id'] = $fileId;
253 $data['src'] = self::getSrcFromStyles($styles, '1x');
254
255 $fileId2x = (int)$node->getAttribute('data-fileid2x');
256 if ($fileId2x)
257 {
258 $data['id2x'] = $fileId2x;
259 $data['src2x'] = self::getSrcFromStyles($styles, '2x');
260 }
261 }
262 }
263
264 return $data;
265 }
266
267 protected static function getNodeDataLazy(DOM\Node $node): ?array
268 {
269 $data = null;
270
271 $styles = $node->getAttribute('data-style');
272 if ($styles)
273 {
274 $stylesConverted = [];
275 foreach (explode(';', $styles) as $style)
276 {
277 // can has mode then one ':', can't use explode!
278 $separator = strpos( $style, ':');
279 $key = substr($style, 0, $separator);
280 $val = substr($style, $separator + 1);
281 $stylesConverted[$key] = trim($val);
282 }
283
284 $fileId = (int)$node->getAttribute('data-fileid');
285 if ($fileId)
286 {
287 $data = ['isLazy' => 'Y'];
288
289 $data['id'] = $fileId;
290 $data['lazyOrigSrc'] = self::getSrcFromStyles($stylesConverted, '1x');
291
292 $fileId2x = (int)$node->getAttribute('data-fileid2x');
293 if ($fileId2x)
294 {
295 $data['id2x'] = $fileId2x;
296 $data['lazyOrigSrc2x'] = self::getSrcFromStyles($stylesConverted, '2x');
297 }
298 }
299 }
300
301 return $data;
302 }
303
310 protected static function getSrcFromStyles(array $styles, string $resolution): ?string
311 {
312 if (array_key_exists($resolution, self::STYLES_VARIABLES_WITH_FILES))
313 {
314 foreach (self::STYLES_VARIABLES_WITH_FILES[$resolution] as $var => $pattern)
315 {
316 $matches = [];
317 if (
318 $styles[$var]
319 && preg_match(self::STYLES_URL_REGEXP, $styles[$var], $matches)
320 && $matches[1]
321 )
322 {
323 return Manager::getUrlFromFile($matches[1]);
324 }
325 }
326 }
327
328 return null;
329 }
330
337 public static function getSearchableNode(Block $block, string $selector): array
338 {
339 if (!self::isCorrectNodeType($block, $selector))
340 {
341 $data = Img::getSearchableNode($block, $selector);
342 }
343 else
344 {
345 $data = [];
346 }
347 return $data;
348 }
349
356 protected static function isCorrectNodeType(Block $block, string $selector): bool
357 {
358 $matches = [];
359 $pattern = '/' . substr($selector, 1) . '[^\"]*/i';
360 if (preg_match($pattern, $block->getContent(), $matches) === 1)
361 {
362 $pattern = '/[\s]?' . self::STYLES_NODE_CLASS . '[\s]?/i';
363 if (preg_match($pattern, $matches[0]) === 1)
364 {
365 return true;
366 }
367 }
368 return false;
369 }
370}
static getFilesFromBlock($blockId)
Definition file.php:412
static deleteFromBlock($blockId, $fileId=array())
Definition file.php:363
static getUrlFromFile($file)
Definition manager.php:1067
static getSearchableNode($block, $selector)
Definition img.php:370
static getNode(\Bitrix\Landing\Block $block, $selector)
Definition img.php:221
static saveNode(\Bitrix\Landing\Block $block, $selector, array $data)
Definition img.php:29
static isCorrectNodeType(Block $block, string $selector)
Definition styleimg.php:356
static getSrcFromStyles(array $styles, string $resolution)
Definition styleimg.php:310
static getSearchableNode(Block $block, string $selector)
Definition styleimg.php:337
static getNode(Block $block, $selector)
Definition styleimg.php:184
static getNodeDataLazy(DOM\Node $node)
Definition styleimg.php:267
static getNodeData(DOM\Node $node)
Definition styleimg.php:238
static saveNode(Block $block, $selector, array $data)
Definition styleimg.php:49