diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2021-10-11 21:49:47 +0100 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2021-10-11 21:49:47 +0100 |
| commit | 523a84dd4b7cb611955e949fd6debc4a3c037b91 (patch) | |
| tree | 3ca2999adda1cf3e180390538d91f45f696fe175 | |
| parent | 0f273c0a8ead390346571c4be2296fa6e384eff6 (diff) | |
Implement very basic editing
| -rw-r--r-- | public/edit/index.php | 33 | ||||
| -rw-r--r-- | src/DTS/BookmarkRepository.php | 69 | ||||
| -rw-r--r-- | src/templates/edit.php | 17 | ||||
| -rw-r--r-- | src/templates/index.php | 2 |
4 files changed, 107 insertions, 14 deletions
diff --git a/public/edit/index.php b/public/edit/index.php new file mode 100644 index 0000000..0cc1c27 --- /dev/null +++ b/public/edit/index.php @@ -0,0 +1,33 @@ +<?php + +declare(strict_types=1); + +use DTS\BookmarkRepository; +use DTS\Template; +use function DTS\Functions\respondAndExit; + +require_once(__DIR__.'/../../autoload.php'); + +$config = require_once(__DIR__.'/../../config.php'); + +$bookmarks = new BookmarkRepository($config['path_to_repository']); + +$template = new Template($config['path_to_templates']); + +$id = filter_input(INPUT_GET, 'id') ?? filter_input(INPUT_POST, 'id') ?? null; + +$bookmarks->load(); + +$bookmark = $bookmarks->find($id); + +if ('POST' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { + $bookmark->url = $_POST['url']; + $bookmark->title = $_POST['title']; + $bookmark->tag = $_POST['tag']; + + $bookmarks->update($bookmark); +} + +$html = $template->render('edit', compact('bookmark')); + +respondAndExit(200, 'OK', $html); diff --git a/src/DTS/BookmarkRepository.php b/src/DTS/BookmarkRepository.php index ab54594..d673338 100644 --- a/src/DTS/BookmarkRepository.php +++ b/src/DTS/BookmarkRepository.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace DTS; +use DTS\Bookmark; + class BookmarkRepository implements \Iterator { private string $pathToRepository; @@ -61,24 +63,32 @@ class BookmarkRepository implements \Iterator return $this; } + public function find(string $id): ?Bookmark + { + $index = $this->findBookmarkIndex($id); + + return $this->repository[$index] ?? null; + } + public function add(Bookmark $bookmark): bool { - if (($fp = fopen($this->pathToRepository, 'a')) === FALSE) { - throw new \Exception("Unable to open repository {$this->pathToRepository}"); - } + $this->repository[] = $bookmark; - $saved = fputcsv($fp, [ - $bookmark->id, - $bookmark->url, - $bookmark->title, - $bookmark->tag, - $bookmark->addedAt, - (int)$bookmark->unread, - ]); + return $this->store(); + } - fclose($fp); + public function update(Bookmark $bookmark): bool + { + $index = $this->findBookmarkIndex($bookmark->id); + + if ($index !== null) { + $this->repository[$index] = $bookmark; + print_r($bookmark); - return $saved !== false; + return $this->store(); + } + + return false; } public function current(): mixed @@ -105,4 +115,37 @@ class BookmarkRepository implements \Iterator { return isset($this->repository[$this->position]); } + + private function findBookmarkIndex(string $id): ?int + { + foreach ($this->repository as $index => $bookmark) { + if ($bookmark->id == $id) { + return $index; + } + } + + return null; + } + + public function store(): bool + { + if (($fp = fopen($this->pathToRepository, 'w')) === FALSE) { + throw new \Exception("Unable to open repository {$this->pathToRepository}"); + } + + foreach ($this->repository as $bookmark) { + fputcsv($fp, [ + $bookmark->id, + $bookmark->url, + $bookmark->title, + $bookmark->tag, + $bookmark->addedAt, + (int)$bookmark->unread, + ]); + } + + fclose($fp); + + return true; + } } diff --git a/src/templates/edit.php b/src/templates/edit.php new file mode 100644 index 0000000..a3c6930 --- /dev/null +++ b/src/templates/edit.php @@ -0,0 +1,17 @@ +<!doctype html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Bookmarks</title> + </head> + <body> + <form action="/edit" method="POST"> + <input type="hidden" name="id" value="<?php echo $bookmark->id; ?>"/> + <input type="text" name="url" maxlength="512" autofocus value="<?php echo $bookmark->url; ?>"><br> + <input type="text" name="title" maxlength="256" value="<?php echo $bookmark->title; ?>"><br> + <input type="text" name="tag" maxlength="8" value="<?php echo $bookmark->tag; ?>"><br> + <button type="submit">Update</button> + </form> + </body> +</html> diff --git a/src/templates/index.php b/src/templates/index.php index 9656551..2703851 100644 --- a/src/templates/index.php +++ b/src/templates/index.php @@ -8,7 +8,7 @@ <body> <ul> <?php foreach ($bookmarks as $bookmark) { ?> - <li><?php echo "[$bookmark->id] $bookmark->url"; ?></li> + <li><a href="/edit?id=<?php echo $bookmark->id; ?>"><?php echo $bookmark->title; ?></a></li> <?php } ?> </ul> </body> |
