Files
SWE/swe-b1-a-dev/webseite/dashboard/api/create-topic.php
2025-01-03 13:29:27 +01:00

36 lines
873 B
PHP

<?php
require_once("../../classes/TopicData.php");
require_once("../../classes/Config.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Validate and sanitize input
$displayName = htmlspecialchars($_POST['displayName']);
$subjectId = $_POST['subjectId'];
$description = $_POST['description'];
$icon = $_POST['icon'];
$article = $_POST['article'];
// Generate ID from displayName
$id = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $displayName));
// Create topic
$topic = TopicData::createNew(
$id,
$subjectId,
$displayName,
$icon,
$description,
[], // empty related topics array
$article
);
if (!$topic || !$topic->save()) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Failed to create topic']);
exit();
}
echo json_encode(['success' => true]);