ProductImage::addAction

  1. Bitrix24 API (v. 23.675.0)
  2. catalog
  3. ProductImage
  4. addAction
  • Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/controller/productimage.php
  • Класс: BitrixCatalogControllerProductImage
  • Вызов: ProductImage::addAction
public function addAction(array $fields, array $fileContent, CRestServer $restServer = null): ?array
{
	if (!Loader::includeModule('rest'))
	{
		return null;
	}

	$product = $this->getProduct((int)$fields['PRODUCT_ID']);
	if (!$product)
	{
		$this->addError(new Error('Product was not found'));
		return null;
	}

	$r = $this->checkPermissionProductWrite($product);
	if (!$r->isSuccess())
	{
		$this->addErrors($r->getErrors());
		return null;
	}

	$fileData = CRestUtil::saveFile($fileContent);
	if (!$fileData)
	{
		$this->addError(new Error('Could not save image.'));
		return null;
	}

	$checkPictureResult = CFile::CheckFile($fileData, 0 ,false, CFile::GetImageExtensions());
	if ($checkPictureResult !== '')
	{
		$this->addError(new Error($checkPictureResult));
		return null;
	}

	if ($fields['TYPE'] === DetailImage::CODE)
	{
		$product->getImageCollection()->getDetailImage()->setFileStructure($fileData);
	}
	elseif ($fields['TYPE'] === PreviewImage::CODE)
	{
		$product->getImageCollection()->getPreviewImage()->setFileStructure($fileData);
	}
	else
	{
		if (!$product->getPropertyCollection()->findByCode(MorePhotoImage::CODE))
		{
			$this->addError(
				new Error(
					"Image product property does not exists. Create" . MorePhotoImage::CODE . " property"
				)
			);

			return null;
		}

		$product->getImageCollection()->addValue($fileData);
	}

	$r = $product->save();
	if (!$r->isSuccess())
	{
		$this->addErrors($r->getErrors());
	}

	if ($fields['TYPE'] === DetailImage::CODE)
	{
		$image = $product->getImageCollection()->getDetailImage();
	}
	elseif ($fields['TYPE'] === PreviewImage::CODE)
	{
		$image = $product->getImageCollection()->getPreviewImage();
	}
	else
	{
		$morePhotos = $product->getImageCollection()->getMorePhotos();
		$image = end($morePhotos);
	}

	return ['PRODUCT_IMAGE' => $this->prepareFileStructure($image, $restServer)];
}

Добавить комментарий