Files

108 lines
4.2 KiB
PHP

<?php
require_once '../include/init.php';
require_once '../include/auth.php'; // ensures user is logged in
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
$response = [];
try {
switch ($method) {
case 'GET':
// Get all tabs
$stmt = $pdo->query("SELECT * FROM tabs ORDER BY id");
$response = $stmt->fetchAll();
break;
case 'POST':
// Create a new tab
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['name'])) {
$stmt = $pdo->prepare("INSERT INTO tabs (name, user_id, show_search_bar) VALUES (?, ?, ?)");
$stmt->execute([$data['name'], $data['user_id'] ?? null, $data['show_search_bar'] ?? 1]);
$response = ['id' => $pdo->lastInsertId(), 'name' => $data['name']];
} else {
http_response_code(400); // Bad Request
$response = ['error' => 'Name is required'];
}
break;
case 'PUT':
// Update a tab
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['id']) && isset($data['name'])) {
// Prepare fields to update
$fields = [
'name' => $data['name'],
'user_id' => $data['user_id'] ?? null,
'background_image_url' => $data['background_image_url'] ?? null,
'background_brightness' => $data['background_brightness'] ?? 100,
'background_saturation' => $data['background_saturation'] ?? 100,
'theme' => $data['theme'] ?? null,
'theme_transparency' => $data['theme_transparency'] ?? null,
'column_count' => $data['column_count'] ?? 5,
'show_search_bar' => $data['show_search_bar'] ?? 1,
];
// Handle empty strings to set them to NULL for proper fallback
if ($fields['background_image_url'] === '') {
$fields['background_image_url'] = null;
}
if ($fields['theme'] === '') {
$fields['theme'] = null;
}
if ($fields['theme_transparency'] === '' || !is_numeric($fields['theme_transparency'])) {
$fields['theme_transparency'] = null;
}
$stmt = $pdo->prepare("
UPDATE tabs
SET
name = :name,
user_id = :user_id,
background_image_url = :background_image_url,
background_brightness = :background_brightness,
background_saturation = :background_saturation,
theme = :theme,
theme_transparency = :theme_transparency,
column_count = :column_count,
show_search_bar = :show_search_bar
WHERE id = :id
");
$fields['id'] = $data['id'];
$stmt->execute($fields);
$response = ['success' => true];
} else {
http_response_code(400); // Bad Request
$response = ['error' => 'ID and name are required'];
}
break;
case 'DELETE':
// Delete a tab
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['id'])) {
$stmt = $pdo->prepare("DELETE FROM tabs WHERE id = ?");
$stmt->execute([$data['id']]);
$response = ['success' => true];
} else {
http_response_code(400); // Bad Request
$response = ['error' => 'ID is required'];
}
break;
default:
http_response_code(405); // Method Not Allowed
$response = ['error' => 'Method not allowed'];
break;
}
} catch (PDOException $e) {
http_response_code(500); // Internal Server Error
$response = ['error' => 'Database error: ' . $e->getMessage()];
}
echo json_encode($response);