Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
itemattributes.php
1<?php
2
4
5
10
12{
16 protected $fileData;
20 protected $attributes = [];
24 protected $actions = [];
28 protected $sourceUri;
32 protected $options = [];
33
37 protected static $renderClassByContentType = [];
38
46 private function __construct($fileData, $sourceUri, array $options = [])
47 {
48 $this->fileData = $fileData;
49 $this->sourceUri = $sourceUri;
50 $this->options = $options;
51
52 $this->setDefaultAttributes();
53 }
54
55 protected function setDefaultAttributes()
56 {
57 $this
58 ->setAttribute('data-viewer')
59 ->setViewerType(static::getViewerTypeByFile($this->fileData))
60 ->setAttribute('data-src', $this->sourceUri)
61 ;
62 }
63
71 public static function buildByFileId($fileId, $sourceUri)
72 {
73 $fileData = \CFile::getByID($fileId)->fetch();
74 if (!$fileData)
75 {
76 throw new ArgumentException('Invalid fileId', 'fileId');
77 }
78
79 return new static($fileData, $sourceUri);
80 }
81
89 public static function buildByFileData(array $fileData, $sourceUri)
90 {
91 if (empty($fileData['ID']))
92 {
93 throw new ArgumentException('Invalid file data', 'fileData');
94 }
95
96 return new static($fileData, $sourceUri);
97 }
98
99 public static function tryBuildByFileData(array $fileData, $sourceUri)
100 {
101 try
102 {
103 return static::buildByFileData($fileData, $sourceUri);
104 }
105 catch (ArgumentException $exception)
106 {
107 if ($exception->getParameter() == 'fileData')
108 {
109 return static::buildAsUnknownType($sourceUri);
110 }
111
112 throw $exception;
113 }
114 }
115
121 public static function buildAsUnknownType($sourceUri)
122 {
123 $fakeFileData = [
124 'ID' => -1,
125 'CONTENT_TYPE' => 'application/octet-stream',
126 ];
127
128 return new static($fakeFileData, $sourceUri);
129 }
130
131 public static function tryBuildByFileId($fileId, $sourceUri)
132 {
133 try
134 {
135 return static::buildByFileId($fileId, $sourceUri);
136 }
137 catch (ArgumentException $exception)
138 {
139 if ($exception->getParameter() == 'fileId')
140 {
141 return static::buildAsUnknownType($sourceUri);
142 }
143
144 throw $exception;
145 }
146 }
147
153 public function setTitle($title)
154 {
155 return $this->setAttribute('data-title', htmlspecialcharsbx($title));
156 }
157
158 public function setTypeClass(string $class)
159 {
160 return $this->setAttribute('data-viewer-type-class', htmlspecialcharsbx($class));
161 }
162
163 public function setViewerType(string $type): self
164 {
165 return $this->setAttribute('data-viewer-type', $type);
166 }
167
168 public function getTypeClass()
169 {
170 return $this->getAttribute('data-viewer-type-class');
171 }
172
178 public function setGroupBy($id)
179 {
180 return $this->setAttribute('data-viewer-group-by', htmlspecialcharsbx($id));
181 }
182
186 public function unsetGroupBy()
187 {
188 return $this->unsetAttribute('data-viewer-group-by');
189 }
190
194 public function getGroupBy()
195 {
196 return $this->getAttribute('data-viewer-group-by');
197 }
198
204 public function addAction(array $action)
205 {
206 $this->actions[] = $action;
207
208 return $this;
209 }
210
211 public function clearActions(): self
212 {
213 $this->actions = [];
214
215 return $this;
216 }
217
221 public function getActions()
222 {
223 return $this->actions;
224 }
225
230 public function setExtension($extension)
231 {
232 return $this->setAttribute('data-viewer-extension', $extension);
233 }
234
238 public function getExtension()
239 {
240 return $this->getAttribute('data-viewer-extension');
241 }
242
246 public function getViewerType()
247 {
248 if (!$this->issetAttribute('data-viewer-type'))
249 {
250 $this->setViewerType(static::getViewerTypeByFile($this->fileData));
251 }
252
253 return $this->getAttribute('data-viewer-type');
254 }
255
262 public function setAttribute($name, $value = null)
263 {
264 $this->attributes[$name] = $value;
265
266 return $this;
267 }
268
274 public function unsetAttribute($name)
275 {
276 unset($this->attributes[$name]);
277
278 return $this;
279 }
280
286 public function issetAttribute($name)
287 {
288 return isset($this->attributes[$name]);
289 }
290
296 public function getAttribute($name)
297 {
298 if (isset($this->attributes[$name]))
299 {
300 return $this->attributes[$name];
301 }
302
303 return null;
304 }
305
309 public function getAttributes()
310 {
311 return $this->attributes;
312 }
313
320 protected static function getViewerTypeByFile(array $fileArray)
321 {
322 $contentType = $fileArray['CONTENT_TYPE'];
323 $originalName = $fileArray['ORIGINAL_NAME'] ?? null;
324
325 if (isset(static::$renderClassByContentType[$contentType]))
326 {
327 $renderClass = static::$renderClassByContentType[$contentType];
328 if ($renderClass::getSizeRestriction() === null)
329 {
330 return $renderClass::getJsType();
331 }
332 }
333
334 $previewManager = new PreviewManager();
335 $renderClass = $previewManager->getRenderClassByFile([
336 'contentType' => $contentType,
337 'originalName' => $originalName,
338 'size' => $fileArray['FILE_SIZE'] ?? null,
339 ]);
340
341 if ($renderClass === Renderer\Stub::class)
342 {
343 $transformerManager = new TransformerManager();
344 if ($transformerManager->isAvailable())
345 {
347 $transformation = $transformerManager->buildTransformationByFile($fileArray);
348 if ($transformation)
349 {
350 $contentType = $transformation->getOutputContentType();
351 $renderClass = $previewManager->getRenderClassByFile([
352 'contentType' => $contentType,
353 'originalName' => $originalName,
354 ]);
355 }
356 }
357 }
358
359 if ($renderClass !== Renderer\RestrictedBySize::class)
360 {
361 static::$renderClassByContentType[$fileArray['CONTENT_TYPE']] = $renderClass;
362 }
363
364 return $renderClass::getJsType();
365 }
366
370 public function toString()
371 {
372 return (string)$this;
373 }
374
378 public function __toString()
379 {
380 $string = '';
381 foreach ($this->attributes as $key => $value)
382 {
383 if (is_int($key))
384 {
385 $string .= "{$value} ";
386 }
387 else
388 {
389 $value = htmlspecialcharsbx($value);
390 $string .= "{$key}=\"{$value}\" ";
391 }
392 }
393
394 if ($this->actions)
395 {
396 $string .= "data-actions='" . htmlspecialcharsbx(Json::encode($this->actions)) . "'";
397 }
398
399 return $string;
400 }
401
406 public function toDataSet()
407 {
408 $likeDataSet = [];
409 foreach ($this->attributes as $key => $value)
410 {
411 if (is_int($key))
412 {
413 $likeDataSet[$this->convertKeyToDataSet($value)] = null;
414 }
415 else
416 {
417 $likeDataSet[$this->convertKeyToDataSet($key)] = $value;
418 }
419 }
420
421 if ($this->actions)
422 {
423 $likeDataSet[$this->convertKeyToDataSet('data-actions')] = Json::encode($this->actions);
424 }
425
426 return $likeDataSet;
427 }
428
429 protected function convertKeyToDataSet($key)
430 {
431 $key = str_replace('data-', '', $key);
432 $key = str_replace('-', ' ', mb_strtolower($key));
433
434 return lcfirst(str_replace(' ', '', ucwords($key)));
435 }
436}
static tryBuildByFileId($fileId, $sourceUri)
static buildByFileId($fileId, $sourceUri)
static buildByFileData(array $fileData, $sourceUri)
static tryBuildByFileData(array $fileData, $sourceUri)