341 lines
12 KiB
PHP
341 lines
12 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace MintyPHP\Service;
|
||
|
|
|
||
|
|
use MintyPHP\Repository\SettingRepository;
|
||
|
|
use MintyPHP\Repository\TenantRepository;
|
||
|
|
use MintyPHP\Repository\RoleRepository;
|
||
|
|
use MintyPHP\Repository\DepartmentRepository;
|
||
|
|
|
||
|
|
class SettingService
|
||
|
|
{
|
||
|
|
public const DEFAULT_TENANT_KEY = 'default_tenant_id';
|
||
|
|
public const DEFAULT_ROLE_KEY = 'default_role_id';
|
||
|
|
public const DEFAULT_DEPARTMENT_KEY = 'default_department_id';
|
||
|
|
public const APP_TITLE_KEY = 'app_title';
|
||
|
|
public const APP_LOCALE_KEY = 'app_locale';
|
||
|
|
public const APP_THEME_KEY = 'app_theme';
|
||
|
|
public const APP_THEME_USER_KEY = 'app_theme_user';
|
||
|
|
public const APP_PRIMARY_COLOR_KEY = 'app_primary_color';
|
||
|
|
public const SMTP_HOST_KEY = 'smtp_host';
|
||
|
|
public const SMTP_PORT_KEY = 'smtp_port';
|
||
|
|
public const SMTP_USER_KEY = 'smtp_user';
|
||
|
|
public const SMTP_PASSWORD_KEY = 'smtp_password';
|
||
|
|
public const SMTP_SECURE_KEY = 'smtp_secure';
|
||
|
|
public const SMTP_FROM_KEY = 'smtp_from';
|
||
|
|
public const SMTP_FROM_NAME_KEY = 'smtp_from_name';
|
||
|
|
|
||
|
|
public static function get(string $key): ?array
|
||
|
|
{
|
||
|
|
return SettingRepository::find($key);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getValue(string $key): ?string
|
||
|
|
{
|
||
|
|
return SettingRepository::getValue($key);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getInt(string $key): ?int
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue($key);
|
||
|
|
if ($value === null || $value === '') {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return (int) $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function set(string $key, ?string $value, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
return SettingRepository::set($key, $value, $description);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getDefaultTenantId(): ?int
|
||
|
|
{
|
||
|
|
$id = self::getInt(self::DEFAULT_TENANT_KEY);
|
||
|
|
return $id && $id > 0 ? $id : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setDefaultTenantId(?int $tenantId, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $tenantId && $tenantId > 0 ? (string) $tenantId : null;
|
||
|
|
if ($value !== null) {
|
||
|
|
$exists = TenantRepository::find($tenantId ?? 0);
|
||
|
|
if (!$exists) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.default_tenant';
|
||
|
|
return SettingRepository::set(self::DEFAULT_TENANT_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getDefaultRoleId(): ?int
|
||
|
|
{
|
||
|
|
$id = self::getInt(self::DEFAULT_ROLE_KEY);
|
||
|
|
return $id && $id > 0 ? $id : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setDefaultRoleId(?int $roleId, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $roleId && $roleId > 0 ? (string) $roleId : null;
|
||
|
|
if ($value !== null) {
|
||
|
|
$exists = RoleRepository::find($roleId ?? 0);
|
||
|
|
if (!$exists) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.default_role';
|
||
|
|
return SettingRepository::set(self::DEFAULT_ROLE_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getDefaultDepartmentId(): ?int
|
||
|
|
{
|
||
|
|
$id = self::getInt(self::DEFAULT_DEPARTMENT_KEY);
|
||
|
|
return $id && $id > 0 ? $id : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setDefaultDepartmentId(?int $departmentId, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $departmentId && $departmentId > 0 ? (string) $departmentId : null;
|
||
|
|
if ($value !== null) {
|
||
|
|
$exists = DepartmentRepository::find($departmentId ?? 0);
|
||
|
|
if (!$exists) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.default_department';
|
||
|
|
return SettingRepository::set(self::DEFAULT_DEPARTMENT_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getAppTitle(): ?string
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::APP_TITLE_KEY);
|
||
|
|
$value = $value !== null ? trim($value) : null;
|
||
|
|
return $value !== '' ? $value : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setAppTitle(?string $title, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $title !== null ? trim($title) : null;
|
||
|
|
if ($value === '') {
|
||
|
|
$value = null;
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.app_title';
|
||
|
|
return SettingRepository::set(self::APP_TITLE_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getAppLocale(): ?string
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::APP_LOCALE_KEY);
|
||
|
|
$value = $value !== null ? trim((string) $value) : '';
|
||
|
|
return $value !== '' ? $value : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setAppLocale(?string $locale, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $locale !== null ? trim((string) $locale) : '';
|
||
|
|
if ($value === '') {
|
||
|
|
$value = null;
|
||
|
|
} else {
|
||
|
|
$allowed = defined('APP_LOCALES') ? APP_LOCALES : [];
|
||
|
|
if ($allowed && !in_array($value, $allowed, true)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.app_locale';
|
||
|
|
return SettingRepository::set(self::APP_LOCALE_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getAppTheme(): ?string
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::APP_THEME_KEY);
|
||
|
|
$value = $value !== null ? strtolower(trim((string) $value)) : '';
|
||
|
|
if (!in_array($value, ['light', 'dark'], true)) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setAppTheme(?string $theme, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $theme !== null ? strtolower(trim((string) $theme)) : '';
|
||
|
|
if ($value === '' || !in_array($value, ['light', 'dark'], true)) {
|
||
|
|
$value = null;
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.app_theme';
|
||
|
|
return SettingRepository::set(self::APP_THEME_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function isUserThemeAllowed(): bool
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::APP_THEME_USER_KEY);
|
||
|
|
if ($value === null || $value === '') {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return in_array(strtolower($value), ['1', 'true', 'yes', 'on'], true);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setUserThemeAllowed(bool $allowed, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $allowed ? '1' : '0';
|
||
|
|
$desc = $description ?? 'setting.app_theme_user';
|
||
|
|
return SettingRepository::set(self::APP_THEME_USER_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getAppPrimaryColor(): ?string
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::APP_PRIMARY_COLOR_KEY);
|
||
|
|
$value = $value !== null ? strtolower(trim((string) $value)) : '';
|
||
|
|
if ($value === '') {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $value)) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setAppPrimaryColor(?string $color, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $color !== null ? strtolower(trim((string) $color)) : '';
|
||
|
|
if ($value !== '' && $value[0] !== '#') {
|
||
|
|
if (preg_match('/^[0-9a-f]{3}$/i', $value)
|
||
|
|
|| preg_match('/^[0-9a-f]{4}$/i', $value)
|
||
|
|
|| preg_match('/^[0-9a-f]{6}$/i', $value)
|
||
|
|
|| preg_match('/^[0-9a-f]{8}$/i', $value)) {
|
||
|
|
$value = '#' . $value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if ($value === '') {
|
||
|
|
$value = null;
|
||
|
|
} elseif (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $value)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.app_primary_color';
|
||
|
|
return SettingRepository::set(self::APP_PRIMARY_COLOR_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getSmtpHost(): ?string
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::SMTP_HOST_KEY);
|
||
|
|
$value = $value !== null ? trim((string) $value) : '';
|
||
|
|
return $value !== '' ? $value : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setSmtpHost(?string $host, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $host !== null ? trim((string) $host) : '';
|
||
|
|
if ($value === '') {
|
||
|
|
$value = null;
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.smtp_host';
|
||
|
|
return SettingRepository::set(self::SMTP_HOST_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getSmtpPort(): ?int
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::SMTP_PORT_KEY);
|
||
|
|
if ($value === null || $value === '') {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
$port = (int) $value;
|
||
|
|
return $port > 0 ? $port : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setSmtpPort(?int $port, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $port && $port > 0 ? (string) $port : null;
|
||
|
|
$desc = $description ?? 'setting.smtp_port';
|
||
|
|
return SettingRepository::set(self::SMTP_PORT_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getSmtpUser(): ?string
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::SMTP_USER_KEY);
|
||
|
|
$value = $value !== null ? trim((string) $value) : '';
|
||
|
|
return $value !== '' ? $value : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setSmtpUser(?string $user, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $user !== null ? trim((string) $user) : '';
|
||
|
|
if ($value === '') {
|
||
|
|
$value = null;
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.smtp_user';
|
||
|
|
return SettingRepository::set(self::SMTP_USER_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getSmtpPassword(): ?string
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::SMTP_PASSWORD_KEY);
|
||
|
|
$value = $value !== null ? (string) $value : '';
|
||
|
|
return $value !== '' ? $value : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setSmtpPassword(?string $password, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $password !== null ? (string) $password : '';
|
||
|
|
if ($value === '') {
|
||
|
|
$value = null;
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.smtp_password';
|
||
|
|
return SettingRepository::set(self::SMTP_PASSWORD_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getSmtpSecure(): ?string
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::SMTP_SECURE_KEY);
|
||
|
|
$value = $value !== null ? strtolower(trim((string) $value)) : '';
|
||
|
|
if (!in_array($value, ['tls', 'ssl', 'none'], true)) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return $value === 'none' ? null : $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setSmtpSecure(?string $secure, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $secure !== null ? strtolower(trim((string) $secure)) : '';
|
||
|
|
if ($value === '' || $value === 'none') {
|
||
|
|
$value = null;
|
||
|
|
} elseif (!in_array($value, ['tls', 'ssl'], true)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.smtp_secure';
|
||
|
|
return SettingRepository::set(self::SMTP_SECURE_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getSmtpFrom(): ?string
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::SMTP_FROM_KEY);
|
||
|
|
$value = $value !== null ? trim((string) $value) : '';
|
||
|
|
return $value !== '' ? $value : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setSmtpFrom(?string $from, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $from !== null ? trim((string) $from) : '';
|
||
|
|
if ($value === '') {
|
||
|
|
$value = null;
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.smtp_from';
|
||
|
|
return SettingRepository::set(self::SMTP_FROM_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getSmtpFromName(): ?string
|
||
|
|
{
|
||
|
|
$value = SettingRepository::getValue(self::SMTP_FROM_NAME_KEY);
|
||
|
|
$value = $value !== null ? trim((string) $value) : '';
|
||
|
|
return $value !== '' ? $value : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function setSmtpFromName(?string $fromName, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$value = $fromName !== null ? trim((string) $fromName) : '';
|
||
|
|
if ($value === '') {
|
||
|
|
$value = null;
|
||
|
|
}
|
||
|
|
$desc = $description ?? 'setting.smtp_from_name';
|
||
|
|
return SettingRepository::set(self::SMTP_FROM_NAME_KEY, $value, $desc);
|
||
|
|
}
|
||
|
|
}
|