From 2a25434862bffd58701b3db828c1f1974de52fda Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sun, 5 Nov 2017 17:13:52 +0100 Subject: [PATCH] Implement basic tag management. --- TODO.md | 2 - controllers/EditTag.php | 152 +++++++++++++++++++++++++++++++++++++ controllers/ManageTags.php | 16 +++- models/Dispatcher.php | 1 + 4 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 controllers/EditTag.php diff --git a/TODO.md b/TODO.md index d870821..b592b32 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,5 @@ TODO: -* Album/tag management - * Alleen persoonstags tonen bij foto's. * Gebruikers persoonstags laten verwijderen vanaf fotopagina. diff --git a/controllers/EditTag.php b/controllers/EditTag.php new file mode 100644 index 0000000..e8995ef --- /dev/null +++ b/controllers/EditTag.php @@ -0,0 +1,152 @@ +isAdmin()) + throw new NotAllowedException(); + + $id_tag = isset($_GET['id']) ? (int) $_GET['id'] : 0; + if (empty($id_tag) && !isset($_GET['add'])) + throw new UnexpectedValueException('Requested tag not found or not requesting a new tag.'); + + // Adding an tag? + if (isset($_GET['add'])) + { + parent::__construct('Add a new tag'); + $form_title = 'Add a new tag'; + $this->page->addClass('edittag'); + } + // Deleting one? + elseif (isset($_GET['delete'])) + { + // So far so good? + $tag = Tag::fromId($id_tag); + if (Session::validateSession('get') && $tag->kind !== 'Album' && $tag->delete()) + { + header('Location: ' . BASEURL . '/managetags/'); + exit; + } + else + trigger_error('Cannot delete tag: an error occured while processing the request.', E_USER_ERROR); + } + // Editing one, then, surely. + else + { + $tag = Tag::fromId($id_tag); + if ($tag->kind === 'Album') + trigger_error('Cannot edit tag: is actually an album.', E_USER_ERROR); + + parent::__construct('Edit tag \'' . $tag->tag . '\''); + $form_title = 'Edit tag \'' . $tag->tag . '\''; + $this->page->addClass('edittag'); + } + + // Session checking! + if (empty($_POST)) + Session::resetSessionToken(); + else + Session::validateSession(); + + if ($id_tag) + $after_form = 'Delete tag'; + elseif (!$id_tag) + $after_form = ''; + + $form = new Form([ + 'request_url' => BASEURL . '/edittag/?' . ($id_tag ? 'id=' . $id_tag : 'add'), + 'content_below' => $after_form, + 'fields' => [ + 'id_parent' => [ + 'type' => 'numeric', + 'label' => 'Parent tag ID', + ], + 'id_asset_thumb' => [ + 'type' => 'numeric', + 'label' => 'Thumbnail asset ID', + 'is_optional' => true, + ], + 'kind' => [ + 'type' => 'select', + 'label' => 'Kind of tag', + 'options' => [ + 'Location' => 'Location', + 'Person' => 'Person', + ], + ], + 'tag' => [ + 'type' => 'text', + 'label' => 'Tag 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($tag) : $_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', '=>' => '', "'" => "", ":"=> "", '/' => '-', '\\' => '-']); + + // Creating a new tag? + if (!$id_tag) + { + $return = Tag::createNew($data); + if ($return === false) + return $formview->adopt(new Alert('Cannot create this tag', 'Something went wrong while creating the tag...', 'error')); + + if (isset($_POST['submit_and_new'])) + { + header('Location: ' . BASEURL . '/edittag/?add'); + exit; + } + } + // Just updating? + else + { + foreach ($data as $key => $value) + $tag->$key = $value; + + $tag->save(); + } + + // Redirect to the tag management page. + header('Location: ' . BASEURL . '/managetags/'); + exit; + } + } +} diff --git a/controllers/ManageTags.php b/controllers/ManageTags.php index bd561d7..476e900 100644 --- a/controllers/ManageTags.php +++ b/controllers/ManageTags.php @@ -15,6 +15,17 @@ class ManageTags extends HTMLController throw new NotAllowedException(); $options = [ + 'form' => [ + 'action' => BASEURL . '/edittag/', + 'method' => 'get', + 'class' => 'floatright', + 'buttons' => [ + 'add' => [ + 'type' => 'submit', + 'caption' => 'Add new tag', + ], + ], + ], 'columns' => [ 'id_post' => [ 'value' => 'id_tag', @@ -25,7 +36,7 @@ class ManageTags extends HTMLController 'header' => 'Tag', 'is_sortable' => true, 'parse' => [ - 'link' => BASEURL . '/managetag/?id={ID_TAG}', + 'link' => BASEURL . '/edittag/?id={ID_TAG}', 'data' => 'tag', ], ], @@ -33,7 +44,7 @@ class ManageTags extends HTMLController 'header' => 'Slug', 'is_sortable' => true, 'parse' => [ - 'link' => BASEURL . '/managetag/?id={ID_TAG}', + 'link' => BASEURL . '/edittag/?id={ID_TAG}', 'data' => 'slug', ], ], @@ -54,6 +65,7 @@ class ManageTags extends HTMLController 'title' => 'Manage tags', 'no_items_label' => 'No tags meet the requirements of the current filter.', 'items_per_page' => 30, + 'index_class' => 'floatleft', 'base_url' => BASEURL . '/managetags/', 'get_data' => function($offset = 0, $limit = 30, $order = '', $direction = 'up') { if (!in_array($order, ['id_post', 'tag', 'slug', 'kind', 'count'])) diff --git a/models/Dispatcher.php b/models/Dispatcher.php index bf5a08b..e6e852a 100644 --- a/models/Dispatcher.php +++ b/models/Dispatcher.php @@ -14,6 +14,7 @@ class Dispatcher 'albums' => 'ViewPhotoAlbums', 'editalbum' => 'EditAlbum', 'editasset' => 'EditAsset', + 'edittag' => 'EditTag', 'edituser' => 'EditUser', 'login' => 'Login', 'logout' => 'Logout',