Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
file.php
1<?php
2namespace Bitrix\Forum;
3
24
55class FileTable extends Main\Entity\DataManager
56{
62 public static function getTableName()
63 {
64 return 'b_forum_file';
65 }
66
72 public static function getMap()
73 {
74 return [
75// (new IntegerField("ID", ["primary" => true, "autocomplete" => true])),
76 (new IntegerField("FORUM_ID", ["required" => true])),
77 (new IntegerField("TOPIC_ID")),
78 (new IntegerField("MESSAGE_ID")),
79 (new IntegerField("FILE_ID", ["primary" => true])),
80 (new IntegerField("USER_ID")),
81 (new Reference("USER", \Bitrix\Main\UserTable::class, Join::on("this.USER_ID", "ref.ID"))),
82 (new DatetimeField("TIMESTAMP_X", ["default_value" => function(){return new DateTime();}])),
83 (new IntegerField("HITS")),
84 (new Reference("FORUM", ForumTable::class, Join::on("this.FORUM_ID", "ref.ID"))),
85 (new Reference("FILE", Main\FileTable::class, Join::on("this.FILE_ID", "ref.ID")))
86 ];
87 }
88
89 public static function deleteBatch(array $filter)
90 {
91 $tableName = static::getTableName();
92 $connection = Application::getConnection();
93 $helper = $connection->getSqlHelper();
94
95 $where = [];
96 foreach ($filter as $key => $value)
97 {
98 $where[] = $helper->prepareAssignment($tableName, $key, $value);
99 }
100 $where = implode(' AND ', $where);
101
102 if($where)
103 {
104 $quotedTableName = $helper->quote($tableName);
105 $connection->queryExecute("DELETE FROM {$quotedTableName} WHERE {$where}");
106 }
107 }
108}
109class File
110{
111 public static function checkFiles(Forum $forum, &$files, $params = ["TOPIC_ID" => 0, "MESSAGE_ID" => 0, "USER_ID" => 0])
112 {
113 $result = new \Bitrix\Main\Result();
114
115 if (empty($files))
116 {
117 return $result;
118 }
119
120 if (array_key_exists("name", $files))
121 {
122 $files = array($files);
123 }
124
125 $filesize = intval(\Bitrix\Main\Config\Option::get("forum", "file_max_size", 5242880));
126 $existingFiles = [];
127 foreach ($files as $key => $file)
128 {
129 if (isset($file["FILE_ID"]) && $file["FILE_ID"] > 0)
130 {
131 $files[$key]["old_file"] = $file["FILE_ID"];
132 $existingFiles[] = $file["FILE_ID"];
133 continue;
134 }
135 if ($file["name"] == '')
136 {
137 unset($files[$key]);
138 continue;
139 }
140
141 // Y - Image files F - Files of specified type A - All files
142 if ($forum["ALLOW_UPLOAD"] == "Y")
143 {
144 $res = \CFile::CheckImageFile($file, $filesize, 0, 0);
145 }
146 elseif ($forum["ALLOW_UPLOAD"] == "F")
147 {
148 $res = \CFile::CheckFile($file, $filesize, false, $forum["ALLOW_UPLOAD_EXT"]);
149 }
150 elseif ($forum["ALLOW_UPLOAD"] == "A")
151 {
152 $res = \CFile::CheckFile($file, $filesize, false, false);
153 }
154 else
155 {
156 $res = "Uploading is forbidden";
157 }
158 if ($res <> '')
159 {
160 $result->addError(new Main\Error($res));
161 }
162 }
163
164 if (!empty($existingFiles))
165 {
166 $dbRes = FileTable::getList([
167 "select" => ["FILE_ID"],
168 "filter" => [
169 "FORUM_ID" => $params["FORUM_ID"] ?: $forum->getId(),
170 [
171 "LOGIC" => "OR",
172 ["TOPIC_ID" => $params["TOPIC_ID"]],
173 ["TOPIC_ID" => null],
174 ],
175 "MESSAGE_ID" => $params["MESSAGE_ID"],
176 "FILE_ID" => $existingFiles
177 ] + ($params["MESSAGE_ID"] > 0 ? [] : ["USER_ID" => $params["USER_ID"]]),
178 "order" => [
179 "FILE_ID" => "ASC"
180 ]
181 ]);
182 while ($res = $dbRes->fetch())
183 {
184 if (in_array($res["FILE_ID"], $existingFiles))
185 {
186 $existingFiles = array_diff($existingFiles, [$res["FILE_ID"]]);
187 }
188 }
189 if (!empty($existingFiles))
190 {
191 $result->addError(new Main\Error("The file is occupied."));
192 }
193 }
194 return $result;
195 }
196
197 public static function saveFiles(&$files, $params, $uploadDir = "forum/upload")
198 {
199 $filesToUpdate = [];
200 $filesToAdd = [];
201 $filesToDel = [];
202 $result = new \Bitrix\Main\Result();
203 foreach ($files as $key => $file)
204 {
205 $file["MODULE_ID"] = "forum";
206
207 if (array_key_exists("del", $file))
208 {
209 $id = $file["old_file"] ?: $file["FILE_ID"];
210 \CFile::Delete($id);
211 $filesToDel[$id] = $file + ["key" => $key];
212 }
213 if ($file["FILE_ID"] > 0)
214 {
215 $filesToUpdate[$file["FILE_ID"]] = $file + ["key" => $key];
216 }
217 else
218 {
219 $id = \CFile::SaveFile($file, $uploadDir);
220 if ($id > 0)
221 {
222 $files[$key]["FILE_ID"] = $id;
223 $filesToAdd[$id] = $file + ["key" => $key];
224 }
225 else
226 {
227 $result->addError(new Main\Error("The file is not saved."));
228 }
229 }
230 }
231 foreach ($filesToDel as $id => $file)
232 {
234 unset($files[$file["key"]]);
235 }
236 if (!empty($filesToUpdate))
237 {
238 $row = [
239 "FORUM_ID" => $params["FORUM_ID"],
240 "TOPIC_ID" => $params["TOPIC_ID"],
241 "MESSAGE_ID" => $params["MESSAGE_ID"],
242 "USER_ID" => $params["USER_ID"]
243 ];
244 FileTable::updateMulti(array_keys($filesToUpdate), $row);
245 foreach ($filesToUpdate as $id => $file)
246 {
247 $files[$file["key"]] = array_merge($files[$file["key"]], $row);
248 }
249 }
250 if (!empty($filesToAdd))
251 {
252 $rows = [];
253 foreach ($filesToAdd as $id => $file)
254 {
255 $row = [
256 "FILE_ID" => $id,
257 "FORUM_ID" => $params["FORUM_ID"],
258 "TOPIC_ID" => $params["TOPIC_ID"],
259 "MESSAGE_ID" => $params["MESSAGE_ID"],
260 "USER_ID" => $params["USER_ID"]
261 ];
262 $files[$file["key"]] = array_merge($files[$file["key"]], $row);
263 $rows[] = $row;
264 }
265 FileTable::addMulti($rows);
266 }
267 $result->setData(array_keys($filesToUpdate) + array_keys($filesToAdd));
268 return $result;
269 }
270}
static checkFiles(Forum $forum, &$files, $params=["TOPIC_ID"=> 0, "MESSAGE_ID"=> 0, "USER_ID"=> 0])
Definition file.php:111
static saveFiles(&$files, $params, $uploadDir="forum/upload")
Definition file.php:197
static deleteBatch(array $filter)
Definition file.php:89
static getTableName()
Definition file.php:62
static getConnection($name="")
static delete($primary)
Definition file.php:242