Skip to content

Commit

Permalink
#180 Added a page with forum topics
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Apr 3, 2022
1 parent adb6ba3 commit 6a6aaca
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 181 deletions.
76 changes: 55 additions & 21 deletions modules/johncms/forum/src/Controllers/ForumSectionsController.php
Expand Up @@ -10,7 +10,10 @@
use Johncms\Forum\ForumUtils;
use Johncms\Forum\Models\ForumFile;
use Johncms\Forum\Models\ForumSection;
use Johncms\Forum\Topics\ForumTopicRepository;
use Johncms\Forum\Topics\Resources\TopicResource;
use Johncms\Http\Session;
use Johncms\Users\User;
use Johncms\Utility\Numbers;

class ForumSectionsController extends BaseForumController
Expand Down Expand Up @@ -46,7 +49,7 @@ public function index(Session $session, ForumCounters $forumCounters): string
);
}

public function section(int $id, Session $session, ForumCounters $forumCounters): string
public function section(int $id, Session $session, ForumCounters $forumCounters, ForumTopicRepository $topicRepository, ?User $user): string
{
$forumSettings = config('forum.settings');
try {
Expand All @@ -69,26 +72,57 @@ public function section(int $id, Session $session, ForumCounters $forumCounters)
$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();
$templateBaseData = [
'id' => $currentSection->id,
'online' => [
'users' => $forumCounters->onlineUsers(),
'guests' => $forumCounters->onlineGuests(),
],
'files_count' => $forumSettings['file_counters'] ? Numbers::formatNumber($currentSection->category_files_count) : 0,
'unread_count' => Numbers::formatNumber($forumCounters->unreadMessages()),
];

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()),
]
);
// If the section contains topics, then show a list of topics
if ($currentSection->section_type) {
$topics = $topicRepository->getTopics($id)->paginate();
$resource = TopicResource::createFromCollection($topics);

// Access to create topics
$createAccess = false;
if (($user && ! $user->hasBan(['forum_read_only', 'forum_create_topics'])) || $user?->hasAnyRole()) {
$createAccess = true;
}

return $this->render->render(
'forum::topics',
array_merge(
$templateBaseData,
[
'pagination' => $topics->render(),
'create_access' => $createAccess,
'topics' => $resource->getItems(),
'total' => $topics->total(),
]
)
);
} else {
// List of forum sections
$sections = (new ForumSection())
->withCount(['subsections', 'topics'])
->where('parent', '=', $id)
->orderBy('sort')
->get();

return $this->render->render(
'forum::section',
array_merge(
$templateBaseData,
[
'sections' => $sections,
'total' => $sections->count(),
]
)
);
}
}
}
13 changes: 6 additions & 7 deletions modules/johncms/forum/src/Models/ForumSection.php
Expand Up @@ -29,18 +29,17 @@
* @property string $meta_keywords
* @property int $sort
* @property int $access
* @property int $section_type
* @property int $old_id
* @property int $section_type - (1 - Topics, 0 or NULL - sections)
*
* @property string $url - URL раздела
* @property string $url - URL раздела
* @property string $subsections_count - Количество подразделов (доступно только при вызове withCount('subsections'))
* @property string $topics_count - Количество подразделов (доступно только при вызове withCount('subsections'))
* @property string $topics_count - Количество подразделов (доступно только при вызове withCount('subsections'))
* @property ForumSection $subsections - Subsections
* @property ForumTopic $topics - Topics
* @property ForumTopic $topics - Topics
* @property ForumFile $category_files - Files of category
* @property ForumFile $section_files - Files of section
* @property ForumFile $section_files - Files of section
* @property int $category_files_count - Count files of category
* @property int $section_files_count - Count files of section
* @property int $section_files_count - Count files of section
*/
class ForumSection extends Model
{
Expand Down
7 changes: 3 additions & 4 deletions modules/johncms/forum/src/Models/ForumTopic.php
Expand Up @@ -182,14 +182,13 @@ static function (Builder $builder) {
*/
public function scopeRead(Builder $query): Builder
{
/** @var User $user */
$user = di(User::class);
if ($user->is_valid) {
if ($user) {
return $query->selectSub(
(new ForumUnread())
->selectRaw('count(*)')
->whereRaw('cms_forum_rdm.time >= forum_topic.last_post_date')
->whereRaw('cms_forum_rdm.topic_id = forum_topic.id')
->whereRaw('forum_read.time >= forum_topic.last_post_date')
->whereRaw('forum_read.topic_id = forum_topic.id')
->where('user_id', '=', $user->id),
'read'
)
Expand Down
11 changes: 11 additions & 0 deletions modules/johncms/forum/src/Topics/ForumTopicRepository.php
Expand Up @@ -40,4 +40,15 @@ public function getUnread(): ?Builder
})
->orderBy('last_post_date');
}

public function getTopics(?int $sectionId = null): ?Builder
{
return ForumTopic::query()
->read()
->when($sectionId, function (Builder $builder) use ($sectionId) {
return $builder->where('section_id', '=', $sectionId);
})
->orderByDesc('pinned')
->orderByDesc('last_post_date');
}
}
35 changes: 35 additions & 0 deletions modules/johncms/forum/src/Topics/Resources/TopicResource.php
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Johncms\Forum\Topics\Resources;

use Johncms\Forum\Models\ForumTopic;
use Johncms\Http\Resources\AbstractResource;
use Johncms\Users\User;

/**
* @mixin ForumTopic
*/
class TopicResource extends AbstractResource
{
public function toArray(): array
{
$user = di(User::class);
return [
'name' => $this->name,
'user_name' => $this->user_name,
'post_count' => $user?->hasAnyRole() ? $this->mod_post_count : $this->post_count,
'last_post_author' => $user?->hasAnyRole() ? $this->mod_last_post_author_name : $this->last_post_author_name,
'last_post_date' => $user?->hasAnyRole() ? format_date($this->mod_last_post_date) : format_date($this->last_post_date),
'pinned' => $this->pinned,
'has_poll' => $this->has_poll,
'deleted' => $this->deleted,
'closed' => $this->closed,
'has_icons' => $this->has_icons,
'url' => $this->url,
'last_page_url' => $this->last_page_url,
'unread' => $this->unread,
];
}
}
27 changes: 13 additions & 14 deletions modules/johncms/forum/templates/topics.phtml
Expand Up @@ -15,8 +15,7 @@
* @var $unread_count
* @var $create_access
* @var $pagination
* @var \Johncms\Forum\Models\ForumTopic $topics
* @var \Johncms\Forum\Models\ForumTopic $topic
* @var array $topics
*/

$this->layout('system::layout/default');
Expand Down Expand Up @@ -55,28 +54,28 @@ $this->layout('system::layout/default');
<div class="d-flex align-items-center w-100">
<?php if ($topic['has_icons']): ?>
<div class="topic-icons d-flex align-items-center">
<?php if ($topic->pinned): ?>
<?php if ($topic['pinned']): ?>
<div class="me-1" title="<?= __('Pinned topic') ?>">
<svg class="icon">
<use xlink:href="<?= $this->asset('icons/sprite.svg') ?>#pin"/>
</svg>
</div>
<?php endif; ?>
<?php if ($topic->has_poll): ?>
<?php if ($topic['has_poll']): ?>
<div class="me-1" title="<?= __('Topic has poll') ?>">
<svg class="icon">
<use xlink:href="<?= $this->asset('icons/sprite.svg') ?>#bar-chart"/>
</svg>
</div>
<?php endif; ?>
<?php if ($topic->closed): ?>
<?php if ($topic['closed']): ?>
<div class="me-1" title="<?= __('Closed topic') ?>">
<svg class="icon">
<use xlink:href="<?= $this->asset('icons/sprite.svg') ?>#lock"/>
</svg>
</div>
<?php endif; ?>
<?php if ($topic->deleted): ?>
<?php if ($topic['deleted']): ?>
<div class="me-1" title="<?= __('Deleted topic') ?>">
<svg class="icon">
<use xlink:href="<?= $this->asset('icons/sprite.svg') ?>#x"/>
Expand All @@ -85,25 +84,25 @@ $this->layout('system::layout/default');
<?php endif; ?>
</div>
<?php endif; ?>
<a href="<?= $topic->url ?>" class="section-name flex-grow-1 flex-md-grow-0"><?= $topic->name ?></a>
<?php if ($topic->deleted): ?>
<a href="<?= $topic['url'] ?>" class="section-name flex-grow-1 flex-md-grow-0"><?= $topic['name'] ?></a>
<?php if ($topic['deleted']): ?>
<div class="ms-3">
<span class="badge bg-danger"><?= __('Deleted topic') ?></span>
</div>
<?php else: ?>
<?php if (! empty($topic->last_page_url)): ?>
<a href="<?= $topic->last_page_url ?>" title="<?= __('Last page') ?>">
<span class="badge rounded-pill <?= $topic->unread ? 'bg-danger' : 'badge-light border' ?> ms-3"><?= $topic->show_posts_count ?></span>
<?php if (! empty($topic['last_page_url'])): ?>
<a href="<?= $topic['last_page_url'] ?>" title="<?= __('Last page') ?>">
<span class="badge rounded-pill <?= $topic['unread'] ? 'bg-danger' : 'bg-light border' ?> ms-3"><?= $topic['post_count'] ?></span>
</a>
<?php else: ?>
<span class="badge rounded-pill <?= $topic->unread ? 'bg-danger' : 'badge-light border' ?> ms-3"><?= $topic->show_posts_count ?></span>
<span class="badge rounded-pill <?= $topic['unread'] ? 'bg-danger' : 'bg-light border' ?> ms-3"><?= $topic['post_count'] ?></span>
<?php endif ?>
<?php endif; ?>
</div>
</div>
<div class="small pt-2 text-muted">
<?= __('Author') ?>: <?= $topic->user_name ?>,
<?= __('Last post') ?>: <?= $topic->show_last_post_date ?>, <?= $topic->show_last_author ?>
<?= __('Author') ?>: <?= $topic['user_name'] ?>,
<?= __('Last post') ?>: <?= $topic['last_post_date'] ?>, <?= $topic['last_post_author'] ?>
</div>
</div>
<?php endforeach; ?>
Expand Down
2 changes: 1 addition & 1 deletion themes/default/assets/css/app.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion themes/default/assets/css/app.rtl.css

Large diffs are not rendered by default.

0 comments on commit 6a6aaca

Please sign in to comment.