1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Import.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Controller;
4
5use Bitrix\Im\Chat;
6use Bitrix\Im\V2\Chat\ChatError;
7use Bitrix\Im\V2\Import\ImportError;
8use Bitrix\Im\V2\Import\ImportService;
9use Bitrix\Main\Engine\Controller;
10use Bitrix\Main\Engine\CurrentUser;
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 {
32 $this->addError(new ImportError(ImportError::ACCESS_ERROR));
33
34 return null;
35 }
36 $isOpen = ($fields['isOpen'] ?? 'N') === 'Y';
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 {
65 $this->addError(new ImportError(ImportError::ACCESS_ERROR));
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 {
103 $this->addError(new ChatError(ChatError::NOT_FOUND));
104
105 return null;
106 }
107
108 if (!(new ImportService($chat, (int)$user->getId()))->hasAccess())
109 {
110 $this->addError(new ImportError(ImportError::ACCESS_ERROR));
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 {
126 $this->addError(new ChatError(ChatError::NOT_FOUND));
127
128 return null;
129 }
130
131 $importService = new ImportService($chat, (int)$user->getId());
132
133 if (!$importService->hasAccess())
134 {
135 $this->addError(new ImportError(ImportError::ACCESS_ERROR));
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 {
159 $this->addError(new ChatError(ChatError::NOT_FOUND));
160
161 return null;
162 }
163
164 $importService = new ImportService($chat, (int)$user->getId());
165
166 if (!$importService->hasAccess())
167 {
168 $this->addError(new ImportError(ImportError::ACCESS_ERROR));
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 {
192 $this->addError(new ChatError(ChatError::NOT_FOUND));
193
194 return null;
195 }
196
197 $importService = new ImportService($chat, (int)$user->getId());
198
199 if (!$importService->hasAccess())
200 {
201 $this->addError(new ImportError(ImportError::ACCESS_ERROR));
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}
$type
Определения options.php:106
const NOT_FOUND
Определения ChatError.php:20
getDefaultPreFilters()
Определения Import.php:16
commitPrivateAction(int $chatId, string $newIsMain, CurrentUser $user, \CRestServer $server, string $hideOriginal='Y')
Определения Import.php:187
createGroupAction(int $ownerId, CurrentUser $user, array $fields=[], string $externalId='')
Определения Import.php:28
getFolderIdAction(int $chatId, CurrentUser $user)
Определения Import.php:98
commitGroupAction(int $chatId, array $users, CurrentUser $user, \CRestServer $server)
Определения Import.php:154
abortAction(int $chatId, CurrentUser $user)
Определения Import.php:121
createPrivateAction(array $users, CurrentUser $user, string $externalId='')
Определения Import.php:61
const PRIVATE_CHAT_COUNT_USERS_ERROR
Определения ImportError.php:18
static create(array $chatData)
Определения ImportService.php:52
static isAdmin(int $userId)
Определения ImportService.php:419
static getById($id)
Определения datamanager.php:364
static GetFolderModel($chatId, $createFolder=true)
Определения im_disk.php:1786
static ChangeFolderMembers($chatId, $userId, $append=true)
Определения im_disk.php:1974
Определения rest.php:24
getClientId()
Определения rest.php:362
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$fileContent
Определения file_property.php:47
const IM_MESSAGE_CHAT
Определения include.php:23
const IM_MESSAGE_OPEN
Определения include.php:24
const IM_MESSAGE_PRIVATE
Определения include.php:22
Определения action.php:3
Определения handlers.php:8
$user
Определения mysql_to_pgsql.php:33
</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
$fields
Определения yandex_run.php:501