Skip to content

Commit

Permalink
#180 Refactoring and fixes for topics
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Apr 3, 2022
1 parent ab618d8 commit e116af0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 43 deletions.
57 changes: 26 additions & 31 deletions modules/johncms/forum/src/Models/TopicMutators.php
Expand Up @@ -12,8 +12,11 @@

namespace Johncms\Forum\Models;

use Illuminate\Support\Str;
use Johncms\System\Legacy\Tools;
use Johncms\Users\User;
use Johncms\Utility\Numbers;
use Johncms\Utility\Pagination;

/**
* Trait TopicMutators
Expand All @@ -29,93 +32,85 @@
trait TopicMutators
{
/**
* Ссылка на страницу просмотра топика
* Topic url
*
* @return string
*/
public function getUrlAttribute(): string
{
return '/forum/?type=topic&id=' . $this->id;
return route('forum.topic', ['id' => $this->id, 'topicName' => Str::slug($this->name)]);
}

/**
* Поличество постов для отображения
* The number of posts to display
*
* @return string
*/
public function getShowPostsCountAttribute(): string
{
if ($this->current_user->rights >= 7) {
return (string) $this->tools->formatNumber($this->mod_post_count);
if ($this->current_user?->hasAnyRole()) {
return (string) Numbers::formatNumber($this->mod_post_count);
}
return (string) $this->tools->formatNumber($this->post_count);
return (string) Numbers::formatNumber($this->post_count);
}

/**
* Автор поста для отображения
* The name of the author to display
*
* @return string
*/
public function getShowLastAuthorAttribute(): string
{
if ($this->current_user->rights >= 7) {
if ($this->current_user?->hasAnyRole()) {
return $this->mod_last_post_author_name;
}
return $this->last_post_author_name;
}

/**
* Дата последнего поста для отображения
* The date of the last post to display
*
* @return string
*/
public function getShowLastPostDateAttribute(): string
{
if ($this->current_user->rights >= 7) {
return $this->tools->displayDate($this->mod_last_post_date);
if ($this->current_user?->hasAnyRole()) {
return format_date($this->mod_last_post_date);
}
return $this->tools->displayDate($this->last_post_date);
return format_date($this->last_post_date);
}

/**
* Ссылка на последнюю страницу топика
* Url to the last page of the topic
*
* @return string
*/
public function getLastPageUrlAttribute(): string
{
// TODO: Change it
/*if ($this->current_user->rights >= 7) {
$page = ceil($this->mod_post_count / $this->current_user->set_user->kmess);
} else {
$page = ceil($this->post_count / $this->current_user->set_user->kmess);
$total = $this->current_user?->hasAnyRole() ? $this->mod_post_count : $this->post_count;
$pagination = new Pagination($total);
$lastPage = $pagination->getTotalPages();
$query = [];
if ($lastPage > 1) {
$query['page'] = $lastPage;
}

if ($page > 1) {
return '/forum/?type=topic&id=' . $this->id . '&page=' . $page;
}*/

return '';
return route('forum.topic', ['id' => $this->id, 'topicName' => Str::slug($this->name)], $query);
}

/**
* Ссылка на последнюю страницу топика
*
* @return bool
*/
public function getHasIconsAttribute(): bool
{
return ($this->pinned || $this->has_poll || $this->closed || $this->deleted);
}

/**
* Признак непрочитанного топика
* The mark of an unread topic
*
* @return bool
*/
public function getUnreadAttribute(): bool
{
return $this->read !== null && $this->read === 0;
return $this->read === 0;
}

/**
Expand All @@ -125,7 +120,7 @@ public function getUnreadAttribute(): bool
*/
public function getFormattedViewCountAttribute(): string
{
return (string) $this->tools->formatNumber($this->view_count);
return (string) Numbers::formatNumber($this->view_count);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions modules/johncms/forum/src/Topics/Resources/TopicResource.php
Expand Up @@ -6,7 +6,6 @@

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

/**
* @mixin ForumTopic
Expand All @@ -15,13 +14,12 @@ 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),
'post_count' => $this->show_posts_count,
'last_post_author' => $this->show_last_author,
'last_post_date' => $this->show_last_post_date,
'pinned' => $this->pinned,
'has_poll' => $this->has_poll,
'deleted' => $this->deleted,
Expand Down
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Support\Str;
use Johncms\Forum\Models\ForumTopic;
use Johncms\Http\Resources\AbstractResource;
use Johncms\Users\User;

/**
* @mixin ForumTopic
Expand All @@ -19,13 +18,12 @@ class UnreadTopicResource 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),
'post_count' => $this->show_posts_count,
'last_post_author' => $this->show_last_author,
'last_post_date' => $this->show_last_post_date,
'pinned' => $this->pinned,
'has_poll' => $this->has_poll,
'deleted' => $this->deleted,
Expand Down
6 changes: 4 additions & 2 deletions system/helpers.php
Expand Up @@ -188,13 +188,15 @@ function redirect(string $url)
*
* @param string $route_name
* @param array $params
* @param array $queryParams
* @return string
*/
function route(string $route_name, array $params = []): string
function route(string $route_name, array $params = [], array $queryParams = []): string
{
$router = di(RouterFactory::class);
$route = $router->getRouter()->getNamedRoute($route_name)->getPath($params);
return str_replace('//', '/', $route);
$queryString = http_build_query($queryParams);
return str_replace('//', '/', $route) . (! empty($queryString) ? '?' . $queryString : '');
}

/**
Expand Down

0 comments on commit e116af0

Please sign in to comment.