Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Import.php
1<?php
2
4
11use CFile;
12use CRestUtil;
13
14class Import extends Controller
15{
16 protected function getDefaultPreFilters()
17 {
18 return array_merge(
19 parent::getDefaultPreFilters(),
20 [
21 new \Bitrix\Rest\Engine\ActionFilter\Scope('im.import'),
22 new \Bitrix\Main\Engine\ActionFilter\Scope(\Bitrix\Main\Engine\ActionFilter\Scope::REST),
23 new \Bitrix\Rest\Engine\ActionFilter\AuthType(\Bitrix\Rest\Engine\ActionFilter\AuthType::APPLICATION)
24 ]
25 );
26 }
27
28 public function createGroupAction(int $ownerId, CurrentUser $user, array $fields = [], string $externalId = ''): ?array
29 {
30 if (!ImportService::isAdmin((int)$user->getId()))
31 {
33
34 return null;
35 }
36 $isOpen = ($fields['isOpen'] ?? 'N') === 'Y';
37 $type = $isOpen ? \IM_MESSAGE_OPEN : \IM_MESSAGE_CHAT;
38
39 $chatParams = [
40 'TITLE' => $fields['title'] ?? null,
41 'DESCRIPTION' => $fields['description'] ?? null,
42 'TYPE' => $type,
43 'AVATAR_ID' => $this->saveAvatar($fields['avatar'] ?? null),
44 'ENTITY_ID' => $externalId,
45 'AUTHOR_ID' => $ownerId,
46 'USERS' => false,
47 ];
48
49 $initResult = ImportService::create($chatParams);
50
51 if (!$initResult->isSuccess())
52 {
53 $this->addErrors($initResult->getErrors());
54
55 return null;
56 }
57
58 return $this->convertKeysToCamelCase($initResult->getResult());
59 }
60
61 public function createPrivateAction(array $users, CurrentUser $user, string $externalId = ''): ?array
62 {
63 if (!ImportService::isAdmin((int)$user->getId()))
64 {
66
67 return null;
68 }
69 if (count($users) !== 2)
70 {
72
73 return null;
74 }
75
76 $users = array_map('intval', $users);
77
78 $chatParams = [
79 'TYPE' => \IM_MESSAGE_PRIVATE,
80 'ENTITY_ID' => $externalId,
81 'ENTITY_DATA_1' => "{$users[0]}|{$users[1]}",
82 'AUTHOR_ID' => $users[0],
83 'USERS' => false,
84 ];
85
86 $initResult = ImportService::create($chatParams);
87
88 if (!$initResult->isSuccess())
89 {
90 $this->addErrors($initResult->getErrors());
91
92 return null;
93 }
94
95 return $this->convertKeysToCamelCase($initResult->getResult());
96 }
97
98 public function getFolderIdAction(int $chatId, CurrentUser $user): ?array
99 {
100 $chat = Chat::getById($chatId, ['CHECK_ACCESS' => 'N']);
101 if (!$chat)
102 {
104
105 return null;
106 }
107
108 if (!(new ImportService($chat, (int)$user->getId()))->hasAccess())
109 {
111
112 return null;
113 }
114
115 $folderId = \CIMDisk::GetFolderModel($chatId)->getId();
116 \CIMDisk::ChangeFolderMembers($chatId, (int)$user->getId());
117
118 return ['chatFolderId' => $folderId];
119 }
120
121 public function abortAction(int $chatId, CurrentUser $user): ?array
122 {
123 $chat = Chat::getById($chatId, ['CHECK_ACCESS' => 'N']);
124 if (!$chat)
125 {
127
128 return null;
129 }
130
131 $importService = new ImportService($chat, (int)$user->getId());
132
133 if (!$importService->hasAccess())
134 {
136
137 return null;
138 }
139
140 $abortResult = $importService->abort();
141
142 if (!$abortResult->isSuccess())
143 {
144 $this->addErrors($abortResult->getErrors());
145
146 return null;
147 }
148
149 return [
150 'success' => true
151 ];
152 }
153
154 public function commitGroupAction(int $chatId, array $users, CurrentUser $user, \CRestServer $server): ?array
155 {
156 $chat = Chat::getById($chatId, ['CHECK_ACCESS' => 'N']);
157 if (!$chat)
158 {
160
161 return null;
162 }
163
164 $importService = new ImportService($chat, (int)$user->getId());
165
166 if (!$importService->hasAccess())
167 {
169
170 return null;
171 }
172
173 $finalizeResult = $importService->commitGroup($users, $server->getClientId());
174
175 if (!$finalizeResult->isSuccess())
176 {
177 $this->addErrors($finalizeResult->getErrors());
178
179 return null;
180 }
181
182 return [
183 'success' => true
184 ];
185 }
186
187 public function commitPrivateAction(int $chatId, string $newIsMain, CurrentUser $user, \CRestServer $server, string $hideOriginal = 'Y'): ?array
188 {
189 $chat = Chat::getById($chatId, ['CHECK_ACCESS' => 'N']);
190 if (!$chat)
191 {
193
194 return null;
195 }
196
197 $importService = new ImportService($chat, (int)$user->getId());
198
199 if (!$importService->hasAccess())
200 {
202
203 return null;
204 }
205
206 $finalizeResult = $importService->commitPrivate($newIsMain === 'Y', $hideOriginal === 'Y', $server->getClientId());
207
208 if (!$finalizeResult->isSuccess())
209 {
210 $this->addErrors($finalizeResult->getErrors());
211
212 return null;
213 }
214
215 return [
216 'success' => true
217 ];
218 }
219
220 private function saveAvatar(?string $fileContent): ?int
221 {
222 if (!isset($fileContent) || !$fileContent)
223 {
224 return null;
225 }
226
227 $file = CRestUtil::saveFile($fileContent);
228 $imageCheck = (new \Bitrix\Main\File\Image($file["tmp_name"]))->getInfo();
229 if(
230 !$imageCheck
231 || !$imageCheck->getWidth()
232 || $imageCheck->getWidth() > 5000
233 || !$imageCheck->getHeight()
234 || $imageCheck->getHeight() > 5000
235 )
236 {
237 return null;
238 }
239
240 if (!$file || !(mb_strpos($file['type'], 'image/') === 0))
241 {
242 return null;
243 }
244
245 return CFile::saveFile($file, 'im');
246 }
247
248}
static getById(int $id)
commitPrivateAction(int $chatId, string $newIsMain, CurrentUser $user, \CRestServer $server, string $hideOriginal='Y')
Definition Import.php:187
createGroupAction(int $ownerId, CurrentUser $user, array $fields=[], string $externalId='')
Definition Import.php:28
getFolderIdAction(int $chatId, CurrentUser $user)
Definition Import.php:98
commitGroupAction(int $chatId, array $users, CurrentUser $user, \CRestServer $server)
Definition Import.php:154
abortAction(int $chatId, CurrentUser $user)
Definition Import.php:121
createPrivateAction(array $users, CurrentUser $user, string $externalId='')
Definition Import.php:61
static create(array $chatData)