Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
filetable.php
1<?php
8namespace Bitrix\Sender;
9
16use Bitrix\Fileman\Block\Editor as BlockEditor;
17use Bitrix\Fileman\Block\EditorMail as BlockEditorMail;
19
20Loc::loadMessages(__FILE__);
21
23{
24 private static $fileList = [];
25 public const TYPES = [
26 'LETTER' => 0,
27 'TEMPLATE' => 1,
28 ];
34 public static function getTableName()
35 {
36 return 'b_sender_file';
37 }
38
44 public static function getMap()
45 {
46 return array(
47 'ID' => array(
48 'data_type' => 'integer',
49 'autocomplete' => true,
50 'primary' => true,
51 ),
52 'FILE_ID' => array(
53 'data_type' => 'integer',
54 'required' => true,
55 ),
56 'ENTITY_TYPE' => array(
57 'data_type' => 'integer',
58 'required' => true,
59 ),
60 'ENTITY_ID' => array(
61 'data_type' => 'integer',
62 'required' => true,
63 ),
64 'DATE_INSERT' => array(
65 'data_type' => 'datetime',
66 'required' => true,
67 'default_value' => new MainType\DateTime(),
68 ),
69 );
70 }
71
72 public static function syncFiles(int $entityId, int $entityType, string $template, bool $deleteFiles = true, bool $onDeleteEntity = false)
73 {
74 preg_match_all(
75 '@src="([^"]+)"@',
76 $template,
77 $matches
78 );
79 $urls = array_pop($matches);
80 // get file list from html
81 $fileNameList = [];
82 $fileNameSearchList = [];
83 $files = [];
84 foreach ($urls as $path)
85 {
86 preg_match("/[^\/|\\\]+$/", $path, $url);
87
88 if (!$url)
89 {
90 continue;
91 }
92
93 if (!empty($url[0]))
94 {
95 $fileNameList[] = $url[0];
96 if (array_key_exists($url[0], static::$fileList))
97 {
98 $files[] = static::$fileList[$url[0]];
99 continue;
100 }
101 $fileNameSearchList[] = $url[0];
102 }
103
104 }
105
106 if (!empty($fileNameSearchList))
107 {
108 // get files from main FileTable which exists in html
109 $selectedFiles = \Bitrix\Main\FileTable::getList([
110 'select' => ['ID', 'FILE_NAME'],
111 'filter' => [
112 '=MODULE_ID' => 'sender',
113 '@FILE_NAME' => $fileNameSearchList,
114 ],
115 'order' => [
116 'ID' => 'ASC'
117 ]
118 ])->fetchAll();
119 $files = array_merge($files, $selectedFiles);
120
121 if (empty($selectedFiles))
122 {
123 foreach ($fileNameList as $fileName)
124 {
125 static::$fileList[$fileName] = null;
126 }
127 }
128 }
129
130 $batchData = [];
131 // get files in current sender file table
132 $currentFiles = array_column(self::getCurrentFiles($entityId, $entityType), 'FILE_ID');
133
134 $preparedFiles = [];
135 foreach ($currentFiles as $fileId)
136 {
137 $preparedFiles[$fileId] = $fileId;
138 }
139
140 $filesToDelete = [];
141 foreach ($files as $file)
142 {
143 if (is_null($file))
144 {
145 continue;
146 }
147 if (!isset(static::$fileList[$file['FILE_NAME']]))
148 {
149 static::$fileList[$file['FILE_NAME']] = $file;
150 }
151
152 if ($onDeleteEntity && in_array($file['ID'], $preparedFiles))
153 {
154 $filesToDelete[] = $file['ID'];
155 unset($preparedFiles[$file['ID']]);
156 continue;
157 }
158 // do nothing if file in current template
159 if (in_array($file['ID'], $preparedFiles))
160 {
161 unset($preparedFiles[$file['ID']]);
162 continue;
163 }
164
165 if (isset($batchData[$file['FILE_NAME']]))
166 {
167 $filesToDelete[] = $file['ID'];
168 continue;
169 }
170
171 foreach ($fileNameList as $fileName)
172 {
173 if ($fileName === $file['FILE_NAME'])
174 {
175 $batchData[$fileName] = [
176 'ENTITY_TYPE' => $entityType,
177 'ENTITY_ID' => $entityId,
178 'FILE_ID' => $file['ID'],
179 'DATE_INSERT' => new MainType\DateTime()
180 ];
181 }
182 }
183 }
184
185 foreach ($preparedFiles as $file)
186 {
187 self::deleteIfCan($file, $entityId, $entityType, $deleteFiles);
188 }
189
190 foreach ($filesToDelete as $file)
191 {
192 self::deleteIfCan($file, $entityId, $entityType, $deleteFiles);
193 }
194 SqlBatch::insert(self::getTableName(), $batchData);
195 }
196
197 private static function getCurrentFiles(int $entityId, int $entityType)
198 {
199 return self::getList([
200 'select' => ['FILE_ID'],
201 'filter' => [
202 '=ENTITY_ID' => $entityId,
203 '=ENTITY_TYPE' => $entityType,
204 ],
205 ])->fetchAll();
206 }
207
208 private static function deleteIfCan(int $fileId, int $entityId, int $entityType, bool $deleteFiles)
209 {
210 self::deleteList([
211 '=FILE_ID' => $fileId,
212 '=ENTITY_TYPE' => $entityType,
213 '=ENTITY_ID' => $entityId,
214 ]);
215
216 $hasFiles = self::getList([
217 'select' => ['ID',],
218 'filter' => [
219 '=FILE_ID' => $fileId
220 ],
221 'limit' => 1
222 ]
223 )->fetch();
224
225 if ($deleteFiles)
226 {
227 $deleteFiles = 1 === \COption::GetOptionInt(
228 'sender',
229 'sender_file_load_completed',
230 0
231 );
232 }
233
234 if (!$hasFiles && $deleteFiles)
235 {
236 \CFile::Delete($fileId);
237 }
238 }
239
240
248 public static function deleteList(array $filter)
249 {
250 $entity = static::getEntity();
251 $connection = $entity->getConnection();
252
253 \CTimeZone::disable();
254 $sql = sprintf(
255 'DELETE FROM %s WHERE %s',
256 $connection->getSqlHelper()->quote($entity->getDbTableName()),
257 Query::buildFilterSql($entity, $filter)
258 );
259 $res = $connection->query($sql);
260 \CTimeZone::enable();
261
262 return $res;
263 }
264}
static loadMessages($file)
Definition loc.php:64
static syncFiles(int $entityId, int $entityType, string $template, bool $deleteFiles=true, bool $onDeleteEntity=false)
Definition filetable.php:72
static deleteList(array $filter)