47 $this->addError(
new Error(
'Empty productID'));
52 $product = $this->getProduct($productId);
55 $this->addError(
new Error(
'Product was not found'));
60 $r = $this->checkPermissionProductRead($product);
63 $this->addErrors($r->getErrors());
69 if ($product[
'PREVIEW_PICTURE'])
71 $imageIds[] = $product[
'PREVIEW_PICTURE'];
73 if ($product[
'DETAIL_PICTURE'])
75 $imageIds[] = $product[
'DETAIL_PICTURE'];
77 if ($this->getMorePhotoPropertyId((
int)$product[
'IBLOCK_ID']))
79 $imageIds = [...$imageIds, ...$this->getMorePhotoPropertyValues($product)];
83 $fileTableResult = FileTable::getList([
'filter' => [
'=ID' => $imageIds]]);
84 while ($image = $fileTableResult->fetch())
86 if ($product[
'PREVIEW_PICTURE'] === $image[
'ID'])
88 $type =
'PREVIEW_PICTURE';
90 elseif ($product[
'DETAIL_PICTURE'] === $image[
'ID'])
92 $type =
'DETAIL_PICTURE';
98 $image[
'TYPE'] =
$type;
99 $image[
'PRODUCT_ID'] = $product[
'ID'];
101 $result[] = $this->prepareFileStructure($image, $restServer,
$select);
122 $product = $this->getProduct($productId);
125 $this->addError(
new Error(
'Product was not found'));
130 $r = $this->checkPermissionProductRead($product);
131 if (!$r->isSuccess())
133 $this->addErrors($r->getErrors());
138 $image = $this->getImageById($id, $product);
146 return [$this->
getServiceItemName() => $this->prepareFileStructure($image, $restServer)];
160 if (!Loader::includeModule(
'rest'))
165 $product = $this->getProduct((
int)
$fields[
'PRODUCT_ID']);
168 $this->addError(
new Error(
'Product was not found'));
172 $r = $this->checkPermissionProductWrite($product);
173 if (!$r->isSuccess())
175 $this->addErrors($r->getErrors());
182 $this->addError(
new Error(
'Could not save image.'));
186 $checkPictureResult = \CFile::CheckFile($fileData, 0 ,
false, \CFile::GetImageExtensions());
187 if ($checkPictureResult !==
'')
189 $this->addError(
new Error($checkPictureResult));
193 if (
$fields[
'TYPE'] ===
'DETAIL_PICTURE' ||
$fields[
'TYPE'] ===
'PREVIEW_PICTURE')
195 $updateFields = [
$fields[
'TYPE'] => $fileData];
199 $fields[
'TYPE'] =
'MORE_PHOTO';
200 $morePhotoProperty = $this->getMorePhotoProperty((
int)$product[
'IBLOCK_ID']);
201 if (!$morePhotoProperty->isSuccess())
203 $this->addErrors($morePhotoProperty->getErrors());
210 'VALUE' => $fileData,
219 if (
$fields[
'TYPE'] ===
'DETAIL_PICTURE' ||
$fields[
'TYPE'] ===
'PREVIEW_PICTURE')
221 $error = $this->updateProductImage((
int)$product[
'ID'], $updateFields);
225 $error = $this->updateProductMorePhoto((
int)$product[
'ID'], (
int)$product[
'IBLOCK_ID'], $updateFields);
230 $error =
'Internal error adding product image. Try adding again.';
241 if (
$fields[
'TYPE'] ===
'DETAIL_PICTURE' ||
$fields[
'TYPE'] ===
'PREVIEW_PICTURE')
243 $product = $this->getProduct((
int)
$fields[
'PRODUCT_ID'], [
$fields[
'TYPE']]);
244 $imageId = $product[
$fields[
'TYPE']];
248 $morePhotoIds = $this->getMorePhotoPropertyValues($product);
251 $this->addError(
new Error(
'Empty image.'));
255 $imageId = end($morePhotoIds);
257 $image = FileTable::getRowById($imageId);
264 $image[
'TYPE'] =
$fields[
'TYPE'];
265 $image[
'PRODUCT_ID'] =
$fields[
'PRODUCT_ID'];
267 return [$this->
getServiceItemName() => $this->prepareFileStructure($image, $restServer)];
287 $product = $this->getProduct($productId);
290 $this->addError(
new Error(
'Product was not found'));
295 $r = $this->checkPermissionProductWrite($product);
296 if (!$r->isSuccess())
298 $this->addErrors($r->getErrors());
303 if ($id === (
int)$product[
'PREVIEW_PICTURE'])
305 $updateFields = [
'PREVIEW_PICTURE' => \CIBlock::makeFileArray(
null,
true)];
307 elseif ($id === (
int)$product[
'DETAIL_PICTURE'])
309 $updateFields = [
'DETAIL_PICTURE' => \CIBlock::makeFileArray(
null,
true)];
313 $morePhotoPropertyValueId = $this->getMorePhotoPropertyValueId($product, $id);
314 if (!$morePhotoPropertyValueId)
320 $updateFields = [$morePhotoPropertyValueId => \CIBlock::makeFileArray(
null,
true)];
327 if ($id === (
int)$product[
'PREVIEW_PICTURE'] || $id === (
int)$product[
'DETAIL_PICTURE'])
329 $error = $this->updateProductImage((
int)$product[
'ID'], $updateFields);
333 $error = $this->updateProductMorePhoto((
int)$product[
'ID'], (
int)$product[
'IBLOCK_ID'], $updateFields);
338 $error =
'Internal error deleting product image. Try deleting again.';
353 private function prepareFileStructure(
356 array $selectedFields =
null
360 if (!$selectedFields)
362 $selectedFields = array_keys($this->getViewManager()->getView($this)->getFields());
365 foreach ($selectedFields as
$name)
371 if (
$name ===
'NAME')
383 ? \CRestUtil::getDownloadUrl([
'id' => $image[
'ID']], $restServer)
384 : \CFile::getFileSRC($image)
411 return \Bitrix\Iblock\ElementTable::getRow([
412 'select' =>
$select ?: [
'ID',
'IBLOCK_ID',
'PREVIEW_PICTURE',
'DETAIL_PICTURE'],
413 'filter' => [
'=ID' => $productId],
417 private function getImageById(
int $id,
array $product): ?
array
419 if ((
int)$product[
'PREVIEW_PICTURE'] === $id)
421 $type =
'PREVIEW_PICTURE';
423 elseif ((
int)$product[
'DETAIL_PICTURE'] === $id)
425 $type =
'DETAIL_PICTURE';
429 $morePhotoIds = $this->getMorePhotoPropertyValues($product);
430 if (!in_array($id, $morePhotoIds))
435 $type =
'MORE_PHOTO';
438 $image = FileTable::getRowById($id);
444 $image[
'TYPE'] =
$type;
445 $image[
'PRODUCT_ID'] = $product[
'ID'];
450 private function getMorePhotoPropertyValueId(
array $product,
int $value): ?int
452 $morePhotoPropertyId = $this->getMorePhotoPropertyId((
int)$product[
'IBLOCK_ID']);
453 if (!$morePhotoPropertyId)
458 $propertyValuesResult = $this->getPropertyValues($product, $morePhotoPropertyId,
true);
459 $morePhotoPropertyIds = $propertyValuesResult[$morePhotoPropertyId];
460 if (!$morePhotoPropertyIds)
465 $valueIndex = array_search($value, $morePhotoPropertyIds);
466 if ($valueIndex ===
false)
471 return (
int)$propertyValuesResult[
'PROPERTY_VALUE_ID'][$morePhotoPropertyId][$valueIndex] ??
null;
474 private function getMorePhotoPropertyValues(
array $product):
array
476 $morePhotoPropertyId = $this->getMorePhotoPropertyId((
int)$product[
'IBLOCK_ID']);
477 if (!$morePhotoPropertyId)
481 $propertyValuesResult = $this->getPropertyValues($product, $morePhotoPropertyId);
483 return $propertyValuesResult[$morePhotoPropertyId] ?? [];
486 private function getPropertyValues(
array $product,
int $propertyId,
bool $extMode =
false):
array
488 return \CIBlockElement::getPropertyValues(
489 $product[
'IBLOCK_ID'],
491 'ID' => $product[
'ID'],
492 'IBLOCK_ID' => $product[
'IBLOCK_ID'],
501 private function getMorePhotoPropertyId(
int $iblockId): ?int
503 return PropertyTable::getRow([
507 '=CODE' =>
'MORE_PHOTO',
517 private function getMorePhotoProperty(
int $iblockId): Result
520 $row = PropertyTable::getRow([
528 '=CODE' =>
'MORE_PHOTO',
538 'Image product property does not exists. Create MORE_PHOTO property'
544 if ($row[
'ACTIVE'] !==
'Y')
547 'Image product property does not active. Activate MORE_PHOTO property'
556 'Image product property is of the wrong type'
562 $result->setData([
'ID' => (
int)$row[
'ID']]);
567 private function updateProductImage(
int $productId,
array $updateFields): string
569 $iblockElement = new \CIBlockElement();
570 $iblockElement->update($productId, $updateFields);
572 return $iblockElement->getLastError();
575 private function updateProductMorePhoto(
int $productId,
int $iblockId,
array $updateFields): string
577 \CIBlockElement::SetPropertyValues(
586 return $exception->GetString();
592 private function checkPermissionProductRead(
array $product): Result
595 if (!$r->isSuccess())
603 private function checkPermissionProductWrite(
array $product): Result
606 if (!$r->isSuccess())
614 private function checkPermissionProduct(
array $product,
string $permission,
int $errorCode): Result
617 if(!\CIBlockElementRights::UserHasRightTo($product[
'IBLOCK_ID'], $product[
'ID'], $permission))
619 $r->addError(
new Error(
'Access Denied', $errorCode));
629 if (!$this->accessController->check(ActionDictionary::ACTION_CATALOG_VIEW))
642 !$this->accessController->check(ActionDictionary::ACTION_CATALOG_READ)
643 && !$this->accessController->check(ActionDictionary::ACTION_CATALOG_VIEW)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)