actuel-inactuel/site/collections/categories.php

42 lines
1.1 KiB
PHP
Raw Normal View History

2024-04-09 10:44:12 +02:00
<?php
2024-06-20 09:50:39 +02:00
function createCategories($textsPage) {
2024-04-09 10:44:12 +02:00
$categories = array();
foreach ($textsPage->categories()->split() as $category) {
$categories[$category] = array(
'title' => $category,
2024-10-16 08:53:53 +02:00
'texts' => new Pages()
2024-04-09 10:44:12 +02:00
);
}
2024-06-20 09:50:39 +02:00
return $categories;
}
2024-04-09 10:44:12 +02:00
2024-06-20 09:50:39 +02:00
function fillCategoriesWithTexts($emptyCategories, $texts) {
$filledCategories = $emptyCategories;
2024-04-09 10:44:12 +02:00
foreach ($texts as $text) {
try {
2024-06-20 09:50:39 +02:00
$textCategory = $text->category()->value();
2024-10-16 08:53:53 +02:00
$filledCategories[$textCategory]['texts']->add($text);
2024-04-09 10:44:12 +02:00
} catch (\Throwable $th) {
throw new Exception(json_encode($th->getFile() . ' : ' . $th->getMessage()));
}
}
2024-06-20 09:50:39 +02:00
// exclude empty categories
return array_filter($filledCategories, function($category) {
return count($category['texts']) > 0;
});
}
return function ($site) {
2024-10-16 08:53:53 +02:00
$textsPage = $site->find('textes');
2024-06-20 09:50:39 +02:00
$years = $textsPage->children();
$texts = $years->children()->index();
$emptyCategories = createCategories($textsPage);
$filledCategories = fillCategoriesWithTexts($emptyCategories, $texts);
2024-04-09 10:44:12 +02:00
2024-06-20 09:50:39 +02:00
return $filledCategories;
2024-04-09 10:44:12 +02:00
};