diff options
author | Daniel Weipert <code@drogueronin.de> | 2022-03-06 00:48:14 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2022-03-06 00:48:14 +0100 |
commit | b2a86c7df7d5a473e80034832a01b21444fa50e6 (patch) | |
tree | 2b18e08124ff3e8602a20ba27fef3ae4d066cc28 /src/Controllers/EntriesController.php | |
parent | df428380e288db75b41ace5d6274f44916517f9f (diff) |
Refactor
Diffstat (limited to 'src/Controllers/EntriesController.php')
-rw-r--r-- | src/Controllers/EntriesController.php | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/Controllers/EntriesController.php b/src/Controllers/EntriesController.php new file mode 100644 index 0000000..13aa8e0 --- /dev/null +++ b/src/Controllers/EntriesController.php @@ -0,0 +1,84 @@ +<?php + +namespace FlatFileForms\Controllers; + +use Yosymfony\Toml\Toml; + +class EntriesController +{ + public function getEntries() + { + /**@var Utilities $utilities*/ + global $utilities; + + $entries = []; + + $dateFrom = new \DateTime($_GET['dateFrom']); + $dateTo = new \DateTime($_GET['dateTo'] ?? 'now'); + + $dateRangeYears = range($dateFrom->format('Y'), $dateTo->format('Y')); + $dateRangeYearsCount = count($dateRangeYears); + foreach ($dateRangeYears as $dateRangeYearIdx => $dateRangeYear) { + $yearPath = "$formPath/entries/$dateRangeYear"; + if (! is_dir($yearPath)) { + continue; + } + + if ($dateRangeYearsCount === 1) { + $dateRangeMonths = range($dateFrom->format('m'), $dateTo->format('m')); + } + else if ($dateRangeYearIdx === 0) { + $dateRangeMonths = range($dateFrom->format('m'), 12); + } + else if ($dateRangeYearIdx === $dateRangeYearsCount - 1) { + $dateRangeMonths = range(1, $dateTo->format('m')); + } + else { + $dateRangeMonths = range(1, 12); + } + + $dateRangeMonthsCount = count($dateRangeMonths); + foreach ($dateRangeMonths as $dateRangeMonthIdx => $dateRangeMonth) { + $monthPath = "$yearPath/" . sprintf('%02d', $dateRangeMonth); + if (! is_dir($monthPath)) { + continue; + } + + if ($dateRangeMonthsCount === 1) { + $dateRangeDays = range($dateFrom->format('d'), $dateTo->format('d')); + } + else if ($dateRangeYearIdx === 0 && $dateRangeMonthIdx === 0) { + $dateRangeDays = range($dateFrom->format('d'), 31); + } + else if ($dateRangeYearIdx === $dateRangeYearsCount - 1 && $dateRangeMonthIdx === $dateRangeMonthsCount - 1) { + $dateRangeDays = range(1, $dateTo->format('d')); + } + else { + $dateRangeDays = range(1, 31); + } + + foreach ($dateRangeDays as $dateRangeDay) { + $dayPath = "$monthPath/" . sprintf('%02d', $dateRangeDay); + if (! is_dir($dayPath)) { + continue; + } + + $entriesForDay = $utilities->scandir($dayPath); + foreach ($entriesForDay as $entryForDay) { + $entry = Toml::parseFile("$dayPath/$entryForDay"); + if (isset($_GET['flat'])) { + $entries[] = $entry; + } else { + $entries[$dateRangeYear][$dateRangeMonth][$dateRangeDay][] = $entry; + } + } + } + + } + } + + $content['data'] = $entries; + + return $content; + } +} |