Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
img.php
1<?php
2
3namespace Bitrix\Landing\Node;
4
5use \Bitrix\Landing\File;
7use \Bitrix\Landing\Manager;
8use \Bitrix\Main\Web\DOM\StyleInliner;
9use \Bitrix\Landing\Node;
10
12{
17 public static function getHandlerJS()
18 {
19 return 'BX.Landing.Node.Img';
20 }
21
29 public static function saveNode(\Bitrix\Landing\Block $block, $selector, array $data)
30 {
31 $doc = $block->getDom();
32 $resultList = $doc->querySelectorAll($selector);
33 $valueBefore = static::getNode($block, $selector);
34 $files = null;
35
36 foreach ($data as $pos => $value)
37 {
38 // 2x - this for retina support
39 $src = (isset($value['src']) && is_string($value['src'])) ? trim($value['src']) : '';
40 $src2x = (isset($value['src2x']) && is_string($value['src2x'])) ? trim($value['src2x']) : '';
41 $alt = (isset($value['alt']) && is_string($value['alt'])) ? trim($value['alt']) : '';
42 $id = isset($value['id']) ? intval($value['id']) : 0;
43 $id2x = isset($value['id2x']) ? intval($value['id2x']) : 0;
44 $isLazy = isset($value['isLazy']) && $value['isLazy'] === 'Y';
45
46 if ($src)
47 {
48 $src = str_replace('http://', 'https://', $src);
49 }
50 if ($src2x)
51 {
52 $src2x = str_replace('http://', 'https://', $src2x);
53 }
54
55 if (isset($value['url']))
56 {
57 $url = is_array($value['url'])
58 ? json_encode($value['url'])
59 : $value['url'];
60 }
61 else
62 {
63 $url = '';
64 }
65
66 if (isset($resultList[$pos]))
67 {
68 // check permissions to this file ids
69 if ($id || $id2x)
70 {
71 if ($files === null)
72 {
73 $files = File::getFilesFromBlock($block->getId());
74 }
75 if (!in_array($id, $files))
76 {
77 $id = 0;
78 }
79 if (!in_array($id2x, $files))
80 {
81 $id2x = 0;
82 }
83 }
84 // update in content
85 if ($resultList[$pos]->getTagName() !== 'IMG')
86 {
87 $styles = StyleInliner::getStyle($resultList[$pos]);
88 $oldStyles = [];
89 $newStyles = [];
90 // collect existing styles
91 foreach ($styles as $key => $styleValue)
92 {
93 if ($key !== 'background' && $key !== 'background-image')
94 {
95 $oldStyles[] = "{$key}: {$styleValue};";
96 }
97 }
98 // add images to bg
99 if ($src)
100 {
101 // and one two additional bg
102 $newStyles = [
103 "background-image: url('{$src}');",
104 ];
105 if ($src2x)
106 {
107 $newStyles = array_merge(
108 $newStyles,
109 [
110 "background-image: -webkit-image-set(url('{$src}') 1x, url('{$src2x}') 2x);",
111 "background-image: image-set(url('{$src}') 1x, url('{$src2x}') 2x);",
112 ]
113 );
114 }
115 }
116 // or remove exists
117 else
118 {
119 foreach (['fileid', 'fileid2x'] as $dataCode)
120 {
121 $oldId = $resultList[$pos]->getAttribute(
122 'data-' . $dataCode
123 );
124 if ($oldId > 0)
125 {
127 $block->getId(),
128 $oldId
129 );
130 }
131 }
132 }
133
134 $style = array_merge($oldStyles, $newStyles);
135 $style = implode(' ', $style);
136 $resultList[$pos]->setAttribute('style', $style);
137
138 // for lazyload
139 if ($isLazy)
140 {
141 $resultList[$pos]->setAttribute('data-lazy-bg', 'Y');
142 $lazyOrigSrc = ($value['lazyOrigSrc'] ?? null);
143 if ($lazyOrigSrc)
144 {
145 $resultList[$pos]->setAttribute('data-src', $lazyOrigSrc);
146 }
147 $lazyOrigSrc2x = ($value['lazyOrigSrc2x'] ?? null);
148 if ($lazyOrigSrc2x)
149 {
150 $resultList[$pos]->setAttribute('data-src2x', $lazyOrigSrc2x);
151 }
152 $lazyOrigStyle = ($value['lazyOrigStyle'] ?? null);
153 if ($lazyOrigStyle)
154 {
155 $resultList[$pos]->setAttribute('data-style', $lazyOrigStyle);
156 }
157 }
158 }
159 else
160 {
161 $resultList[$pos]->setAttribute('alt', $alt);
162 $resultList[$pos]->setAttribute('src', $src);
163 if ($src2x)
164 {
165 $resultList[$pos]->setAttribute('srcset', "{$src2x} 2x");
166 }
167 else
168 {
169 $resultList[$pos]->setAttribute('srcset', '');
170 }
171
172 // for lazyload
173 if ($isLazy)
174 {
175 $resultList[$pos]->setAttribute('data-lazy-img', 'Y');
176 $resultList[$pos]->setAttribute('loading', 'lazy');
177 if ($lazyOrigSrc = $value['lazyOrigSrc'])
178 {
179 $resultList[$pos]->setAttribute('data-src', $lazyOrigSrc);
180 }
181 if ($lazyOrigSrcset = $value['lazyOrigSrcset'])
182 {
183 $resultList[$pos]->setAttribute('data-srcset', $lazyOrigSrcset);
184 }
185 }
186 }
187 $id
188 ? $resultList[$pos]->setAttribute('data-fileid', $id)
189 : $resultList[$pos]->removeAttribute('data-fileid')
190 ;
191 $id2x
192 ? $resultList[$pos]->setAttribute('data-fileid2x', $id2x)
193 : $resultList[$pos]->removeAttribute('data-fileid2x')
194 ;
195 $url
196 ? $resultList[$pos]->setAttribute('data-pseudo-url', $url)
197 : $resultList[$pos]->removeAttribute('data-pseudo-url')
198 ;
199
200 if (History::isActive())
201 {
202 $history = new History($block->getLandingId(), History::ENTITY_TYPE_LANDING);
203 $history->push('EDIT_IMG', [
204 'block' => $block,
205 'selector' => $selector,
206 'position' => (int)$pos,
207 'valueBefore' => $valueBefore[$pos],
208 'valueAfter' => $value,
209 ]);
210 }
211 }
212 }
213 }
214
221 public static function getNode(\Bitrix\Landing\Block $block, $selector)
222 {
223 $data = [];
224 $doc = $block->getDom();
225 $resultList = $doc->querySelectorAll($selector);
226 if (!$resultList)
227 {
228 $resultList = Node\Style::getNodesBySelector($block, $selector);
229 }
230
231 foreach ($resultList as $pos => $res)
232 {
233 $data[$pos] = [
234 'src' => '',
235 'src2x' => '',
236 'id' => null,
237 'id2x' => null,
238 'alt' => '',
239 'isLazy' => 'N',
240 ];
241
242 if ($res->getTagName() !== 'IMG')
243 {
244 $styles = StyleInliner::getStyle($res);
245 if (isset($styles['background-image']))
246 {
247 $src = $src2x = null;
248 // try gets retina srcset
249 if (
250 preg_match_all(
251 '/url\‍(\'*([^\']+)\'*\‍)\s*([\d]*x*)/is',
252 $styles['background-image'],
253 $matches
254 )
255 )
256 {
257 for ($i = 0, $c = count($matches[1]); $i < $c; $i++)
258 {
259 if ($matches[2][$i] === '2x')
260 {
261 $src2x = $matches[1][$i];
262 }
263 else
264 {
265 $src = $matches[1][$i];
266 }
267 }
268 }
269 if ($src || $src2x)
270 {
271 if ($src)
272 {
273 $data[$pos]['src'] = Manager::getUrlFromFile($src);
274 }
275 if ($src2x)
276 {
277 $data[$pos]['src2x'] = Manager::getUrlFromFile($src2x);
278 }
279 }
280
281 // for lazyload
282 if (
283 ($isLazy = $res->getAttribute('data-lazy-bg'))
284 && $isLazy === 'Y'
285 )
286 {
287 $data[$pos]['isLazy'] = 'Y';
288 if ($lazyOrigSrc = $res->getAttribute('data-src'))
289 {
290 $data[$pos]['lazyOrigSrc'] = $lazyOrigSrc;
291 }
292 if ($lazyOrigSrc2x = $res->getAttribute('data-src2x'))
293 {
294 $data[$pos]['lazyOrigSrc2x'] = $lazyOrigSrc2x;
295 }
296 if ($lazyOrigStyle = $res->getAttribute('data-style'))
297 {
298 $data[$pos]['lazyOrigStyle'] = $lazyOrigStyle;
299 }
300 }
301 }
302 }
303 else
304 {
305 $src = $res->getAttribute('src');
306 $srcSet = $res->getAttribute('srcset');
307
308 $data[$pos]['src'] = Manager::getUrlFromFile($src);
309 $data[$pos]['alt'] = $res->getAttribute('alt');
310
311 if (preg_match('/[\,\s]*(.*?)\s+2x/is', $srcSet, $matches))
312 {
313 $data[$pos]['src2x'] = Manager::getUrlFromFile($matches[1]);
314 }
315
316 // for lazyload
317 $isLazy = $res->getAttribute('data-lazy-img');
318 if ($isLazy === 'Y')
319 {
320 $data[$pos]['isLazy'] = 'Y';
321 $lazyOrigSrc = $res->getAttribute('data-src');
322 if ($lazyOrigSrc)
323 {
324 $data[$pos]['lazyOrigSrc'] = $lazyOrigSrc;
325 }
326 $lazyOrigSrcset = $res->getAttribute('data-srcset');
327 if ($lazyOrigSrcset)
328 {
329 if (
330 preg_match('/([^ ]+) 2x/i', $lazyOrigSrcset, $matches)
331 && $matches[1]
332 )
333 {
334 $data[$pos]['lazyOrigSrc2x'] = $matches[1];
335 }
336 // comment just for changes
337 $data[$pos]['lazyOrigSrcset'] = $lazyOrigSrcset;
338 }
339 }
340 }
341
342 if ($val = $res->getAttribute('data-pseudo-url'))
343 {
344 $data[$pos]['url'] = $val;
345 }
346
347 if ($val = $res->getAttribute('data-fileid'))
348 {
349 $data[$pos]['id'] = $val;
350 }
351
352 if (
353 (isset($data[$pos]['src2x']) || isset($data[$pos]['lazyOrigSrc2x']))
354 && ($val = $res->getAttribute('data-fileid2x'))
355 )
356 {
357 $data[$pos]['id2x'] = $val;
358 }
359 }
360
361 return $data;
362 }
363
370 public static function getSearchableNode($block, $selector)
371 {
372 $searchContent = [];
373
374 $nodes = self::getNode($block, $selector);
375 foreach ($nodes as $node)
376 {
377 if (!isset($node['alt']))
378 {
379 continue;
380 }
381 $node['alt'] = self::prepareSearchContent($node['alt']);
382 if ($node['alt'] && !in_array($node['alt'], $searchContent))
383 {
384 $searchContent[] = $node['alt'];
385 }
386 }
387
388 return $searchContent;
389 }
390
397 public static function changeNodeType(array $node, \Bitrix\Landing\Block $block): array
398 {
399 $matches = [];
400 $pattern = '/' . substr($node['code'], 1) . '[^\"]*/i';
401 if (preg_match($pattern, $block->getContent(), $matches) === 1)
402 {
403 $pattern = '/[\s]?g-bg-image[\s]?/i';
404 if (preg_match($pattern, $matches[0]) === 1)
405 {
406 $node['type'] = 'styleimg';
407 $node['handler'] = StyleImg::getHandlerJS();
408 }
409 }
410
411 return $node;
412 }
413}
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 changeNodeType(array $node, \Bitrix\Landing\Block $block)
Definition img.php:397
static getNode(\Bitrix\Landing\Block $block, $selector)
Definition img.php:221
static getHandlerJS()
Definition img.php:17
static saveNode(\Bitrix\Landing\Block $block, $selector, array $data)
Definition img.php:29
static prepareSearchContent($value)
Definition node.php:51