From 0c861bf97608690e9bc037ab4631aef104203fe1 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sun, 12 Nov 2023 17:26:03 +0100 Subject: [PATCH] EditAsset: allow changing an asset's parent album --- controllers/EditAsset.php | 48 ++++++++++++++++++++++++++++++++++--- models/Asset.php | 39 ++++++++++++++++++++++++++++++ templates/EditAssetForm.php | 25 ++++++++++++++++--- 3 files changed, 106 insertions(+), 6 deletions(-) diff --git a/controllers/EditAsset.php b/controllers/EditAsset.php index fed113d8..930d01db 100644 --- a/controllers/EditAsset.php +++ b/controllers/EditAsset.php @@ -31,6 +31,25 @@ class EditAsset extends HTMLController exit; } + // Get a list of available photo albums + $allAlbums = []; + foreach (PhotoAlbum::getHierarchy('tag', 'up') as $album) + $allAlbums[$album['id_tag']] = $album['tag']; + + // Figure out the current album id + $currentAlbumId = 0; + $currentAlbumSlug = ''; + $currentTags = $asset->getTags(); + foreach ($currentTags as $tag) + { + if ($tag->kind === 'Album') + { + $currentAlbumId = $tag->id_tag; + $currentAlbumSlug = $tag->slug; + break; + } + } + if (!empty($_POST)) { if (isset($_GET['updatethumb'])) @@ -48,6 +67,26 @@ class EditAsset extends HTMLController $asset->setKeyData(htmlspecialchars($_POST['title']), $slug, $date_captured, intval($_POST['priority'])); } + // Changing parent album? + if ($_POST['id_album'] != $currentAlbumId) + { + $targetAlbum = Tag::fromId($_POST['id_album']); + + // First move the asset, then sort out the album tag + if (($retCode = $asset->moveToSubDir($targetAlbum->slug)) === true) + { + if (!isset($_POST['tag'])) + $_POST['tag'] = []; + + // Unset tag for current parent album + if (isset($_POST['tag'][$currentAlbumId])) + unset($_POST['tag'][$currentAlbumId]); + + // Set tag for new parent album + $_POST['tag'][$_POST['id_album']] = true; + } + } + // Handle tags $new_tags = []; if (isset($_POST['tag']) && is_array($_POST['tag'])) @@ -98,10 +137,13 @@ class EditAsset extends HTMLController header('Location: ' . BASEURL . '/editasset/?id=' . $asset->getId()); } - // Get list of thumbnails - $thumbs = $this->getThumbs($asset); + $page = new EditAssetForm([ + 'asset' => $asset, + 'thumbs' => $this->getThumbs($asset), + 'allAlbums' => $allAlbums, + 'currentAlbumId' => $currentAlbumId, + ]); - $page = new EditAssetForm($asset, $thumbs); parent::__construct('Edit asset \'' . $asset->getTitle() . '\' (' . $asset->getFilename() . ') - ' . SITE_TITLE); $this->page->adopt($page); } diff --git a/models/Asset.php b/models/Asset.php index 31041b38..f880386b 100644 --- a/models/Asset.php +++ b/models/Asset.php @@ -398,6 +398,45 @@ class Asset return $this->id_user_uploaded == $user->getUserId(); } + public function moveToSubDir($destSubDir) + { + // Verify the original exists + $source = ASSETSDIR . '/' . $this->subdir . '/' . $this->filename; + if (!file_exists($source)) + return -1; + + // Ensure the intended target file doesn't exist yet + $destDir = ASSETSDIR . '/' . $destSubDir; + $destFile = $destDir . '/' . $this->filename; + + if (file_exists($destFile)) + return -2; + + // Can we write to the target directory? + if (!is_writable($destDir)) + return -3; + + // Perform move + if (rename($source, $destFile)) + { + $this->subdir = $destSubDir; + $this->slug = $this->subdir . '/' . $this->title; + Registry::get('db')->query(' + UPDATE assets + SET subdir = {string:subdir}, + slug = {string:slug} + WHERE id_asset = {int:id_asset}', + [ + 'id_asset' => $this->id_asset, + 'subdir' => $this->subdir, + 'slug' => $this->slug, + ]); + return true; + } + + return -4; + } + public function replaceFile($filename) { // No filename? Abort! diff --git a/templates/EditAssetForm.php b/templates/EditAssetForm.php index 3589cf12..8bc2f5b8 100644 --- a/templates/EditAssetForm.php +++ b/templates/EditAssetForm.php @@ -8,13 +8,17 @@ class EditAssetForm extends Template { + private $allAlbums; private $asset; + private $currentAlbumId; private $thumbs; - public function __construct(Asset $asset, array $thumbs = []) + public function __construct(array $options) { - $this->asset = $asset; - $this->thumbs = $thumbs; + $this->allAlbums = $options['allAlbums']; + $this->asset = $options['asset']; + $this->currentAlbumId = $options['currentAlbumId']; + $this->thumbs = $options['thumbs']; } public function html_main() @@ -67,6 +71,21 @@ class EditAssetForm extends Template

Key info

+
+ +
+ +
+