From b2a86c7df7d5a473e80034832a01b21444fa50e6 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 6 Mar 2022 00:48:14 +0100 Subject: Refactor --- src/Controllers/EntriesController.php | 84 ++++++++++++++++++++++++++++++++ src/Controllers/FieldsController.php | 25 ++++++++++ src/Controllers/SubmissionController.php | 49 +++++++++++++++++++ src/Controllers/ValidationController.php | 26 ++++++++++ 4 files changed, 184 insertions(+) create mode 100644 src/Controllers/EntriesController.php create mode 100644 src/Controllers/FieldsController.php create mode 100644 src/Controllers/SubmissionController.php create mode 100644 src/Controllers/ValidationController.php (limited to 'src/Controllers') 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 @@ +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; + } +} diff --git a/src/Controllers/FieldsController.php b/src/Controllers/FieldsController.php new file mode 100644 index 0000000..c407604 --- /dev/null +++ b/src/Controllers/FieldsController.php @@ -0,0 +1,25 @@ +buildFields($_GET['page'] ?? null); + + // flatten paged form + if ($utilities->isPagedFieldSet($fields) && isset($_GET['flat'])) { + $fields = array_merge(...array_values($fields)); + } + + $content['data'] = $fields; + + return $content; + } +} diff --git a/src/Controllers/SubmissionController.php b/src/Controllers/SubmissionController.php new file mode 100644 index 0000000..f2c4c80 --- /dev/null +++ b/src/Controllers/SubmissionController.php @@ -0,0 +1,49 @@ +buildFields(); + + // run through validation + $result = $validator->validateRequest($fields); + + $content['data'] = $result['fields']; + + // if there were no validation errors then add entry + if (empty($result['error'])) { + $date = new \Datetime(); + $entry = [ + 'fields' => $_POST, + 'date' => $date->format('c'), + ]; + + $entryBuilder = new TomlBuilder(); + $entryBuilder->addValue('date', $entry['date']); + $entryBuilder->addTable('fields'); + foreach ($entry['fields'] as $entryKey => $entryValue) { + $entryBuilder->addValue($entryKey, $entryValue); + } + + $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( + $entryDirectory . '/' . $entryFilename, + $entryBuilder->getTomlString() + ); + } + else { + $content['error'] = $result['error']; + } + + return $content; + } +} diff --git a/src/Controllers/ValidationController.php b/src/Controllers/ValidationController.php new file mode 100644 index 0000000..f90359a --- /dev/null +++ b/src/Controllers/ValidationController.php @@ -0,0 +1,26 @@ +buildFields($_GET['page'] ?? null); + + $result = $validator->validateRequest($fields); + + $content['data'] = $result['fields']; + if (! empty($result['error'])) { + $content['error'] = $result['error']; + } + + return $content; + } +} -- cgit v1.2.3