From 16ec5470642a672ad11474a3e4d8fa2eda65a37d Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sun, 8 Jul 2018 08:19:37 +0000 Subject: [PATCH] Changes the ConfirmDelete page and updates database code. The ConfirmDelete page now uses parts of the photopage. The Confirmation dialog is its own template which is based on Alert. The database now updates the album thumb to the most recent addition to the album, when the album thumb asset is being deleted. When there are no pictures left in the album 0 will be set. --- controllers/ConfirmDelete.php | 43 --------------------------- controllers/ViewPhoto.php | 46 +++++++++++++++++++++++------ models/Asset.php | 51 +++++++++++++++++++++++++++------ models/Dispatcher.php | 1 - models/Tag.php | 28 ++++++++++++++++++ public/css/default.css | 23 +++++---------- templates/Alert.php | 9 +++++- templates/Button.php | 27 +++++++++++++++++ templates/ConfirmDeletePage.php | 39 +++++++------------------ templates/EditAssetForm.php | 2 +- templates/PhotoPage.php | 12 ++++---- templates/WarningDialog.php | 29 +++++++++++++++++++ 12 files changed, 197 insertions(+), 113 deletions(-) delete mode 100644 controllers/ConfirmDelete.php create mode 100644 templates/Button.php create mode 100644 templates/WarningDialog.php diff --git a/controllers/ConfirmDelete.php b/controllers/ConfirmDelete.php deleted file mode 100644 index 7f204232..00000000 --- a/controllers/ConfirmDelete.php +++ /dev/null @@ -1,43 +0,0 @@ -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/controllers/ViewPhoto.php b/controllers/ViewPhoto.php index 528c2d86..6c15130f 100644 --- a/controllers/ViewPhoto.php +++ b/controllers/ViewPhoto.php @@ -11,17 +11,53 @@ class ViewPhoto extends HTMLController public function __construct() { // Ensure we're logged in at this point. - if (!Registry::get('user')->isLoggedIn()) + $user = Registry::get('user'); + if (!$user->isLoggedIn()) throw new NotAllowedException(); $photo = Asset::fromSlug($_GET['slug']); if (empty($photo)) throw new NotFoundException(); + parent::__construct($photo->getTitle() . ' - ' . SITE_TITLE); + + $author = $photo->getAuthor(); + + if (isset($_REQUEST['confirm_delete']) || isset($_REQUEST['delete_confirmed'])) + $this->handleConfirmDelete($user, $author, $photo); + else + $this->handleViewPhoto($user, $author, $photo); + + // Add an edit button to the admin bar. + if ($user->isAdmin()) + $this->admin_bar->appendItem(BASEURL . '/editasset/?id=' . $photo->getId(), 'Edit this photo'); + } + + private function handleConfirmDelete(User $user, User $author, Asset $photo) + { + if (!($user->isAdmin() || $user->getUserId() === $author->getUserId())) + throw new NotAllowedException(); + + if (isset($_REQUEST['confirm_delete'])) + { + $page = new ConfirmDeletePage($photo->getImage()); + $this->page->adopt($page); + } + else if (isset($_REQUEST['delete_confirmed'])) + { + $album_url = $photo->getSubdir(); + $photo->delete(); + + header('Location: ' . BASEURL . '/' . $album_url); + exit; + } + } + + private function handleViewPhoto(User $user, User $author, Asset $photo) + { if (!empty($_POST)) $this->handleTagging($photo->getImage()); - parent::__construct($photo->getTitle() . ' - ' . SITE_TITLE); $page = new PhotoPage($photo->getImage()); // Exif data? @@ -43,17 +79,11 @@ class ViewPhoto extends HTMLController if ($next_url) $page->setNextPhotoUrl($next_url); - $user = Registry::get('user'); - $author = $photo->getAuthor(); if ($user->isAdmin() || $user->getUserId() === $author->getUserId()) $page->setIsAssetOwner(true); $this->page->adopt($page); $this->page->setCanonicalUrl($photo->getPageUrl()); - - // Add an edit button to the admin bar. - if (Registry::get('user')->isAdmin()) - $this->admin_bar->appendItem(BASEURL . '/editasset/?id=' . $photo->getId(), 'Edit this photo'); } private function handleTagging(Image $photo) diff --git a/models/Asset.php b/models/Asset.php index 62ee0aa1..58716954 100644 --- a/models/Asset.php +++ b/models/Asset.php @@ -485,14 +485,6 @@ 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}', @@ -500,12 +492,53 @@ class Asset 'id_asset' => $this->id_asset, ]); - return $db->query(' + $rows = $db->query(' + SELECT id_tag + FROM assets_tags + WHERE id_asset = {int:id_asset}', + [ + 'id_asset' => $this->id_asset, + ]); + + $recount_tags = []; + if(!empty($rows)) + foreach($rows as $row) + $recount_tags[] = $row['id_tag']; + + $db->query(' + DELETE FROM assets_tags + WHERE id_asset = {int:id_asset}', + [ + 'id_asset' => $this->id_asset, + ]); + + Tag::recount($recount_tags); + + $return = $db->query(' DELETE FROM assets WHERE id_asset = {int:id_asset}', [ 'id_asset' => $this->id_asset, ]); + + $rows = $db->query(' + SELECT id_tag + FROM tags + WHERE id_asset_thumb = {int:id_asset} AND kind = "Album"', + [ + 'id_asset' => $this->id_asset, + ]); + + if (!empty($rows)) + { + foreach ($rows as $row) + { + $tag = Tag::fromId($row['id_tag']); + $tag->resetIdAsset(); + } + } + + return $return; } public function linkTags(array $id_tags) diff --git a/models/Dispatcher.php b/models/Dispatcher.php index 7fb8bc69..f9cc9307 100644 --- a/models/Dispatcher.php +++ b/models/Dispatcher.php @@ -28,7 +28,6 @@ class Dispatcher 'suggest' => 'ProvideAutoSuggest', 'timeline' => 'ViewTimeline', 'uploadmedia' => 'UploadMedia', - 'confirmdelete' => 'ConfirmDelete', ]; // Work around PHP's FPM not always providing PATH_INFO. diff --git a/models/Tag.php b/models/Tag.php index 385e1aa7..dfc9e93c 100644 --- a/models/Tag.php +++ b/models/Tag.php @@ -289,6 +289,34 @@ class Tag ]); } + public function resetIdAsset() + { + $db = Registry::get('db'); + + $row = $db->query(' + SELECT MAX(id_asset) as new_id + FROM assets_tags + WHERE id_tag = {int:id_tag}', + [ + 'id_tag' => $this->id_tag, + ]); + + $new_id = 0; + if(!empty($row)) + { + $new_id = $row->fetch_assoc()['new_id']; + } + + return $db->query(' + UPDATE tags + SET id_asset_thumb = {int:new_id} + WHERE id_tag = {int:id_tag}', + [ + 'new_id' => $new_id, + 'id_tag' => $this->id_tag, + ]); + } + public static function match($tokens) { if (!is_array($tokens)) diff --git a/public/css/default.css b/public/css/default.css index a41421e0..9fbf28ec 100644 --- a/public/css/default.css +++ b/public/css/default.css @@ -8,10 +8,10 @@ @import url(//fonts.googleapis.com/css?family=Open+Sans:400,400italic,700,700italic); @font-face { - font-family: 'Invaders'; - src: url('fonts/invaders.ttf') format('truetype'); - font-weight: normal; - font-style: normal; + font-family: 'Invaders'; + src: url('fonts/invaders.ttf') format('truetype'); + font-weight: normal; + font-style: normal; } body { @@ -135,7 +135,7 @@ ul#nav li a:hover { } .pagination .page-padding { - cursor: pointer; + cursor: pointer; } @@ -565,7 +565,7 @@ a#previous_photo:hover, a#next_photo:hover { content: '→'; } -#sub_photo h2, #sub_photo h3, #photo_exif_box h3 { +#sub_photo h2, #sub_photo h3, #photo_exif_box h3, #user_actions_box h3 { font: 600 20px/30px "Open Sans", sans-serif; margin: 0 0 10px; } @@ -622,22 +622,13 @@ a#previous_photo:hover, a#next_photo:hover { #user_actions_box { background: #fff; box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3); + float: left; margin: 25px 0 25px 0; overflow: auto; padding: 2%; - float: right; 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 ---------------------------------------*/ @media only screen and (max-width: 895px) { diff --git a/templates/Alert.php b/templates/Alert.php index 1267ecae..20549e31 100644 --- a/templates/Alert.php +++ b/templates/Alert.php @@ -19,6 +19,13 @@ class Alert extends SubTemplate { echo '
', (!empty($this->_title) ? ' - ' . $this->_title . '
' : ''), $this->_message, '
'; + ' . $this->_title . '
' : ''), '

', $this->_message, '

'; + + $this->additional_alert_content(); + + echo ''; } + + protected function additional_alert_content() + {} } diff --git a/templates/Button.php b/templates/Button.php new file mode 100644 index 00000000..29024e12 --- /dev/null +++ b/templates/Button.php @@ -0,0 +1,27 @@ +content = $content; + $this->href = $href; + $this->class = $class; + } + + protected function html_content() + { + echo ' + ', $this->content, ''; + } +} diff --git a/templates/ConfirmDeletePage.php b/templates/ConfirmDeletePage.php index 2b003a69..bdba97cb 100644 --- a/templates/ConfirmDeletePage.php +++ b/templates/ConfirmDeletePage.php @@ -6,13 +6,11 @@ * Kabuki CMS (C) 2013-2016, Aaron van Geffen *****************************************************************************/ -class ConfirmDeletePage extends SubTemplate +class ConfirmDeletePage extends PhotoPage { - private $photo; - public function __construct(Image $photo) { - $this->photo = $photo; + parent::__construct($photo); } protected function html_content() @@ -23,30 +21,15 @@ class ConfirmDeletePage extends SubTemplate private function confirm() { - echo ' -
-

Confirm deletion

-

You are about to permanently delete the following photo.

- Delete - Cancel -
'; - } + $buttons = []; + $buttons[] = new Button("Delete", BASEURL . '/' . $this->photo->getSlug() . '?delete_confirmed', "btn btn-red"); + $buttons[] = new Button("Cancel", $this->photo->getPageUrl(), "btn"); - private function photo() - { - echo ' -
- '; - - if ($this->photo->isPortrait()) - echo ' - '; - else - echo ' - '; - - echo ' - -
'; + $alert = new WarningDialog( + "Confirm deletion.", + "You are about to permanently delete the following photo.", + $buttons + ); + $alert->html_content(); } } diff --git a/templates/EditAssetForm.php b/templates/EditAssetForm.php index 0fbf89a0..1dd3ed6a 100644 --- a/templates/EditAssetForm.php +++ b/templates/EditAssetForm.php @@ -23,7 +23,7 @@ class EditAssetForm extends SubTemplate
- Delete asset

Edit asset \'', $this->asset->getTitle(), '\' (', $this->asset->getFilename(), ')

diff --git a/templates/PhotoPage.php b/templates/PhotoPage.php index e7bc0edf..c695c645 100644 --- a/templates/PhotoPage.php +++ b/templates/PhotoPage.php @@ -8,7 +8,7 @@ class PhotoPage extends SubTemplate { - private $photo; + protected $photo; private $exif; private $previous_photo_url = ''; private $next_photo_url = ''; @@ -29,7 +29,8 @@ class PhotoPage extends SubTemplate $this->next_photo_url = $url; } - public function setIsAssetOwner($flag) { + public function setIsAssetOwner($flag) + { $this->is_asset_owner = $flag; } @@ -50,15 +51,14 @@ class PhotoPage extends SubTemplate $this->photoMeta(); - if($this->is_asset_owner) { + if($this->is_asset_owner) $this->addUserActions(); - } echo ' '; } - private function photo() + protected function photo() { echo '
@@ -198,7 +198,7 @@ class PhotoPage extends SubTemplate echo '

Actions

- Delete + Delete
'; } } diff --git a/templates/WarningDialog.php b/templates/WarningDialog.php new file mode 100644 index 00000000..198b7e0f --- /dev/null +++ b/templates/WarningDialog.php @@ -0,0 +1,29 @@ +buttons = $buttons; + } + + protected function additional_alert_content() + { + $this->addButtons(); + } + + private function addButtons() + { + foreach ($this->buttons as $button) + $button->html_content(); + } +}