diff options
| -rw-r--r-- | public/delete/index.php | 27 | ||||
| -rw-r--r-- | public/edit/index.php | 2 | ||||
| -rw-r--r-- | public/index.php | 2 | ||||
| -rw-r--r-- | src/DTS/BookmarkRepository.php | 70 | ||||
| -rw-r--r-- | src/templates/delete.php | 15 | ||||
| -rw-r--r-- | src/templates/index.php | 2 |
6 files changed, 85 insertions, 33 deletions
diff --git a/public/delete/index.php b/public/delete/index.php new file mode 100644 index 0000000..1b2484d --- /dev/null +++ b/public/delete/index.php @@ -0,0 +1,27 @@ +<?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; + +$bookmark = $bookmarks->find($id); + +if ('POST' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { + $bookmarks->delete($bookmark); +} + +$html = $template->render('delete', compact('bookmark')); + +respondAndExit(200, 'OK', $html); diff --git a/public/edit/index.php b/public/edit/index.php index 0cc1c27..65b3fa5 100644 --- a/public/edit/index.php +++ b/public/edit/index.php @@ -16,8 +16,6 @@ $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')) { diff --git a/public/index.php b/public/index.php index 2a9ccb7..7453506 100644 --- a/public/index.php +++ b/public/index.php @@ -18,8 +18,6 @@ $sort = $_GET['sort'] ?? null; $tag = $_GET['tag'] ?? null; -$bookmarks->load(); - if ($tag !== null) { $bookmarks->filter($tag); } diff --git a/src/DTS/BookmarkRepository.php b/src/DTS/BookmarkRepository.php index d673338..5d22100 100644 --- a/src/DTS/BookmarkRepository.php +++ b/src/DTS/BookmarkRepository.php @@ -17,30 +17,8 @@ class BookmarkRepository implements \Iterator function __construct(string $pathToRepository) { $this->pathToRepository = $pathToRepository; - } - - public function load(): void - { - if (!is_file($this->pathToRepository) || !is_readable($this->pathToRepository)) { - throw new \Exception("Unable to locate repository {$this->pathToRepository}"); - } - - if (($fp = fopen($this->pathToRepository, 'r')) === FALSE) { - throw new \Exception("Unable to read from repository {$this->pathToRepository}"); - } - - while (($data = fgetcsv($fp)) !== FALSE) { - $this->repository[] = new Bookmark( - $data[0], // Id. - $data[1], // Url. - $data[2], // Title. - $data[3], // Tag. - $data[4], // Added At. - (bool)$data[4], // Read. - ); - } - fclose($fp); + $this->load(); } public function sort(bool $asc = true): BookmarkRepository @@ -67,14 +45,14 @@ class BookmarkRepository implements \Iterator { $index = $this->findBookmarkIndex($id); - return $this->repository[$index] ?? null; + return $this->repository[$index] ?? null; } public function add(Bookmark $bookmark): bool { $this->repository[] = $bookmark; - return $this->store(); + return $this->save(); } public function update(Bookmark $bookmark): bool @@ -83,9 +61,21 @@ class BookmarkRepository implements \Iterator if ($index !== null) { $this->repository[$index] = $bookmark; - print_r($bookmark); - return $this->store(); + return $this->save(); + } + + return false; + } + + public function delete(Bookmark $bookmark): bool + { + $index = $this->findBookmarkIndex($bookmark->id); + + if ($index !== null) { + unset($this->repository[$index]); + + return $this->save(); } return false; @@ -127,7 +117,31 @@ class BookmarkRepository implements \Iterator return null; } - public function store(): bool + private function load(): void + { + if (!is_file($this->pathToRepository) || !is_readable($this->pathToRepository)) { + throw new \Exception("Unable to locate repository {$this->pathToRepository}"); + } + + if (($fp = fopen($this->pathToRepository, 'r')) === FALSE) { + throw new \Exception("Unable to read from repository {$this->pathToRepository}"); + } + + while (($data = fgetcsv($fp)) !== FALSE) { + $this->repository[] = new Bookmark( + $data[0], // Id. + $data[1], // Url. + $data[2], // Title. + $data[3], // Tag. + $data[4], // Added At. + (bool)$data[4], // Read. + ); + } + + fclose($fp); + } + + private function save(): bool { if (($fp = fopen($this->pathToRepository, 'w')) === FALSE) { throw new \Exception("Unable to open repository {$this->pathToRepository}"); diff --git a/src/templates/delete.php b/src/templates/delete.php new file mode 100644 index 0000000..1be762b --- /dev/null +++ b/src/templates/delete.php @@ -0,0 +1,15 @@ +<!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="/delete" method="POST"> + <input type="hidden" name="id" value="<?php echo $bookmark->id; ?>"/> + <?php echo $bookmark->url.' '.$bookmark->title.' '.$bookmark->tag; ?> + <button type="submit">Delete</button> + </form> + </body> +</html> diff --git a/src/templates/index.php b/src/templates/index.php index 2703851..1298985 100644 --- a/src/templates/index.php +++ b/src/templates/index.php @@ -8,7 +8,7 @@ <body> <ul> <?php foreach ($bookmarks as $bookmark) { ?> - <li><a href="/edit?id=<?php echo $bookmark->id; ?>"><?php echo $bookmark->title; ?></a></li> + <li><a href="/edit?id=<?php echo $bookmark->id; ?>"><?php echo $bookmark->title; ?></a> | <a href="/delete?id=<?php echo $bookmark->id; ?>">Delete</a></li> <?php } ?> </ul> </body> |
