'success']); } else { echo json_encode(['status' => 'error', 'message' => 'รหัสผ่านไม่ถูกต้อง']); } } } exit; } if ($isTestSqlAjax) { while (ob_get_level()) { ob_end_clean(); } header('Content-Type: application/json; charset=utf-8'); require_once __DIR__ . '/../includes/db.php'; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $raw_post = file_get_contents('php://input'); $data = json_decode($raw_post, true); $sql = ''; if ($data && isset($data['sql_text_b64'])) { $sql = trim(urldecode(base64_decode($data['sql_text_b64']))); } elseif ($data && isset($data['sql_text'])) { $sql = trim($data['sql_text']); } if ($sql !== '') { $current_month = (int)date('m'); $current_year = (int)date('Y'); $current_fy = ($current_month >= 10) ? $current_year + 1 : $current_year; $params = []; if (strpos($sql, ':start_ly') !== false) $params['start_ly'] = ($current_fy - 2) . '-10-01'; if (strpos($sql, ':end_ly') !== false) $params['end_ly'] = ($current_fy - 1) . '-09-30'; if (preg_match('/:start\b/', $sql)) $params['start'] = date('Y-m-01'); if (preg_match('/:end\b/', $sql)) $params['end'] = date('Y-m-d'); try { if (stripos($sql, 'SELECT') === 0 && stripos($sql, 'LIMIT') === false) { $sql_to_run = $sql . " LIMIT 5"; } else { $sql_to_run = $sql; } $sql_compiled = $sql; foreach ($params as $k => $v) { $sql_compiled = preg_replace('/:' . $k . '\b/', "'" . $v . "'", $sql_compiled); } $stmt = $pdo->prepare($sql_to_run); $stmt->execute($params); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode(['status' => 'success', 'data' => $results, 'params_used' => $params, 'sql_compiled' => $sql_compiled]); } catch (\Exception $e) { echo json_encode(['status' => 'error', 'message' => $e->getMessage()]); } } else { echo json_encode(['status' => 'error', 'message' => 'Invalid JSON']); } } exit; } if ($isSaveSingleSqlAjax) { while (ob_get_level()) { ob_end_clean(); } header('Content-Type: application/json; charset=utf-8'); require_once __DIR__ . '/../includes/db.php'; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $raw_post = file_get_contents('php://input'); $data = json_decode($raw_post, true); $sql_text = ''; if ($data && isset($data['sql_text_b64'])) { $sql_text = trim(urldecode(base64_decode($data['sql_text_b64']))); } elseif ($data && isset($data['sql_text'])) { $sql_text = trim($data['sql_text']); } if ($data && isset($data['section'], $data['key']) && $sql_text !== '') { $success = false; $msg = ''; if (isset($pdo_dashboard) && $pdo_dashboard) { try { $stmt = $pdo_dashboard->prepare(" INSERT INTO waiting (section_name, query_key, sql_text) VALUES (:section_name, :query_key, :sql_text) ON DUPLICATE KEY UPDATE sql_text = :sql_text2 "); $stmt->execute([ ':section_name' => $data['section'], ':query_key' => $data['key'], ':sql_text' => $sql_text, ':sql_text2' => $sql_text ]); $success = true; $msg = 'บันทึกคำสั่งสำเร็จ'; } catch (\Exception $e) { $msg = 'DB Error: ' . $e->getMessage(); } } else { $sql_config_path = __DIR__ . '/../includes/sql_config.json'; $current_config = file_exists($sql_config_path) ? json_decode(file_get_contents($sql_config_path), true) : []; if (!is_array($current_config)) $current_config = []; if (!isset($current_config[$data['section']])) $current_config[$data['section']] = []; $current_config[$data['section']][$data['key']] = $sql_text; if (file_put_contents($sql_config_path, json_encode($current_config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)) !== false) { $success = true; $msg = 'บันทึกคำสั่งสำเร็จ (JSON)'; } else { $msg = 'ไม่สามารถเขียนไฟล์ JSON ได้'; } } echo json_encode(['status' => $success ? 'success' : 'error', 'message' => $msg]); } else { echo json_encode(['status' => 'error', 'message' => 'Invalid Request']); } } exit; } if ($isSettingsAjax) { while (ob_get_level()) { ob_end_clean(); } header('Content-Type: application/json; charset=utf-8'); require_once __DIR__ . '/../includes/db.php'; require_once __DIR__ . '/../includes/sql_config.php'; // เพื่อให้ได้ตัวแปร $sql_config ล่าสุด if ($_SERVER['REQUEST_METHOD'] === 'POST') { $raw_post = file_get_contents('php://input'); $new_config = json_decode($raw_post, true); if (json_last_error() === JSON_ERROR_NONE) { $success = false; $msg = ''; // 1. ถ้ามี Database Dashboard ให้บันทึกลง Database if (isset($pdo_dashboard) && $pdo_dashboard) { try { $pdo_dashboard->beginTransaction(); // ใช้ UPSERT เพื่อ Insert ถ้ายังไม่มี หรือ Update ถ้ามีแล้ว $stmt = $pdo_dashboard->prepare(" INSERT INTO waiting (section_name, query_key, sql_text) VALUES (:section_name, :query_key, :sql_text) ON DUPLICATE KEY UPDATE sql_text = :sql_text2 "); foreach ($new_config as $section => $keys) { foreach ($keys as $key => $sql_text) { $stmt->execute([ ':section_name' => $section, ':query_key' => $key, ':sql_text' => $sql_text, ':sql_text2' => $sql_text ]); } } $pdo_dashboard->commit(); $success = true; $msg = 'บันทึกการตั้งค่าลงฐานข้อมูลเรียบร้อยแล้ว'; } catch (\PDOException $e) { $pdo_dashboard->rollBack(); $success = false; $msg = 'เกิดข้อผิดพลาดฐานข้อมูล: ' . $e->getMessage(); } } else { // 2. Fallback ไปบันทึกลง JSON $sql_config_path = __DIR__ . '/../includes/sql_config.json'; $current_config = file_exists($sql_config_path) ? json_decode(file_get_contents($sql_config_path), true) : []; if (!is_array($current_config)) $current_config = []; $merged_config = array_replace_recursive($current_config, $new_config); $saved = file_put_contents($sql_config_path, json_encode($merged_config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); if ($saved !== false) { $success = true; $msg = 'บันทึกการตั้งค่าลงไฟล์ JSON แล้ว (ไม่ได้เชื่อมต่อฐานข้อมูล)'; } else { $success = false; $msg = 'ไม่สามารถบันทึกไฟล์ JSON ได้ (กรุณาตรวจสอบสิทธิ์โฟลเดอร์)'; } } if ($success) { echo json_encode(['status' => 'success', 'message' => $msg]); } else { echo json_encode(['status' => 'error', 'message' => $msg]); } } else { echo json_encode(['status' => 'error', 'message' => 'ข้อมูลที่ส่งมาไม่ถูกต้อง (Invalid JSON)']); } } else { // GET request: ส่งค่า $sql_config ซึ่งอาจจะดึงจาก DB หรือ JSON มาแล้ว $json_out = @json_encode(['status' => 'success', 'data' => $sql_config], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); if (!$json_out) { echo json_encode(['status' => 'error', 'message' => 'JSON Encode Error: ' . json_last_error_msg()]); } else { echo $json_out; } } exit; } ?>