111 public static function checkFiles(
Forum $forum, &$files, $params = [
"TOPIC_ID" => 0,
"MESSAGE_ID" => 0,
"USER_ID" => 0])
113 $result = new \Bitrix\Main\Result();
120 if (array_key_exists(
"name", $files))
122 $files = array($files);
125 $filesize = intval(\
Bitrix\Main\
Config\Option::get(
"forum",
"file_max_size", 5242880));
127 foreach ($files as $key => $file)
129 if (isset($file[
"FILE_ID"]) && $file[
"FILE_ID"] > 0)
131 $files[$key][
"old_file"] = $file[
"FILE_ID"];
132 $existingFiles[] = $file[
"FILE_ID"];
135 if ($file[
"name"] ==
'')
142 if ($forum[
"ALLOW_UPLOAD"] ==
"Y")
144 $res = \CFile::CheckImageFile($file, $filesize, 0, 0);
146 elseif ($forum[
"ALLOW_UPLOAD"] ==
"F")
148 $res = \CFile::CheckFile($file, $filesize,
false, $forum[
"ALLOW_UPLOAD_EXT"]);
150 elseif ($forum[
"ALLOW_UPLOAD"] ==
"A")
152 $res = \CFile::CheckFile($file, $filesize,
false,
false);
156 $res =
"Uploading is forbidden";
160 $result->addError(
new Main\
Error($res));
164 if (!empty($existingFiles))
166 $dbRes = FileTable::getList([
167 "select" => [
"FILE_ID"],
169 "FORUM_ID" => $params[
"FORUM_ID"] ?: $forum->getId(),
172 [
"TOPIC_ID" => $params[
"TOPIC_ID"]],
173 [
"TOPIC_ID" =>
null],
175 "MESSAGE_ID" => $params[
"MESSAGE_ID"],
176 "FILE_ID" => $existingFiles
177 ] + ($params[
"MESSAGE_ID"] > 0 ? [] : [
"USER_ID" => $params[
"USER_ID"]]),
182 while ($res = $dbRes->fetch())
184 if (in_array($res[
"FILE_ID"], $existingFiles))
186 $existingFiles = array_diff($existingFiles, [$res[
"FILE_ID"]]);
189 if (!empty($existingFiles))
191 $result->addError(
new Main\
Error(
"The file is occupied."));
197 public static function saveFiles(&$files, $params, $uploadDir =
"forum/upload")
202 $result = new \Bitrix\Main\Result();
203 foreach ($files as $key => $file)
205 $file[
"MODULE_ID"] =
"forum";
207 if (array_key_exists(
"del", $file))
209 $id = $file[
"old_file"] ?: $file[
"FILE_ID"];
211 $filesToDel[$id] = $file + [
"key" => $key];
213 if ($file[
"FILE_ID"] > 0)
215 $filesToUpdate[$file[
"FILE_ID"]] = $file + [
"key" => $key];
219 $id = \CFile::SaveFile($file, $uploadDir);
222 $files[$key][
"FILE_ID"] = $id;
223 $filesToAdd[$id] = $file + [
"key" => $key];
227 $result->addError(
new Main\
Error(
"The file is not saved."));
231 foreach ($filesToDel as $id => $file)
234 unset($files[$file[
"key"]]);
236 if (!empty($filesToUpdate))
239 "FORUM_ID" => $params[
"FORUM_ID"],
240 "TOPIC_ID" => $params[
"TOPIC_ID"],
241 "MESSAGE_ID" => $params[
"MESSAGE_ID"],
242 "USER_ID" => $params[
"USER_ID"]
244 FileTable::updateMulti(array_keys($filesToUpdate), $row);
245 foreach ($filesToUpdate as $id => $file)
247 $files[$file[
"key"]] = array_merge($files[$file[
"key"]], $row);
250 if (!empty($filesToAdd))
253 foreach ($filesToAdd as $id => $file)
257 "FORUM_ID" => $params[
"FORUM_ID"],
258 "TOPIC_ID" => $params[
"TOPIC_ID"],
259 "MESSAGE_ID" => $params[
"MESSAGE_ID"],
260 "USER_ID" => $params[
"USER_ID"]
262 $files[$file[
"key"]] = array_merge($files[$file[
"key"]], $row);
265 FileTable::addMulti($rows);
267 $result->setData(array_keys($filesToUpdate) + array_keys($filesToAdd));