From 943297900cdd235f4f0bb7af078164d6b25fdf18 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sun, 5 Nov 2017 17:09:01 +0100 Subject: [PATCH] Implement basic album management. --- controllers/EditAlbum.php | 147 +++++++++++++++++++++++++++++++++++ controllers/ManageAlbums.php | 12 +++ models/Dispatcher.php | 1 + models/Tag.php | 2 + templates/FormView.php | 6 +- 5 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 controllers/EditAlbum.php diff --git a/controllers/EditAlbum.php b/controllers/EditAlbum.php new file mode 100644 index 00000000..8448f9c7 --- /dev/null +++ b/controllers/EditAlbum.php @@ -0,0 +1,147 @@ +isAdmin()) + throw new NotAllowedException(); + + $id_tag = isset($_GET['id']) ? (int) $_GET['id'] : 0; + if (empty($id_tag) && !isset($_GET['add'])) + throw new UnexpectedValueException('Requested album not found or not requesting a new album.'); + + // Adding an album? + if (isset($_GET['add'])) + { + parent::__construct('Add a new album'); + $form_title = 'Add a new album'; + $this->page->addClass('editalbum'); + } + // Deleting one? + elseif (isset($_GET['delete'])) + { + // So far so good? + $album = Tag::fromId($id_tag); + if (Session::validateSession('get') && $tag->kind === 'Album' && $tag->delete()) + { + header('Location: ' . BASEURL . '/managealbums/'); + exit; + } + else + trigger_error('Cannot delete album: an error occured while processing the request.', E_USER_ERROR); + } + // Editing one, then, surely. + else + { + $album = Tag::fromId($id_tag); + if ($album->kind !== 'Album') + trigger_error('Cannot edit album: not an album.', E_USER_ERROR); + + parent::__construct('Edit album \'' . $album->tag . '\''); + $form_title = 'Edit album \'' . $album->tag . '\''; + $this->page->addClass('editalbum'); + } + + // Session checking! + if (empty($_POST)) + Session::resetSessionToken(); + else + Session::validateSession(); + + if ($id_tag) + $after_form = 'Delete album'; + elseif (!$id_tag) + $after_form = ''; + + $form = new Form([ + 'request_url' => BASEURL . '/editalbum/?' . ($id_tag ? 'id=' . $id_tag : 'add'), + 'content_below' => $after_form, + 'fields' => [ + 'id_parent' => [ + 'type' => 'numeric', + 'label' => 'Parent album ID', + ], + 'id_asset_thumb' => [ + 'type' => 'numeric', + 'label' => 'Thumbnail asset ID', + 'is_optional' => true, + ], + 'tag' => [ + 'type' => 'text', + 'label' => 'Album title', + 'size' => 50, + 'maxlength' => 255, + ], + 'slug' => [ + 'type' => 'text', + 'label' => 'URL slug', + 'size' => 50, + 'maxlength' => 255, + ], + 'description' => [ + 'type' => 'textbox', + 'label' => 'Description', + 'size' => 50, + 'maxlength' => 255, + 'is_optional' => true, + ], + ], + ]); + + // Create the form, add in default values. + $form->setData($id_tag ? get_object_vars($album) : $_POST); + $formview = new FormView($form, $form_title ?? ''); + $this->page->adopt($formview); + + if (!empty($_POST)) + { + $form->verify($_POST); + + // Anything missing? + if (!empty($form->getMissing())) + return $formview->adopt(new Alert('Some data missing', 'Please fill out the following fields: ' . implode(', ', $form->getMissing()), 'error')); + + $data = $form->getData(); + + // Quick stripping. + $data['slug'] = strtr(strtolower($data['slug']), [' ' => '-', '--' => '-', '&' => 'and', '=>' => '', "'" => "", ":"=> "", '/' => '-', '\\' => '-']); + + // TODO: when updating slug, update slug for all photos in this album. + + // Creating a new album? + if (!$id_tag) + { + $data['kind'] = 'Album'; + $return = Tag::createNew($data); + if ($return === 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'); + exit; + } + } + // Just updating? + else + { + foreach ($data as $key => $value) + $album->$key = $value; + + $album->save(); + } + + // Redirect to the album management page. + header('Location: ' . BASEURL . '/managealbums/'); + exit; + } + } +} diff --git a/controllers/ManageAlbums.php b/controllers/ManageAlbums.php index e0e262b5..f6a1b59c 100644 --- a/controllers/ManageAlbums.php +++ b/controllers/ManageAlbums.php @@ -15,6 +15,17 @@ class ManageAlbums extends HTMLController throw new NotAllowedException(); $options = [ + 'form' => [ + 'action' => BASEURL . '/editalbum/', + 'method' => 'get', + 'class' => 'floatright', + 'buttons' => [ + 'add' => [ + 'type' => 'submit', + 'caption' => 'Add new album', + ], + ], + ], 'columns' => [ 'id_album' => [ 'value' => 'id_tag', @@ -49,6 +60,7 @@ class ManageAlbums extends HTMLController 'title' => 'Manage albums', 'no_items_label' => 'No albums meet the requirements of the current filter.', 'items_per_page' => 9999, + 'index_class' => 'floatleft', 'base_url' => BASEURL . '/managealbums/', 'get_data' => function($offset = 0, $limit = 9999, $order = '', $direction = 'up') { if (!in_array($order, ['id_tag', 'tag', 'slug', 'count'])) diff --git a/models/Dispatcher.php b/models/Dispatcher.php index 29151bba..bf5a08b0 100644 --- a/models/Dispatcher.php +++ b/models/Dispatcher.php @@ -12,6 +12,7 @@ class Dispatcher { $possibleActions = [ 'albums' => 'ViewPhotoAlbums', + 'editalbum' => 'EditAlbum', 'editasset' => 'EditAsset', 'edituser' => 'EditUser', 'login' => 'Login', diff --git a/models/Tag.php b/models/Tag.php index bc25e777..a6942e88 100644 --- a/models/Tag.php +++ b/models/Tag.php @@ -260,6 +260,8 @@ class Tag id_parent = {int:id_parent}, id_asset_thumb = {int:id_asset_thumb}, tag = {string:tag}, + slug = {string:slug}, + description = {string:description}, count = {int:count} WHERE id_tag = {int:id_tag}', get_object_vars($this)); diff --git a/templates/FormView.php b/templates/FormView.php index 026698bb..7ab40ff7 100644 --- a/templates/FormView.php +++ b/templates/FormView.php @@ -24,8 +24,8 @@ class FormView extends SubTemplate { if (!empty($this->title)) echo ' -

', $this->title, '

-
'; +
+

', $this->title, '

'; foreach ($this->_subtemplates as $template) $template->html_main(); @@ -132,7 +132,7 @@ class FormView extends SubTemplate case 'numeric': echo ' - '; + '; break; case 'file':