102 lines
4.1 KiB
PHP
102 lines
4.1 KiB
PHP
<?php
|
|
require_once '../init.php';
|
|
requireLogin();
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$action = $_POST['action'] ?? $_GET['action'] ?? '';
|
|
|
|
try {
|
|
switch ($action) {
|
|
case 'add':
|
|
$name = trim($_POST['name'] ?? '');
|
|
$url = trim($_POST['url'] ?? '');
|
|
$display_order = (int)($_POST['display_order'] ?? 0);
|
|
$status = $_POST['status'] ?? 'normal';
|
|
$logo_url = trim($_POST['logo_url'] ?? '');
|
|
|
|
if (!$name || !$url) {
|
|
throw new Exception("ชื่อและลิงค์เว็บไซต์เป็นข้อมูลจำเป็น");
|
|
}
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO websites (name, url, status, logo_url, display_order) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([$name, $url, $status, $logo_url, $display_order]);
|
|
|
|
logAction($pdo, 'Add Website', "Added website: $name ($url)");
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
|
|
case 'edit':
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
$name = trim($_POST['name'] ?? '');
|
|
$url = trim($_POST['url'] ?? '');
|
|
$display_order = (int)($_POST['display_order'] ?? 0);
|
|
$status = $_POST['status'] ?? 'normal';
|
|
$logo_url = trim($_POST['logo_url'] ?? '');
|
|
|
|
if (!$id || !$name || !$url) {
|
|
throw new Exception("ข้อมูลไม่ครบถ้วน");
|
|
}
|
|
|
|
$stmt = $pdo->prepare("UPDATE websites SET name = ?, url = ?, status = ?, logo_url = ?, display_order = ? WHERE id = ?");
|
|
$stmt->execute([$name, $url, $status, $logo_url, $display_order, $id]);
|
|
|
|
logAction($pdo, 'Edit Website', "Edited website ID $id to: $name");
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
|
|
case 'delete':
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
if (!$id) throw new Exception("ไม่พบ ID");
|
|
|
|
$stmt = $pdo->prepare("SELECT name FROM websites WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$web = $stmt->fetch();
|
|
|
|
if ($web) {
|
|
$stmt = $pdo->prepare("DELETE FROM websites WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
logAction($pdo, 'Delete Website', "Deleted website: {$web['name']}");
|
|
}
|
|
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
|
|
case 'reset_clicks':
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
if (!$id) throw new Exception("ไม่พบ ID");
|
|
|
|
$stmt = $pdo->prepare("SELECT name FROM websites WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$web = $stmt->fetch();
|
|
|
|
if ($web) {
|
|
$stmt = $pdo->prepare("UPDATE websites SET click_count = 0 WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
logAction($pdo, 'Reset Clicks', "Reset click count for website: {$web['name']}");
|
|
}
|
|
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
|
|
case 'save_settings':
|
|
$site_name = trim($_POST['site_name'] ?? '');
|
|
$site_subtitle = trim($_POST['site_subtitle'] ?? '');
|
|
|
|
if (!$site_name) throw new Exception("ชื่อระบบเป็นข้อมูลจำเป็น");
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)");
|
|
$stmt->execute(['site_name', $site_name]);
|
|
$stmt->execute(['site_subtitle', $site_subtitle]);
|
|
|
|
logAction($pdo, 'Update Settings', "Updated site settings");
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
|
|
default:
|
|
throw new Exception("Invalid action");
|
|
}
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
|
}
|