72 public static function syncFiles(
int $entityId,
int $entityType,
string $template,
bool $deleteFiles =
true,
bool $onDeleteEntity =
false)
79 $urls = array_pop($matches);
82 $fileNameSearchList = [];
84 foreach ($urls as $path)
86 preg_match(
"/[^\/|\\\]+$/", $path, $url);
95 $fileNameList[] = $url[0];
96 if (array_key_exists($url[0], static::$fileList))
98 $files[] = static::$fileList[$url[0]];
101 $fileNameSearchList[] = $url[0];
106 if (!empty($fileNameSearchList))
109 $selectedFiles = \Bitrix\Main\FileTable::getList([
110 'select' => [
'ID',
'FILE_NAME'],
112 '=MODULE_ID' =>
'sender',
113 '@FILE_NAME' => $fileNameSearchList,
119 $files = array_merge($files, $selectedFiles);
121 if (empty($selectedFiles))
123 foreach ($fileNameList as $fileName)
125 static::$fileList[$fileName] =
null;
132 $currentFiles = array_column(self::getCurrentFiles($entityId, $entityType),
'FILE_ID');
135 foreach ($currentFiles as $fileId)
137 $preparedFiles[$fileId] = $fileId;
141 foreach ($files as $file)
147 if (!isset(static::$fileList[$file[
'FILE_NAME']]))
149 static::$fileList[$file[
'FILE_NAME']] = $file;
152 if ($onDeleteEntity && in_array($file[
'ID'], $preparedFiles))
154 $filesToDelete[] = $file[
'ID'];
155 unset($preparedFiles[$file[
'ID']]);
159 if (in_array($file[
'ID'], $preparedFiles))
161 unset($preparedFiles[$file[
'ID']]);
165 if (isset($batchData[$file[
'FILE_NAME']]))
167 $filesToDelete[] = $file[
'ID'];
171 foreach ($fileNameList as $fileName)
173 if ($fileName === $file[
'FILE_NAME'])
175 $batchData[$fileName] = [
176 'ENTITY_TYPE' => $entityType,
177 'ENTITY_ID' => $entityId,
178 'FILE_ID' => $file[
'ID'],
185 foreach ($preparedFiles as $file)
187 self::deleteIfCan($file, $entityId, $entityType, $deleteFiles);
190 foreach ($filesToDelete as $file)
192 self::deleteIfCan($file, $entityId, $entityType, $deleteFiles);
194 SqlBatch::insert(self::getTableName(), $batchData);