File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Services/PlatformSettingService.php
Back
<?php /** * PlatformSettingService * Admin-configurable platform settings with validation, group management, and audit logging */ require_once __DIR__ . '/../Models/PlatformSetting.php'; require_once __DIR__ . '/../Models/AdminAuditLog.php'; class PlatformSettingService { private PlatformSetting $settingModel; private AdminAuditLog $auditLog; // Validation rules per setting key [type, min, max] private array $validationRules = [ 'default_commission_rate' => ['float', 0.0, 50.0], 'max_products_per_store' => ['int', 1, 10000], 'session_lifetime_minutes' => ['int', 5, 1440], 'max_login_attempts' => ['int', 1, 20], 'lockout_duration_minutes' => ['int', 1, 1440], 'allow_seller_registration' => ['bool', null, null], 'maintenance_mode' => ['bool', null, null], 'cod_enabled' => ['bool', null, null], 'bank_transfer_enabled' => ['bool', null, null], 'notify_new_order' => ['bool', null, null], 'notify_order_shipped' => ['bool', null, null], ]; public function __construct() { $this->settingModel = new PlatformSetting(); $this->auditLog = new AdminAuditLog(); } public function getAllGrouped(): array { return $this->settingModel->getAll(); } public function get(string $key, $default = null) { return $this->settingModel->get($key, $default); } public function saveSettings(array $formData, int $adminId): array { $errors = []; $saved = 0; foreach ($formData as $key => $value) { $value = trim((string)$value); // Validate known keys if (isset($this->validationRules[$key])) { [$type, $min, $max] = $this->validationRules[$key]; $validationError = $this->validate($key, $value, $type, $min, $max); if ($validationError) { $errors[] = $validationError; continue; } // Normalize booleans if ($type === 'bool') { $value = in_array($value, ['1', 'true', 'on', 'yes'], true) ? '1' : '0'; } } $old = $this->settingModel->get($key); $this->settingModel->set($key, $value, $adminId); if ($old !== $value) { $this->auditLog->record($adminId, 'settings.change', 'settings', 'setting', null, "{$key}: '{$old}' -> '{$value}'"); $saved++; } } if (!empty($errors)) { return ['success' => false, 'message' => implode(', ', $errors)]; } return ['success' => true, 'message' => "บันทึก {$saved} การตั้งค่าเรียบร้อยแล้ว"]; } private function validate(string $key, string $value, string $type, $min, $max): ?string { if ($type === 'int') { if (!ctype_digit($value)) return "ค่า {$key} ต้องเป็นตัวเลขจำนวนเต็ม"; $v = (int)$value; if ($min !== null && $v < $min) return "ค่า {$key} ต้องไม่น้อยกว่า {$min}"; if ($max !== null && $v > $max) return "ค่า {$key} ต้องไม่มากกว่า {$max}"; } elseif ($type === 'float') { if (!is_numeric($value)) return "ค่า {$key} ต้องเป็นตัวเลข"; $v = (float)$value; if ($min !== null && $v < $min) return "ค่า {$key} ต้องไม่น้อยกว่า {$min}"; if ($max !== null && $v > $max) return "ค่า {$key} ต้องไม่มากกว่า {$max}"; } return null; } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings