From 8977f8cec4ca88f13528792a453eb27328e22845 Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Sun, 14 Nov 2021 08:49:37 +0000 Subject: Add description to todo --- public/css/site.css | 9 ++++++++- public/store/index.php | 1 + public/update/index.php | 1 + public/view/index.php | 37 +++++++++++++++++++++++++++++++++++++ src/DTS/Old.php | 1 + src/DTS/Todo.php | 2 ++ src/DTS/TodoRepository.php | 6 ++++-- src/DTS/Validated.php | 2 ++ src/DTS/Validator.php | 19 +++++++++++++++++++ src/templates/confirm_deletion.php | 2 +- src/templates/form_fields.php | 4 ++++ src/templates/index.php | 2 +- src/templates/view.php | 20 ++++++++++++++++++++ 13 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 public/view/index.php create mode 100644 src/templates/view.php diff --git a/public/css/site.css b/public/css/site.css index c1010a2..d34f380 100644 --- a/public/css/site.css +++ b/public/css/site.css @@ -18,6 +18,7 @@ body { h1, h2, +h3, label { font-weight: bold; font-size: 1.125rem; @@ -37,6 +38,10 @@ h2:before { content: "## "; } +h3 { + color: #86EFAC; +} + section { padding: 1rem; } @@ -71,13 +76,15 @@ label { } input, +textarea, button { border-radius: .25rem; display: block; width: 100%; } -input { +input, +textarea { border: 2px solid #FFFFFF; padding: .375rem .75rem; } diff --git a/public/store/index.php b/public/store/index.php index c4d24fa..0f472d8 100644 --- a/public/store/index.php +++ b/public/store/index.php @@ -40,6 +40,7 @@ $todos = new TodoRepository($config['path_to_repository']); $todo = new Todo(); $todo->task = $validated->task; +$todo->description = $validated->description; $todo->tag = $validated->tag; $todo->addedAt = date('Y-m-d H:i:s'); diff --git a/public/update/index.php b/public/update/index.php index 82b3c3f..749f429 100644 --- a/public/update/index.php +++ b/public/update/index.php @@ -45,6 +45,7 @@ if ($validator->errors->count()) { $validated = $validator->validated; $todo->task = $validated->task; +$todo->description = $validated->description; $todo->tag = $validated->tag; if (!$todos->update($todo)) { diff --git a/public/view/index.php b/public/view/index.php new file mode 100644 index 0000000..798cefd --- /dev/null +++ b/public/view/index.php @@ -0,0 +1,37 @@ +find($id); + +if ($todo === null) { + respondAndExit(404, 'Not Found'); +} + +$html = $template->render('view', compact( + 'todo' +)); + +respondAndExit(200, 'OK', $html); diff --git a/src/DTS/Old.php b/src/DTS/Old.php index 0641329..6e8cbee 100644 --- a/src/DTS/Old.php +++ b/src/DTS/Old.php @@ -8,6 +8,7 @@ class Old { const FIELDS = [ 'task', + 'description', 'tag', ]; diff --git a/src/DTS/Todo.php b/src/DTS/Todo.php index 9b08e56..ca10e1d 100644 --- a/src/DTS/Todo.php +++ b/src/DTS/Todo.php @@ -10,6 +10,8 @@ class Todo public string $task = ''; + public string $description = ''; + public string $tag = ''; public string $addedAt; diff --git a/src/DTS/TodoRepository.php b/src/DTS/TodoRepository.php index 5ec759f..358ac9d 100644 --- a/src/DTS/TodoRepository.php +++ b/src/DTS/TodoRepository.php @@ -124,8 +124,9 @@ class TodoRepository implements \Iterator $todo->id = $id++; $todo->task = $data[0]; - $todo->tag = $data[1]; - $todo->addedAt = $data[2]; + $todo->description = $data[1]; + $todo->tag = $data[2]; + $todo->addedAt = $data[3]; $this->repository[] = $todo; @@ -150,6 +151,7 @@ class TodoRepository implements \Iterator foreach ($this->repository as $todo) { $success = $success && fputcsv($fp, [ $todo->task, + $todo->description, $todo->tag, $todo->addedAt, ]) !== false; diff --git a/src/DTS/Validated.php b/src/DTS/Validated.php index 9610538..9415a5f 100644 --- a/src/DTS/Validated.php +++ b/src/DTS/Validated.php @@ -8,5 +8,7 @@ class Validated { public string $task = ''; + public string $description = ''; + public string $tag = ''; } diff --git a/src/DTS/Validator.php b/src/DTS/Validator.php index 2f9c255..a2ab48f 100644 --- a/src/DTS/Validator.php +++ b/src/DTS/Validator.php @@ -21,6 +21,8 @@ class Validator $this->validateTask($request['task'], 2, 256); + $this->validateDescription($request['description'], 8, 512); + $this->validateTag($request['tag'], 2, 16); } @@ -37,6 +39,23 @@ class Validator } } + private function validateDescription(string $description, int $minLength, int $maxLength): void + { + $description = trim($description); + + if ($description === '') { + return; + } + + if (strlen($description) < $minLength || strlen($description) > $maxLength) { + $this->errors->add('description', "Must be between $minLength and $maxLength in characters in length"); + } + + if (!$this->errors->has('tite')) { + $this->validated->description = $description; + } + } + private function validateTag(string $tag, int $minLength, int $maxLength): void { $tag = trim($tag); diff --git a/src/templates/confirm_deletion.php b/src/templates/confirm_deletion.php index 85cf924..87ddf8a 100644 --- a/src/templates/confirm_deletion.php +++ b/src/templates/confirm_deletion.php @@ -14,7 +14,7 @@ Back
- url | $todo->task | $todo->tag"); ?> + task | $todo->tag"); ?>

Copyright © 2021 David T. Sadler.

diff --git a/src/templates/form_fields.php b/src/templates/form_fields.php index 505cf26..3b58adb 100644 --- a/src/templates/form_fields.php +++ b/src/templates/form_fields.php @@ -2,6 +2,10 @@ has('task')) { ?>

get('task'))); ?>

+ +has('description')) { ?> +

get('description'))); ?>

+ has('tag')) { ?>

get('tag'))); ?>

diff --git a/src/templates/index.php b/src/templates/index.php index d309b2c..5abe8b3 100644 --- a/src/templates/index.php +++ b/src/templates/index.php @@ -25,7 +25,7 @@
  1. - task); ?> | tag) { ?>tag); ?> | Edit | Delete + task); ?> | tag) { ?>tag); ?> | Edit | Delete
diff --git a/src/templates/view.php b/src/templates/view.php new file mode 100644 index 0000000..35d4c36 --- /dev/null +++ b/src/templates/view.php @@ -0,0 +1,20 @@ + + + + + + Edit - Todo + + + + +
+

Todo

+

View

+ Back +

task | $todo->tag"); ?>

+

description), false); ?>

+

Copyright © 2021 David T. Sadler.

+
+ + -- cgit v1.2.3-13-gbd6f