1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
FileCollection.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Entity\File;
4
5use Bitrix\Disk\File;
6use Bitrix\Disk\Driver;
7use Bitrix\Disk\Folder;
8use Bitrix\Disk\Internals\FileTable;
9use Bitrix\Im\Model\EO_FileTemporary;
10use Bitrix\Im\Model\EO_FileTemporary_Collection;
11use Bitrix\Im\V2\Entity\EntityCollection;
12use Bitrix\Im\V2\Entity\User\UserPopupItem;
13use Bitrix\Im\V2\Registry;
14use Bitrix\Im\V2\Rest\PopupData;
15use Bitrix\Im\V2\Result;
16use Bitrix\Im\V2\TariffLimit\DateFilterable;
17use Bitrix\Im\V2\TariffLimit\FilterResult;
18use Bitrix\Main\Loader;
19use Bitrix\Main\ORM\Query\Query;
20use Bitrix\Main\Type\DateTime;
21
27{
28 protected static array $preloadDiskFiles = [];
29
34 public function __construct(?array $diskFiles = null, ?int $chatId = null)
35 {
36 parent::__construct();
37
38 if ($diskFiles !== null)
39 {
40 foreach ($diskFiles as $diskFile)
41 {
42 $this[] = new FileItem($diskFile, $chatId);
43 }
44 }
45 }
46
47 public static function getRestEntityName(): string
48 {
49 return 'files';
50 }
51
57 public static function initByDiskFilesIds(array $diskFilesIds, ?int $chatId = null): self
58 {
59 if (empty($diskFilesIds) || !Loader::includeModule('disk'))
60 {
61 return new static();
62 }
63
64 [$preloadDiskFiles, $filesToLoad] = static::getPreloadDiskFile($diskFilesIds);
65 $diskFiles = [];
66
67 if (!empty($filesToLoad))
68 {
69 $diskFiles = File::getModelList([
70 'filter' => Query::filter()->whereIn('ID', $filesToLoad)->where('TYPE', FileTable::TYPE),
71 'with' => ['PREVIEW'],
72 ]);
73 }
74
75 return new static(array_merge($diskFiles, $preloadDiskFiles), $chatId);
76 }
77
78 public function getDiskFiles(): array
79 {
80 $diskFiles = [];
81
82 foreach ($this as $file)
83 {
84 $diskFile = $file->getDiskFile();
85 if ($diskFile)
86 {
87 $diskFiles[$diskFile->getId()] = $diskFile;
88 }
89 }
90
91 return $diskFiles;
92 }
93
94 public function getMessageOut(): array
95 {
96 $result = [];
97
98 foreach ($this as $file)
99 {
100 $messageOut = $file->getMessageOut();
101 if ($messageOut)
102 {
103 $result[] = $messageOut;
104 }
105 }
106
107 return $result;
108 }
109
114 public function copyTo(Folder $folder): Result
115 {
116 $result = new Result();
117 $copies = new static();
118
119 foreach ($this as $fileEntity)
120 {
121 $copy = $fileEntity->copyTo($folder)->getResult();
122 if (isset($copy))
123 {
124 $copies[] = $copy;
125 }
126 }
127
128 if ($copies->count() > 0)
129 {
130 $result->setResult($copies);
131 }
132 else
133 {
135 }
136
137 return $result;
138 }
139
143 public function copyToOwnSavedFiles(): Result
144 {
145 $result = $this->getOwnStorageFolderByType(FolderType::SavedFiles);
146 if (!$result->hasResult())
147 {
148 return $result;
149 }
150
151 return $this->copyTo($result->getResult());
152 }
153
158 {
159 $result = $this->getOwnStorageFolderByType(FolderType::UploadedFiles);
160 if (!$result->hasResult())
161 {
162 return $result;
163 }
164
165 return $this->copyTo($result->getResult());
166 }
167
172 private function getOwnStorageFolderByType(FolderType $folderType): Result
173 {
174 $result = new Result();
175
176 $userId = $this->getContext()->getUserId();
177 $storage = Driver::getInstance()->getStorageByUserId($userId);
178 if (!isset($storage))
179 {
180 return $result->addError(new FileError(FileError::STORAGE_NOT_FOUND));
181 }
182
183 $folder = $storage->getSpecificFolderByCode($folderType->value);
184 if (!isset($folder))
185 {
186 return $result->addError(new FileError(FileError::FOLDER_NOT_FOUND));
187 }
188
189 return $result->setResult($folder);
190 }
191
192 public function addToTmp(string $source): Result
193 {
194 $tmpCollection = new EO_FileTemporary_Collection();
195
196 foreach ($this as $file)
197 {
198 $tmpEntity = new EO_FileTemporary(['DISK_FILE_ID' => $file->getId(), 'SOURCE' => $source]);
199 $tmpCollection->add($tmpEntity);
200 }
201
202 $addResult = $tmpCollection->save(true);
203
204 if (!$addResult->isSuccess())
205 {
206 return (new Result())->addErrors($addResult->getErrors());
207 }
208
209 return new Result();
210 }
211
212 public function getFileDiskAttributes(int $chatId, array $options = []): array
213 {
214 $resultData = [];
215 foreach ($this as $file)
216 {
217 $resultData[$file->getDiskFileId()] = \CIMDisk::GetFileParams($chatId, $file->getDiskFile(), $options = []);
218 }
219
220 return $resultData;
221 }
222
227 public static function addDiskFilesToPreload(array $diskFiles): void
228 {
229 foreach ($diskFiles as $diskFile)
230 {
231 if ($diskFile instanceof File)
232 {
233 static::$preloadDiskFiles[$diskFile->getId()] = $diskFile;
234 }
235 }
236 }
237
238 protected static function getPreloadDiskFile(array $diskFileIds): array
239 {
241 $filesToLoad = [];
242
243 foreach ($diskFileIds as $diskFileId)
244 {
245 if (isset(self::$preloadDiskFiles[$diskFileId]))
246 {
247 $preloadDiskFiles[] = self::$preloadDiskFiles[$diskFileId];
248 }
249 else
250 {
251 $filesToLoad[] = $diskFileId;
252 }
253 }
254
255 return [$preloadDiskFiles, $filesToLoad];
256 }
257
258 public function getPopupData(array $excludedList = []): PopupData
259 {
260 $data = new PopupData([new UserPopupItem()], $excludedList);
261
262 return parent::getPopupData($excludedList)->merge($data);
263 }
264
265 public function filterByDate(DateTime $date): FilterResult
266 {
267 $filtered = $this->filter(
268 static fn (FileItem $file) => $file->getDiskFile()?->getCreateTime()?->getTimestamp() > $date->getTimestamp()
269 );
270
271 return (new FilterResult())->setResult($filtered)->setFiltered($this->count() !== $filtered->count());
272 }
273
274 public function getRelatedChatId(): ?int
275 {
276 return $this->getAny()?->getChatId();
277 }
278}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getPreloadDiskFile(array $diskFileIds)
Определения FileCollection.php:238
getFileDiskAttributes(int $chatId, array $options=[])
Определения FileCollection.php:212
static addDiskFilesToPreload(array $diskFiles)
Определения FileCollection.php:227
getPopupData(array $excludedList=[])
Определения FileCollection.php:258
static array $preloadDiskFiles
Определения FileCollection.php:28
__construct(?array $diskFiles=null, ?int $chatId=null)
Определения FileCollection.php:34
filterByDate(DateTime $date)
Определения FileCollection.php:265
static initByDiskFilesIds(array $diskFilesIds, ?int $chatId=null)
Определения FileCollection.php:57
addToTmp(string $source)
Определения FileCollection.php:192
const FOLDER_NOT_FOUND
Определения FileError.php:13
const STORAGE_NOT_FOUND
Определения FileError.php:12
filter(callable $predicate)
Определения Registry.php:37
getAny()
Определения Registry.php:61
Определения result.php:20
getTimestamp()
Определения date.php:218
static GetFileParams($chatId, $fileModel, $options=[])
Определения im_disk.php:1672
$options
Определения commerceml2.php:49
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
@ UploadedFiles
Определения FolderType.php:16
Определения Image.php:9
if(empty($decryptedData)) $storage
Определения quickway.php:270
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936