Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Command.php
1<?php
2
4
10Loc::loadMessages(__FILE__);
11
12class Command implements RestConvertible
13{
14 protected const CACHE_PATH = '/bx/im/command/';
15 protected const CACHE_TTL = 31536000;
16 protected const CACHE_KEY = 'cache_command_bot_';
17 protected const REST_NAME = 'commandList';
18
19 protected bool $loadRestLang;
20 protected ?int $botId;
21 protected ?string $lang = LANGUAGE_ID;
22
23 public function __construct($botId)
24 {
25 $this->botId = $botId;
26 }
27
28 protected function getBotId(): ?int
29 {
30 return isset($this->botId) ? (int)$this->botId : null;
31 }
32
33 public function getCommandList($id): array
34 {
35 $cache = $this->getSavedCache($id);
36 $cachedCommandList = $cache->getVars();
37
38 if ($cachedCommandList !== false)
39 {
40 return $cachedCommandList;
41 }
42
43 $commandList = $this->getCommandListFromDb($id);
44 if (empty($commandList))
45 {
46 return [];
47 }
48
49 $this->saveInCache($cache, $commandList);
50
51 return $commandList;
52 }
53
54 protected function getSavedCache(int $id): Cache
55 {
56 $cache = Application::getInstance()->getCache();
57
58 $cacheDir = $this->getCacheDir($id);
59 $cache->initCache(self::CACHE_TTL, $this->getCacheKey($id), $cacheDir);
60
61 return $cache;
62 }
63
64 protected function getCacheDir(int $id): string
65 {
66 $cacheSubDir = substr(md5($id),2,2);
67
68 return self::CACHE_PATH . "{$cacheSubDir}/" . self::CACHE_KEY . $id . "/{$this->getCacheKey($id)}/";
69 }
70
71 protected static function getDeleteDir(int $id): string
72 {
73 $cacheSubDir = substr(md5($id),2,2);
74
75 return self::CACHE_PATH . "{$cacheSubDir}/" . self::CACHE_KEY . $id . "/";
76 }
77
78 protected function getCacheKey($id): string
79 {
80 return $this->lang . '_' . $id;
81 }
82
83 protected function getCommandListFromDb(int $id): ?array
84 {
85 $result = [];
86
87 $query =CommandTable::query()
88 ->setSelect(['*'])
89 ->where('BOT_ID', $id)
90 ->exec()
91 ;
92
93 while ($row = $query->fetch())
94 {
95 $result[$row['ID']] = $row;
96 }
97
98
99 return $result;
100 }
101
102 protected function saveInCache(Cache $cache, array $commandList): void
103 {
104 $cache->startDataCache();
105 $cache->endDataCache($commandList);
106 }
107
108 public static function cleanCache(int $id): void
109 {
110 Application::getInstance()->getCache()->cleanDir(self::getDeleteDir($id));
111 }
112
113 public static function getRestEntityName(): string
114 {
115 return self::REST_NAME;
116 }
117
118 public function toRestFormat(array $option = []): array
119 {
120 $this->lang = $option['langId'] ?? LANGUAGE_ID;
121 $commandList = $this->getCommandList($this->getBotId());
122 $commandList = $this->prepareData($commandList);
123 $commandList = $this->mergeWithDefaultCommands($commandList);
124
125 $result = [];
126 foreach ($commandList as $command)
127 {
128 $result[] = [
129 'id' => is_numeric($command['ID']) ? (int)$command['ID'] : $command['ID'],
130 'botId' => (int)$command['BOT_ID'],
131 'command' => '/'. $command['COMMAND'],
132 'category' => $command['CATEGORY'],
133 'common' => $command['COMMON'],
134 'context' => $command['CONTEXT'],
135 'title' => $command['TITLE'],
136 'params' => $command['PARAMS'],
137 'extranet' => $command['EXTRANET_SUPPORT'],
138 ];
139 }
140
141 return $result;
142 }
143
144 protected function prepareData(array $commandList): array
145 {
146 $this->loadRestLang = false;
147 $result = [];
148
149 foreach ($commandList as $command)
150 {
151 $command['COMMAND_ID'] = $command['ID'];
152 $command['CONTEXT'] = '';
153
154 if ($command['BOT_ID'] > 0)
155 {
156 $command['CATEGORY'] = \Bitrix\Im\User::getInstance($command['BOT_ID'])->getFullName();
157 }
158 else if ($command['MODULE_ID'] == 'im')
159 {
160 $command['CATEGORY'] = Loc::getMessage('COMMAND_IM_CATEGORY');
161 }
162 else
163 {
164 $module = (new \CModule())->createModuleObject($command['MODULE_ID']);
165 $command['CATEGORY'] = $module->MODULE_NAME;
166 }
167
168 if (!empty($command['CLASS']) && !empty($command['METHOD_LANG_GET']))
169 {
170 $command = $this->setModuleParams($command, $this->lang);
171 }
172 else
173 {
174 $command = $this->setModuleRestParams($command);
175 }
176
177 $result[(int)$command['ID']] = $command;
178 }
179
180 if ($this->loadRestLang)
181 {
182 $result = $this->commandRestLang($result);
183 }
184
185 $this->sortCommandData($result);
186
187 return $result;
188 }
189
190 protected function setModuleParams(array $command): array
191 {
192 if (\Bitrix\Main\Loader::includeModule($command['MODULE_ID'])
193 && class_exists($command["CLASS"])
194 && method_exists($command["CLASS"], $command["METHOD_LANG_GET"]))
195 {
196 $localize = call_user_func_array(
197 [$command["CLASS"], $command["METHOD_LANG_GET"]],
198 [$command['COMMAND'], $this->lang]
199 );
200
201 if ($localize)
202 {
203 $command['TITLE'] = $localize['TITLE'];
204 $command['PARAMS'] = $localize['PARAMS'];
205 }
206 else
207 {
208 $command['HIDDEN'] = 'Y';
209 $command['METHOD_LANG_GET'] = '';
210 }
211 }
212 else
213 {
214 $command['HIDDEN'] = 'Y';
215 $command['METHOD_LANG_GET'] = '';
216 }
217
218 return $command;
219 }
220 protected function setModuleRestParams(array $command): array
221 {
222 $command['TITLE'] = '';
223 $command['PARAMS'] = '';
224
225 if ($command['MODULE_ID'] === 'rest')
226 {
227 $this->loadRestLang = true;
228
229 if ($command['BOT_ID'] <= 0 && $command['APP_ID'])
230 {
231 $res = \Bitrix\Rest\AppTable::getList([
232 'filter' => array('=CLIENT_ID' => $command['APP_ID']),
233 ]);
234
235 if ($app = $res->fetch())
236 {
237 $command['CATEGORY'] = !empty($app['APP_NAME'])
238 ? $app['APP_NAME']
239 : (!empty($app['APP_NAME_DEFAULT'])
240 ? $app['APP_NAME_DEFAULT']
241 : $app['CODE']
242 )
243 ;
244 }
245 }
246 }
247
248 return $command;
249 }
250
251 protected function commandRestLang(array $result): array
252 {
253 $langSet = [];
254 $orm = \Bitrix\Im\Model\CommandLangTable::getList();
255 while ($commandLang = $orm->fetch())
256 {
257 if (!isset($result[$commandLang['ID']]))
258 {
259 continue;
260 }
261
262 $langSet[$commandLang['ID']][$commandLang['LANGUAGE_ID']]['TITLE'] = $commandLang['TITLE'];
263 $langSet[$commandLang['ID']][$commandLang['LANGUAGE_ID']]['PARAMS'] = $commandLang['PARAMS'];
264 }
265
266 $langAlter = \Bitrix\Im\Bot::getDefaultLanguage();
267 foreach ($result as $commandId => $commandData)
268 {
269 if (isset($langSet[$commandId][$this->lang]))
270 {
271 $result[$commandId]['TITLE'] = $langSet[$commandId][$this->lang]['TITLE'];
272 $result[$commandId]['PARAMS'] = $langSet[$commandId][$this->lang]['PARAMS'];
273 }
274 else if (isset($langSet[$commandId][$langAlter]))
275 {
276 $result[$commandId]['TITLE'] = $langSet[$commandId][$langAlter]['TITLE'];
277 $result[$commandId]['PARAMS'] = $langSet[$commandId][$langAlter]['PARAMS'];
278 }
279 else if (isset($langSet[$commandId]))
280 {
281 $langSetCommand = array_values($langSet[$commandId]);
282 $result[$commandId]['TITLE'] = $langSetCommand[0]['TITLE'];
283 $result[$commandId]['PARAMS'] = $langSetCommand[0]['PARAMS'];
284 }
285 }
286
287 foreach ($result as $key => $value)
288 {
289 if (empty($value['TITLE']))
290 {
291 $result[$key]['HIDDEN'] = 'Y';
292 $commandLang['METHOD_LANG_GET'] = '';
293 }
294 }
295
296 return $result;
297 }
298
299 protected function sortCommandData(array $result): array
300 {
301 if (!empty($result))
302 {
303 \Bitrix\Main\Type\Collection::sortByColumn(
304 $result,
305 Array('MODULE_ID' => SORT_ASC),
306 '',
307 null,
308 true
309 );
310 }
311
312 return $result;
313 }
314
315 protected function mergeWithDefaultCommands($commandList): array
316 {
317 $defaultCommands = [
318 [
319 'COMMAND' => 'me',
320 'TITLE' => Loc::getMessage("COMMAND_DEF_ME_TITLE"),
321 'PARAMS' => Loc::getMessage("COMMAND_DEF_ME_PARAMS"),
322 'HIDDEN' => 'N',
323 'EXTRANET_SUPPORT' => 'Y',
324 ],
325 [
326 'COMMAND' => 'loud',
327 'TITLE' => Loc::getMessage("COMMAND_DEF_LOUD_TITLE"),
328 'PARAMS' => Loc::getMessage("COMMAND_DEF_LOUD_PARAMS"),
329 'HIDDEN' => 'N',
330 'EXTRANET_SUPPORT' => 'Y',
331 ],
332 [
333 'COMMAND' => '>>',
334 'TITLE' => Loc::getMessage("COMMAND_DEF_QUOTE_TITLE"),
335 'PARAMS' => Loc::getMessage("COMMAND_DEF_QUOTE_PARAMS"),
336 'HIDDEN' => 'N',
337 'EXTRANET_SUPPORT' => 'Y',
338 ],
339 [
340 'COMMAND' => 'rename',
341 'TITLE' => Loc::getMessage("COMMAND_DEF_RENAME_TITLE"),
342 'PARAMS' => Loc::getMessage("COMMAND_DEF_RENAME_PARAMS"),
343 'HIDDEN' => 'N',
344 'EXTRANET_SUPPORT' => 'Y',
345 'CATEGORY' => Loc::getMessage("COMMAND_DEF_CATEGORY_CHAT"),
346 'CONTEXT' => 'chat',
347 ],
348 [
349 'COMMAND' => 'getDialogId',
350 'TITLE' => Loc::getMessage("COMMAND_DEF_DIALOGID_TITLE"),
351 'HIDDEN' => 'N',
352 'EXTRANET_SUPPORT' => 'N',
353 'CATEGORY' => Loc::getMessage("COMMAND_DEF_CATEGORY_CHAT")
354 ],
355 [
356 'COMMAND' => 'webrtcDebug',
357 'TITLE' => Loc::getMessage("COMMAND_DEF_WD_TITLE"),
358 'HIDDEN' => 'N',
359 'EXTRANET_SUPPORT' => 'Y',
360 'CATEGORY' => Loc::getMessage("COMMAND_DEF_CATEGORY_DEBUG"),
361 'CONTEXT' => 'call'
362 ],
363 ];
364
365 $imCommands = Array();
366 foreach ($defaultCommands as $i => $command)
367 {
368 $newCommand['ID'] = 'def'.$i;
369 $newCommand['BOT_ID'] = 0;
370 $newCommand['APP_ID'] = '';
371 $newCommand['COMMAND'] = $command['COMMAND'];
372 $newCommand['HIDDEN'] = $command['HIDDEN'] ?? 'N';
373 $newCommand['COMMON'] = 'Y';
374 $newCommand['EXTRANET_SUPPORT'] = $command['EXTRANET_SUPPORT'] ?? 'N';
375 $newCommand['SONET_SUPPORT'] = $command['SONET_SUPPORT'] ?? 'N';
376 $newCommand['CLASS'] = '';
377 $newCommand['METHOD_COMMAND_ADD'] = '';
378 $newCommand['METHOD_LANG_GET'] = '';
379 if (!$command['TITLE'])
380 {
381 $newCommand['HIDDEN'] = 'Y';
382 }
383 $newCommand['MODULE_ID'] = 'im';
384 $newCommand['COMMAND_ID'] = $newCommand['ID'];
385 $newCommand['CATEGORY'] = $command['CATEGORY'] ?? Loc::getMessage('COMMAND_IM_CATEGORY');
386 $newCommand['CONTEXT'] = $command['CONTEXT'] ?? '';
387 $newCommand['TITLE'] = $command['TITLE'] ?? '';
388 $newCommand['PARAMS'] = $command['PARAMS'] ?? '';
389
390 $imCommands[$newCommand['COMMAND_ID']] = $newCommand;
391 }
392
393 $result = $imCommands;
394 if (is_array($commandList))
395 {
396 foreach ($commandList as $key => $command)
397 {
398 $result[$key] = $command;
399 }
400 }
401
402 return $result;
403 }
404}
saveInCache(Cache $cache, array $commandList)
Definition Command.php:102
prepareData(array $commandList)
Definition Command.php:144
endDataCache($vars=false)
Definition cache.php:407
startDataCache($TTL=false, $uniqueString=false, $initDir=false, $vars=array(), $baseDir='cache')
Definition cache.php:348
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29