diff --git a/app/Controllers/Forum/ForumController.php b/app/Controllers/Forum/ForumController.php index e205953..eb79ae7 100644 --- a/app/Controllers/Forum/ForumController.php +++ b/app/Controllers/Forum/ForumController.php @@ -160,4 +160,14 @@ class ForumController extends Controller return redirect(route('forums.forum', $forum->id)); } + + public function moveDestinations(): string + { + $forums = array_column(DB::table('forums') + ->where('forum_type', 0) + ->where('forum_id', '!=', config('forum.trash')) + ->get(['forum_id', 'forum_name']), 'forum_name', 'forum_id'); + + return $this->json($forums); + } } diff --git a/app/Controllers/Forum/TopicController.php b/app/Controllers/Forum/TopicController.php index 7a05645..282b00b 100644 --- a/app/Controllers/Forum/TopicController.php +++ b/app/Controllers/Forum/TopicController.php @@ -173,23 +173,26 @@ class TopicController extends Controller /** * Move a topic. * @param int $id + * @throws HttpRouteNotFoundException * @throws HttpMethodNotAllowedException * @return string */ public function move(int $id): string { extract($this->modBase($id)); - $dest_forum = new Forum($_REQUEST['forum_id'] ?? 0); + $dest_forum = new Forum($_POST['destination'] ?? 0); - if (!$forum->perms->topicMove - || $dest_forum->id === 0 - || $dest_forum->perms->view) { + if ($dest_forum->id === 0 || !$dest_forum->perms->view) { + throw new HttpRouteNotFoundException; + } + + if (!$forum->perms->topicMove) { throw new HttpMethodNotAllowedException; } $topic->move($dest_forum->id); - return redirect(route('forums.topic', $topic->id)); + return route('forums.topic', $topic->id); } /** diff --git a/resources/views/yuuno/forum/elements/forumMod.twig b/resources/views/yuuno/forum/elements/forumMod.twig index 054acb2..3f73b33 100644 --- a/resources/views/yuuno/forum/elements/forumMod.twig +++ b/resources/views/yuuno/forum/elements/forumMod.twig @@ -7,6 +7,9 @@ {% if forumLock is defined %} {% endif %} +{% if forumMove is defined %} + +{% endif %} {% if forumRestore is defined %} {% endif %} diff --git a/resources/views/yuuno/forum/topic.twig b/resources/views/yuuno/forum/topic.twig index b95ac57..9880014 100644 --- a/resources/views/yuuno/forum/topic.twig +++ b/resources/views/yuuno/forum/topic.twig @@ -29,6 +29,8 @@ {% endif %} {% if forum.perms.topicMove %} + {% set forumMove = true %} + {% if topic.oldForum %} {% set forumRestore = true %} {% endif %} @@ -55,6 +57,7 @@ function deletePost(id) { var confirm = new Sakura.Dialogue; confirm.SetType(Sakura.DialogueType.Confirm); + confirm.Title = "Deleting post " + id; confirm.Text = "Are you sure?"; confirm.AddCallback(Sakura.DialogueButton.Yes, function () { var deleter = new Sakura.AJAX; @@ -74,6 +77,75 @@ confirm.Display(); } + {% if forumMove is defined %} + function yuunoMoveTopic() { + var btn = Sakura.DOM.ID('topic-move-btn'), + moveDiag = new Sakura.Dialogue, + client = new Sakura.AJAX, + title = "Moving topic '{{ topic.title }}'", + unsetSpinner = function () { + Sakura.DOM.RemoveClass(btn, ['input__button--disabled']); + btn.innerHTML = ''; + }; + + Sakura.DOM.AddClass(btn, ['input__button--disabled']); + btn.innerHTML = ''; + + moveDiag.Title = title; + + client.SetUrl("{{ route('forums.move-destinations') }}"); + client.AddCallback(200, function () { + unsetSpinner(); + + moveDiag.SetType(Sakura.DialogueType.OKCancel); + moveDiag.Text = "Select a destination..."; + moveDiag.DropDownItems.AddObject(client.JSON()); + moveDiag.AddCallback(Sakura.DialogueButton.Ok, function () { + var returnDiag = new Sakura.Dialogue; + + client.Reset(); + client.SetUrl("{{ route('forums.topic.move', topic.id) }}"); + client.Form(); + client.SetSend({ + "session": Sakura.Config.SessionId, + "destination": moveDiag.DropDownSelected.Key + }); + + client.AddCallback(200, function () { + window.location.assign(client.Response()); + }); + client.AddCallback(404, function () { + returnDiag.Title = "Error"; + returnDiag.Text = "Destination forum doesn't exist!"; + returnDiag.Display(); + }); + client.AddCallback(403, function () { + returnDiag.Title = "Error"; + returnDiag.Text = "You aren't allowed to move topics!"; + returnDiag.Display(); + }); + client.AddCallback(0, function () { + returnDiag.Title = "Something happened!"; + returnDiag.Text = "Something happened!"; + returnDiag.Display(); + }); + + client.Start(Sakura.HTTPMethod.POST); + moveDiag.Close(); + }); + moveDiag.Display(); + }); + client.AddCallback(0, function () { + unsetSpinner(); + + moveDiag.Text = "Failed to fetch destinations."; + moveDiag.Display(); + }); + + client.Start(Sakura.HTTPMethod.GET); + } + {% endif %} + hljs.initHighlightingOnLoad(); {% endblock %} diff --git a/routes.php b/routes.php index 792f344..455ff01 100644 --- a/routes.php +++ b/routes.php @@ -104,12 +104,13 @@ Router::group(['before' => 'maintenance'], function () { Router::get('/{id:i}/lock', 'Forum.TopicController@lock', 'forums.topic.lock'); Router::get('/{id:i}/delete', 'Forum.TopicController@delete', 'forums.topic.delete'); Router::get('/{id:i}/restore', 'Forum.TopicController@restore', 'forums.topic.restore'); - Router::get('/{id:i}/move', 'Forum.TopicController@move', 'forums.topic.move'); + Router::post('/{id:i}/move', 'Forum.TopicController@move', 'forums.topic.move'); Router::post('/{id:i}/reply', 'Forum.TopicController@reply', 'forums.topic.reply'); }); // Forum Router::get('/', 'Forum.ForumController@index', 'forums.index'); + Router::get('/move-destinations', 'Forum.ForumController@moveDestinations', 'forums.move-destinations'); Router::get('/{id:i}', 'Forum.ForumController@forum', 'forums.forum'); Router::get('/{id:i}/mark', 'Forum.ForumController@markRead', 'forums.mark'); Router::get('/{id:i}/new', 'Forum.TopicController@create', 'forums.new');