1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
FileItem.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Link\File;
4
5use Bitrix\Disk\TypeFile;
6use Bitrix\Im\Model\LinkFileTable;
7use Bitrix\Im\Model\EO_LinkFile;
8use Bitrix\Im\V2\Common\MigrationStatusCheckerTrait;
9use Bitrix\Im\V2\Entity;
10use Bitrix\Im\V2\Link\BaseLinkItem;
11use Bitrix\Im\V2\Entity\File\FileError;
12use Bitrix\Im\V2\Rest\PopupData;
13use Bitrix\Im\V2\Rest\RestEntity;
14use Bitrix\Im\V2\Result;
15use Bitrix\Main\ArgumentTypeException;
16
18{
19 use MigrationStatusCheckerTrait;
20
21 public const MEDIA_SUBTYPE = 'MEDIA';
22 public const AUDIO_SUBTYPE = 'AUDIO';
23 public const BRIEF_SUBTYPE = 'BRIEF';
24 public const OTHER_SUBTYPE = 'OTHER';
25 public const DOCUMENT_SUBTYPE = 'DOCUMENT';
26 public const BRIEF_CODE = 'resume';
27 public const MEDIA_ORIGINAL_CODE = 'media_original';
28
29 protected static string $migrationOptionName = 'im_link_file_migration';
30
31 protected Subtype $subtype;
32
36 public function __construct($source = null)
37 {
38 $this->initByDefault();
39
40 if (!empty($source))
41 {
42 $this->load($source);
43 }
44 }
45
46 public function save(): Result
47 {
48 if (!static::isMigrationFinished())
49 {
50 return new Result;
51 }
52
53 return parent::save();
54 }
55
56 protected function resolveSubtype(): Result
57 {
58 if (!isset($this->subtype))
59 {
60 $this->getSubtype();
61 }
62
63 return new Result();
64 }
65
66 public static function getEntityClassName(): string
67 {
68 return Entity\File\FileItem::class;
69 }
70
71 public static function getRestEntityName(): string
72 {
73 return 'link';
74 }
75
76 protected function saveSubtypeValueFilter(Subtype $subtype): string
77 {
78 return $subtype->value;
79 }
80
81 protected function loadSubtypeValueFilter(string $subtype): Subtype
82 {
83 return Subtype::tryFromOrDefault($subtype);
84 }
85
86 public function setSubtype(Subtype|string $subtype): self
87 {
88 if (is_string($subtype))
89 {
90 $this->subtype = Subtype::tryFromOrDefault($subtype);
91 }
92 else
93 {
94 $this->subtype = $subtype;
95 }
96
97 return $this;
98 }
99
100 public function getSubtype(): Subtype
101 {
102 $this->subtype ??= $this->calculateSubtype();
103
104 return $this->subtype;
105 }
106
107 protected function calculateSubtype(): Subtype
108 {
109 $this->fillFile();
110
111 if (!isset($this->entity))
112 {
113 return Subtype::Other;
114 }
115
116 $diskFile = $this->getEntity()->getDiskFile();
117 $realFile = $diskFile?->getRealObject() ?? $diskFile;
118
119 if ($realFile?->getCode() === static::BRIEF_CODE)
120 {
121 return Subtype::Brief;
122 }
123
124 if ($realFile?->getCode() === static::MEDIA_ORIGINAL_CODE)
125 {
126 return Subtype::Other;
127 }
128
129 return Subtype::tryFromDiskFileType($diskFile?->getTypeFile());
130 }
131
132 public function getSubtypeGroup(): ?SubtypeGroup
133 {
135 }
136
137 public function fillFile(): self
138 {
139 if (isset($this->entity))
140 {
141 return $this;
142 }
143
144 $fileEntity = Entity\File\FileItem::initByDiskFileId($this->getEntityId(), $this->getChatId());
145
146 if ($fileEntity !== null)
147 {
148 $this->setEntity($fileEntity);
149 }
150
151 return $this;
152 }
153
154 public static function getDataClass(): string
155 {
156 return LinkFileTable::class;
157 }
158
162 public static function normalizeFilterFromJsonFormat(null|string|array $filter): null|string|array
163 {
164 if (is_string($filter))
165 {
166 return mb_strtoupper($filter);
167 }
168
169 if (is_array($filter))
170 {
171 return array_map('mb_strtoupper', $filter);
172 }
173
174 return null;
175 }
176
177 public static function getByDiskFileId(int $diskFileId): ?self
178 {
179 $entity = LinkFileTable::query()
180 ->setSelect(['ID', 'MESSAGE_ID', 'CHAT_ID', 'SUBTYPE', 'DISK_FILE_ID', 'DATE_CREATE', 'AUTHOR_ID'])
181 ->where('DISK_FILE_ID', $diskFileId)
182 ->setLimit(1)
183 ->fetchObject()
184 ;
185
186 if ($entity === null)
187 {
188 return null;
189 }
190
191 return (new static($entity))->fillFile();
192 }
193
194 public function setChatId(int $chatId): BaseLinkItem
195 {
196 if (isset($this->entity))
197 {
198 $this->getEntity()->setChatId($chatId);
199 }
200
201 return parent::setChatId($chatId);
202 }
203
207 public function getEntity(): Entity\File\FileItem
208 {
209 $this->fillFile();
210
211 return $this->entity;
212 }
213
219 public function setEntity(RestEntity $entity): self
220 {
221 if (!($entity instanceof Entity\File\FileItem))
222 {
223 throw new ArgumentTypeException(get_class($entity));
224 }
225
226 return parent::setEntity($entity->setChatId($this->chatId ?? null));
227 }
228
229 public function getPopupData(array $excludedList = []): PopupData
230 {
231 return parent::getPopupData($excludedList)->add(new Entity\File\FilePopupItem($this->getEntity()));
232 }
233
234 protected static function getEntityIdFieldName(): string
235 {
236 return 'DISK_FILE_ID';
237 }
238
239 protected static function mirrorDataEntityFields(): array
240 {
241 $additionalFields = [
242 'SUBTYPE' => [
243 'field' => 'subtype',
244 'set' => 'setSubtype',
245 'get' => 'getSubtype',
246 'beforeSave' => 'resolveSubtype',
247 'saveFilter' => 'saveSubtypeValueFilter',
248 'loadFilter' => 'loadSubtypeValueFilter'
249 ]
250 ];
251
252 return array_merge(parent::mirrorDataEntityFields(), $additionalFields);
253 }
254
255 public function toRestFormat(array $option = []): array
256 {
257 return [
258 'id' => $this->getPrimaryId(),
259 'messageId' => $this->getMessageId(),
260 'chatId' => $this->getChatId(),
261 'authorId' => $this->getAuthorId(),
262 'dateCreate' => $this->getDateCreate()->format('c'),
263 'fileId' => $this->getEntityId(),
264 'subType' => $this->getSubtype(),
265 'group' => $this->getSubtypeGroup(),
266 ];
267 }
268}
static initByDiskFileId(int $diskFileId, ?int $chatId=null)
Определения FileItem.php:67
add(PopupDataItem $item)
Определения PopupData.php:40
Определения result.php:20
getCode()
Определения entity.php:804
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$entity
$filter
Определения iblock_catalog_list.php:54
Определения ufield.php:9
Определения Image.php:9
$option
Определения options.php:1711