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 | |
parent | 75f3e8ab4b3d65d6eba16575ca0a4a669b7e27ac (diff) |
Get entries from and to date
-rw-r--r-- | config.example.toml | 3 | ||||
-rw-r--r-- | src/App.php | 101 | ||||
-rw-r--r-- | tests/Test.php | 4 |
3 files changed, 101 insertions, 7 deletions
diff --git a/config.example.toml b/config.example.toml index a506a09..a943c4f 100644 --- a/config.example.toml +++ b/config.example.toml @@ -1,6 +1,3 @@ [app] contentFolderPath = './content' -[form] -entriesFolderName = 'entries' - 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; + } } diff --git a/tests/Test.php b/tests/Test.php index b8a02f3..0543c1f 100644 --- a/tests/Test.php +++ b/tests/Test.php @@ -83,7 +83,7 @@ class Test extends TestCase <?php function validate_formname_name($field, $value) { - if ($value !== \'Adelbert\') { + if ($value !== \'Harry\') { $field[\'is_valid\'] = false; } @@ -290,7 +290,7 @@ class Test extends TestCase // valid response validation_function $response = $this->request('POST', 'customername/formname/submit?key=' . $this->apiKey, [ 'form_params' => [ - 'name' => 'Adelbert', + 'name' => 'Harry', 'email' => 'EMAIL', 'date' => 'DATE', ], |