1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Params.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Chat\Param;
4
5use Bitrix\Im\Model\ChatParamTable;
6use Bitrix\Im\Model\EO_ChatParam;
7use Bitrix\Im\Model\EO_ChatParam_Collection;
8use Bitrix\Im\V2\Registry;
9use Bitrix\Im\V2\Result;
10use Bitrix\Main\Application;
11use Bitrix\Main\Event;
12use Bitrix\Main\EventResult;
13use Bitrix\Main\Loader;
14use Bitrix\Main\ORM;
15use IteratorAggregate;
16
20class Params extends Registry
21{
22 public const EVENT_CHAT_PARAM_INIT = 'OnChatParamInit';
23 public const
24 IS_COPILOT = 'IS_COPILOT',
25 COPILOT_ROLES = 'COPILOT_ROLES',
26 COPILOT_MAIN_ROLE = 'COPILOT_MAIN_ROLE',
27 TEXT_FIELD_ENABLED = 'TEXT_FIELD_ENABLED',
28 BACKGROUND_ID = 'BACKGROUND_ID',
29 CONTAINS_COLLABER = 'CONTAINS_COLLABER',
30 MANAGE_MESSAGES_AUTO_DELETE = 'MANAGE_MESSAGES_AUTO_DELETE',
31 COPILOT_ENGINE_CODE = 'COPILOT_ENGINE_CODE',
32 USER_DELETE_MESSAGE_DISABLED = 'USER_DELETE_MESSAGE_DISABLED'
33 ;
34
35 public const CHAT_PARAMS = [
36 self::IS_COPILOT,
37 self::COPILOT_ROLES,
38 self::COPILOT_MAIN_ROLE,
39 self::TEXT_FIELD_ENABLED,
40 self::CONTAINS_COLLABER,
41 self::BACKGROUND_ID,
42 self::MANAGE_MESSAGES_AUTO_DELETE,
43 self::COPILOT_ENGINE_CODE,
44 self::USER_DELETE_MESSAGE_DISABLED,
45 ];
46
47 protected int $chatId;
48 protected string $paramName = '';
49 protected bool $isCreated = false;
50 protected array $droppedItems = [];
51 protected static ?array $eventParams = null;
52
53 private const CACHE_TTL = 18144000;
54 private static ?array $instance = [];
55
56 private function __construct(object|array $array = [], int $flags = 0, string $iteratorClass = "ArrayIterator")
57 {
58 parent::__construct($array, $flags, $iteratorClass);
59 }
60
61 public static function getInstance(int $chatId): Params
62 {
63 if (isset(self::$instance[$chatId]))
64 {
65 return self::$instance[$chatId];
66 }
67
68 self::$instance[$chatId] = new static();
69
70 return self::$instance[$chatId]->setChatId($chatId)->getParams();
71 }
72
73 protected function createParam($paramName): Param
74 {
75 $eventParam = $this->createEventParam($paramName);
76 if (isset($eventParam))
77 {
78 return $eventParam;
79 }
80
81 return match ($paramName) {
82 self::CONTAINS_COLLABER,
83 self::TEXT_FIELD_ENABLED,
84 self::IS_COPILOT,
85 self::USER_DELETE_MESSAGE_DISABLED => (new Param())->setType(Param::TYPE_BOOL),
86 default => (new Param())->setType(Param::TYPE_STRING),
87 };
88 }
89
90 protected function createEventParam(string $paramName): ?Param
91 {
92 foreach (self::$eventParams as $name => $data)
93 {
94 if ($name === $paramName)
95 {
96 if (isset($data['type']) && $this->isValidType((string)$data['type']))
97 {
98 return (new Param())->setType($data['type']);
99 }
100
101 if (
102 Loader::includeModule($data['moduleId'])
103 && class_exists($data['className'])
104 && is_subclass_of($data['className'], Param::class)
105 )
106 {
107 return (new $data['className']);
108 }
109 }
110 }
111
112 return null;
113 }
114
115 protected function load($source): Params
116 {
117 if (self::$eventParams === null)
118 {
119 $this->getParamsByEvent();
120 }
121
122 if (is_array($source))
123 {
124 $this->initByArray($source);
125 }
126 elseif ($source instanceof ORM\Objectify\Collection)
127 {
128 $this->initByEntitiesCollection($source);
129 }
130 elseif ($source instanceof ORM\Objectify\EntityObject)
131 {
132 $this->initByDataEntity($source);
133 }
134
135 $this->isLoaded = true;
136
137 return $this;
138 }
139
140 protected function isValidType(string $type): bool
141 {
142 return in_array($type, Param::PARAM_TYPES, true);
143 }
144
145 protected function getParamsByEvent(): void
146 {
147 $allParams = [];
148
149 $event = new Event('im', self::EVENT_CHAT_PARAM_INIT);
150 $event->send();
151 $resultList = $event->getResults();
152
153 foreach ($resultList as $eventResult)
154 {
155 if ($eventResult->getType() === EventResult::SUCCESS)
156 {
157 $params = $eventResult->getParameters();
158
159 if (is_array($params))
160 {
161 foreach ($params as $paramName => $paramData)
162 {
163 $allParams[$paramName] = $paramData;
164 }
165 }
166 }
167 }
168
169 self::$eventParams = $allParams;
170 }
171
172 public static function loadWithoutChat(array $source): self
173 {
174 $params = new self;
175 $source = $params->filterParamData($source);
176
177 foreach ($source as $key => $item)
178 {
179 if (!isset($item['CHAT_ID']))
180 {
181 $source[$key]['CHAT_ID'] = 0;
182 }
183 }
184
185 $params->setChatId(0);
186 $params->setIsCreated(true);
187
188 return $params->load($source);
189 }
190
191 public function saveWithNewChatId(int $chatId): Result
192 {
193 $this->setChatId($chatId);
194 foreach ($this as $item)
195 {
196 $item->setChatId($chatId);
197 }
198
199 $this->setIsCreated(false);
200
201 return $this->save();
202 }
203
204 protected function getParams(): Params
205 {
206 $chatId = $this->getChatId();
207
208 $cache = Application::getInstance()->getCache();
209 if ($cache->initCache(self::CACHE_TTL, $this->getCacheId(), $this->getCacheDir()))
210 {
211 $params = $cache->getVars();
212 }
213 else
214 {
215 $params = Param::getDataClass()::query()
216 ->setSelect(['ID', 'CHAT_ID', 'PARAM_NAME', 'PARAM_VALUE', 'PARAM_JSON'])
217 ->where('CHAT_ID', '=', $chatId)
218 ->fetchAll()
219 ;
220
221 $cache->startDataCache();
222 $cache->endDataCache($params);
223 }
224
225 $this->load($params);
226
227 return $this;
228 }
229
230 protected function initByEntitiesCollection(ORM\Objectify\Collection $entitiesCollection): Params
231 {
233 foreach ($entitiesCollection as $entity)
234 {
235 $paramName = $entity->getParamName();
236 if (!parent::offsetExists($paramName))
237 {
238 $this[$paramName] = $this->createParam($paramName);
239 }
240
241 $item = $this[$paramName];
242
243 $entity->setChatId($this->getChatId());
244 $item->load($entity);
245 }
246
247 return $this;
248 }
249
250 protected function initByArray(array $items): Params
251 {
252 foreach ($items as $entityId => $entity)
253 {
254 if (is_array($entity) && isset($entity['PARAM_NAME']))
255 {
256 $paramName = $entity['PARAM_NAME'];
257 if (!parent::offsetExists($paramName))
258 {
259 $this[$paramName] = $this->createParam($paramName);
260 }
261
262 $item = $this[$paramName];
263
264 $entity['CHAT_ID'] = $this->getChatId();
265 $item->load($entity);
266 }
267 }
268
269 return $this;
270 }
271
273 {
274 $paramName = $entity->getParamName();
275 if (!parent::offsetExists($paramName))
276 {
277 $this[$paramName] = $this->createParam($paramName);
278 }
279
280 $item = $this[$paramName];
281
282 $entity->setChatId($this->getChatId());
283 $item->load($entity);
284
285 return $this;
286 }
287
288 public function save(): Result
289 {
290 $result = new Result;
291
293 $entityCollectionClass = Param::getDataClass()::getCollectionClass();
294 $dataEntityCollection = new $entityCollectionClass;
295
296 $dropIds = [];
297
298 $itemKeyToUnset = [];
299 foreach ($this as $item)
300 {
301 if ($item->isDeleted())
302 {
303 $itemKeyToUnset[] = $item->getName();
304 continue;
305 }
306 if (!$item->hasValue())
307 {
308 continue;
309 }
310
311 if ($item instanceof Param)
312 {
313 if (!$item->isChanged())
314 {
315 continue;
316 }
317
318 $prepareResult = $item->prepareFields();
319 if ($prepareResult->isSuccess())
320 {
321 if ($prepareResult->getData()['SKIP_SAVE'] ?? false)
322 {
323 continue;
324 }
325 if ($item->isChanged())
326 {
327 $dataEntityCollection->add($item->getDataEntity());
328 $item->markChanged(false);
329 }
330 }
331 else
332 {
333 $result->addErrors($prepareResult->getErrors());
334 }
335 }
336 }
337 $this->unsetByKeys($itemKeyToUnset);
338
339 foreach ($this->droppedItems as $item)
340 {
341 if ($item instanceof Param)
342 {
343 if ($item->getPrimaryId())
344 {
345 $dropIds[] = $item->getPrimaryId();
346 }
347 }
348 }
349
350 $saveResult = $dataEntityCollection->save(true);
351 if ($saveResult->isSuccess())
352 {
353 if (!empty($dropIds))
354 {
355 ChatParamTable::deleteBatch(['=ID' => $dropIds]);
356 }
357
358 self::cleanCache($this->chatId);
359
360 $this->droppedItems = [];
361 }
362 else
363 {
364 $result->addErrors($saveResult->getErrors());
365 }
366
367 return $result;
368 }
369
370 public function addParamByName(string $paramName, mixed $paramValue, bool $withSave = true): Result
371 {
372 $result = new Result();
373
374 if (!$this->isValidParamName($paramName))
375 {
376 return $result;
377 }
378
379 $this->load([['CHAT_ID' => $this->getChatId(), 'PARAM_NAME' => $paramName, 'PARAM_VALUE' => $paramValue]]);
380
381 if ($this->get($paramName) !== null)
382 {
383 $this->get($paramName)->markChanged();
384 }
385
386 if ($withSave)
387 {
388 return $this->save();
389 }
390
391 return $result;
392 }
393
394 public function addParamByArray(?array $chatParams): Result
395 {
396 $result = new Result();
397
398 if (!isset($chatParams))
399 {
400 return $result;
401 }
402
403 $chatParams = $this->filterParamData($chatParams);
404
405 $addParams = [];
406 foreach ($chatParams as $chatParam)
407 {
408 if (!isset($chatParam['PARAM_NAME']) || !isset($chatParam['PARAM_VALUE']))
409 {
410 continue;
411 }
412
413 switch ($chatParam['PARAM_NAME'])
414 {
415 default:
416 $addParams[] = [
417 'CHAT_ID' => $this->getChatId(),
418 'PARAM_NAME' => $chatParam['PARAM_NAME'] ?? null,
419 'PARAM_VALUE' => $chatParam['PARAM_VALUE'] ?? null,
420 ];
421 }
422 }
423
424 $this->load($addParams);
425
426 foreach ($addParams as $param)
427 {
428 if ($this->get($param['PARAM_NAME']) !== null)
429 {
430 $this->get($param['PARAM_NAME'])->markChanged();
431 }
432 }
433
434 return $this->save();
435 }
436
437 public function addParamByObject(Param $param): Result
438 {
439 if ($param->getName() !== null)
440 {
441 $this[$param->getName()] = $param;
442 $param->setChatId($this->getChatId());
443
444 return $this->save();
445 }
446
447 return (new Result())->addError((new ParamError(ParamError::EMPTY_PARAM_NAME)));
448 }
449
450 public function isValidParamName(string $paramName): bool
451 {
452 foreach (self::$eventParams as $name => $param)
453 {
454 if ($paramName === $name)
455 {
456 return true;
457 }
458 }
459
460 return in_array($paramName, self::CHAT_PARAMS, true);
461 }
462
463 protected function filterParamData(array $chatParams): array
464 {
465 foreach ($chatParams as $key => $chatParam)
466 {
467 if (!is_array($chatParam))
468 {
469 unset($chatParams[$key]);
470 continue;
471 }
472
473 if (!isset($chatParam['PARAM_NAME']) || !isset($chatParam['PARAM_VALUE']))
474 {
475 unset($chatParams[$key]);
476 }
477 }
478
479 return $chatParams;
480 }
481
482 public function updateParam(string $paramName, $paramValue): Result
483 {
484 if ($this->offsetExists($paramName))
485 {
486 $param = $this->offsetGet($paramName);
487
488 if ($param instanceof Param)
489 {
490 $param->setValue($paramValue);
491 return $this->save();
492 }
493 }
494
495 return (new Result())->addError((new ParamError(ParamError::EMPTY_PARAM)));
496 }
497
501 public function deleteAll(): Params
502 {
503 $keysToUnset = [];
504 foreach ($this as $key => $item)
505 {
506 $keysToUnset[$key] = $key;
507 }
508 $this->unsetByKeys($keysToUnset);
509
510 if ($this->getChatId())
511 {
512 $filter = [
513 '=CHAT_ID' => $this->getChatId(),
514 ];
515
516 ChatParamTable::deleteBatch($filter);
517 }
518
519 $this->droppedItems = [];
520 $this->isLoaded = false;
521
522 self::cleanCache($this->getChatId());
523
524 return $this;
525 }
526
527 public function deleteParam(string $paramName, bool $withSave = true): Params
528 {
529 if (!$this->offsetExists($paramName))
530 {
531 return $this;
532 }
533
534 if (!$withSave)
535 {
536 $this->getParams()->offsetUnset($paramName);
537 }
538 elseif ($this->getChatId())
539 {
540 $filter = [
541 '=CHAT_ID' => $this->getChatId(),
542 '=PARAM_NAME' => $paramName,
543 ];
544
545 ChatParamTable::deleteBatch($filter);
546
547 self::cleanCache($this->getChatId());
548 }
549
550 return $this;
551 }
552
557 public function offsetUnset($key): void
558 {
559 if ($this->offsetExists($key))
560 {
561 $this[$key]->markDrop();
562 $this->droppedItems[] = $this[$key];
563 }
564
565 parent::offsetUnset($key);
566 }
567
568 public function get(string $paramName): ?Param
569 {
570 return $this[$paramName] ?? null;
571 }
572
573 public function getChatId(): int
574 {
575 return $this->chatId;
576 }
577
578 public function setChatId(int $chatId): self
579 {
580 $this->chatId = $chatId;
581 foreach ($this as $param)
582 {
583 $param->setChatId($this->chatId);
584 }
585
586 return $this;
587 }
588
589 public function isCreated(): bool
590 {
591 return $this->isCreated;
592 }
593 public function setIsCreated(bool $isCreated): void
594 {
595 $this->isCreated = $isCreated;
596 }
597
598 private function getCacheId(): string
599 {
600 return "chat_params_{$this->chatId}";
601 }
602
603 private function getCacheDir(): string
604 {
605 return static::getCacheDirByChatId($this->chatId);
606 }
607
608 private static function getCacheDirByChatId(int $chatId): string
609 {
610 $cacheSubDir = $chatId % 100;
611
612 return "/bx/imc/chatdata/params/1/{$cacheSubDir}/{$chatId}";
613 }
614
615 public static function cleanCache(int $chatId): void
616 {
617 Application::getInstance()->getCache()->cleanDir(static::getCacheDirByChatId($chatId));
618 }
619}
const CHAT_PARAMS
Определения Params.php:35
const USER_DELETE_MESSAGE_DISABLED
Определения Params.php:32
const EVENT_CHAT_PARAM_INIT
Определения Params.php:22
offsetUnset($key)
Определения Params.php:557
static cleanCache(int $chatId)
Определения Params.php:615
getParamsByEvent()
Определения Params.php:145
addParamByObject(Param $param)
Определения Params.php:437
addParamByName(string $paramName, mixed $paramValue, bool $withSave=true)
Определения Params.php:370
array $droppedItems
Определения Params.php:50
const CONTAINS_COLLABER
Определения Params.php:29
saveWithNewChatId(int $chatId)
Определения Params.php:191
string $paramName
Определения Params.php:48
bool $isCreated
Определения Params.php:49
isValidParamName(string $paramName)
Определения Params.php:450
int $chatId
Определения Params.php:47
addParamByArray(?array $chatParams)
Определения Params.php:394
const COPILOT_MAIN_ROLE
Определения Params.php:26
static loadWithoutChat(array $source)
Определения Params.php:172
load($source)
Определения Params.php:115
setIsCreated(bool $isCreated)
Определения Params.php:593
initByDataEntity(ORM\Objectify\EntityObject $entity)
Определения Params.php:272
const IS_COPILOT
Определения Params.php:24
updateParam(string $paramName, $paramValue)
Определения Params.php:482
createParam($paramName)
Определения Params.php:73
setChatId(int $chatId)
Определения Params.php:578
const MANAGE_MESSAGES_AUTO_DELETE
Определения Params.php:30
filterParamData(array $chatParams)
Определения Params.php:463
static getInstance(int $chatId)
Определения Params.php:61
deleteParam(string $paramName, bool $withSave=true)
Определения Params.php:527
isValidType(string $type)
Определения Params.php:140
const TEXT_FIELD_ENABLED
Определения Params.php:27
static array $eventParams
Определения Params.php:51
createEventParam(string $paramName)
Определения Params.php:90
const COPILOT_ENGINE_CODE
Определения Params.php:31
const COPILOT_ROLES
Определения Params.php:25
initByArray(array $items)
Определения Params.php:250
const BACKGROUND_ID
Определения Params.php:28
getName()
Определения Param.php:315
unsetByKeys(array $keys)
Определения Registry.php:11
Определения result.php:20
static getInstance()
Определения servicelocator.php:33
Определения event.php:5
getDataClass()
Определения entity.php:696
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$entity
$filter
Определения iblock_catalog_list.php:54
const PARAM_TYPES
Определения Param.php:25
const TYPE_BOOL
Определения Param.php:19
string $name
Определения Param.php:37
const TYPE_STRING
Определения Param.php:17
string $type
Определения Param.php:34
setType(string $type)
Определения Param.php:121
$entityId
Определения payment.php:4
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$items
Определения template.php:224