From e40c05c1f842dc52bb2983ddfa00054c7dc69016 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sat, 7 Jul 2018 12:21:12 +0000 Subject: [PATCH] Adds a Confirm Delete page and actually delete the assets. --- controllers/ConfirmDelete.php | 43 +++++++++++++++++++++++++++ models/Asset.php | 8 +++++ models/Dispatcher.php | 1 + public/css/default.css | 8 +++++ templates/ConfirmDeletePage.php | 52 +++++++++++++++++++++++++++++++++ templates/EditAssetForm.php | 2 +- templates/PhotoPage.php | 2 +- 7 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 controllers/ConfirmDelete.php create mode 100644 templates/ConfirmDeletePage.php diff --git a/controllers/ConfirmDelete.php b/controllers/ConfirmDelete.php new file mode 100644 index 0000000..7f20423 --- /dev/null +++ b/controllers/ConfirmDelete.php @@ -0,0 +1,43 @@ +isLoggedIn()) + throw new NotAllowedException(); + + $photo = Asset::fromSlug($_GET['slug']); + if (empty($photo)) + throw new NotFoundException(); + + $author = $photo->getAuthor(); + if (!($user->isAdmin() || $user->getUserId() === $author->getUserId())) + throw new NotAllowedException(); + + if (isset($_REQUEST['confirmed'])) + $this->handleDelete($photo); + + parent::__construct('Confirm deletion' . ' - ' . SITE_TITLE); + $page = new ConfirmDeletePage($photo->getImage()); + + $this->page->adopt($page); + } + + private function handleDelete(Asset $photo) { + $album_url = $photo->getSubdir(); + + $photo->delete(); + + header('Location: ' . BASEURL . '/' . $album_url); + exit; + } +} diff --git a/models/Asset.php b/models/Asset.php index 6d15a09..62ee0aa 100644 --- a/models/Asset.php +++ b/models/Asset.php @@ -485,6 +485,14 @@ class Asset if (!unlink(ASSETSDIR . '/' . $this->subdir . '/' . $this->filename)) return false; + $db->query(' + UPDATE tags + SET id_asset_thumb = 0 + WHERE id_asset_thumb = {int:id_asset} AND kind = "Album"', + [ + 'id_asset' => $this->id_asset, + ]); + $db->query(' DELETE FROM assets_meta WHERE id_asset = {int:id_asset}', diff --git a/models/Dispatcher.php b/models/Dispatcher.php index f9cc930..7fb8bc6 100644 --- a/models/Dispatcher.php +++ b/models/Dispatcher.php @@ -28,6 +28,7 @@ class Dispatcher 'suggest' => 'ProvideAutoSuggest', 'timeline' => 'ViewTimeline', 'uploadmedia' => 'UploadMedia', + 'confirmdelete' => 'ConfirmDelete', ]; // Work around PHP's FPM not always providing PATH_INFO. diff --git a/public/css/default.css b/public/css/default.css index 736b974..a41421e 100644 --- a/public/css/default.css +++ b/public/css/default.css @@ -629,6 +629,14 @@ a#previous_photo:hover, a#next_photo:hover { width: 20%; } +#confirm_box { + background: #fff; + box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3); + padding: 2%; + margin: 25px 0 25px 0; + text-align: center; +} + /* Responsive: smartphone in portrait ---------------------------------------*/ diff --git a/templates/ConfirmDeletePage.php b/templates/ConfirmDeletePage.php new file mode 100644 index 0000000..2b003a6 --- /dev/null +++ b/templates/ConfirmDeletePage.php @@ -0,0 +1,52 @@ +photo = $photo; + } + + protected function html_content() + { + $this->confirm(); + $this->photo(); + } + + private function confirm() + { + echo ' +
+

Confirm deletion

+

You are about to permanently delete the following photo.

+ Delete + Cancel +
'; + } + + private function photo() + { + echo ' +
+ '; + + if ($this->photo->isPortrait()) + echo ' + '; + else + echo ' + '; + + echo ' + +
'; + } +} diff --git a/templates/EditAssetForm.php b/templates/EditAssetForm.php index 14e29f8..0fbf89a 100644 --- a/templates/EditAssetForm.php +++ b/templates/EditAssetForm.php @@ -23,7 +23,7 @@ class EditAssetForm extends SubTemplate