1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
filetable.php
См. документацию.
1<?php
8namespace Bitrix\Sender;
9
10use Bitrix\Main\DB;
11use Bitrix\Main\ORM;
12use Bitrix\Main\ORM\Fields\ExpressionField;
13use Bitrix\Main\ORM\Query\Query;
14use Bitrix\Main\Type as MainType;
15use Bitrix\Main\Localization\Loc;
16use Bitrix\Main\Loader;
17use Bitrix\Fileman\Block\Editor as BlockEditor;
18use Bitrix\Fileman\Block\EditorMail as BlockEditorMail;
19use Bitrix\Sender\Internals\Model\MessageFieldTable;
20use Bitrix\Sender\Internals\SqlBatch;
21use Bitrix\Sender\Internals\Model\FileInfoTable;
22
23Loc::loadMessages(__FILE__);
24
26{
27 private static $fileList = [];
28 public const TYPES = [
29 'LETTER' => 0,
30 'TEMPLATE' => 1,
31 ];
37 public static function getTableName()
38 {
39 return 'b_sender_file';
40 }
41
47 public static function getMap()
48 {
49 return array(
50 'ID' => array(
51 'data_type' => 'integer',
52 'autocomplete' => true,
53 'primary' => true,
54 ),
55 'FILE_ID' => array(
56 'data_type' => 'integer',
57 'required' => true,
58 ),
59 'ENTITY_TYPE' => array(
60 'data_type' => 'integer',
61 'required' => true,
62 ),
63 'ENTITY_ID' => array(
64 'data_type' => 'integer',
65 'required' => true,
66 ),
67 'DATE_INSERT' => array(
68 'data_type' => 'datetime',
69 'required' => true,
70 'default_value' => new MainType\DateTime(),
71 ),
72 );
73 }
74
75 public static function syncFiles(int $entityId, int $entityType, string $template, bool $deleteFiles = true, bool $onDeleteEntity = false)
76 {
77 preg_match_all(
78 '@src="([^"]+)"@',
81 );
82 $urls = array_pop($matches);
83 // get file list from html
84 $fileNameList = [];
85 $fileNameSearchList = [];
86 $files = [];
87 foreach ($urls as $path)
88 {
89 preg_match("/[^\/|\\\]+$/", $path, $url);
90
91 if (!$url)
92 {
93 continue;
94 }
95
96 if (!empty($url[0]))
97 {
98 $fileNameList[] = $url[0];
99 if (array_key_exists($url[0], static::$fileList))
100 {
101 $files[] = static::$fileList[$url[0]];
102 continue;
103 }
104 $fileNameSearchList[] = $url[0];
105 }
106
107 }
108
109 $useSenderFileInfoTable = \COption::GetOptionString('sender', 'sender_file_info_load_completed', 'N') === 'Y';
110 $attachmentFileIds = [];
111 $attachmentFiles = [];
112 if (
113 $entityType === FileTable::TYPES['LETTER']
114 && $useSenderFileInfoTable
115 )
116 {
117 $attachmentFiles = self::getMessageAttachmentFiles($entityId);
118
119 if (!empty($attachmentFiles))
120 {
121 foreach ($attachmentFiles as $attachmentFile)
122 {
123 $attachmentFileIds[] = (int)$attachmentFile['ID'];
124 }
125 $files = array_merge($files, $attachmentFiles);
126 }
127
128 }
129
130 if (!empty($fileNameSearchList))
131 {
132 // get files from main FileTable which exists in html
133 if ($useSenderFileInfoTable)
134 {
135 $selectedFiles = FileInfoTable::query()
136 ->setSelect(['ID', 'FILE_NAME'])
137 ->whereIn('FILE_NAME', $fileNameSearchList)
138 ->setOrder(['ID' => 'ASC'])
139 ->fetchAll()
140 ;
141 }
142 else
143 {
144 $selectedFiles = \Bitrix\Main\FileTable::getList([
145 'select' => ['ID', 'FILE_NAME'],
146 'filter' => [
147 '=MODULE_ID' => 'sender',
148 '@FILE_NAME' => $fileNameSearchList,
149 ],
150 'order' => [
151 'ID' => 'ASC'
152 ]
153 ])->fetchAll();
154 }
155
156 $files = array_merge($files, $selectedFiles);
157
158 if (empty($selectedFiles))
159 {
160 foreach ($fileNameList as $fileName)
161 {
162 static::$fileList[$fileName] = null;
163 }
164 }
165 }
166
167 $batchData = [];
168 // get files in current sender file table
169 $currentFiles = array_column(self::getCurrentFiles($entityId, $entityType), 'FILE_ID');
170
171 $preparedFiles = [];
172 $alreadyAttachedFileIds = [];
173 foreach ($currentFiles as $fileId)
174 {
175 if (in_array((int)$fileId, $attachmentFileIds, true))
176 {
177 $alreadyAttachedFileIds[] = (int)$fileId;
178 continue;
179 }
180
181 $preparedFiles[$fileId] = $fileId;
182 }
183
184 $filesToDelete = [];
185 foreach ($files as $file)
186 {
187 if (is_null($file))
188 {
189 continue;
190 }
191
192 if (!$onDeleteEntity && in_array((int)$file['ID'], $attachmentFileIds, true))
193 {
194 continue;
195 }
196
197 if (!isset(static::$fileList[$file['FILE_NAME']]))
198 {
199 static::$fileList[$file['FILE_NAME']] = $file;
200 }
201
202 if ($onDeleteEntity && in_array($file['ID'], $preparedFiles))
203 {
204 $filesToDelete[] = $file['ID'];
205 unset($preparedFiles[$file['ID']]);
206 continue;
207 }
208 // do nothing if file in current template
209 if (in_array($file['ID'], $preparedFiles))
210 {
211 unset($preparedFiles[$file['ID']]);
212 continue;
213 }
214
215 if (isset($batchData[$file['FILE_NAME']]))
216 {
217 $filesToDelete[] = $file['ID'];
218 continue;
219 }
220
221 foreach ($fileNameList as $fileName)
222 {
223 if ($fileName === $file['FILE_NAME'])
224 {
225 $batchData[$fileName] = [
226 'ENTITY_TYPE' => $entityType,
227 'ENTITY_ID' => $entityId,
228 'FILE_ID' => $file['ID'],
229 'DATE_INSERT' => new MainType\DateTime()
230 ];
231 }
232 }
233 }
234
235 foreach ($attachmentFiles as $file)
236 {
237 if (in_array((int)$file['ID'], $alreadyAttachedFileIds, true))
238 {
239 continue;
240 }
241
242 $batchData['ATTACHMENT_FILE' . $file['ID']] = [
243 'ENTITY_TYPE' => $entityType,
244 'ENTITY_ID' => $entityId,
245 'FILE_ID' => $file['ID'],
246 'DATE_INSERT' => new MainType\DateTime()
247 ];
248 }
249
250 foreach ($preparedFiles as $file)
251 {
252 self::deleteIfCan($file, $entityId, $entityType, $deleteFiles);
253 }
254
255 foreach ($filesToDelete as $file)
256 {
257 self::deleteIfCan($file, $entityId, $entityType, $deleteFiles);
258 }
259 SqlBatch::insert(self::getTableName(), $batchData);
260 }
261
262 private static function getCurrentFiles(int $entityId, int $entityType)
263 {
264 return self::getList([
265 'select' => ['FILE_ID'],
266 'filter' => [
267 '=ENTITY_ID' => $entityId,
268 '=ENTITY_TYPE' => $entityType,
269 ],
270 ])->fetchAll();
271 }
272
273 private static function getMessageAttachmentFiles(int $messageId, bool $useSenderFileInfoTable = true): array
274 {
275 $fileField = MessageFieldTable::getById([
276 'MESSAGE_ID' => $messageId,
277 'CODE' => 'ATTACHMENT',
278 ])->fetch();
279
280 if (
281 !$fileField
282 || $fileField['TYPE'] !== 'file'
283 || empty($fileField['VALUE'])
284 )
285 {
286 return [];
287 }
288
289 $attachmentFiles = explode(',', $fileField['VALUE']);
290 $attachmentIds = [];
291 foreach ($attachmentFiles as $attachmentFile)
292 {
293 if (is_numeric($attachmentFile))
294 {
295 $attachmentIds[] = (int)$attachmentFile;
296 }
297 }
298
299 if (empty($attachmentIds))
300 {
301 return [];
302 }
303
304 if ($useSenderFileInfoTable)
305 {
306 $attachmentFiles = FileInfoTable::query()
307 ->setSelect(['ID', 'FILE_NAME'])
308 ->whereIn('ID', $attachmentIds)
309 ->setOrder(['ID' => 'ASC'])
310 ->fetchAll()
311 ;
312 }
313 else
314 {
315 $attachmentFiles = \Bitrix\Main\FileTable::getList([
316 'select' => ['ID', 'FILE_NAME'],
317 'filter' => [
318 '=MODULE_ID' => 'sender',
319 '@ID' => $attachmentIds,
320 ],
321 'order' => [
322 'ID' => 'ASC'
323 ],
324 ])->fetchAll();
325 }
326
327 return $attachmentFiles;
328 }
329
330 private static function deleteIfCan(int $fileId, int $entityId, int $entityType, bool $deleteFiles)
331 {
332 self::deleteList([
333 '=FILE_ID' => $fileId,
334 '=ENTITY_TYPE' => $entityType,
335 '=ENTITY_ID' => $entityId,
336 ]);
337
338 $hasFiles = self::getList([
339 'select' => ['ID',],
340 'filter' => [
341 '=FILE_ID' => $fileId
342 ],
343 'limit' => 1
344 ]
345 )->fetch();
346
347 if ($deleteFiles)
348 {
349 $deleteFiles = 1 === \COption::GetOptionInt(
350 'sender',
351 'sender_file_load_completed',
352 0
353 );
354 }
355
356 if (!$hasFiles && $deleteFiles)
357 {
358 \CFile::Delete($fileId);
359 FileInfoTable::delete($fileId);
360 }
361 }
362
363
371 public static function deleteList(array $filter)
372 {
373 $entity = static::getEntity();
374 $connection = $entity->getConnection();
375
376 \CTimeZone::disable();
377 $sql = sprintf(
378 'DELETE FROM %s WHERE %s',
379 $connection->getSqlHelper()->quote($entity->getDbTableName()),
380 Query::buildFilterSql($entity, $filter)
381 );
382 $res = $connection->query($sql);
383 \CTimeZone::enable();
384
385 return $res;
386 }
387}
$path
Определения access_edit.php:21
$connection
Определения actionsdefinitions.php:38
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
Определения file.php:45
static syncFiles(int $entityId, int $entityType, string $template, bool $deleteFiles=true, bool $onDeleteEntity=false)
Определения filetable.php:75
static getMap()
Определения filetable.php:47
static deleteList(array $filter)
Определения filetable.php:371
const TYPES
Определения filetable.php:28
static getTableName()
Определения filetable.php:37
$template
Определения file_edit.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$filter
Определения iblock_catalog_list.php:54
$files
Определения mysql_to_pgsql.php:30
$entityId
Определения payment.php:4
$fileName
Определения quickway.php:305
$matches
Определения index.php:22
$url
Определения iframe.php:7