fipamo/brain/utility/Sorting.inc.php

84 lines
2.3 KiB
PHP

<?php
use function _\find;
use function _\filter;
class Sorting
{
private static $_tags = [];
private static $_archive = [];
public function __construct()
{
}
public static function tags()
{
$pages = (new Book("../content/pages"))->getContents();
foreach ($pages as $page) {
$temp = [];
$temp = explode(",", $page["tags"]);
foreach ($temp as $tag) {
$label = trim($tag);
if (!find(self::$_tags, ["tag_name" => $label])) {
array_push(self::$_tags, [
"tag_name" => $label,
"slug" => StringTools::safeString($label),
"count" => 1,
]);
} else {
$item = find(self::$_tags, ["tag_name" => $label]);
//echo "TAG: " . $item["tag_name"] . "\n";
$count = $item["count"];
self::$_tags[$label]["count"] = $count + 1;
}
}
}
return self::$_tags;
}
public static function archive()
{
$pages = (new Book("../content/pages"))->getContents();
$years = [];
$archive = [];
foreach ($pages as $page) {
$year = date("Y", date($page["rawCreated"]));
//echo $page["title"] . " : " . $year . "\n";
if (!find($years, ["year" => $year])) {
$findPages = filter($pages, ["createdYear" => $year]);
//var_dump($findPages);
array_push($years, ["year" => $year, "count" => count($findPages)]);
}
}
foreach ($years as $year) {
$sorted = [];
$filtered = filter($pages, ["createdYear" => $year["year"]]);
foreach ($filtered as $obj) {
$month = date("m", date($obj["rawCreated"]));
if (!find($sorted, ["month" => $month])) {
$perMonth = filter($pages, [
"createdYear" => $year["year"],
"createdMonth" => $month,
"deleted" => false,
"published" => true,
"layout" => "page",
]);
array_push($sorted, [
"month" => $month,
"full_month" => date("F", date($obj["rawCreated"])),
"count" => count($perMonth),
"pages" => $perMonth,
]);
}
}
array_push(self::$_archive, [
"year" => $year["year"],
"year_data" => $sorted,
]);
}
return self::$_archive;
}
}