Files
tinker_tickets/api/get_template.php
T

51 lines
1.6 KiB
PHP
Raw Normal View History

2026-01-01 19:00:42 -05:00
<?php
2026-01-29 10:55:15 -05:00
/**
* Get Template API
* Returns a ticket template by ID
*/
require_once dirname(__DIR__) . '/middleware/RateLimitMiddleware.php';
RateLimitMiddleware::apply('api');
2026-01-29 10:55:15 -05:00
require_once dirname(__DIR__) . '/helpers/ErrorHandler.php';
ErrorHandler::init();
try {
if (session_status() === PHP_SESSION_NONE) session_start();
2026-01-29 10:55:15 -05:00
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 = isset($_GET['template_id']) ? (int)$_GET['template_id'] : 0;
2026-01-29 10:55:15 -05:00
if ($templateId <= 0 || (string)$templateId !== (string)($_GET['template_id'] ?? '')) {
2026-01-29 10:55:15 -05:00
ErrorHandler::sendValidationError(
['template_id' => 'Valid template ID required'],
'Invalid request'
);
}
// 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);
2026-01-01 19:00:42 -05:00
}