EditAsset: allow changing an asset's parent album

This commit is contained in:
Aaron van Geffen 2023-11-12 17:26:03 +01:00
parent 44c6bf5914
commit 0c861bf976
3 changed files with 106 additions and 6 deletions

View File

@ -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);
}

View File

@ -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!

View File

@ -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
<div class="content-box key_info">
<h3>Key info</h3>
<div class="row mb-2">
<label class="col-form-label col-sm-3">Album:</label>
<div class="col-sm">
<select class="form-select" name="id_album">';
foreach ($this->allAlbums as $id_album => $album)
echo '
<option value="', $id_album, '"',
$this->currentAlbumId == $id_album ? ' selected' : '',
'>', htmlspecialchars($album), '</option>';
echo '
</select>
</div>
</div>
<div class="row mb-2">
<label class="col-form-label col-sm-3">Title (internal):</label>
<div class="col-sm">