designtopack/public/site/templates/projects.json.php
isUnknown 0a980603a4 Ajout de collectLight() pour optimiser le chargement du listing des projets
Problème : projects.json.php causait un dépassement mémoire en collectant
toutes les notifications complètes (avec author, location, text, etc.) pour
tous les projets.

Solution : Nouvelle méthode collectLight() qui ne retourne que les données
minimales nécessaires au frontend pour afficher les indicateurs :
- id, type, isRead, date
- location.project.uri (pour le filtrage)

Les détails complets sont toujours chargés dans project.json.php individuel.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-15 11:18:59 +01:00

68 lines
2.4 KiB
PHP

<?php
if (!$kirby->user()) {
return json_encode([
'page' => $genericData,
'user' => []
]);
}
function getProjectData($project, $user, $collector)
{
// Utiliser collectLight() pour économiser la mémoire (seulement id, type, isRead, date)
$notifications = [];
if ($collector) {
try {
$notifications = $collector->collectLight($project, $user);
} catch (\Throwable $e) {
error_log("Error collecting light notifications for project {$project->uri()}: " . $e->getMessage());
$notifications = [];
}
}
$data = [
'title' => $project->title()->value(),
'url' => $project->url(),
'uri' => '/' . $project->uri(),
'modified' => $project->modified('Y-MM-d'),
'currentStep' => $project->currentStep()->value(),
'status' => $project->status(),
'logo' => $project->client()->toPage() ? $project->client()->toPage()->logo()->toFile()->url() : '',
'steps' => $project->getSteps(),
'notifications' => $notifications,
'uuid' => (string) $project->uuid(),
'slug' => (string) $project->slug(),
'isDTLEnabled' => $project->isDTLEnabled()->isTrue(),
'hasOptimizationRequest' => $project->hasOptimizationRequest()->isTrue(),
];
if ($project->isDTLEnabled()) {
$data['designToLight'] = processDTLProposals($project);
}
return $data;
}
// Récupérer le collector de notifications
$notificationCollector = $kirby->option('adrienpayet.pdc-notifications.collector');
$currentUser = $kirby->user();
try {
$children = $currentUser->role() == 'admin'
? $page->childrenAndDrafts()->map(fn($project) => getProjectData($project, $currentUser, $notificationCollector))->values()
: $currentUser->projects()->toPages()->map(fn($project) => getProjectData($project, $currentUser, $notificationCollector))->values();
} catch (\Throwable $th) {
throw new Exception($th->getMessage() . ' line ' . $th->getLine() . ' in file ' . $th->getFile(), 1);
$children = [];
}
$specificData = [
'children' => $children,
];
$pageData = array_merge($genericData, $specificData);
echo json_encode([
'page' => $pageData,
'user' => $userData,
]);