53 lines
1.9 KiB
PHP
53 lines
1.9 KiB
PHP
<?php
|
|
require_once '../include/init.php';
|
|
require_once '../include/auth.php';
|
|
|
|
// --- Security Check: Only Admin can access this API ---
|
|
if (!isAdmin()) {
|
|
http_response_code(403); // Forbidden
|
|
echo json_encode(['error' => 'Permission denied.']);
|
|
exit;
|
|
}
|
|
|
|
header('Content-Type: application/json');
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$response = [];
|
|
|
|
try {
|
|
if ($method === 'GET') {
|
|
// Get all hidden tab settings
|
|
$stmt = $pdo->query("SELECT user_id, tab_id FROM user_tab_visibility");
|
|
$response = $stmt->fetchAll();
|
|
|
|
} elseif ($method === 'POST') {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$tab_id = $data['tab_id'] ?? null;
|
|
$user_id = $data['user_id'] ?? null;
|
|
$is_visible = $data['is_visible'] ?? true;
|
|
|
|
if ($tab_id && $user_id) {
|
|
if ($is_visible) {
|
|
// If it should be visible, DELETE the entry from the hide table
|
|
$stmt = $pdo->prepare("DELETE FROM user_tab_visibility WHERE user_id = ? AND tab_id = ?");
|
|
$stmt->execute([$user_id, $tab_id]);
|
|
} else {
|
|
// If it should be hidden, INSERT an entry (ignore if it already exists)
|
|
$stmt = $pdo->prepare("INSERT OR IGNORE INTO user_tab_visibility (user_id, tab_id) VALUES (?, ?)");
|
|
$stmt->execute([$user_id, $tab_id]);
|
|
}
|
|
$response = ['success' => true];
|
|
} else {
|
|
http_response_code(400); // Bad Request
|
|
$response = ['error' => 'Tab ID and User ID are required.'];
|
|
}
|
|
} else {
|
|
http_response_code(405); // Method Not Allowed
|
|
$response = ['error' => 'Method not allowed'];
|
|
}
|
|
} catch (PDOException $e) {
|
|
http_response_code(500); // Internal Server Error
|
|
$response = ['error' => 'Database error: ' . $e->getMessage()];
|
|
}
|
|
|
|
echo json_encode($response);
|