45 public function listAction(
int $productId, array $select = [], \CRestServer $restServer =
null): ?
Page
49 $this->addError(
new Error(
'Empty productID'));
53 $product = $this->getProduct($productId);
56 $this->addError(
new Error(
'Product was not found'));
61 $r = $this->checkPermissionProductRead($product);
64 $this->addErrors($r->getErrors());
69 foreach ($product->getImageCollection() as $image)
71 $result[] = $this->prepareFileStructure($image, $restServer, $select);
90 public function getAction(
int $id,
int $productId, \CRestServer $restServer =
null): ?array
92 $product = $this->getProduct($productId);
95 $this->addError(
new Error(
'Product was not found'));
99 $r = $this->checkPermissionProductRead($product);
100 if (!$r->isSuccess())
102 $this->addErrors($r->getErrors());
106 $r = $this->hasImage($id, $product);
107 if (!$r->isSuccess())
109 $this->addErrors($r->getErrors());
114 $image = $product->getImageCollection()->findById($id);
116 return [
'PRODUCT_IMAGE' => $this->prepareFileStructure($image, $restServer)];
128 public function addAction(array $fields, array $fileContent, \CRestServer $restServer =
null): ?array
130 if (!Loader::includeModule(
'rest'))
135 $product = $this->getProduct((
int)$fields[
'PRODUCT_ID']);
138 $this->addError(
new Error(
'Product was not found'));
142 $r = $this->checkPermissionProductWrite($product);
143 if (!$r->isSuccess())
145 $this->addErrors($r->getErrors());
149 $fileData = \CRestUtil::saveFile($fileContent);
152 $this->addError(
new Error(
'Could not save image.'));
156 $checkPictureResult = \CFile::CheckFile($fileData, 0 ,
false, \CFile::GetImageExtensions());
157 if ($checkPictureResult !==
'')
159 $this->addError(
new Error($checkPictureResult));
163 if ($fields[
'TYPE'] === DetailImage::CODE)
165 $product->getImageCollection()->getDetailImage()->setFileStructure($fileData);
167 elseif ($fields[
'TYPE'] === PreviewImage::CODE)
169 $product->getImageCollection()->getPreviewImage()->setFileStructure($fileData);
173 if (!$product->getPropertyCollection()->findByCode(MorePhotoImage::CODE))
177 "Image product property does not exists. Create" . MorePhotoImage::CODE .
" property"
184 $product->getImageCollection()->addValue($fileData);
187 $r = $product->save();
188 if (!$r->isSuccess())
190 $this->addErrors($r->getErrors());
193 if ($fields[
'TYPE'] === DetailImage::CODE)
195 $image = $product->getImageCollection()->getDetailImage();
197 elseif ($fields[
'TYPE'] === PreviewImage::CODE)
199 $image = $product->getImageCollection()->getPreviewImage();
203 $morePhotos = $product->getImageCollection()->getMorePhotos();
204 $image = end($morePhotos);
207 return [
'PRODUCT_IMAGE' => $this->prepareFileStructure($image, $restServer)];
220 $product = $this->getProduct($productId);
223 $this->addError(
new Error(
'Product was not found'));
227 $r = $this->checkPermissionProductWrite($product);
228 if (!$r->isSuccess())
230 $this->addErrors($r->getErrors());
234 $r = $this->hasImage($id, $product);
235 if (!$r->isSuccess())
237 $this->addErrors($r->getErrors());
242 ->getImageCollection()
247 $r = $product->save();
248 if (!$r->isSuccess())
250 $this->addErrors($r->getErrors());
258 private function prepareFileStructure(
260 \CRestServer $restServer =
null,
261 array $selectedFields =
null
265 if (!$selectedFields)
267 $selectedFields = array_keys($this->getViewManager()->getView($this)->getFields());
270 foreach ($selectedFields as $name)
274 $result[$name] = $baseImage->
getField(
'ID');
276 if ($name ===
'NAME')
278 $result[$name] = $baseImage->
getField(
'FILE_NAME');
280 elseif ($name ===
'DETAIL_URL')
282 $result[$name] = $baseImage->
getSource();
284 elseif ($name ===
'DOWNLOAD_URL')
288 ? \CRestUtil::getDownloadUrl([
'id' => $baseImage->
getId()], $restServer)
289 : $baseImage->getSource()
292 elseif ($name ===
'CREATE_TIME')
294 $result[$name] = $baseImage->
getField(
'TIMESTAMP_X');
296 elseif ($name ===
'PRODUCT_ID')
298 $result[$name] = $baseImage->
getParent()->getId();
300 elseif ($name ===
'TYPE')
302 $result[$name] = $baseImage->
getCode();
314 private function getProduct(
int $productId): ?
BaseEntity
316 $product = ServiceContainer::getRepositoryFacade()->loadProduct($productId);
322 return ServiceContainer::getRepositoryFacade()->loadVariation($productId);
325 private function hasImage(
int $id, BaseEntity $product): Result
328 if (!$r->isSuccess())
333 $image = $product->getImageCollection()->findById($id);
336 $r->addError(
new Error(
'Image does not exist'));
345 if (!isset($this->
get($id)[
'ID']))
347 $r->addError(
new Error(
'Image does not exist'));
356 if (!$r->isSuccess())
361 return $this->checkPermissionProduct($product, self::IBLOCK_ELEMENT_READ, 200040300010);
364 private function checkPermissionProductWrite(BaseEntity $product): Result
367 if (!$r->isSuccess())
372 return $this->checkPermissionProduct($product, self::IBLOCK_ELEMENT_EDIT, 200040300020);
375 private function checkPermissionProduct(BaseEntity $product,
string $permission,
int $errorCode): Result
378 if(!\CIBlockElementRights::UserHasRightTo($product->getIblockId(), $product->getId(), $permission))
380 $r->addError(
new Error(
'Access Denied', $errorCode));
390 if (!$this->accessController->check(ActionDictionary::ACTION_CATALOG_VIEW))
392 $r->addError(
new Error(
'Access Denied', 200040300020));
403 !$this->accessController->check(ActionDictionary::ACTION_CATALOG_READ)
404 && !$this->accessController->check(ActionDictionary::ACTION_CATALOG_VIEW)
407 $r->addError(
new Error(
'Access Denied', 200040300010));