Files
SWE/swe-b1-a-dev/webseite/dashboard/api/create-subject.php
2024-12-22 17:41:21 +01:00

43 lines
1.2 KiB
PHP

<?php
require_once("../../classes/SubjectData.php");
require_once("../../classes/Config.php");
// Debug logging
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Log all POST data
error_log("POST data: " . print_r($_POST, true));
// Validate and sanitize input
$displayName = htmlspecialchars($_POST['displayName']);
$description = $_POST['description'];
$color = preg_replace('/[^#A-Fa-f0-9]/', '', $_POST['color']);
$icon = $_POST['icon']; // Get raw icon value
// Log processed values
error_log("Processing subject creation:");
error_log("Display Name: " . $displayName);
error_log("Color: " . $color);
// error_log("Icon: " . $icon);
// Generate ID from displayName
$id = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $displayName));
// Set default icon if none is provided
if (empty($icon)) {
$icon = 'fa-book';
}
// Create and save subject
$subject = SubjectData::createNew($id, $displayName, $description, $color, $icon, []);
if (!$subject || !$subject->save()) {
error_log("Failed to create/save subject");
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Failed to create or save subject']);
exit();
}
echo json_encode(['success' => true]);