- Модуль: catalogmobile
- Путь к файлу: ~/bitrix/modules/catalogmobile/lib/Controller/StoreDocumentDetails.php
- Класс: Bitrix\CatalogMobile\Controller\StoreDocumentDetails
- Вызов: StoreDocumentDetails::updateFiles
private function updateFiles(int $documentId, array $files): void
{
$isSuccess = true;
$pendingFiles = null;
$fileIds = array_filter($files, static fn ($file) => !is_array($file));
$filesToSave = array_filter($files, static fn ($file) => is_array($file) && !empty($file['token']));
if (!empty($filesToSave))
{
$tokens = array_column($filesToSave, 'token');
$pendingFiles = $this->getDocumentPendingFiles($tokens);
$fileIds = array_merge($fileIds, $pendingFiles->getFileIds());
}
$existingFiles = StoreDocumentFileTable::getList([
'select' => ['ID', 'FILE_ID'],
'filter' => ['=DOCUMENT_ID' => $documentId],
])->fetchAll();
$existingFileIds = array_column($existingFiles, 'FILE_ID');
$filesToDelete = array_diff($existingFileIds, $fileIds);
if (!empty($filesToDelete))
{
$fileIdToPrimary = array_column($existingFiles, 'ID', 'FILE_ID');
$idsToDelete = array_intersect_key($fileIdToPrimary, array_fill_keys($filesToDelete, true));
foreach ($idsToDelete as $id)
{
$deleteResult = StoreDocumentFileTable::delete($id);
if ($deleteResult->isSuccess())
{
\CFile::Delete($id);
}
else
{
$isSuccess = false;
}
}
}
$filesToAdd = array_diff($fileIds, $existingFileIds);
foreach ($filesToAdd as $fileToAdd)
{
$addResult = StoreDocumentFileTable::add([
'DOCUMENT_ID' => $documentId,
'FILE_ID' => $fileToAdd,
]);
if ($addResult->isSuccess())
{
if ($pendingFile = $pendingFiles->getByFileId($fileToAdd))
{
$pendingFile->makePersistent();
}
}
else
{
$isSuccess = false;
}
}
if (!$isSuccess)
{
$this->addNonCriticalError(
new Error(
Loc::getMessage('MOBILE_CONTROLLER_CATALOG_DETAILS_ERROR_DOCUMENT_PRODUCTS_SAVE_ERROR')
)
);
}
}