- Модуль: catalog
- Путь к файлу: ~/bitrix/modules/catalog/lib/controller/product.php
- Класс: BitrixCatalogControllerProduct
- Вызов: Product::downloadAction
public function downloadAction(array $fields): ?EngineResponseBFile
{
$productId = $fields['PRODUCT_ID'];
$fieldName = $fields['FIELD_NAME'];
$id = $fields['FILE_ID'];
$file = [];
$r = $this->exists($productId);
if($r->isSuccess())
{
$iblockId = $this->get($productId)['IBLOCK_ID'];
if($this->checkFieldsDownload(['NAME'=>$fieldName, 'IBLOCK_ID'=>$iblockId]) == true)
{
$files = [];
$iBlock = CIBlock::GetArrayByID($iblockId);
if ($productId > 0)
{
$element = CIBlockElement::GetList(
array(),
array(
"CATALOG_ID" => $iBlock["ID"],
"=ID" => $productId,
"CHECK_PERMISSIONS" => "N",
),
false,
false,
array("ID", $fieldName)
);
while ($res = $element->GetNext())
{
if (isset($res[$fieldName]))
{
$files[] = $res[$fieldName];
}
elseif (isset($res[$fieldName."_VALUE"]))
{
if (is_array($res[$fieldName."_VALUE"]))
{
$files = array_merge($files, $res[$fieldName."_VALUE"]);
}
else
{
$files[] = $res[$fieldName."_VALUE"];
}
}
}
}
if (!in_array($id, $files))
{
$r->addError(new Error('Product file wrong'));
}
else
{
$file = CFile::GetFileArray($id);
if (is_array($file) == false)
{
$r->addError(new Error('Product is empty'));
}
}
}
else
{
$r->addError(new Error('Name file field is not available'));
}
}
if($r->isSuccess())
{
return BitrixMainEngineResponseBFile::createByFileId($file['ID']);
}
else
{
$this->addErrors($r->getErrors());
return null;
}
}