diff options
author | Daniel Weipert <code@drogueronin.de> | 2022-01-10 00:31:23 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2022-01-10 11:29:13 +0100 |
commit | 843af14c19f62ca642bcd7af59b8ea413f681c50 (patch) | |
tree | 2153be74e35942c552fd0079460c256b9b2f8b96 /src/App.php | |
parent | 75f3e8ab4b3d65d6eba16575ca0a4a669b7e27ac (diff) |
Get entries from and to date
Diffstat (limited to 'src/App.php')
-rw-r--r-- | src/App.php | 101 |
1 files changed, 99 insertions, 2 deletions
diff --git a/src/App.php b/src/App.php index 0c0773b..bad2412 100644 --- a/src/App.php +++ b/src/App.php @@ -56,6 +56,83 @@ class App $content['data'] = $fields; } + else if (str_ends_with($path, '/entries')) { + if (! isset($_GET['dateFrom'])) { + $response->setStatusCode(Response::HTTP_BAD_REQUEST); + throw new \Exception('dateFrom parameter missing'); + } + + $this->formPath = $formPath = $contentRoot . str_replace('/entries', '', $path); + + $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 = $this->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; + } + else { $content['data'] = Toml::parseFile($contentRoot . $path . '.toml'); } @@ -93,7 +170,7 @@ class App $builder->addValue($entryKey, $entryValue); } - $entryDirectory = $formPath . '/' . $_ENV['form']['entriesFolderName'] . '/' . $date->format('Y/m/d'); + $entryDirectory = $formPath . '/entries/' . $date->format('Y/m/d'); @mkdir($entryDirectory, 0774, true); $entryFilename = $date->format('Ymd_Hi_') . hash('adler32', serialize($entry)) . '.toml'; file_put_contents( @@ -104,7 +181,7 @@ class App } } } catch (\Exception $exception) { - $content['error'] = $exception->getMessage(); + $content['error'] = basename(get_class($exception)) . ': ' . $exception->getMessage(); } $response->headers->set('Content-Type', 'application/json'); @@ -320,5 +397,25 @@ class App return ! (isset($firstItem['name']) && ! is_array($firstItem['name'])); } + + /** + * @param string|array $paths + */ + public function scandir($paths) + { + $paths = (array)$paths; + + $scanned = []; + foreach ($paths as $path) { + $filtered = array_values( + array_filter( + scandir($path), fn ($item) => ! in_array($item, ['.', '..']) + ) + ); + array_push($scanned, ...$filtered); + } + + return $scanned; + } } |