102 lines
3.6 KiB
PHP
102 lines
3.6 KiB
PHP
<?php
|
|
ini_set('display_errors', 0);
|
|
error_reporting(0);
|
|
require_once __DIR__ . '/config.php';
|
|
require_once __DIR__ . '/glpi_api.php';
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
|
|
http_response_code(403);
|
|
exit(json_encode(['error' => 'Forbidden']));
|
|
}
|
|
|
|
$user = checkAuth($pdo);
|
|
if (!$user) {
|
|
http_response_code(401);
|
|
exit(json_encode(['error' => 'Unauthorized']));
|
|
}
|
|
|
|
$ticketId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
|
if (!$ticketId) {
|
|
exit(json_encode(['error' => 'Invalid ticket ID']));
|
|
}
|
|
|
|
$glpi = new GlpiApi(GLPI_API_URL, GLPI_APP_TOKEN, GLPI_USERNAME, GLPI_PASSWORD);
|
|
|
|
$ticket = $glpi->getTicket($ticketId);
|
|
if (!$ticket) {
|
|
exit(json_encode(['error' => 'Unable to fetch ticket from GLPI']));
|
|
}
|
|
|
|
// Маппинг статусов GLPI (числовые -> русские названия)
|
|
$statusMap = [
|
|
1 => 'Новый',
|
|
2 => 'В обработке',
|
|
3 => 'Закрыт',
|
|
4 => 'Решён',
|
|
5 => 'Отложен',
|
|
6 => 'Ожидает',
|
|
];
|
|
$priorityMap = [
|
|
1 => 'Низкий',
|
|
2 => 'Средний',
|
|
3 => 'Высокий',
|
|
4 => 'Срочный',
|
|
5 => 'Критический',
|
|
];
|
|
|
|
$statusName = $ticket['status_name'] ?? ($statusMap[$ticket['status']] ?? 'Неизвестно');
|
|
$priorityName = $ticket['priority_name'] ?? ($priorityMap[$ticket['priority']] ?? 'Неизвестно');
|
|
|
|
// Парсим содержимое для получения читаемого описания (без ID)
|
|
$contentText = $ticket['content'] ?? '';
|
|
$locationName = '';
|
|
$computerName = '';
|
|
$categoryName = '';
|
|
|
|
if (preg_match('/ID локации:\s*(\d+)/', $contentText, $m)) {
|
|
$loc = $glpi->getItem('Location', (int)$m[1]);
|
|
if ($loc) $locationName = $loc['completename'] ?? $loc['name'] ?? '';
|
|
}
|
|
if (preg_match('/ID компьютера:\s*(\d+)/', $contentText, $m)) {
|
|
$comp = $glpi->getItem('Computer', (int)$m[1]);
|
|
if ($comp) $computerName = $comp['name'] ?? '';
|
|
}
|
|
if (preg_match('/ID категории:\s*(\d+)/', $contentText, $m)) {
|
|
$cat = $glpi->getItem('ITILCategory', (int)$m[1]);
|
|
if ($cat) $categoryName = $cat['completename'] ?? $cat['name'] ?? '';
|
|
}
|
|
|
|
// Чистое описание (убираем строки с ID)
|
|
$cleanContent = preg_replace('/ID (локации|компьютера|категории):\s*\d+\s*/', '', $contentText);
|
|
$cleanContent = trim(preg_replace('/Описание:\s*/', '', $cleanContent));
|
|
$readableContentHtml = nl2br(htmlspecialchars($cleanContent));
|
|
|
|
// Комментарии (followups)
|
|
$followups = $glpi->getTicketFollowups($ticketId);
|
|
$processedFollowups = [];
|
|
foreach ($followups as $f) {
|
|
$processedFollowups[] = [
|
|
'date' => date('d.m.Y H:i', strtotime($f['date'] ?? $f['date_creation'] ?? 'now')),
|
|
'content' => nl2br(htmlspecialchars($f['content'] ?? '')),
|
|
'author' => htmlspecialchars($f['users_id_name'] ?? ($f['users_name'] ?? 'Система'))
|
|
];
|
|
}
|
|
|
|
$result = [
|
|
'id' => $ticket['id'],
|
|
'name' => $ticket['name'],
|
|
'content' => $readableContentHtml,
|
|
'status' => $statusName,
|
|
'priority' => $priorityName,
|
|
'date' => date('d.m.Y H:i', strtotime($ticket['date'] ?? $ticket['date_creation'])),
|
|
'solvedate' => isset($ticket['solvedate']) ? date('d.m.Y H:i', strtotime($ticket['solvedate'])) : null,
|
|
'location' => $locationName,
|
|
'computer' => $computerName,
|
|
'category' => $categoryName,
|
|
'followups' => $processedFollowups
|
|
];
|
|
|
|
echo json_encode($result, JSON_UNESCAPED_UNICODE);
|
|
exit; |