1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Img.php
См. документацию.
1<?php
2declare(strict_types=1);
3
4namespace Bitrix\Landing\Copilot\Data\Node;
5
6use Bitrix\Landing;
7use Bitrix\Landing\Copilot\Data;
8use Bitrix\Landing\Copilot\Data\Block\Operator;
9use Bitrix\Landing\Copilot\Data\Type\NodeType;
10use Bitrix\Landing\Copilot\Data\Type\ImgValueDto;
11use Bitrix\Landing\Rights;
12use Bitrix\Main\Web\Uri;
13
14class Img extends Node
15{
16 protected const TYPE = NodeType::Img;
17
22 private array $values = [];
23
28 private array $sizeData = [];
29 private bool $editInStyle = false;
30 private ?array $genderData = null;
31
32 public function __construct(string $code, array $data)
33 {
34 parent::__construct($code, $data);
35 $this->setData($data);
36 }
37
38 public function setData(array $data): void
39 {
40 if (isset($data['editInStyle']))
41 {
42 $this->editInStyle = (bool)$data['editInStyle'];
43 }
44
45 if (isset($data['sizeData']))
46 {
47 foreach ($data['sizeData'] as $position => $sizeData)
48 {
49 $this->sizeData[$position] = [
50 'width' => $sizeData['width'] ?? null,
51 'height' => $sizeData['height'] ?? null,
52 'aspectRatio' => $sizeData['aspectRatio'] ?? null,
53 ];
54 }
55 }
56
57 if (isset($data['genderData']))
58 {
59 $this->genderData = $data['genderData'];
60 }
61
62 // get data from typed values array
63 if (isset($data['values']))
64 {
65 $this->values = [];
66 foreach ($data['values'] as $position => $value)
67 {
68 $this->values[$position] = new ImgValueDto();
69 if (isset($value['defaultSrc']))
70 {
71 $this->values[$position]->defaultSrc = $value['defaultSrc'];
72 }
73 if (isset($value['defaultSrc2x']))
74 {
75 $this->values[$position]->defaultSrc2x = $value['defaultSrc2x'];
76 }
77 if (isset($value['src']))
78 {
79 $this->values[$position]->src = $value['src'];
80 $this->values[$position]->src2x = $value['src2x'] ?? $value['src'];
81 if (isset($value['id']))
82 {
83 $this->values[$position]->id = $value['id'];
84 $this->values[$position]->id2x = $value['id2x'] ?? $value['id'];
85 }
86 }
87 }
88
89 return;
90 }
91
92 // ... or from partial data
93 $dataSrc['src'] = $data['src'] ?? $data['defaultSrc'];
94 $dataSrc['src2x'] = $data['src2x'] ?? $data['defaultSrc2x'];
95
96 $maxLength = max(
97 is_array($dataSrc['src']) ? count($dataSrc['src']) : 0,
98 is_array($dataSrc['src2x']) ? count($dataSrc['src2x']) : 0
99 );
100
101 for ($position = 0; $position < $maxLength; $position++)
102 {
103 if (!isset($this->values[$position]))
104 {
105 $this->values[$position] = new ImgValueDto();
106 }
107
108 if (isset($dataSrc['src'][$position]))
109 {
110 $src = $dataSrc['src'][$position];
111 $this->values[$position]->defaultSrc = $src;
112 $this->values[$position]->src = $src;
113 }
114
115 if (isset($dataSrc['src2x'][$position]))
116 {
117 $src2x = $dataSrc['src2x'][$position];
118 $this->values[$position]->defaultSrc2x = $src2x;
119 $this->values[$position]->src2x = $src2x;
120 }
121 }
122 }
123
130 public function setValue(array $value, int $position): self
131 {
132 if (isset($this->values[$position]))
133 {
134 $defaultSrc = $this->values[$position]->defaultSrc;
135 $defaultSrc2x = $this->values[$position]->defaultSrc2x;
136 }
137
138 $newValue = new ImgValueDto();
139 if (isset($value['src']))
140 {
141 $newValue->defaultSrc = $defaultSrc ?? null;
142 $newValue->defaultSrc2x = $defaultSrc2x ?? null;
143 $newValue->src = $value['src'];
144 $newValue->src2x = $value['src2x'] ?? $value['src'];
145 if (isset($value['id']))
146 {
147 $newValue->id = $value['id'];
148 $newValue->id2x = $value['id2x'] ?? $value['id'];
149 }
150 }
151 $this->values[$position] = $newValue;
152
153 return $this;
154 }
155
156 public function setParentBlock(Data\Block $block): self
157 {
158 parent::setParentBlock($block);
159
160 // todo: need match is wrapper here?
161 // if ($this->block->getBgType() === 'image')
162 // {
163 // if (in_array(substr($this->getCode(), 1), $block->getWrapperClasses()))
164 // {
165 // $this->isWrapper = true;
166 // }
167 // }
168
169 return $this;
170 }
171
179 public function getValues(?int $position = null): array
180 {
181 $values = [];
182 foreach ($this->values as $pos => $value)
183 {
184 $current = [];
185 if (isset($value->defaultSrc))
186 {
187 $current['defaultSrc'] = $value->defaultSrc;
188 }
189 if (isset($value->defaultSrc2x))
190 {
191 $current['defaultSrc2x'] = $value->defaultSrc2x;
192 }
193 if (isset($value->src))
194 {
195 $current['src'] = $value->src;
196 $current['src2x'] = $value->src2x ?? $value->src;
197
198 if (isset($value->id))
199 {
200 $current['id'] = $value->id;
201 $current['id2x'] = $value->id2x ?? $value->id;
202 }
203 }
204
205 if (!empty($current))
206 {
207 $current['type'] = 'image';
208 $values[$pos] = $current;
209 }
210 }
211
212 if (isset($position))
213 {
214 $values = array_filter(
215 $values,
216 static function ($key) use ($position){
217 return $key === $position;
218 },
219 ARRAY_FILTER_USE_KEY
220 );
221 }
222
223 return $values;
224 }
225
226 public function getSizeData(): ?array
227 {
228 return $this->sizeData;
229 }
230
231 public function setSrc(array $src): self
232 {
233 foreach ($src as $position => $srcItem)
234 {
235 if (!isset($this->values[$position]))
236 {
237 $this->values[$position] = new ImgValueDto();
238 $this->values[$position]->id = null;
239 $this->values[$position]->id2x = null;
240 $this->values[$position]->defaultSrc = null;
241 $this->values[$position]->defaultSrc2x = null;
242 }
243
244 if (is_string($srcItem))
245 {
246 $this->values[$position]->src = $srcItem;
247 $this->values[$position]->src2x = $srcItem;
248 }
249
250 if (is_array($srcItem))
251 {
252 $this->values[$position]->src = $srcItem['src'] ?? null;
253 $this->values[$position]->src2x = $srcItem['src2x'] ?? $this->values[$position]->src;
254 }
255 }
256
257 return $this;
258 }
259
260 public function setDefaultSrc(array $defaultSrc): self
261 {
262 foreach ($defaultSrc as $position => $defaultSrcItem)
263 {
264 if (!isset($this->values[$position]))
265 {
266 $this->values[$position] = new ImgValueDto();
267 $this->values[$position]->id = null;
268 $this->values[$position]->id2x = null;
269 $this->values[$position]->src = null;
270 $this->values[$position]->src2x = null;
271 }
272
273 if (is_string($defaultSrcItem))
274 {
275 $this->values[$position]->defaultSrc = $defaultSrcItem;
276 $this->values[$position]->defaultSrc2x = $defaultSrcItem;
277 }
278
279 if (is_array($defaultSrcItem))
280 {
281 $this->values[$position]->defaultSrc = $defaultSrcItem['src'] ?? null;
282 $this->values[$position]->defaultSrc2x =
283 $defaultSrcItem['src2x'] ?? $this->values[$position]->defaultSrc;
284 }
285 }
286
287 return $this;
288 }
289
290 public function isEditInStyle(): bool
291 {
292 return $this->editInStyle;
293 }
294
295 public function setGenderData(array $genderData): self
296 {
297 // todo: need node type checking!
298 $this->genderData = $genderData;
299
300 return $this;
301 }
302
303 // todo: use trait for repeated methods
304 public function getGenderData(): ?array
305 {
306 return $this->genderData;
307 }
308
309 public function setImageFromPath(string $path, int $position): static
310 {
311 $fileArray = self::makeFileArrayByPath($path);
312 $sizeData = $this->getSizeData()[$position];
313 if (!isset($fileArray, $sizeData))
314 {
315 return $this;
316 }
317
318 $defaultSrc = $this->values[$position]->defaultSrc;
319 $defaultSrc2x = $this->values[$position]->defaultSrc2x;
320
321 $this->values[$position] = new ImgValueDto();
322 $this->values[$position]->defaultSrc = $defaultSrc;
323 $this->values[$position]->defaultSrc2x = $defaultSrc2x;
324
325 foreach ([1, 2] as $x)
326 {
327 $targetSize = self::matchResizeSize(
328 $fileArray['width'],
329 $fileArray['height'],
330 (int)$sizeData['width'] * $x,
331 $sizeData['aspectRatio'],
332 );
333
334 $params = $targetSize;
335 $params['resize_type'] = BX_RESIZE_IMAGE_EXACT;
337 $fileArray,
338 false,
339 $params
340 );
341
342 if ($newFile)
343 {
344 if ($x === 1)
345 {
346 $this->values[$position]->id = (int)$newFile['ID'];
347 $this->values[$position]->src = $newFile['SRC'];
348 }
349 elseif ($x === 2)
350 {
351 $this->values[$position]->id2x = (int)$newFile['ID'];
352 $this->values[$position]->src2x = $newFile['SRC'];
353 }
354 }
355 }
356
357 return $this;
358 }
359
365 private static function makeFileArrayByPath(string $path): ?array
366 {
367 $uri = new Uri($path);
368 $query = $uri->getQuery();
369 mb_parse_str($query, $params);
370 if (
371 !isset($params['id'])
372 || (int)$params['id'] <= 0
373 )
374 {
375 return null;
376 }
377
378 $fileId = (int)$params['id'];
379 $file = \CFile::GetByID($fileId)->Fetch();
380 $fileArray = \CFIle::makeFileArray($fileId);
381
382 if (isset($fileArray['tmp_name']))
383 {
384 if (
385 (int)$file['WIDTH'] > 0
386 && (int)$file['HEIGHT'] > 0
387 )
388 {
389 $fileArray['width'] = (int)$file['WIDTH'];
390 $fileArray['height'] = (int)$file['HEIGHT'];
391 }
392
393 return $fileArray;
394 }
395
396 return null;
397 }
398
406 protected static function matchResizeSize(
407 int $sourceWidth,
408 int $sourceHeight,
409 int $targetWidth,
410 string $aspectName
411 ): array
412 {
413 $aspects = Operator::getAllowedAspectRatios();
414 $aspect = $aspects[$aspectName] ?? 1;
415
416 $targetHeight = $targetWidth / $aspect;
417
418 if (
419 $targetWidth > $sourceWidth
420 || $targetHeight > $sourceHeight
421 )
422 {
423 if (($targetHeight * $aspect) <= $sourceWidth)
424 {
425 $targetWidth = $sourceHeight * $aspect;
426 $targetHeight = $sourceHeight;
427 }
428 else
429 {
430 $targetWidth = $sourceWidth;
431 $targetHeight = $sourceWidth / $aspect;
432 }
433 }
434
435 return [
436 'width' => $targetWidth,
437 'height' => $targetHeight,
438 ];
439 }
440
441 public function toArray(): array
442 {
443 $data = parent::toArray();
444 $data['editInStyle'] = $this->isEditInStyle();
445 $data['values'] = $this->getValues();
446 $data['sizeData'] = $this->getSizeData();
447 $data['genderData'] = $this->getGenderData();
448
449 return $data;
450 }
451
452 public function toLanding(?int $position = null): bool
453 {
454 if (!$this->canApplyToLanding())
455 {
456 return false;
457 }
458
459 $block = $this->getBlockInstance();
460 if (!$block)
461 {
462 return false;
463 }
464
465 // todo: need rights?
466 $rightsBefore = Rights::isOn();
468
469 $dataToSave = [];
470 foreach ($this->getValues($position) as $pos => $value)
471 {
472 if (isset($value['id']))
473 {
474 Landing\File::addToBlock($block->getId(), $value['id']);
475 }
476 if (isset($value['id2x']))
477 {
478 Landing\File::addToBlock($block->getId(), $value['id2x']);
479 }
480
481 $dataToSave[$pos] = $value;
482 }
483
484 $saved = false;
485 $updated = $block->updateNodes([
486 $this->getCode() => $dataToSave,
487 ]);
488 if ($updated)
489 {
490 $saved = $block->save();
491 }
492
493 if ($rightsBefore)
494 {
496 }
497
498 return $updated && $saved;
499 }
500}
$path
Определения access_edit.php:21
setGenderData(array $genderData)
Определения Img.php:295
__construct(string $code, array $data)
Определения Img.php:32
setDefaultSrc(array $defaultSrc)
Определения Img.php:260
setValue(array $value, int $position)
Определения Img.php:130
setImageFromPath(string $path, int $position)
Определения Img.php:309
setData(array $data)
Определения Img.php:38
getValues(?int $position=null)
Определения Img.php:179
setSrc(array $src)
Определения Img.php:231
toLanding(?int $position=null)
Определения Img.php:452
setParentBlock(Data\Block $block)
Определения Img.php:156
static matchResizeSize(int $sourceWidth, int $sourceHeight, int $targetWidth, string $aspectName)
Определения Img.php:406
static addToBlock(int $blockId, $fileId, bool $temp=false)
Определения file.php:305
static savePicture($file, $ext=false, $params=array())
Определения manager.php:590
static isOn()
Определения rights.php:125
static setGlobalOn()
Определения rights.php:116
static setGlobalOff()
Определения rights.php:107
Определения uri.php:17
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$query
Определения get_search.php:11
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
const BX_RESIZE_IMAGE_EXACT
Определения constants.php:12
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
Определения urlrewrite.php:61
Определения aliases.php:105
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799