77 lines
3.0 KiB
PHP
77 lines
3.0 KiB
PHP
<?php
|
|
// api/get_news.php - PHP REST Endpoint to fetch news, agencies, and system settings from database
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
require_once __DIR__ . '/../config/db.php';
|
|
|
|
try {
|
|
$dbInfo = getDbConnection();
|
|
$pdo = $dbInfo['pdo'];
|
|
|
|
// Increment view count if ID specified
|
|
if (isset($_GET['view_id']) && !empty($_GET['view_id'])) {
|
|
$viewStmt = $pdo->prepare("UPDATE news SET views = views + 1 WHERE id = ?");
|
|
$viewStmt->execute([$_GET['view_id']]);
|
|
}
|
|
|
|
// Query news list with agency and category names
|
|
$stmt = $pdo->query("
|
|
SELECT n.*, a.name AS agency_name, a.code AS agency_code, c.name AS category_name
|
|
FROM news n
|
|
JOIN agencies a ON n.agency_id = a.id
|
|
JOIN categories c ON n.category_id = c.id
|
|
ORDER BY n.publish_date DESC, n.created_at DESC
|
|
");
|
|
$newsList = $stmt->fetchAll();
|
|
|
|
// Query attachments for each news document
|
|
foreach ($newsList as &$news) {
|
|
$attachStmt = $pdo->prepare("SELECT file_name AS name, file_type AS type, file_size AS size, file_url AS url FROM attachments WHERE news_id = ?");
|
|
$attachStmt->execute([$news['id']]);
|
|
$news['attachments'] = $attachStmt->fetchAll();
|
|
|
|
$news['isPinned'] = (bool)$news['is_pinned'];
|
|
$news['isUrgent'] = (bool)$news['is_urgent'];
|
|
$news['isHidden'] = (bool)($news['is_hidden'] ?? 0);
|
|
$news['docNum'] = $news['doc_num'];
|
|
$news['agencyId'] = $news['agency_id'];
|
|
$news['categoryId'] = $news['category_id'];
|
|
$news['date'] = $news['publish_date'];
|
|
$news['views'] = (int)$news['views'];
|
|
}
|
|
|
|
// Query active agencies list
|
|
$agenciesStmt = $pdo->query("SELECT id, name, code, COALESCE(admin_password, '1234') AS admin_password, gdrive_folder_id FROM agencies ORDER BY id ASC");
|
|
$agenciesList = $agenciesStmt->fetchAll();
|
|
|
|
// Query categories list
|
|
$categoriesStmt = $pdo->query("SELECT id, name, agency_id FROM categories ORDER BY id ASC");
|
|
$categoriesList = $categoriesStmt->fetchAll();
|
|
|
|
// Query system settings
|
|
$settingsStmt = $pdo->query("SELECT setting_key, setting_value FROM system_settings");
|
|
$rawSettings = $settingsStmt->fetchAll();
|
|
$settings = [];
|
|
foreach ($rawSettings as $row) {
|
|
$settings[$row['setting_key']] = $row['setting_value'];
|
|
}
|
|
|
|
// Query templates list
|
|
$templatesStmt = $pdo->query("SELECT id, agency_id, name, category_id, doc_num_format, title, content FROM templates ORDER BY created_at ASC");
|
|
$templatesList = $templatesStmt->fetchAll();
|
|
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'driver' => $dbInfo['driver'],
|
|
'data' => $newsList,
|
|
'agencies' => $agenciesList,
|
|
'categories' => $categoriesList,
|
|
'templates' => $templatesList,
|
|
'settings' => $settings
|
|
], JSON_UNESCAPED_UNICODE);
|
|
} catch (\Exception $e) {
|
|
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
?>
|