Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
imageinput.php
1<?php
2
4
10
12{
13 public const FILE_ID_SALT = 'catalog.image.input.file.id';
14
16 private $entity;
17
18 private $autoSavingEnabled = true;
19 private $morePhotoPropertyId;
20 private $inputName;
21 private $inputId;
22 private $values;
23
24 public function __construct(BaseIblockElementEntity $entity = null)
25 {
26 $this->entity = $entity;
27 }
28
29 public function setInputName($name)
30 {
31 $this->inputName = $name;
32 }
33
34 public function disableAutoSaving(): self
35 {
36 $this->autoSavingEnabled = false;
37
38 return $this;
39 }
40
41 public function isEmpty(): bool
42 {
43 return count($this->getValues()) === 0;
44 }
45
46 public function getFormattedField(): array
47 {
48 $fileValues = $this->getValues();
49 $signedFileValues = $this->getSignedValues();
50
51 return [
52 'id' => 'bx_file_'.$this->getInputId(),
53 'values' => $signedFileValues,
54 'isEmpty' => empty($fileValues),
55 'preview' => $this->getPreview(),
56 'input' => $this->getHtml($this->getImageParams(), $fileValues, [
57 'disabled' => $this->entity === null,
58 ]),
59 'emptyInput' => $this->getHtml($this->getImageParams(), [], [
60 'disabled' => true,
61 ]),
62 ];
63 }
64
66 {
67 return new Response\Component(
68 'bitrix:catalog.image.input',
69 '',
70 [
71 'FILE_SETTINGS' => $this->getImageParams(),
72 'FILE_VALUES' => $this->getValues(),
73 'FILE_SIGNED_VALUES' => $this->getSignedValues(),
74 'LOADER_PREVIEW' => $this->getPreview(),
75 'ENABLE_AUTO_SAVING' => $this->autoSavingEnabled,
76 'PRODUCT_ENTITY' => $this->entity,
77 'INPUT_ID' => 'bx_file_'.$this->getInputId(),
78 ]
79 );
80 }
81
82 private function getImageParams(): array
83 {
84 return [
85 'name' => $this->getInputName(),
86 'id' => $this->getInputId(),
87 'description' => 'Y',
88 'allowUpload' => 'I',
89 'allowUploadExt' => null,
90 'maxCount' => !$this->isMorePhotoEnabled() ? 1 : null,
91 'upload' => true,
92 'medialib' => false,
93 'fileDialog' => true,
94 'cloud' => true,
95 ];
96 }
97
98 private function getInputName(): string
99 {
100 $defaultName = $this->isMorePhotoEnabled() ? 'PROPERTY_'.$this->getMorePhotoPropertyId().'_n#IND#' : 'DETAIL_PICTURE';
101 return $this->inputName ?? $defaultName;
102 }
103
104 private function getInputId(): string
105 {
106 if (!$this->inputId)
107 {
108 $id = uniqid($this->getInputName().'_', false);
109 $this->inputId = strtolower(preg_replace('/[^a-z0-9]/i', '_', $id));
110 }
111
112 return $this->inputId;
113 }
114
115 private function getEntityId(): int
116 {
117 return $this->entity ? (int)$this->entity->getId() : 0;
118 }
119
120 private function getIblockId(): int
121 {
122 return $this->entity ? (int)$this->entity->getIblockId() : 0;
123 }
124
125 private function getValues(): array
126 {
127 if (isset($this->values))
128 {
129 return $this->values;
130 }
131
132 if ($this->getEntityId() <= 0)
133 {
134 return [];
135 }
136
137 $this->values = [];
138 $photoCollection = $this->entity->getFrontImageCollection();
139 if ($this->isMorePhotoEnabled())
140 {
141 foreach ($photoCollection as $item)
142 {
143 if ($item instanceof MorePhotoImage)
144 {
145 $propName = str_replace('n#IND#', $item->getPropertyValueId(), $this->getInputName());
146 $this->values[$propName] = $item->getId();
147 }
148 else
149 {
150 $this->values[$item::CODE] = $item->getId();
151 }
152 }
153 }
154 else
155 {
156 $item = $photoCollection->getFrontImage();
157 if ($item)
158 {
159 $this->values[$item::CODE] = $item->getId();
160 }
161 }
162
163 return $this->values;
164 }
165
166 private function getSignedValues(): array
167 {
168 $signedValues = [];
169
170 foreach ($this->getValues() as $name => $fileId)
171 {
172 if ($fileId !== null && is_numeric($fileId))
173 {
174 static $signer = null;
175 if ($signer === null)
176 {
177 $signer = new Signer;
178 }
179
180 $signedValues[$name] = $signer->sign((string)$fileId, self::FILE_ID_SALT);
181 }
182 }
183
184 return $signedValues;
185 }
186
187 private function isMorePhotoEnabled(): bool
188 {
189 return (int)$this->getMorePhotoPropertyId() > 0;
190 }
191
192 private function getMorePhotoPropertyId(): ?int
193 {
194 if ($this->morePhotoPropertyId === null)
195 {
196 $this->morePhotoPropertyId = 0;
197 $propertyRaw = \Bitrix\Iblock\PropertyTable::getList([
198 'select' => ['ID'],
199 'filter' => [
200 '=IBLOCK_ID' => $this->getIblockId(),
201 '=ACTIVE' => 'Y',
202 '=CODE' => MorePhotoImage::CODE,
203 ],
204 'limit' => 1,
205 'cache' => [
206 'ttl' => 86400,
207 ],
208 ]);
209
210 if ($morePhotoProperty = $propertyRaw->fetch())
211 {
212 $this->morePhotoPropertyId = $morePhotoProperty['ID'];
213 }
214 }
215
216 return $this->morePhotoPropertyId;
217 }
218
219 private function getHtml(array $settings = [], array $values = [], array $options = []): string
220 {
221 ob_start();
222
223 $GLOBALS['APPLICATION']->includeComponent(
224 'bitrix:catalog.image.input',
225 '',
226 [
227 'FILE_SETTINGS' => $settings,
228 'FILE_VALUES' => $values,
229 'FILE_SIGNED_VALUES' => $this->getSignedValues(),
230 'LOADER_PREVIEW' => (string)($options['preview'] ?? ''),
231 'DISABLED' => (bool)($options['disabled'] ?? false),
232 'ENABLE_AUTO_SAVING' => $this->autoSavingEnabled,
233 'PRODUCT_ENTITY' => $this->entity,
234 'INPUT_ID' => 'bx_file_'.$this->getInputId(),
235 ]
236 );
237
238 return ob_get_clean();
239 }
240
241 private function getPreview(): string
242 {
243 $imageData = [];
244
245 if ($this->getEntityId() > 0)
246 {
247 $photoCollection = $this->entity->getFrontImageCollection();
248 foreach ($photoCollection as $item)
249 {
250 if (!$item->getFileStructure())
251 {
252 continue;
253 }
254
255 $attributes = ItemAttributes::tryBuildByFileData($item->getFileStructure(), $item->getSource());
256 $attributes->setAttribute('data-viewer-type', 'image');
257 $attributes->setGroupBy("group-{$this->entity->getId()}");
258 $imageData[] = [
259 'SOURCE' => $item->getSource(),
260 'ATTRIBUTES' => $attributes,
261 ];
262 }
263 }
264
265 $fileCount = min(count($imageData), 3);
266
267 switch ($fileCount)
268 {
269 case 3:
270 $multipleClass = ' ui-image-input-img-block-multiple';
271 break;
272
273 case 2:
274 $multipleClass = ' ui-image-input-img-block-double';
275 break;
276
277 case 0:
278 $multipleClass = ' ui-image-input-img-block-empty';
279 break;
280
281 case 1:
282 default:
283 $multipleClass = '';
284 break;
285 }
286
287 $imageString = '';
288 foreach ($imageData as $key => $image)
289 {
290 $displayClass = '';
291 if ($key !== 0)
292 {
293 $displayClass = 'main-ui-hide';
294 }
295 $imageString .= "
296 <img class='ui-image-input-img {$displayClass}' {$image['ATTRIBUTES']} src='{$image['SOURCE']}'>
297 ";
298 }
299
300 return "
301 <div class='ui-image-input-img-block{$multipleClass}'>
302 <div class='ui-image-input-img-block-inner'>
303 <div class='ui-image-input-img-item'>
304 {$imageString}
305 </div>
306 </div>
307 </div>
308 ";
309 }
310}
__construct(BaseIblockElementEntity $entity=null)
static tryBuildByFileData(array $fileData, $sourceUri)
$GLOBALS['____1979065141']
Definition mutator.php:1