summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid T. Sadler <davidtsadler@googlemail.com>2021-11-14 08:49:37 +0000
committerDavid T. Sadler <davidtsadler@googlemail.com>2021-11-14 08:49:37 +0000
commit8977f8cec4ca88f13528792a453eb27328e22845 (patch)
tree3479e7c7a3cec0a0ca923ea3c134d7243797b19d
parent3226a642822a15e6cf937d3f0ec20fdf71651ea4 (diff)
Add description to todo
-rw-r--r--public/css/site.css9
-rw-r--r--public/store/index.php1
-rw-r--r--public/update/index.php1
-rw-r--r--public/view/index.php37
-rw-r--r--src/DTS/Old.php1
-rw-r--r--src/DTS/Todo.php2
-rw-r--r--src/DTS/TodoRepository.php6
-rw-r--r--src/DTS/Validated.php2
-rw-r--r--src/DTS/Validator.php19
-rw-r--r--src/templates/confirm_deletion.php2
-rw-r--r--src/templates/form_fields.php4
-rw-r--r--src/templates/index.php2
-rw-r--r--src/templates/view.php20
13 files changed, 101 insertions, 5 deletions
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 @@
+<?php
+
+declare(strict_types=1);
+
+use DTS\TodoRepository;
+use DTS\Session;
+use DTS\Template;
+
+use function DTS\Functions\respondAndExit;
+
+require_once(__DIR__.'/../../autoload.php');
+
+$config = require_once(__DIR__.'/../../config.php');
+
+$session = Session::getInstance();
+
+if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'GET') {
+ respondAndExit(405, 'Method Not Allowed');
+}
+
+$id = filter_input(INPUT_GET, 'id');
+
+$todos = new TodoRepository($config['path_to_repository']);
+
+$template = new Template($config['path_to_templates']);
+
+$todo = $todos->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 @@
<a href="/">Back</a>
<form action="/delete/" method="POST">
<input type="hidden" name="id" value="<?= $todo->id; ?>"/>
- <?= htmlentities("$todo->url | $todo->task | $todo->tag"); ?>
+ <?= htmlentities("$todo->task | $todo->tag"); ?>
<button type="submit">Delete</button>
</form>
<p>Copyright © 2021 David T. Sadler.</p>
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 @@
<?php if ($errors->has('task')) { ?>
<p><?= htmlentities(implode(', ', $errors->get('task'))); ?></p>
<?php } ?>
+<label>Description<textarea name="description" maxlength="512" rows="8"><?= htmlspecialchars($old->get('description', $todo->description)); ?></textarea></label>
+<?php if ($errors->has('description')) { ?>
+ <p><?= htmlentities(implode(', ', $errors->get('description'))); ?></p>
+<?php } ?>
<label>Tag<input type="text" name="tag" maxlength="16" value="<?= htmlspecialchars($old->get('tag', $todo->tag)); ?>"></label>
<?php if ($errors->has('tag')) { ?>
<p><?= htmlentities(implode(', ', $errors->get('tag'))); ?></p>
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 @@
<ol>
<?php foreach ($todos as $todo) { ?>
<li>
- <?= htmlentities($todo->task); ?></a> | <?php if ($todo->tag) { ?><a class="no-decoration tag" href="/?tag=<?= htmlentities($todo->tag); ?>"><?= htmlentities($todo->tag); ?></a> | <?php } ?><a class="no-decoration" href="/edit/?id=<?= $todo->id; ?>">Edit</a> | <a class="no-decoration" href="/delete/confirm/?id=<?= $todo->id; ?>">Delete</a>
+ <a href="/view/?id=<?= $todo->id; ?>"><?= htmlentities($todo->task); ?></a> | <?php if ($todo->tag) { ?></a><a class="no-decoration tag" href="/?tag=<?= htmlentities($todo->tag); ?>"><?= htmlentities($todo->tag); ?></a> | <?php } ?><a class="no-decoration" href="/edit/?id=<?= $todo->id; ?>">Edit</a> | <a class="no-decoration" href="/delete/confirm/?id=<?= $todo->id; ?>">Delete</a>
</li>
<?php } ?>
</ol>
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 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <title>Edit - Todo</title>
+ <link rel="shortcut icon" href="/images/favicon.png">
+ <link rel="stylesheet" href="/css/site.css">
+ </head>
+ <body>
+ <section>
+ <h1>Todo</h1>
+ <h2>View</h2>
+ <a href="/">Back</a>
+ <h3><?= htmlentities("$todo->task | $todo->tag"); ?></h3>
+ <p><?= nl2br(htmlentities($todo->description), false); ?></p>
+ <p>Copyright © 2021 David T. Sadler.</p>
+ </section>
+ </body>
+</html>