From b91759e6c16089b1d8340d4c91b8b25f4d675706 Mon Sep 17 00:00:00 2001 From: Nils Asmussen Date: Wed, 3 Aug 2016 23:04:47 +0200 Subject: [PATCH] Added a move album operation. Since albums can have subalbums now, it makes sense to not only merge albums, but also move them. --- php/Access/Admin.php | 9 +++++++ php/Modules/Album.php | 34 ++++++++++++++++++++++++ src/scripts/album.js | 54 +++++++++++++++++++++++++++++++++++--- src/scripts/contextMenu.js | 52 ++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 4 deletions(-) diff --git a/php/Access/Admin.php b/php/Access/Admin.php index e66dc5f..df622f6 100644 --- a/php/Access/Admin.php +++ b/php/Access/Admin.php @@ -28,6 +28,7 @@ final class Admin extends Access { case 'Album::setPublic': self::setAlbumPublicAction(); break; case 'Album::delete': self::deleteAlbumAction(); break; case 'Album::merge': self::mergeAlbumsAction(); break; + case 'Album::move': self::moveAlbumsAction(); break; // Photo functions case 'Photo::get': self::getPhotoAction(); break; @@ -143,6 +144,14 @@ final class Admin extends Access { } + private static function moveAlbumsAction() { + + Validator::required(isset($_POST['albumIDs']), __METHOD__); + $album = new Album($_POST['albumIDs']); + Response::json($album->move()); + + } + // Photo functions private static function getPhotoAction() { diff --git a/php/Modules/Album.php b/php/Modules/Album.php index 0c91348..95ebee5 100644 --- a/php/Modules/Album.php +++ b/php/Modules/Album.php @@ -665,6 +665,40 @@ final class Album { } + /** + * @return boolean Returns true when successful. + */ + public function move() { + + // Check dependencies + Validator::required(isset($this->albumIDs), __METHOD__); + + // Call plugins + Plugins::get()->activate(__METHOD__, 0, func_get_args()); + + // Convert to array + $albumIDs = explode(',', $this->albumIDs); + + // Get first albumID + $albumID = array_splice($albumIDs, 0, 1); + $albumID = $albumID[0]; + + // $albumIDs contains all IDs without the first albumID + // Convert to string + $filteredIDs = implode(',', $albumIDs); + + // Move albums + $query = Database::prepare(Database::get(), "UPDATE ? SET parent = ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $albumID, $filteredIDs)); + $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__); + + // Call plugins + Plugins::get()->activate(__METHOD__, 1, func_get_args()); + + if ($result===false) return false; + return true; + + } + /** * @return boolean Returns true when successful. */ diff --git a/src/scripts/album.js b/src/scripts/album.js index 3a55193..346a19b 100644 --- a/src/scripts/album.js +++ b/src/scripts/album.js @@ -635,7 +635,7 @@ album.getArchive = function(albumID) { } -album.merge = function(albumIDs, titles = []) { +const getMessage = function(albumIDs, titles, operation) { let title = '' let sTitle = '' @@ -660,14 +660,20 @@ album.merge = function(albumIDs, titles = []) { // Fallback for second album without a title if (sTitle==='') sTitle = 'Untitled' - msg = lychee.html`

Are you sure you want to merge the album '$${ sTitle }' into the album '$${ title }'?

` + msg = lychee.html`

Are you sure you want to ${ operation } the album '$${ sTitle }' into the album '$${ title }'?

` } else { - msg = lychee.html`

Are you sure you want to merge all selected albums into the album '$${ title }'?

` + msg = lychee.html`

Are you sure you want to ${ operation } all selected albums into the album '$${ title }'?

` } + return msg + +} + +album.merge = function(albumIDs, titles = []) { + const action = function() { basicModal.close() @@ -690,7 +696,7 @@ album.merge = function(albumIDs, titles = []) { } basicModal.show({ - body: msg, + body: getMessage(albumIDs, titles, 'merge'), buttons: { action: { title: 'Merge Albums', @@ -704,4 +710,44 @@ album.merge = function(albumIDs, titles = []) { } }) +} + +album.move = function(albumIDs, titles = []) { + + const action = function() { + + basicModal.close() + + let params = { + albumIDs: albumIDs.join() + } + + api.post('Album::move', params, function(data) { + + if (data!==true) { + lychee.error(null, params, data) + } else { + albums.refresh() + lychee.goto() + } + + }) + + } + + basicModal.show({ + body: getMessage(albumIDs, titles, 'move'), + buttons: { + action: { + title: 'Move Albums', + fn: action, + class: 'red' + }, + cancel: { + title: "Don't Move", + fn: basicModal.close + } + } + }) + } \ No newline at end of file diff --git a/src/scripts/contextMenu.js b/src/scripts/contextMenu.js index 9a16b4e..fec875f 100644 --- a/src/scripts/contextMenu.js +++ b/src/scripts/contextMenu.js @@ -51,6 +51,24 @@ const getAlbumFrom = function(albums, id) { } +const getSubIDs = function(albums, albumID) { + + let ids = [ albumID ] + + for (a in albums) { + if (albums[a].parent==albumID) { + + let sub = getSubIDs(albums, albums[a].id) + for (id in sub) + ids.push(sub[id]) + + } + } + + return ids + +} + const countSubAlbums = function(photoIDs) { let count = 0 @@ -123,6 +141,7 @@ contextMenu.album = function(albumID, e) { let items = [ { title: build.iconic('pencil') + 'Rename', fn: () => album.setTitle([ albumID ]) }, { title: build.iconic('collapse-left') + 'Merge', visible: showMerge, fn: () => { basicContext.close(); contextMenu.mergeAlbum(albumID, e) } }, + { title: build.iconic('folder') + 'Move', visible: showMerge, fn: () => { basicContext.close(); contextMenu.moveAlbum([ albumID ], e) } }, { title: build.iconic('trash') + 'Delete', fn: () => album.delete([ albumID ]) } ] @@ -147,6 +166,7 @@ contextMenu.albumMulti = function(albumIDs, e) { { title: build.iconic('pencil') + 'Rename All', fn: () => album.setTitle(albumIDs) }, { title: build.iconic('collapse-left') + 'Merge All', visible: showMerge && autoMerge, fn: () => album.merge(albumIDs) }, { title: build.iconic('collapse-left') + 'Merge', visible: showMerge && !autoMerge, fn: () => { basicContext.close(); contextMenu.mergeAlbum(albumIDs[0], e) } }, + { title: build.iconic('folder') + 'Move All', visible: showMerge, fn: () => { basicContext.close(); contextMenu.moveAlbum(albumIDs, e) } }, { title: build.iconic('trash') + 'Delete All', fn: () => album.delete(albumIDs) } ] @@ -210,6 +230,38 @@ contextMenu.mergeAlbum = function(albumID, e) { } +contextMenu.moveAlbum = function(albumIDs, e) { + + api.post('Albums::get', { parent: -1 }, function(data) { + + let items = [] + + if (data.albums && data.num>1) { + + let title = albums.getByID(albumIDs[0]).title + // Disable all childs + // It's not possible to move us into them + let exclude = [] + for (i in albumIDs) { + let sub = getSubIDs(data.albums, String(albumIDs[i])) + for (s in sub) + exclude.push(sub[s]) + } + + items = buildAlbumList(data.albums, exclude, (a) => album.move([ a.id ].concat(albumIDs), [ a.title, title ])) + + items.unshift({ title: 'Root', fn: () => album.move([ 0 ].concat(albumIDs), [ 'Root', title ]) }) + + } + + if (items.length===0) return false + + basicContext.show(items, e.originalEvent, contextMenu.close) + + }) + +} + contextMenu.photo = function(photoID, e) { // Notice for 'Move':