-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
87 lines (75 loc) · 3.3 KB
/
Copy pathapi.php
File metadata and controls
87 lines (75 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
// api.php — Endpoint AJAX JSON pour index.php
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-cache');
require_once __DIR__ . '/db.php';
$action = $_GET['action'] ?? '';
try {
switch ($action) {
case 'articles':
$page = max(1, (int)($_GET['page'] ?? 1));
$perPage = min(100, max(1, (int)($_GET['per_page'] ?? 40)));
$catId = isset($_GET['cat_id']) && $_GET['cat_id'] !== '' ? (int)$_GET['cat_id'] : null;
$offset = ($page - 1) * $perPage;
$db = getDB();
$where = $catId ? "WHERE a.categorie_id = $catId" : '';
$total = (int)$db->query("SELECT COUNT(*) FROM ARTICLE a $where")->fetchColumn();
$stmt = $db->prepare("
SELECT a.id, a.type, a.titre, a.contenu, a.created_at,
a.categorie_id, c.nom as categorie_nom
FROM ARTICLE a
LEFT JOIN CATEGORIE c ON a.categorie_id = c.id
$where
ORDER BY a.created_at DESC
LIMIT ? OFFSET ?
");
$stmt->execute([$perPage, $offset]);
$articles = $stmt->fetchAll();
// Tronquer le contenu pour le JSON (pas besoin du texte entier pour la grille)
foreach ($articles as &$art) {
// Garder contenu complet pour popup (on tronque à 5000 chars max côté JSON)
$art['contenu'] = mb_substr($art['contenu'], 0, 5000);
}
echo json_encode([
'articles' => $articles,
'total' => $total,
'page' => $page,
'per_page' => $perPage,
], JSON_UNESCAPED_UNICODE);
break;
case 'categories':
$db = getDB();
$cats = $db->query("
SELECT c.id, c.nom, c.study_count,
COUNT(a.id) as article_count
FROM CATEGORIE c
LEFT JOIN ARTICLE a ON a.categorie_id = c.id
GROUP BY c.id
HAVING article_count > 0
ORDER BY article_count DESC, c.nom
LIMIT 50
")->fetchAll();
echo json_encode($cats, JSON_UNESCAPED_UNICODE);
break;
case 'stats':
$db = getDB();
echo json_encode([
'total_articles' => (int)$db->query("SELECT COUNT(*) FROM ARTICLE")->fetchColumn(),
'total_actualites'=> (int)$db->query("SELECT COUNT(*) FROM ACTUALITEGOOGLE")->fetchColumn(),
'total_categories'=> (int)$db->query("SELECT COUNT(*) FROM CATEGORIE")->fetchColumn(),
'total_loops' => (int)$db->query("SELECT COUNT(*) FROM ARTICLE WHERE type='revue_presse'")->fetchColumn(),
], JSON_UNESCAPED_UNICODE);
break;
case 'logs':
$db = getDB();
$logs = $db->query("SELECT * FROM LOG_CONSOLE ORDER BY id DESC LIMIT 100")->fetchAll();
echo json_encode(array_reverse($logs), JSON_UNESCAPED_UNICODE);
break;
default:
http_response_code(400);
echo json_encode(['error' => 'Unknown action']);
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}