3namespace Bitrix\Im\V2\Chat\Param;
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;
12use Bitrix\Main\EventResult;
13use Bitrix\Main\Loader;
38 self::COPILOT_MAIN_ROLE,
39 self::TEXT_FIELD_ENABLED,
40 self::CONTAINS_COLLABER,
42 self::MANAGE_MESSAGES_AUTO_DELETE,
43 self::COPILOT_ENGINE_CODE,
44 self::USER_DELETE_MESSAGE_DISABLED,
53 private const CACHE_TTL = 18144000;
54 private static ?
array $instance = [];
56 private function __construct(
object|
array $array = [],
int $flags = 0,
string $iteratorClass =
"ArrayIterator")
58 parent::__construct($array, $flags, $iteratorClass);
63 if (isset(self::$instance[
$chatId]))
65 return self::$instance[
$chatId];
68 self::$instance[
$chatId] =
new static();
76 if (isset($eventParam))
82 self::CONTAINS_COLLABER,
83 self::TEXT_FIELD_ENABLED,
102 Loader::includeModule(
$data[
'moduleId'])
103 && class_exists(
$data[
'className'])
104 && is_subclass_of(
$data[
'className'], Param::class)
107 return (
new $data[
'className']);
117 if (self::$eventParams ===
null)
122 if (is_array($source))
128 $this->initByEntitiesCollection($source);
135 $this->isLoaded =
true;
149 $event =
new Event(
'im', self::EVENT_CHAT_PARAM_INIT);
151 $resultList =
$event->getResults();
153 foreach ($resultList as $eventResult)
155 if ($eventResult->getType() === EventResult::SUCCESS)
157 $params = $eventResult->getParameters();
169 self::$eventParams = $allParams;
175 $source =
$params->filterParamData($source);
177 foreach ($source as
$key => $item)
179 if (!isset($item[
'CHAT_ID']))
181 $source[
$key][
'CHAT_ID'] = 0;
194 foreach ($this as $item)
201 return $this->save();
209 if ($cache->initCache(self::CACHE_TTL, $this->getCacheId(), $this->getCacheDir()))
216 ->setSelect([
'ID',
'CHAT_ID',
'PARAM_NAME',
'PARAM_VALUE',
'PARAM_JSON'])
217 ->where(
'CHAT_ID',
'=',
$chatId)
221 $cache->startDataCache();
233 foreach ($entitiesCollection as
$entity)
288 public function save():
Result
294 $dataEntityCollection =
new $entityCollectionClass;
298 $itemKeyToUnset = [];
299 foreach ($this as $item)
301 if ($item->isDeleted())
303 $itemKeyToUnset[] = $item->getName();
306 if (!$item->hasValue())
311 if ($item instanceof
Param)
313 if (!$item->isChanged())
318 $prepareResult = $item->prepareFields();
319 if ($prepareResult->isSuccess())
321 if ($prepareResult->getData()[
'SKIP_SAVE'] ??
false)
325 if ($item->isChanged())
327 $dataEntityCollection->add($item->getDataEntity());
328 $item->markChanged(
false);
333 $result->addErrors($prepareResult->getErrors());
339 foreach ($this->droppedItems as $item)
341 if ($item instanceof Param)
343 if ($item->getPrimaryId())
345 $dropIds[] = $item->getPrimaryId();
350 $saveResult = $dataEntityCollection->save(
true);
351 if ($saveResult->isSuccess())
353 if (!empty($dropIds))
355 ChatParamTable::deleteBatch([
'=ID' => $dropIds]);
360 $this->droppedItems = [];
364 $result->addErrors($saveResult->getErrors());
379 $this->
load([[
'CHAT_ID' => $this->
getChatId(),
'PARAM_NAME' => $paramName,
'PARAM_VALUE' => $paramValue]]);
381 if ($this->
get($paramName) !==
null)
383 $this->
get($paramName)->markChanged();
388 return $this->save();
398 if (!isset($chatParams))
406 foreach ($chatParams as $chatParam)
408 if (!isset($chatParam[
'PARAM_NAME']) || !isset($chatParam[
'PARAM_VALUE']))
413 switch ($chatParam[
'PARAM_NAME'])
418 'PARAM_NAME' => $chatParam[
'PARAM_NAME'] ??
null,
419 'PARAM_VALUE' => $chatParam[
'PARAM_VALUE'] ??
null,
424 $this->
load($addParams);
426 foreach ($addParams as $param)
428 if ($this->
get($param[
'PARAM_NAME']) !==
null)
430 $this->
get($param[
'PARAM_NAME'])->markChanged();
434 return $this->save();
439 if ($param->
getName() !==
null)
441 $this[$param->
getName()] = $param;
444 return $this->save();
452 foreach (self::$eventParams as
$name => $param)
460 return in_array(
$paramName, self::CHAT_PARAMS,
true);
465 foreach ($chatParams as
$key => $chatParam)
467 if (!is_array($chatParam))
469 unset($chatParams[
$key]);
473 if (!isset($chatParam[
'PARAM_NAME']) || !isset($chatParam[
'PARAM_VALUE']))
475 unset($chatParams[
$key]);
488 if ($param instanceof
Param)
490 $param->setValue($paramValue);
491 return $this->save();
504 foreach ($this as
$key => $item)
516 ChatParamTable::deleteBatch(
$filter);
519 $this->droppedItems = [];
520 $this->isLoaded =
false;
536 $this->
getParams()->offsetUnset($paramName);
542 '=PARAM_NAME' => $paramName,
545 ChatParamTable::deleteBatch(
$filter);
559 if ($this->offsetExists(
$key))
561 $this[
$key]->markDrop();
562 $this->droppedItems[] = $this[
$key];
565 parent::offsetUnset(
$key);
581 foreach ($this as $param)
583 $param->setChatId($this->chatId);
598 private function getCacheId(): string
600 return "chat_params_{$this->chatId}";
603 private function getCacheDir(): string
605 return static::getCacheDirByChatId($this->chatId);
608 private static function getCacheDirByChatId(
int $chatId): string
612 return "/bx/imc/chatdata/params/1/{$cacheSubDir}/{$chatId}";
const USER_DELETE_MESSAGE_DISABLED
const EVENT_CHAT_PARAM_INIT
static cleanCache(int $chatId)
addParamByObject(Param $param)
addParamByName(string $paramName, mixed $paramValue, bool $withSave=true)
saveWithNewChatId(int $chatId)
isValidParamName(string $paramName)
addParamByArray(?array $chatParams)
static loadWithoutChat(array $source)
setIsCreated(bool $isCreated)
initByDataEntity(ORM\Objectify\EntityObject $entity)
updateParam(string $paramName, $paramValue)
const MANAGE_MESSAGES_AUTO_DELETE
filterParamData(array $chatParams)
static getInstance(int $chatId)
deleteParam(string $paramName, bool $withSave=true)
isValidType(string $type)
static array $eventParams
createEventParam(string $paramName)
const COPILOT_ENGINE_CODE
initByArray(array $items)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']