prepare($sql); $stmt->execute([ 'user_id' => $user_id, 'user_name' => $user_name, 'action' => $action, 'detail' => $detail, 'ip' => $ip_address ]); } catch (PDOException $e) { // Silently fail if log cannot be written, or handle appropriately error_log("Failed to write log: " . $e->getMessage()); } } function check_login() { if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) { header("location: index.php"); exit; } } function get_setting($pdo, $setting_name) { $sql = "SELECT setting_value FROM system_settings WHERE setting_name = :name LIMIT 1"; try { $stmt = $pdo->prepare($sql); $stmt->execute(['name' => $setting_name]); $row = $stmt->fetch(); return $row ? $row['setting_value'] : ''; } catch (PDOException $e) { return ''; } } function save_setting($pdo, $setting_name, $setting_value, $updated_by) { $sql = "INSERT INTO system_settings (setting_name, setting_value, updated_by) VALUES (:name, :value, :updated_by) ON DUPLICATE KEY UPDATE setting_value = :value2, updated_by = :updated_by2"; try { $stmt = $pdo->prepare($sql); $stmt->execute([ 'name' => $setting_name, 'value' => $setting_value, 'updated_by' => $updated_by, 'value2' => $setting_value, 'updated_by2' => $updated_by ]); return true; } catch (PDOException $e) { return false; } } function format_thai_date($date_str, $with_time = false) { if (empty($date_str) || $date_str === '0000-00-00 00:00:00' || $date_str === '0000-00-00') return '-'; $timestamp = strtotime($date_str); if (!$timestamp) return '-'; $thai_months = [ 1 => 'ม.ค.', 2 => 'ก.พ.', 3 => 'มี.ค.', 4 => 'เม.ย.', 5 => 'พ.ค.', 6 => 'มิ.ย.', 7 => 'ก.ค.', 8 => 'ส.ค.', 9 => 'ก.ย.', 10 => 'ต.ค.', 11 => 'พ.ย.', 12 => 'ธ.ค.' ]; $day = date('j', $timestamp); $month = $thai_months[(int)date('n', $timestamp)]; $year = date('Y', $timestamp) + 543; if ($with_time) { $time = date('H:i:s', $timestamp); return "$day $month $year $time"; } return "$day $month $year"; } ?>