- Add ErrorHandler class for consistent error handling and logging - Provides methods for common error responses (401, 403, 404, 422, 500) - Includes error logging to temp directory - Update get_template.php to use ErrorHandler (example migration) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
/**
|
|
* Get Template API
|
|
* Returns a ticket template by ID
|
|
*/
|
|
|
|
require_once dirname(__DIR__) . '/helpers/ErrorHandler.php';
|
|
ErrorHandler::init();
|
|
|
|
try {
|
|
session_start();
|
|
require_once dirname(__DIR__) . '/config/config.php';
|
|
require_once dirname(__DIR__) . '/helpers/Database.php';
|
|
require_once dirname(__DIR__) . '/models/TemplateModel.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// Check authentication
|
|
if (!isset($_SESSION['user']) || !isset($_SESSION['user']['user_id'])) {
|
|
ErrorHandler::sendUnauthorizedError('Not authenticated');
|
|
}
|
|
|
|
// Get template ID from query parameter
|
|
$templateId = $_GET['template_id'] ?? null;
|
|
|
|
if (!$templateId || !is_numeric($templateId)) {
|
|
ErrorHandler::sendValidationError(
|
|
['template_id' => 'Valid template ID required'],
|
|
'Invalid request'
|
|
);
|
|
}
|
|
|
|
// Cast to integer for safety
|
|
$templateId = (int)$templateId;
|
|
|
|
// Get template
|
|
$conn = Database::getConnection();
|
|
$templateModel = new TemplateModel($conn);
|
|
$template = $templateModel->getTemplateById($templateId);
|
|
|
|
if ($template) {
|
|
echo json_encode(['success' => true, 'template' => $template]);
|
|
} else {
|
|
ErrorHandler::sendNotFoundError('Template not found');
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
ErrorHandler::log($e->getMessage(), E_ERROR);
|
|
ErrorHandler::sendErrorResponse('Failed to retrieve template', 500, $e);
|
|
}
|