8use Bitrix\Main\Db\SqlQueryException;
19 public const SITE =
'site';
20 public const MAIL =
'mail';
21 public const XMPP =
'xmpp';
22 public const PUSH =
'push';
34 private static $types = [
47 $this->module = $module;
56 $this->module = $module;
62 public function setName(
string $name): void
72 public function isAllowed(
int $userId,
string $type): bool
79 $encodedSetting = self::encodeName($this->module, $this->name, $type);
87 $encodedSetting = self::encodeName(
'im',
'default', $type);
93 OptionUserTable::query()
94 ->addSelect(
'USER_ID')
95 ->registerRuntimeField(
100 Join::on(
'this.USER_ID',
'ref.ID'),
101 [
'join_type' => Join::TYPE_INNER]
104 ->registerRuntimeField(
108 OptionStateTable::class,
109 Join::on(
'this.NOTIFY_GROUP_ID',
'ref.GROUP_ID')
110 ->where(
'ref.NAME', $encodedSetting),
111 [
'join_type' => Join::TYPE_LEFT]
114 ->whereExpr(
"COALESCE(%s, '$value') = 'Y'", [
'OPTION_STATE.VALUE'])
115 ->where(
'USER_ID', $userId)
116 ->where(
'USER.ACTIVE',
'Y')
117 ->where(
'USER.IS_REAL_USER',
'Y')
121 $result = $result !==
false;
123 if ($type !== self::PUSH)
134 ->where(
'USER_ID', $userId)
137 $pushId = $query->fetch();
139 return $pushId !==
false;
154 if (empty($userList))
160 if (empty($userList))
165 $encodedSetting = self::encodeName($this->module, $this->name, $type);
173 $encodedSetting = self::encodeName(
'im',
'default', $type);
179 if (count($userList) < 1000)
181 $filteredUsers = $this->filterChunk($userList, $encodedSetting, $value);
185 $chunkList = array_chunk($userList, static::CHUNK_LENGTH);
186 foreach ($chunkList as $chunk)
188 $filteredUsers = array_merge($filteredUsers, $this->filterChunk($chunk, $encodedSetting, $value));
192 if ($type !== self::PUSH)
194 return $filteredUsers;
198 if (empty($filteredUsers))
200 return $filteredUsers;
203 $rowFilteredPushUsers =
205 ->addSelect(
'USER_ID')
206 ->whereIn(
'USER_ID', $filteredUsers)
210 foreach ($rowFilteredPushUsers->exec() as $user)
212 $filteredUsers[] = (int)$user[
'USER_ID'];
215 return array_unique($filteredUsers);
218 private function filterChunk(array $userListChunk, $encodedSettingName, $value): array
221 OptionUserTable::query()
222 ->addSelect(
'USER_ID')
223 ->registerRuntimeField(
228 Join::on(
'this.USER_ID',
'ref.ID'),
229 [
'join_type' => Join::TYPE_INNER]
232 ->registerRuntimeField(
236 OptionStateTable::class,
237 Join::on(
'this.NOTIFY_GROUP_ID',
'ref.GROUP_ID')
238 ->where(
'ref.NAME', $encodedSettingName),
239 [
'join_type' => Join::TYPE_LEFT]
242 ->whereExpr(
"COALESCE(%s, '$value') = 'Y'", [
'OPTION_STATE.VALUE'])
243 ->whereIn(
'USER_ID', $userListChunk)
244 ->where(
'USER.ACTIVE',
'Y')
245 ->where(
'USER.IS_REAL_USER',
'Y');
248 foreach ($query->exec() as $user)
250 $filteredUsers[] = (int)$user[
'USER_ID'];
253 return $filteredUsers;
263 if (isset(
$defaultSettings[$this->module][
'NOTIFY'][$this->name][
'DISABLED'][mb_strtoupper($feature)]))
265 return (
bool)
$defaultSettings[$this->module][
'NOTIFY'][$this->name][
'DISABLED'][mb_strtoupper($feature)];
281 public function getValue(
int $userId,
string $type): bool
283 $encodedName = self::encodeName($this->module, $this->name, $type);
294 !empty($cachedPreset)
301 if (!$notifySettings)
308 if (is_null($notifySettings[$encodedName]))
313 return $notifySettings[$encodedName] ===
'Y';
317 OptionStateTable::query()
319 ->registerRuntimeField(
323 OptionUserTable::class,
324 Join::on(
'this.GROUP_ID',
'ref.NOTIFY_GROUP_ID'),
325 [
'join_type' => Join::TYPE_INNER]
328 ->where(
'OPTION_USER.USER_ID', $userId)
329 ->where(
'NAME', $encodedName)
332 $value = $query->fetch()[
'VALUE'];
339 return $value ===
'Y';
349 if (!empty(self::$defaultSettings))
356 $event = ExecuteModuleEventEx($events);
358 if (!is_array($event))
363 foreach ($event as $moduleId => $notifyType)
365 self::$defaultSettings[$moduleId][
'NAME'] =
366 isset($notifyType[
'NOTIFY'], $notifyType[
'NAME'])
367 ? $notifyType[
'NAME']
370 $notify = $notifyType[
'NOTIFY'] ?? $notifyType;
372 foreach ($notify as $notifyEvent => $config)
374 $config[
'DISABLED'] = $config[
'DISABLED'] ?? [];
376 if (!isset($config[
'PUSH']) || $config[
'PUSH'] ===
'NONE')
381 $disabled[
'SITE'] = in_array(self::SITE, $config[
'DISABLED'],
true);
382 $disabled[
'MAIL'] = in_array(self::MAIL, $config[
'DISABLED'],
true);
383 $disabled[
'XMPP'] = in_array(self::XMPP, $config[
'DISABLED'],
true);
384 $disabled[
'PUSH'] = in_array(self::PUSH, $config[
'DISABLED'],
true);
386 $config[
'DISABLED'] = $disabled;
389 $config[
'SITE'] = !isset($config[
'SITE']) || $config[
'SITE'] ==
'Y';
390 $config[
'MAIL'] = !isset($config[
'MAIL']) || $config[
'MAIL'] ==
'Y';
391 $config[
'XMPP'] = !isset($config[
'XMPP']) || $config[
'XMPP'] ==
'Y';
392 $config[
'PUSH'] = isset($config[
'PUSH']) && $config[
'PUSH'] ==
'Y';
394 $config[
'LIFETIME'] = isset($config[
'LIFETIME']) ? (int)$config[
'LIFETIME'] : 0;
396 self::$defaultSettings[$moduleId][
'NOTIFY'][$notifyEvent] = $config;
408 $send[
'SITE'] = $generalSettings[
'notifySchemeSendSite'] ?? $defaultGeneralSettings[
'notifySchemeSendSite'];
409 $send[
'MAIL'] = $generalSettings[
'notifySchemeSendEmail'] ?? $defaultGeneralSettings[
'notifySchemeSendEmail'];
410 $send[
'XMPP'] = $generalSettings[
'notifySchemeSendXmpp'] ?? $defaultGeneralSettings[
'notifySchemeSendXmpp'];
411 $send[
'PUSH'] = $generalSettings[
'notifySchemeSendPush'] ?? $defaultGeneralSettings[
'notifySchemeSendPush'];
415 foreach ($notifySettings as $moduleId => $moduleSchema)
417 foreach ($moduleSchema[
'NOTIFY'] as $eventName => $eventSchema)
419 foreach ([
'SITE',
'MAIL',
'XMPP',
'PUSH'] as $type)
421 if ($eventSchema[
'DISABLED'][$type])
426 $notifySettings[$moduleId][
'NOTIFY'][$eventName][$type] =
429 : $eventSchema[$type]
435 return $notifySettings;
450 OptionStateTable::query()
451 ->setSelect([
'NAME',
'VALUE'])
452 ->registerRuntimeField(
456 OptionUserTable::class,
457 Join::on(
'this.GROUP_ID',
'ref.NOTIFY_GROUP_ID'),
458 [
'join_type' => Join::TYPE_INNER]
461 ->where(
'OPTION_USER_TABLE.USER_ID', $userId)
462 ->whereLike(
'NAME', static::ENTITY.
'%')
466 foreach ($query->exec() as $rowSetting)
468 $rowSettings[] = $rowSetting;
471 if (empty($rowSettings))
476 $decodedSettings = static::decodeSettings($rowSettings);
493 OptionStateTable::query()
494 ->setSelect([
'NAME',
'VALUE'])
495 ->where(
'GROUP_ID', $groupId)
496 ->whereLike(
'NAME', static::ENTITY .
'%')
500 foreach ($query->exec() as $rowSetting)
502 $settings[] = $rowSetting;
505 if (empty($settings))
510 $settings = static::decodeSettings($settings);
531 OptionStateTable::query()
532 ->setSelect([
'NAME',
'VALUE'])
533 ->where(
'GROUP_ID', $groupId)
534 ->whereLike(
'NAME', self::ENTITY .
'%')
537 $enabledSettings = [];
538 $disabledSettings = [];
539 foreach ($query->exec() as $row)
541 if (array_key_exists($row[
'NAME'], $encodedSettings))
543 if ($row[
'VALUE'] === $encodedSettings[$row[
'NAME']])
545 unset($encodedSettings[$row[
'NAME']]);
548 if ($encodedSettings[$row[
'NAME']] ===
'Y')
550 $enabledSettings[] = [
551 'GROUP_ID' => $groupId,
552 'NAME' => $row[
'NAME']
554 unset($encodedSettings[$row[
'NAME']]);
557 if ($encodedSettings[$row[
'NAME']] ===
'N')
559 $disabledSettings[] = [
560 'GROUP_ID' => $groupId,
561 'NAME' => $row[
'NAME']
563 unset($encodedSettings[$row[
'NAME']]);
568 foreach ($encodedSettings as $name => $value)
576 'GROUP_ID' => $groupId,
581 if (!empty($addedSettings))
583 OptionStateTable::addMulti($addedSettings,
true);
585 if (!empty($enabledSettings))
587 OptionStateTable::updateMulti($enabledSettings, [
'VALUE' =>
'Y'],
true);
589 if (!empty($disabledSettings))
591 OptionStateTable::updateMulti($disabledSettings, [
'VALUE' =>
'N'],
true);
602 public static function setSettings(
int $groupId, array $settings = [],
bool $forInitialize =
false): void
604 if (empty($settings) && !$forInitialize)
609 $selectedSettings = [];
611 OptionStateTable::query()
612 ->setSelect([
'NAME',
'VALUE'])
613 ->where(
'GROUP_ID', $groupId)
614 ->whereLike(
'NAME', self::ENTITY .
'%')
616 foreach ($query->exec() as $row)
618 $selectedSettings[$row[
'NAME']] = $row[
'VALUE'];
625 foreach ($encodedSettings as $name => $value)
627 if (!array_key_exists($name,
$defaultSettings) || isset($selectedSettings[$name]))
633 'GROUP_ID' => $groupId,
639 OptionStateTable::addMulti($rows,
true);
648 $names[$moduleId][
'NAME'] = $notifyTypes[
'NAME'];
649 if ($notifyTypes[
'NAME'] ==
'')
651 $moduleObject = \CModule::CreateModuleObject($moduleId);
652 $names[$moduleId][
'NAME'] = $moduleObject->MODULE_NAME;
654 foreach ($notifyTypes[
'NOTIFY'] as $eventId => $event)
656 $names[$moduleId][
'NOTIFY'][$eventId] = $event[
'NAME'];
672 $decodedSettings = [];
674 foreach ($rowSettings as $rowSetting)
676 if (!$rowSetting[
'NAME'])
681 $setting = self::decodeName($rowSetting[
'NAME']);
683 if ($setting ===
null)
690 if(!in_array((
int)$setting[self::TYPE], [1,2,3,4]))
695 $type = self::getType((
int)$setting[self::TYPE]);
697 $decodedSettings[$module][
'NOTIFY'][$name][$type] = $rowSetting[
'VALUE'] ===
'Y';
700 return $decodedSettings;
713 $encodedSettings = [];
715 foreach ($settings as $moduleName => $notifies)
717 if (!is_array($notifies))
722 foreach ($notifies as $notify)
724 if (!is_array($notify))
729 foreach ($notify as $notifyName => $types)
731 foreach ($types as $type => $value)
733 $setting = self::encodeName($moduleName, $notifyName, $type);
735 if (!$setting || mb_strlen($setting) > 64)
740 $encodedSettings[$setting] = $value ?
'Y' :
'N';
747 return $encodedSettings;
757 private static function decodeName(
string $setting): ?array
759 $row = explode(static::SEPARATOR, $setting);
761 if (!array_key_exists(self::MODULE, $row)
762 || !array_key_exists(self::NAME, $row)
763 || !array_key_exists(self::TYPE, $row)
781 private static function encodeName(
string $module,
string $name,
string $type): ?string
788 $postfix = self::getPostfix($type);
790 if ($postfix ===
null)
811 private static function getPostfix(
string $type): ?int
813 return self::$types[mb_strtoupper($type)] ??
null;
821 private static function getType(
int $postfix): ?string
823 $arr = array_flip(self::$types);
825 return $arr[$postfix];
static getUserPresetFromCache(int $userId)
static filterAllowedUsersBySimpleNotificationSettings(array $userList, string $notifyType)
static getDefaultSettings()
static allowedUserBySimpleNotificationSettings(int $userId, string $notifyType)
static updateGroupSettings(int $groupId, array $settings)
checkDisableFeature(string $feature)
isAllowed(int $userId, string $type)
static setSettings(int $groupId, array $settings=[], bool $forInitialize=false)
static getDefaultSettings()
getDefaultFeature(string $feature)
filterAllowedUsers(array $userList, string $type)
static getGroupSettings(int $groupId)
__construct(string $module, string $name)
static getUserSettings(int $userId)
static encodeSettings(array $settings)
static getSimpleNotifySettings(array $generalSettings)
getValue(int $userId, string $type)
static decodeSettings(array $rowSettings)
setModule(string $module)