Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
FileItem.php
1<?php
2
4
5use Bitrix\Disk\TypeFile;
7use Bitrix\Im\Model\EO_LinkFile;
8use Bitrix\Im\V2\Common\MigrationStatusCheckerTrait;
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
27 public const ALLOWED_SUBTYPE = [
28 self::MEDIA_SUBTYPE,
29 self::AUDIO_SUBTYPE,
30 self::BRIEF_SUBTYPE,
31 self::OTHER_SUBTYPE,
32 self::DOCUMENT_SUBTYPE,
33 ];
34
35 public const BRIEF_CODE = 'resume';
36 public const MEDIA_ORIGINAL_CODE = 'media_original';
37
38 protected static string $migrationOptionName = 'im_link_file_migration';
39
40 protected string $subtype;
41
45 public function __construct($source = null)
46 {
47 $this->initByDefault();
48
49 if (!empty($source))
50 {
51 $this->load($source);
52 }
53 }
54
55 public function save(): Result
56 {
57 if (!static::isMigrationFinished())
58 {
59 return new Result;
60 }
61
62 return parent::save();
63 }
64
65 protected function validateSubtype(): Result
66 {
67 if (static::isSubtypeValid($this->getSubtype()))
68 {
69 return new Result;
70 }
71
73 }
74
75 public static function isSubtypeValid(string $subtype): bool
76 {
77 return in_array($subtype, static::ALLOWED_SUBTYPE, true);
78 }
79
80 public static function getSubtypeFromJsonFormat(string $subtypeInJsonFormat): string
81 {
82 return mb_strtoupper($subtypeInJsonFormat);
83 }
84
85 public static function getEntityClassName(): string
86 {
87 return Entity\File\FileItem::class;
88 }
89
90 public static function getRestEntityName(): string
91 {
92 return 'link';
93 }
94
95 public function setSubtype(string $subtype): self
96 {
97 $this->subtype = $subtype;
98 return $this;
99 }
100
101 public function getSubtype(): string
102 {
103 $this->subtype ??= $this->calculateSubtype();
104
105 return $this->subtype;
106 }
107
108 protected function calculateSubtype(): string
109 {
110 $this->fillFile();
111
112 if (!isset($this->entity))
113 {
114 return self::OTHER_SUBTYPE;
115 }
116
117 $diskFile = $this->getEntity()->getDiskFile();
118 $realFile = $diskFile->getRealObject() ?? $diskFile;
119
120 if ($realFile->getCode() === static::BRIEF_CODE)
121 {
122 return static::BRIEF_SUBTYPE;
123 }
124
125 if ($realFile->getCode() === static::MEDIA_ORIGINAL_CODE)
126 {
127 return static::OTHER_SUBTYPE;
128 }
129
130 return $this->getSubtypeByDiskFileType($diskFile->getTypeFile());
131 }
132
133 protected function getSubtypeByDiskFileType(string $diskFileType): string
134 {
135 switch ($diskFileType)
136 {
137 case TypeFile::IMAGE:
138 case TypeFile::VIDEO:
139 return static::MEDIA_SUBTYPE;
140
141 case TypeFile::DOCUMENT:
142 case TypeFile::PDF:
143 return static::DOCUMENT_SUBTYPE;
144
145 case TypeFile::AUDIO:
146 return static::AUDIO_SUBTYPE;
147
148 default:
149 return static::OTHER_SUBTYPE;
150 }
151 }
152
153 public function fillFile(): self
154 {
155 if (isset($this->entity))
156 {
157 return $this;
158 }
159
160 $fileEntity = \Bitrix\Im\V2\Entity\File\FileItem::initByDiskFileId($this->getEntityId(), $this->getChatId());
161
162 if ($fileEntity !== null)
163 {
164 $this->setEntity($fileEntity);
165 }
166
167 return $this;
168 }
169
170 public static function getDataClass(): string
171 {
172 return LinkFileTable::class;
173 }
174
175 public static function getByDiskFileId(int $diskFileId): ?self
176 {
177 $entity = LinkFileTable::query()
178 ->setSelect(['ID', 'MESSAGE_ID', 'CHAT_ID', 'SUBTYPE', 'DISK_FILE_ID', 'DATE_CREATE', 'AUTHOR_ID'])
179 ->where('DISK_FILE_ID', $diskFileId)
180 ->setLimit(1)
181 ->fetchObject()
182 ;
183
184 if ($entity === null)
185 {
186 return null;
187 }
188
189 return (new static($entity))->fillFile();
190 }
191
192 public function setChatId(int $chatId): BaseLinkItem
193 {
194 if (isset($this->entity))
195 {
196 $this->getEntity()->setChatId($chatId);
197 }
198
199 return parent::setChatId($chatId);
200 }
201
205 public function getEntity(): \Bitrix\Im\V2\Entity\File\FileItem
206 {
207 $this->fillFile();
208
209 return $this->entity;
210 }
211
217 public function setEntity(RestEntity $entity): self
218 {
219 if (!($entity instanceof \Bitrix\Im\V2\Entity\File\FileItem))
220 {
221 throw new ArgumentTypeException(get_class($entity));
222 }
223
224 return parent::setEntity($entity->setChatId($this->chatId ?? null));
225 }
226
227 public function getPopupData(array $excludedList = []): PopupData
228 {
229 return parent::getPopupData($excludedList)->add(new Entity\File\FilePopupItem($this->getEntity()));
230 }
231
232 protected static function getEntityIdFieldName(): string
233 {
234 return 'DISK_FILE_ID';
235 }
236
237 protected static function mirrorDataEntityFields(): array
238 {
239 $additionalFields = [
240 'SUBTYPE' => [
241 'field' => 'subtype',
242 'set' => 'setSubtype',
243 'get' => 'getSubtype',
244 'beforeSave' => 'validateSubtype',
245 ]
246 ];
247
248 return array_merge(parent::mirrorDataEntityFields(), $additionalFields);
249 }
250
251 public function toRestFormat(array $option = []): array
252 {
253 return [
254 'id' => $this->getPrimaryId(),
255 'messageId' => $this->getMessageId(),
256 'chatId' => $this->getChatId(),
257 'authorId' => $this->getAuthorId(),
258 'dateCreate' => $this->getDateCreate()->format('c'),
259 'fileId' => $this->getEntityId(),
260 'subType' => mb_strtolower($this->getSubtype()),
261 ];
262 }
263}
add(PopupDataItem $item)
Definition PopupData.php:37
addError(Error $error)
Definition result.php:50