diff --git a/controllers/EditAlbum.php b/controllers/EditAlbum.php index 8448f9c7..eeeaa7be 100644 --- a/controllers/EditAlbum.php +++ b/controllers/EditAlbum.php @@ -15,11 +15,11 @@ class EditAlbum extends HTMLController throw new NotAllowedException(); $id_tag = isset($_GET['id']) ? (int) $_GET['id'] : 0; - if (empty($id_tag) && !isset($_GET['add'])) + if (empty($id_tag) && !isset($_GET['add']) && $_GET['action'] !== 'addalbum') throw new UnexpectedValueException('Requested album not found or not requesting a new album.'); // Adding an album? - if (isset($_GET['add'])) + if (isset($_GET['add']) || $_GET['action'] === 'addalbum') { parent::__construct('Add a new album'); $form_title = 'Add a new album'; @@ -30,7 +30,7 @@ class EditAlbum extends HTMLController { // So far so good? $album = Tag::fromId($id_tag); - if (Session::validateSession('get') && $tag->kind === 'Album' && $tag->delete()) + if (Session::validateSession('get') && $album->kind === 'Album' && $album->delete()) { header('Location: ' . BASEURL . '/managealbums/'); exit; @@ -96,8 +96,24 @@ class EditAlbum extends HTMLController ], ]); + if (empty($_POST) && isset($_GET['tag'])) + { + $parentTag = Tag::fromId($_GET['tag']); + if ($parentTag->kind === 'Album') + { + $formDefaults = [ + 'id_parent' => $parentTag->id_tag, + 'tag' => 'New Album Title Here', + 'slug' => ($parentTag->slug ? $parentTag->slug . '/' : '') . 'NEW_ALBUM_SLUG_HERE', + ]; + } + } + + if (!isset($formDefaults)) + $formDefaults = isset($album) ? get_object_vars($album) : $_POST; + // Create the form, add in default values. - $form->setData($id_tag ? get_object_vars($album) : $_POST); + $form->setData($formDefaults); $formview = new FormView($form, $form_title ?? ''); $this->page->adopt($formview); @@ -112,6 +128,8 @@ class EditAlbum extends HTMLController $data = $form->getData(); // Quick stripping. + $data['tag'] = htmlentities($data['tag']); + $data['description'] = htmlentities($data['description']); $data['slug'] = strtr(strtolower($data['slug']), [' ' => '-', '--' => '-', '&' => 'and', '=>' => '', "'" => "", ":"=> "", '/' => '-', '\\' => '-']); // TODO: when updating slug, update slug for all photos in this album. @@ -120,13 +138,13 @@ class EditAlbum extends HTMLController if (!$id_tag) { $data['kind'] = 'Album'; - $return = Tag::createNew($data); - if ($return === false) + $newTag = Tag::createNew($data); + if ($newTag === false) return $formview->adopt(new Alert('Cannot create this album', 'Something went wrong while creating the album...', 'error')); if (isset($_POST['submit_and_new'])) { - header('Location: ' . BASEURL . '/editalbum/?add'); + header('Location: ' . BASEURL . '/editalbum/?add&tag=' . $data['id_parent']); exit; } } diff --git a/models/Dispatcher.php b/models/Dispatcher.php index e6e852a6..0350bd1a 100644 --- a/models/Dispatcher.php +++ b/models/Dispatcher.php @@ -11,6 +11,7 @@ class Dispatcher public static function route() { $possibleActions = [ + 'addalbum' => 'EditAlbum', 'albums' => 'ViewPhotoAlbums', 'editalbum' => 'EditAlbum', 'editasset' => 'EditAsset', diff --git a/models/Tag.php b/models/Tag.php index a6942e88..385e1aa7 100644 --- a/models/Tag.php +++ b/models/Tag.php @@ -272,7 +272,7 @@ class Tag $db = Registry::get('db'); $res = $db->query(' - DELETE FROM posts_tags + DELETE FROM assets_tags WHERE id_tag = {int:id_tag}', [ 'id_tag' => $this->id_tag,