PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/im/lib/Configuration/Manager.php

https://gitlab.com/alexprowars/bitrix
PHP | 273 lines | 220 code | 44 blank | 9 comment | 21 complexity | 43fe939abc6ce8dda747bafb2cbc94a8 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Im\Configuration;
  3. use Bitrix\Im\Common;
  4. use Bitrix\Main\ArgumentException;
  5. use Bitrix\Main\Error;
  6. use Bitrix\Main\ObjectPropertyException;
  7. use Bitrix\Main\Result;
  8. use Bitrix\Main\SystemException;
  9. use Bitrix\Pull\Event;
  10. use CModule;
  11. use COption;
  12. class Manager
  13. {
  14. private const GENERAL = 'general';
  15. private const NOTIFY = 'notify';
  16. private const PRIVACY_SEARCH = 'privacySearch';
  17. private const STATUS = 'status';
  18. public static function getUserSettings(int $userId): Result
  19. {
  20. $result = new Result();
  21. try
  22. {
  23. $preset = Configuration::getUserPreset($userId);
  24. }
  25. catch (ObjectPropertyException | ArgumentException | SystemException $exception)
  26. {
  27. $result->addError(new Error($exception->getMessage(), $exception->getCode()));
  28. return $result;
  29. }
  30. $result->setData($preset);
  31. return $result;
  32. }
  33. /**
  34. * @param int $userId
  35. * @param array{notify: array, general: array} $settings
  36. * @return Result
  37. * @throws ArgumentException
  38. * @throws ObjectPropertyException
  39. * @throws SystemException
  40. */
  41. public static function setUserSettings(int $userId, array $settings): Result
  42. {
  43. $result = new Result();
  44. if (
  45. !array_key_exists(self::NOTIFY, $settings)
  46. || !array_key_exists(self::GENERAL, $settings)
  47. )
  48. {
  49. $result->addError(new Error('Incorrect data when receiving chat settings', 400));
  50. return $result;
  51. }
  52. self::updateUserStatus($userId, $settings['general']);
  53. self::sendSettingsChangeEvent($userId, $settings['general']);
  54. self::disableUserSearch($userId, $settings['general']);
  55. $userPresetId =
  56. \Bitrix\Im\Model\OptionGroupTable::query()
  57. ->addSelect('ID')
  58. ->where('USER_ID', $userId)
  59. ->fetch()['ID']
  60. ;
  61. if (isset($settings['general']['notifyScheme']) && $settings['general']['notifyScheme'] === 'simple')
  62. {
  63. $settings['notify'] = self::getSimpleNotifySettings($settings['general']);
  64. }
  65. if (!$userPresetId)
  66. {
  67. $userPresetId = Configuration::createUserPreset($userId, $settings);
  68. }
  69. else
  70. {
  71. Configuration::updatePresetSettings($userPresetId, $userId, $settings);
  72. Configuration::chooseExistingPreset($userPresetId, $userId);
  73. }
  74. Configuration::cleanUserCache($userId);
  75. self::enableUserSearch($userId, $settings['general']);
  76. return $result;
  77. }
  78. public static function setUserSetting(int $userId, string $type, array $settings): Result
  79. {
  80. $result = new Result();
  81. if (!in_array($type, [self::NOTIFY, self::GENERAL], true))
  82. {
  83. $result->addError(new Error('Incorrect data when receiving chat settings', 400));
  84. return $result;
  85. }
  86. $userPresetId =
  87. \Bitrix\Im\Model\OptionGroupTable::query()
  88. ->addSelect('ID')
  89. ->where('USER_ID', $userId)
  90. ->fetch()['ID']
  91. ;
  92. if ($type === self::NOTIFY)
  93. {
  94. if (!$userPresetId)
  95. {
  96. $preset['notify'] = $settings;
  97. $preset['general'] = [];
  98. Configuration::createUserPreset($userId, $preset);
  99. return $result;
  100. }
  101. Notification::updateGroupSettings($userPresetId, $settings);
  102. }
  103. if ($type === self::GENERAL)
  104. {
  105. self::updateUserStatus($userId, $settings);
  106. self::sendSettingsChangeEvent($userId, $settings);
  107. self::disableUserSearch($userId, $settings);
  108. if (!$userPresetId)
  109. {
  110. $preset['general'] = array_replace_recursive(General::getDefaultSettings(), $settings);
  111. $preset['notify'] = [];
  112. Configuration::createUserPreset($userId, $preset);
  113. return $result;
  114. }
  115. General::updateGroupSettings($userPresetId, $settings);
  116. self::enableUserSearch($userId, $settings);
  117. }
  118. Configuration::cleanUserCache($userId);
  119. return $result;
  120. }
  121. public static function isSettingsMigrated(): bool
  122. {
  123. return COption::GetOptionString('im', 'migration_to_new_settings') === 'Y';
  124. }
  125. public static function isUserMigrated(int $userId): bool
  126. {
  127. $lastConvertedId = COption::GetOptionInt('im', 'last_converted_user');
  128. return $userId < $lastConvertedId;
  129. }
  130. public static function getNotifyAccess($userId, $moduleId, $eventId, $type)
  131. {
  132. $generalSettings = General::createWithUserId($userId);
  133. $notifyScheme = $generalSettings->getValue('notifyScheme');
  134. if ($notifyScheme !== 'expert')
  135. {
  136. if ($type === Notification::SITE)
  137. {
  138. return $generalSettings->getValue('notifySchemeSendSite');
  139. }
  140. if ($type === Notification::MAIL)
  141. {
  142. return $generalSettings->getValue('notifySchemeSendEmail');
  143. }
  144. if ($type === Notification::PUSH)
  145. {
  146. return $generalSettings->getValue('notifySchemeSendPush');
  147. }
  148. if ($type === Notification::XMPP)
  149. {
  150. return $generalSettings->getValue('notifySchemeSendXmpp');
  151. }
  152. }
  153. return (new Notification($moduleId, $eventId))->isAllowed($userId, $type);
  154. }
  155. private static function sendSettingsChangeEvent(int $userId, array $generalSettings): void
  156. {
  157. // TODO: refactoring required for the new interface
  158. if (isset($generalSettings['openDesktopFromPanel']) && CModule::IncludeModule('pull'))
  159. {
  160. Event::add($userId, [
  161. 'module_id' => 'im',
  162. 'command' => 'settingsUpdate',
  163. 'expiry' => 5,
  164. 'params' => [
  165. 'openDesktopFromPanel' => $generalSettings['openDesktopFromPanel'],
  166. ],
  167. 'extra' => Common::getPullExtra()
  168. ]);
  169. }
  170. }
  171. private static function disableUserSearch(int $userId, array $generalSettings): void
  172. {
  173. $defaultSettings = General::getDefaultSettings();
  174. if (
  175. array_key_exists(self::PRIVACY_SEARCH, $generalSettings)
  176. && $defaultSettings[self::PRIVACY_SEARCH] === $generalSettings[self::PRIVACY_SEARCH]
  177. )
  178. {
  179. global $USER_FIELD_MANAGER;
  180. $USER_FIELD_MANAGER->Update("USER", $userId, ['UF_IM_SEARCH' => '']);
  181. }
  182. }
  183. private static function enableUserSearch(int $userId, array $generalSettings): void
  184. {
  185. if (isset($generalSettings[self::PRIVACY_SEARCH]))
  186. {
  187. global $USER_FIELD_MANAGER;
  188. $USER_FIELD_MANAGER->Update(
  189. "USER",
  190. $userId,
  191. [
  192. 'UF_IM_SEARCH' => $generalSettings[self::PRIVACY_SEARCH],
  193. ]
  194. );
  195. }
  196. }
  197. private static function updateUserStatus(int $userId, array $generalSettings): void
  198. {
  199. if (isset($generalSettings[self::STATUS]))
  200. {
  201. \CIMStatus::Set($userId, ['STATUS' => $generalSettings[self::STATUS]]);
  202. }
  203. }
  204. public static function getSimpleNotifySettings(array $generalSettings): array
  205. {
  206. $defaultGeneralSettings = General::getDefaultSettings();
  207. $send['SITE'] = $generalSettings['notifySchemeSendSite'] ?? $defaultGeneralSettings['notifySchemeSendSite'];
  208. $send['MAIL'] = $generalSettings['notifySchemeSendEmail'] ?? $defaultGeneralSettings['notifySchemeSendEmail'];
  209. $send['XMPP'] = $generalSettings['notifySchemeSendXmpp'] ?? $defaultGeneralSettings['notifySchemeSendXmpp'];
  210. $send['PUSH'] = $generalSettings['notifySchemeSendPush'] ?? $defaultGeneralSettings['notifySchemeSendPush'];
  211. $notifySettings = Notification::getDefaultSettings();
  212. foreach ($notifySettings as $moduleId => $moduleSchema)
  213. {
  214. foreach ($moduleSchema['NOTIFY'] as $eventName => $eventSchema)
  215. {
  216. foreach (['SITE', 'MAIL', 'XMPP', 'PUSH'] as $type)
  217. {
  218. $disableType = $eventSchema['DISABLED'][$type];
  219. $notifySettings[$moduleId]['NOTIFY'][$eventName][$type] =
  220. !$send[$type]
  221. ? false
  222. : $eventSchema[$type]
  223. ;
  224. }
  225. }
  226. }
  227. return $notifySettings;
  228. }
  229. }