diff options
Diffstat (limited to 'src/Controllers/SubmissionController.php')
-rw-r--r-- | src/Controllers/SubmissionController.php | 49 |
1 files changed, 49 insertions, 0 deletions
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 @@ +<?php + +namespace FlatFileForms\Controllers; + +use FlatFileForms\Builder; +use FlatFileForms\Validator; +use Yosymfony\Toml\TomlBuilder; + +class SubmissionController +{ + public function submit(Builder $builder, Validator $validator, $formPath) + { + $fields = $builder->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; + } +} |