1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
itemattributes.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\UI\Viewer;
4
5use Bitrix\Main\ArgumentException;
6use Bitrix\Main\File\Image\Rectangle;
7use Bitrix\Main\UI\Viewer\Transformation\Transformation;
8use Bitrix\Main\UI\Viewer\Transformation\TransformerManager;
9use Bitrix\Main\Web\Json;
10use Bitrix\Main\Web\Uri;
11
13{
14 private const FAKE_FILEDATA = [
15 'ID' => -1,
16 'CONTENT_TYPE' => 'application/octet-stream',
17 ];
18
22 protected $fileData;
26 protected $attributes = [];
30 protected $actions = [];
34 protected $sourceUri;
38 protected $options = [];
39
43 protected static $renderClassByContentType = [];
44
52 private function __construct($fileData, $sourceUri, array $options = [])
53 {
54 $this->fileData = $fileData;
55 $this->sourceUri = $sourceUri;
56 $this->options = $options;
57
58 $this->setDefaultAttributes();
59 }
60
61 protected function setDefaultAttributes()
62 {
63 $this
64 ->setAttribute('data-viewer')
65 ->setViewerType(static::getViewerTypeByFile($this->fileData))
66 ->setAttribute('data-src', $this->sourceUri)
67 ;
68
69 if ($this->getViewerType() === Renderer\Image::getJsType())
70 {
71 $sourceImageWidth = $this->fileData['WIDTH'] ?? 0;
72 $sourceImageHeight = $this->fileData['HEIGHT'] ?? 0;
73 if ($sourceImageWidth > 0 && $sourceImageHeight > 0)
74 {
75 $sourceUri = $this->sourceUri instanceof Uri ? $this->sourceUri : new Uri((string)$this->sourceUri);
76 $imageRenderer = new Renderer\Image($this->fileData['ORIGINAL_NAME'] ?? '', $sourceUri);
77 $sourceRectangle = new Rectangle($sourceImageWidth, $sourceImageHeight);
78 $destinationRectangle = new Rectangle($imageRenderer->getWidth(), $imageRenderer->getHeight());
79 $needResize = $sourceRectangle->resize($destinationRectangle, $imageRenderer->getResizeType());
80 if (!$needResize)
81 {
82 $this->setAttribute('data-viewer-resized');
83 }
84 }
85 }
86 }
87
95 public static function buildByFileId($fileId, $sourceUri)
96 {
97 $fileData = \CFile::getByID($fileId)->fetch();
98 if (!$fileData)
99 {
100 throw new ArgumentException('Invalid fileId', 'fileId');
101 }
102
103 return new static($fileData, $sourceUri);
104 }
105
113 public static function buildByFileData(array $fileData, $sourceUri)
114 {
115 if (empty($fileData['ID']))
116 {
117 throw new ArgumentException('Invalid file data', 'fileData');
118 }
119
120 return new static($fileData, $sourceUri);
121 }
122
124 {
125 try
126 {
127 return static::buildByFileData($fileData, $sourceUri);
128 }
129 catch (ArgumentException $exception)
130 {
131 if ($exception->getParameter() == 'fileData')
132 {
133 return static::buildAsUnknownType($sourceUri);
134 }
135
136 throw $exception;
137 }
138 }
139
145 public static function buildAsUnknownType($sourceUri)
146 {
147 return new static(self::FAKE_FILEDATA, $sourceUri);
148 }
149
150 protected static function isFakeFileData(array $fileData): bool
151 {
152 return
153 ($fileData['ID'] === self::FAKE_FILEDATA['ID'])
154 && ($fileData['CONTENT_TYPE'] === self::FAKE_FILEDATA['CONTENT_TYPE'])
155 ;
156 }
157
158 public static function tryBuildByFileId($fileId, $sourceUri)
159 {
160 try
161 {
162 return static::buildByFileId($fileId, $sourceUri);
163 }
164 catch (ArgumentException $exception)
165 {
166 if ($exception->getParameter() == 'fileId')
167 {
168 return static::buildAsUnknownType($sourceUri);
169 }
170
171 throw $exception;
172 }
173 }
174
180 public function setTitle($title)
181 {
182 return $this->setAttribute('data-title', htmlspecialcharsbx($title));
183 }
184
185 public function setTypeClass(string $class)
186 {
187 return $this->setAttribute('data-viewer-type-class', htmlspecialcharsbx($class));
188 }
189
190 public function setViewerType(string $type): self
191 {
192 return $this->setAttribute('data-viewer-type', $type);
193 }
194
195 public function getTypeClass()
196 {
197 return $this->getAttribute('data-viewer-type-class');
198 }
199
205 public function setGroupBy($id)
206 {
207 return $this->setAttribute('data-viewer-group-by', htmlspecialcharsbx($id));
208 }
209
213 public function unsetGroupBy()
214 {
215 return $this->unsetAttribute('data-viewer-group-by');
216 }
217
221 public function getGroupBy()
222 {
223 return $this->getAttribute('data-viewer-group-by');
224 }
225
231 public function addAction(array $action)
232 {
233 $this->actions[] = $action;
234
235 return $this;
236 }
237
238 public function clearActions(): self
239 {
240 $this->actions = [];
241
242 return $this;
243 }
244
248 public function getActions()
249 {
250 return $this->actions;
251 }
252
257 public function setExtension($extension)
258 {
259 return $this->setAttribute('data-viewer-extension', $extension);
260 }
261
265 public function getExtension()
266 {
267 return $this->getAttribute('data-viewer-extension');
268 }
269
273 public function getViewerType()
274 {
275 if (!$this->issetAttribute('data-viewer-type'))
276 {
277 $this->setViewerType(static::getViewerTypeByFile($this->fileData));
278 }
279
280 return $this->getAttribute('data-viewer-type');
281 }
282
289 public function setAttribute($name, $value = null)
290 {
291 $this->attributes[$name] = $value;
292
293 return $this;
294 }
295
301 public function unsetAttribute($name)
302 {
303 unset($this->attributes[$name]);
304
305 return $this;
306 }
307
313 public function issetAttribute($name)
314 {
315 return isset($this->attributes[$name]);
316 }
317
323 public function getAttribute($name)
324 {
325 if (isset($this->attributes[$name]))
326 {
327 return $this->attributes[$name];
328 }
329
330 return null;
331 }
332
336 public function getAttributes()
337 {
338 return $this->attributes;
339 }
340
347 protected static function getViewerTypeByFile(array $fileArray)
348 {
349 $contentType = $fileArray['CONTENT_TYPE'];
350 $originalName = $fileArray['ORIGINAL_NAME'] ?? null;
351
352 if (isset(static::$renderClassByContentType[$contentType]))
353 {
354 $renderClass = static::$renderClassByContentType[$contentType];
355 if ($renderClass::getSizeRestriction() === null)
356 {
357 return $renderClass::getJsType();
358 }
359 }
360
361 $previewManager = new PreviewManager();
362 $renderClass = $previewManager->getRenderClassByFile([
363 'contentType' => $contentType,
364 'originalName' => $originalName,
365 'size' => $fileArray['FILE_SIZE'] ?? null,
366 ]);
367
368 if ($renderClass === Renderer\Stub::class)
369 {
370 $transformerManager = new TransformerManager();
371 if ($transformerManager->isAvailable())
372 {
374 $transformation = $transformerManager->buildTransformationByFile($fileArray);
375 if ($transformation)
376 {
377 $contentType = $transformation->getOutputContentType();
378 $renderClass = $previewManager->getRenderClassByFile([
379 'contentType' => $contentType,
380 'originalName' => $originalName,
381 ]);
382 }
383 }
384 }
385
386 if ($renderClass !== Renderer\RestrictedBySize::class)
387 {
388 static::$renderClassByContentType[$fileArray['CONTENT_TYPE']] = $renderClass;
389 }
390
391 return $renderClass::getJsType();
392 }
393
397 public function toString()
398 {
399 return (string)$this;
400 }
401
405 public function __toString()
406 {
407 $string = '';
408 foreach ($this->attributes as $key => $value)
409 {
410 if (is_int($key))
411 {
412 $string .= "{$value} ";
413 }
414 else
415 {
416 $value = htmlspecialcharsbx($value);
417 $string .= "{$key}=\"{$value}\" ";
418 }
419 }
420
421 if ($this->actions)
422 {
423 $string .= "data-actions='" . htmlspecialcharsbx(Json::encode($this->actions)) . "'";
424 }
425
426 return $string;
427 }
428
433 public function toDataSet()
434 {
435 $likeDataSet = [];
436 foreach ($this->attributes as $key => $value)
437 {
438 if (is_int($key))
439 {
440 $likeDataSet[$this->convertKeyToDataSet($value)] = null;
441 }
442 else
443 {
444 $likeDataSet[$this->convertKeyToDataSet($key)] = $value;
445 }
446 }
447
448 if ($this->actions)
449 {
450 $likeDataSet[$this->convertKeyToDataSet('data-actions')] = Json::encode($this->actions);
451 }
452
453 return $likeDataSet;
454 }
455
461 public function toVueBind(): array
462 {
463 $result = [];
464 foreach ($this->attributes as $key => $value)
465 {
466 if (is_int($key))
467 {
468 $result[$value] = '';
469 }
470 else
471 {
472 $result[$key] = $value ?? '';
473 }
474 }
475
476 if ($this->actions)
477 {
478 $result['data-actions'] = Json::encode($this->actions);
479 }
480
481 return $result;
482 }
483
484 protected function convertKeyToDataSet($key)
485 {
486 $key = str_replace('data-', '', $key);
487 $key = str_replace('-', ' ', mb_strtolower($key));
488
489 return lcfirst(str_replace(' ', '', ucwords($key)));
490 }
491}
$type
Определения options.php:106
static tryBuildByFileId($fileId, $sourceUri)
Определения itemattributes.php:158
setViewerType(string $type)
Определения itemattributes.php:190
static buildByFileId($fileId, $sourceUri)
Определения itemattributes.php:95
static buildAsUnknownType($sourceUri)
Определения itemattributes.php:145
addAction(array $action)
Определения itemattributes.php:231
static buildByFileData(array $fileData, $sourceUri)
Определения itemattributes.php:113
setTypeClass(string $class)
Определения itemattributes.php:185
setAttribute($name, $value=null)
Определения itemattributes.php:289
static isFakeFileData(array $fileData)
Определения itemattributes.php:150
static tryBuildByFileData(array $fileData, $sourceUri)
Определения itemattributes.php:123
Определения uri.php:17
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
$name
Определения menu_edit.php:35
if(empty($signedUserToken)) $key
Определения quickway.php:257
$contentType
Определения quickway.php:301
$title
Определения pdf.php:123
$action
Определения file_dialog.php:21