158 lines
5.4 KiB
PHP
158 lines
5.4 KiB
PHP
<?php
|
|
// models/Setting.php
|
|
require_once dirname(__DIR__) . '/config/database.php';
|
|
|
|
class Setting {
|
|
private $conn;
|
|
private $table_name = "settings";
|
|
|
|
public function __construct() {
|
|
$this->conn = Database::getInstance();
|
|
}
|
|
|
|
/**
|
|
* ดึงค่า setting ตัวเดียว โดยใช้ key
|
|
*/
|
|
public function get($key, $default = null) {
|
|
$query = "SELECT setting_value FROM " . $this->table_name . " WHERE setting_key = :key LIMIT 1";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->bindParam(':key', $key);
|
|
$stmt->execute();
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
$row = $stmt->fetch();
|
|
return $row['setting_value'];
|
|
}
|
|
return $default;
|
|
}
|
|
|
|
/**
|
|
* ตั้งค่า setting ตัวเดียว โดยใช้ key
|
|
*/
|
|
public function set($key, $value) {
|
|
// เช็คว่ามี key นี้อยู่หรือไม่
|
|
$check_query = "SELECT id FROM " . $this->table_name . " WHERE setting_key = :key LIMIT 1";
|
|
$check_stmt = $this->conn->prepare($check_query);
|
|
$check_stmt->bindParam(':key', $key);
|
|
$check_stmt->execute();
|
|
|
|
if ($check_stmt->rowCount() > 0) {
|
|
$query = "UPDATE " . $this->table_name . " SET setting_value = :value WHERE setting_key = :key";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->bindParam(':key', $key);
|
|
$stmt->bindParam(':value', $value);
|
|
} else {
|
|
// สมมติ category เป็น 'general' และ label ให้เท่ากับ key สำหรับค่าใหม่ (ถ้ามี)
|
|
$query = "INSERT INTO " . $this->table_name . " (setting_key, setting_value, category, label) VALUES (:key, :value, 'general', :label)";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->bindParam(':key', $key);
|
|
$stmt->bindParam(':value', $value);
|
|
$stmt->bindParam(':label', $key);
|
|
}
|
|
|
|
return $stmt->execute();
|
|
}
|
|
|
|
/**
|
|
* ดึงค่า setting ทั้งหมด โดยแบ่งตาม category
|
|
*/
|
|
public function getAllByCategory() {
|
|
$query = "SELECT * FROM " . $this->table_name . " ORDER BY category, id";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->execute();
|
|
|
|
$results = [];
|
|
while ($row = $stmt->fetch()) {
|
|
$cat = $row['category'];
|
|
if (!isset($results[$cat])) {
|
|
$results[$cat] = [];
|
|
}
|
|
$results[$cat][] = $row;
|
|
}
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* ดึงค่า setting ทั้งหมดแบบ Flat Array (key => value)
|
|
*/
|
|
public function getAllFlat() {
|
|
$query = "SELECT setting_key, setting_value FROM " . $this->table_name;
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->execute();
|
|
|
|
$results = [];
|
|
while ($row = $stmt->fetch()) {
|
|
$results[$row['setting_key']] = $row['setting_value'];
|
|
}
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* ดึงค่า setting ทั้งหมด โดยจัดกลุ่มเป็น associative array: category => [key => value]
|
|
*/
|
|
public function getAllGrouped() {
|
|
$query = "SELECT * FROM " . $this->table_name;
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->execute();
|
|
|
|
$results = [];
|
|
while ($row = $stmt->fetch()) {
|
|
$cat = $row['category'];
|
|
$key = $row['setting_key'];
|
|
$val = $row['setting_value'];
|
|
|
|
if (!isset($results[$cat])) {
|
|
$results[$cat] = [];
|
|
}
|
|
$results[$cat][$key] = $val;
|
|
}
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* ดึงค่า setting เฉพาะกลุ่ม (Category) แบบ Associative Array
|
|
*/
|
|
public function getGroup($category) {
|
|
$query = "SELECT setting_key, setting_value FROM " . $this->table_name . " WHERE category = :cat";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->bindParam(':cat', $category);
|
|
$stmt->execute();
|
|
|
|
$results = [];
|
|
while ($row = $stmt->fetch()) {
|
|
$results[$row['setting_key']] = $row['setting_value'];
|
|
}
|
|
|
|
// If empty, let's also try to fetch from flat array just in case it was inserted into 'general'
|
|
// This makes it backwards compatible with newly inserted settings.
|
|
if (empty($results)) {
|
|
$flat = $this->getAllFlat();
|
|
return $flat;
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* อัปเดตค่า setting หลายตัวพร้อมกัน
|
|
*/
|
|
public function updateBulk($settings_array) {
|
|
$query = "UPDATE " . $this->table_name . " SET setting_value = :val WHERE setting_key = :key";
|
|
$stmt = $this->conn->prepare($query);
|
|
|
|
$this->conn->beginTransaction();
|
|
try {
|
|
foreach ($settings_array as $key => $val) {
|
|
$stmt->bindValue(':val', $val);
|
|
$stmt->bindValue(':key', $key);
|
|
$stmt->execute();
|
|
}
|
|
$this->conn->commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
$this->conn->rollBack();
|
|
return false;
|
|
}
|
|
}
|
|
}
|