diff --git a/controllers/EditAsset.php b/controllers/EditAsset.php index fed113d..930d01d 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 31041b3..f880386 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 3589cf1..8bc2f5b 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