18 private $autoSavingEnabled =
true;
19 private $morePhotoPropertyId;
26 $this->entity = $entity;
31 $this->inputName = $name;
36 $this->autoSavingEnabled =
false;
43 return count($this->getValues()) === 0;
48 $fileValues = $this->getValues();
49 $signedFileValues = $this->getSignedValues();
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,
59 'emptyInput' => $this->getHtml($this->getImageParams(), [], [
67 return new Response\Component(
68 'bitrix:catalog.image.input',
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(),
82 private function getImageParams(): array
85 'name' => $this->getInputName(),
86 'id' => $this->getInputId(),
89 'allowUploadExt' =>
null,
90 'maxCount' => !$this->isMorePhotoEnabled() ? 1 :
null,
98 private function getInputName(): string
100 $defaultName = $this->isMorePhotoEnabled() ?
'PROPERTY_'.$this->getMorePhotoPropertyId().
'_n#IND#' :
'DETAIL_PICTURE';
101 return $this->inputName ?? $defaultName;
104 private function getInputId(): string
108 $id = uniqid($this->getInputName().
'_',
false);
109 $this->inputId = strtolower(preg_replace(
'/[^a-z0-9]/i',
'_', $id));
112 return $this->inputId;
115 private function getEntityId(): int
117 return $this->entity ? (int)$this->entity->getId() : 0;
120 private function getIblockId(): int
122 return $this->entity ? (int)$this->entity->getIblockId() : 0;
125 private function getValues(): array
127 if (isset($this->values))
129 return $this->values;
132 if ($this->getEntityId() <= 0)
138 $photoCollection = $this->entity->getFrontImageCollection();
139 if ($this->isMorePhotoEnabled())
141 foreach ($photoCollection as $item)
143 if ($item instanceof MorePhotoImage)
145 $propName = str_replace(
'n#IND#', $item->getPropertyValueId(), $this->getInputName());
146 $this->values[$propName] = $item->getId();
150 $this->values[$item::CODE] = $item->getId();
156 $item = $photoCollection->getFrontImage();
159 $this->values[$item::CODE] = $item->getId();
163 return $this->values;
166 private function getSignedValues(): array
170 foreach ($this->getValues() as $name => $fileId)
172 if ($fileId !==
null && is_numeric($fileId))
174 static $signer =
null;
175 if ($signer ===
null)
177 $signer =
new Signer;
180 $signedValues[$name] = $signer->sign((
string)$fileId, self::FILE_ID_SALT);
184 return $signedValues;
187 private function isMorePhotoEnabled(): bool
189 return (
int)$this->getMorePhotoPropertyId() > 0;
192 private function getMorePhotoPropertyId(): ?int
194 if ($this->morePhotoPropertyId ===
null)
196 $this->morePhotoPropertyId = 0;
197 $propertyRaw = \Bitrix\Iblock\PropertyTable::getList([
200 '=IBLOCK_ID' => $this->getIblockId(),
210 if ($morePhotoProperty = $propertyRaw->fetch())
212 $this->morePhotoPropertyId = $morePhotoProperty[
'ID'];
216 return $this->morePhotoPropertyId;
219 private function getHtml(array $settings = [], array $values = [], array $options = []): string
223 $GLOBALS[
'APPLICATION']->includeComponent(
224 'bitrix:catalog.image.input',
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(),
238 return ob_get_clean();
241 private function getPreview(): string
245 if ($this->getEntityId() > 0)
247 $photoCollection = $this->entity->getFrontImageCollection();
248 foreach ($photoCollection as $item)
250 if (!$item->getFileStructure())
256 $attributes->setAttribute(
'data-viewer-type',
'image');
257 $attributes->setGroupBy(
"group-{$this->entity->getId()}");
259 'SOURCE' => $item->getSource(),
260 'ATTRIBUTES' => $attributes,
265 $fileCount = min(count($imageData), 3);
270 $multipleClass =
' ui-image-input-img-block-multiple';
274 $multipleClass =
' ui-image-input-img-block-double';
278 $multipleClass =
' ui-image-input-img-block-empty';
288 foreach ($imageData as $key => $image)
293 $displayClass =
'main-ui-hide';
296 <img class='ui-image-input-img {$displayClass}' {$image['ATTRIBUTES']} src='{$image['SOURCE']}'>
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'>
static tryBuildByFileData(array $fileData, $sourceUri)