Skip to content

Commit

Permalink
#180 added forum section page
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Apr 3, 2022
1 parent f501e0d commit 83acd97
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 16 deletions.
5 changes: 5 additions & 0 deletions modules/johncms/forum/config/routes.php
@@ -1,10 +1,15 @@
<?php

use Johncms\Forum\Controllers\ForumSectionsController;
use Johncms\Forum\Controllers\ForumTopicsController;
use Johncms\Forum\Controllers\LatestTopicsController;
use League\Route\Router;

return function (Router $router) {
$router->get('/forum[/]', [ForumSectionsController::class, 'index'])->setName('forum.index');
$router->get('/forum/unread[/]', [LatestTopicsController::class, 'unread'])->setName('forum.unread');

// Sections, topic
$router->get('/forum/{sectionName:slug}-{id:number}[/]', [ForumSectionsController::class, 'section'])->setName('forum.section');
$router->get('/forum/t/{topicName:slug}-{id:number}[/]', [ForumTopicsController::class, 'showTopic'])->setName('forum.topic');
};
64 changes: 57 additions & 7 deletions modules/johncms/forum/src/Controllers/ForumSectionsController.php
Expand Up @@ -4,14 +4,18 @@

namespace Johncms\Forum\Controllers;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Johncms\Forum\ForumCounters;
use Johncms\Forum\ForumUtils;
use Johncms\Forum\Models\ForumFile;
use Johncms\Forum\Models\ForumSection;
use Johncms\Http\Session;
use Johncms\Utility\Numbers;

class ForumSectionsController extends BaseForumController
{
public function index(ForumCounters $forumCounters): string
public function index(Session $session, ForumCounters $forumCounters): string
{
// Forum categories
$sections = (new ForumSection())
Expand All @@ -22,21 +26,67 @@ public function index(ForumCounters $forumCounters): string
->orderBy('sort')
->get();

unset($_SESSION['fsort_id'], $_SESSION['fsort_users']);
$session->remove(['fsort_id', 'fsort_users']);

$forum_settings = di('config')['forum']['settings'];
$this->metaTagManager->setKeywords($forum_settings['forum_keywords']);
$this->metaTagManager->setDescription($forum_settings['forum_description']);
$forumSettings = config('forum.settings');
$this->metaTagManager->setKeywords($forumSettings['forum_keywords']);
$this->metaTagManager->setDescription($forumSettings['forum_description']);

return $this->render->render(
'forum::index',
[
'sections' => $sections,
'online' => [
'users' => $forumCounters->onlineUsers(),
'users' => $forumCounters->onlineUsers(),
'guests' => $forumCounters->onlineGuests(),
],
'files_count' => $forum_settings['file_counters'] ? Numbers::formatNumber((new ForumFile())->count()) : 0,
'files_count' => $forumSettings['file_counters'] ? Numbers::formatNumber((new ForumFile())->count()) : 0,
'unread_count' => Numbers::formatNumber($forumCounters->unreadMessages()),
]
);
}

public function section(int $id, Session $session, ForumCounters $forumCounters): string
{
$forumSettings = config('forum.settings');
try {
$currentSection = ForumSection::query()
->when($forumSettings['file_counters'], function (Builder $builder) {
return $builder->withCount('categoryFiles');
})
->findOrFail($id);
} catch (ModelNotFoundException) {
ForumUtils::notFound();
}

$session->remove(['fsort_id', 'fsort_users']);

// Build breadcrumbs
ForumUtils::buildBreadcrumbs($currentSection->parent, $currentSection->name);

$this->metaTagManager->setTitle($currentSection->name);
$this->metaTagManager->setPageTitle($currentSection->name);
$this->metaTagManager->setKeywords($currentSection->calculated_meta_keywords);
$this->metaTagManager->setDescription($currentSection->calculated_meta_description);

// List of forum sections
$sections = (new ForumSection())
->withCount(['subsections', 'topics'])
->where('parent', '=', $id)
->orderBy('sort')
->get();

return $this->render->render(
'forum::section',
[
'id' => $currentSection->id,
'sections' => $sections,
'online' => [
'users' => $forumCounters->onlineUsers(),
'guests' => $forumCounters->onlineGuests(),
],
'total' => $sections->count(),
'files_count' => $forumSettings['file_counters'] ? Numbers::formatNumber($currentSection->category_files_count) : 0,
'unread_count' => Numbers::formatNumber($forumCounters->unreadMessages()),
]
);
Expand Down
16 changes: 16 additions & 0 deletions modules/johncms/forum/src/Controllers/ForumTopicsController.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Johncms\Forum\Controllers;

class ForumTopicsController extends BaseForumController
{
/**
* Show topic
*/
public function showTopic(string $topicName, int $id): string
{
return 'In progress';
}
}
3 changes: 2 additions & 1 deletion modules/johncms/forum/src/ForumUtils.php
Expand Up @@ -50,8 +50,9 @@ public static function buildBreadcrumbs(int $parent = 0, string $current_item_na

/**
* Page not found
* @return no-return
*/
public static function notFound(): void
public static function notFound()
{
checkRedirect();
$view = di(Render::class);
Expand Down
10 changes: 2 additions & 8 deletions modules/johncms/forum/templates/section.phtml
Expand Up @@ -18,13 +18,7 @@
* @var $unread_count
*/

$this->layout(
'system::layout/default',
[
'title' => $title,
'page_title' => $page_title,
]
);
$this->layout('system::layout/default');
?>

<?= $this->fetch(
Expand Down Expand Up @@ -52,7 +46,7 @@ $this->layout(
<div class="section-header">
<div class="d-flex align-items-center">
<a href="<?= $section->url ?>" class="section-name"><?= $section->name ?></a>
<span class="badge rounded-pill badge-light border ms-3"><?= ($section->section_type === 1 ? $section->topics_count : $section->subsections_count) ?></span>
<span class="badge rounded-pill bg-light text-primary border ms-3"><?= ($section->section_type === 1 ? $section->topics_count : $section->subsections_count) ?></span>
</div>
</div>
<?php if (! empty($section->description)): ?>
Expand Down

0 comments on commit 83acd97

Please sign in to comment.