Skip to content

Commit

Permalink
#180 Added the ability to close and open topic
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Jun 8, 2022
1 parent 42da802 commit 7935f9d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 36 deletions.
8 changes: 8 additions & 0 deletions modules/johncms/forum/config/routes.php
Expand Up @@ -42,6 +42,14 @@
->middleware(new HasPermissionMiddleware(ForumPermissions::MANAGE_TOPICS))
->setName('forum.confirmDelete');

$route->get('/close-topic/{topicId:number}[/]', [ForumTopicsController::class, 'close'])
->middleware(new HasPermissionMiddleware(ForumPermissions::MANAGE_TOPICS))
->setName('forum.closeTopic');

$route->get('/open-topic/{topicId:number}[/]', [ForumTopicsController::class, 'open'])
->middleware(new HasPermissionMiddleware(ForumPermissions::MANAGE_TOPICS))
->setName('forum.openTopic');

// Delete message
$route->get('/delete-post/{id:number}[/]', [ForumMessagesController::class, 'delete'])->setName('forum.deletePost');
$route->post('/delete-post-confirm/{id:number}[/]', [ForumMessagesController::class, 'confirmDelete'])->setName('forum.confirmDeletePost');
Expand Down
33 changes: 0 additions & 33 deletions modules/johncms/forum/includes/close.php

This file was deleted.

22 changes: 21 additions & 1 deletion modules/johncms/forum/src/Controllers/ForumTopicsController.php
Expand Up @@ -317,7 +317,7 @@ public function edit(int $topicId, ForumUtils $forumUtils, Session $session): st
*/
public function changeTopic(int $topicId, ForumTopicService $topicService): RedirectResponse
{
$topic = ForumTopic::query()->where('id', $topicId)->firstOrFail();
$topic = ForumTopic::query()->findOrFail($topicId);
$form = new CreateTopicForm($topic->toArray());
$form->setSectionId($topic->section_id);
try {
Expand Down Expand Up @@ -370,4 +370,24 @@ public function confirmDelete(int $topicId, User $user, Request $request, ForumT

return new RedirectResponse($topic->section->url);
}

/**
* Close the topic
*/
public function close(int $topicId, ForumTopicService $topicService): RedirectResponse
{
$topic = ForumTopic::query()->findOrFail($topicId);
$topicService->close($topic);
return new RedirectResponse($topic->url);
}

/**
* Open the topic
*/
public function open(int $topicId, ForumTopicService $topicService): RedirectResponse
{
$topic = ForumTopic::query()->findOrFail($topicId);
$topicService->open($topic);
return new RedirectResponse($topic->url);
}
}
22 changes: 22 additions & 0 deletions modules/johncms/forum/src/Services/ForumTopicService.php
Expand Up @@ -202,4 +202,26 @@ public function hide(int | ForumTopic $topic): void
(new ForumFile())->where('topic', $topic->id)->update(['del' => 1]);
});
}

/**
* Mark the topic as closed
*/
public function close(int | ForumTopic $topic): void
{
if (is_int($topic)) {
$topic = ForumTopic::query()->findOrFail($topic);
}
$topic->update(['closed' => true, 'closed_by' => $this->user->display_name]);
}

/**
* Open the topic
*/
public function open(int | ForumTopic $topic): void
{
if (is_int($topic)) {
$topic = ForumTopic::query()->findOrFail($topic);
}
$topic->update(['closed' => null, 'closed_by' => null]);
}
}
4 changes: 2 additions & 2 deletions modules/johncms/forum/templates/topic.phtml
Expand Up @@ -387,9 +387,9 @@ $this->layout('system::layout/default');
<a class="dropdown-item" href="<?= route('forum.editTopic', ['topicId' => $id]) ?>"><?= __('Change the topic') ?></a>

<?php if ($topic->closed): ?>
<a class="dropdown-item" href="/forum/?act=close&amp;id=<?= $id ?>"><?= __('Open Topic') ?></a>
<a class="dropdown-item" href="<?= route('forum.openTopic', ['topicId' => $id]) ?>"><?= __('Open Topic') ?></a>
<?php else: ?>
<a class="dropdown-item" href="/forum/?act=close&amp;id=<?= $id ?>&amp;closed"><?= __('Close Topic') ?></a>
<a class="dropdown-item" href="<?= route('forum.closeTopic', ['topicId' => $id]) ?>"><?= __('Close Topic') ?></a>
<?php endif; ?>

<?php if ($topic->deleted): ?>
Expand Down

0 comments on commit 7935f9d

Please sign in to comment.