106 lines
5.0 KiB
PHP
106 lines
5.0 KiB
PHP
<?php
|
|
require_once(__DIR__ . "/../../lib/mydb.php");
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$action = $_GET['action'] ?? '';
|
|
|
|
try {
|
|
$pdo = db_connect();
|
|
|
|
switch ($action) {
|
|
case 'get_data':
|
|
$categories = $pdo->query("SELECT * FROM prompt_categories ORDER BY COALESCE(parent_id, 0) ASC, sort_order ASC, name ASC")->fetchAll(PDO::FETCH_ASSOC);
|
|
$prompts = $pdo->query("SELECT * FROM prompt_items ORDER BY sort_order ASC, updated_at DESC")->fetchAll(PDO::FETCH_ASSOC);
|
|
$versions = $pdo->query("SELECT * FROM prompt_versions ORDER BY version_number DESC")->fetchAll(PDO::FETCH_ASSOC);
|
|
echo json_encode(['success' => true, 'categories' => $categories, 'prompts' => $prompts, 'versions' => $versions]);
|
|
break;
|
|
|
|
case 'move_category':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
// input: { id, parent_id, sort_order }
|
|
$stmt = $pdo->prepare("UPDATE prompt_categories SET parent_id = ?, sort_order = ? WHERE id = ?");
|
|
$stmt->execute([$input['parent_id'], $input['sort_order'], $input['id']]);
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
|
|
case 'move_prompt':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
// input: { id, category_id, sort_order }
|
|
$stmt = $pdo->prepare("UPDATE prompt_items SET category_id = ?, sort_order = ? WHERE id = ?");
|
|
$stmt->execute([$input['category_id'], $input['sort_order'], $input['id']]);
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
|
|
case 'save_category':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if (isset($input['id']) && is_numeric($input['id'])) {
|
|
$stmt = $pdo->prepare("UPDATE prompt_categories SET name = ?, parent_id = ? WHERE id = ?");
|
|
$stmt->execute([$input['name'], $input['parent_id'], $input['id']]);
|
|
$id = $input['id'];
|
|
} else {
|
|
$stmt = $pdo->prepare("INSERT INTO prompt_categories (name, parent_id) VALUES (?, ?)");
|
|
$stmt->execute([$input['name'], $input['parent_id']]);
|
|
$id = $pdo->lastInsertId();
|
|
}
|
|
echo json_encode(['success' => true, 'id' => $id]);
|
|
break;
|
|
|
|
case 'delete_category':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$stmt = $pdo->prepare("DELETE FROM prompt_categories WHERE id = ?");
|
|
$stmt->execute([$input['id']]);
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
|
|
case 'save_prompt':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if (isset($input['id']) && is_numeric($input['id'])) {
|
|
$stmt = $pdo->prepare("UPDATE prompt_items SET name = ?, category_id = ?, description = ? WHERE id = ?");
|
|
$stmt->execute([$input['name'], $input['category_id'], $input['description'] ?? '', $input['id']]);
|
|
$id = $input['id'];
|
|
} else {
|
|
$stmt = $pdo->prepare("INSERT INTO prompt_items (name, category_id, description) VALUES (?, ?, ?)");
|
|
$stmt->execute([$input['name'], $input['category_id'], $input['description'] ?? '']);
|
|
$id = $pdo->lastInsertId();
|
|
}
|
|
echo json_encode(['success' => true, 'id' => $id]);
|
|
break;
|
|
|
|
case 'delete_prompt':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$stmt = $pdo->prepare("DELETE FROM prompt_items WHERE id = ?");
|
|
$stmt->execute([$input['id']]);
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
|
|
case 'save_version':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
// Get next version number
|
|
$stmt = $pdo->prepare("SELECT MAX(version_number) as max_v FROM prompt_versions WHERE prompt_id = ?");
|
|
$stmt->execute([$input['prompt_id']]);
|
|
$row = $stmt->fetch();
|
|
$nextV = ($row['max_v'] ?? 0) + 1;
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO prompt_versions (prompt_id, content, version_number, change_summary) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$input['prompt_id'], $input['content'], $nextV, $input['change_summary']]);
|
|
$versionId = $pdo->lastInsertId();
|
|
|
|
// Update current_version_id in prompt_items
|
|
$stmt = $pdo->prepare("UPDATE prompt_items SET current_version_id = ?, updated_at = NOW() WHERE id = ?");
|
|
$stmt->execute([$versionId, $input['prompt_id']]);
|
|
|
|
echo json_encode(['success' => true, 'id' => $versionId, 'version_number' => $nextV]);
|
|
break;
|
|
|
|
default:
|
|
echo json_encode(['success' => false, 'error' => 'Invalid action']);
|
|
break;
|
|
}
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|
|
?>
|