Skip to content

Commit

Permalink
#180 Fixed breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Apr 3, 2022
1 parent 6a6aaca commit e0290f6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
Expand Up @@ -49,8 +49,14 @@ public function index(Session $session, ForumCounters $forumCounters): string
);
}

public function section(int $id, Session $session, ForumCounters $forumCounters, ForumTopicRepository $topicRepository, ?User $user): string
{
public function section(
int $id,
Session $session,
ForumCounters $forumCounters,
ForumTopicRepository $topicRepository,
?User $user,
ForumUtils $forumUtils,
): string {
$forumSettings = config('forum.settings');
try {
$currentSection = ForumSection::query()
Expand All @@ -65,7 +71,7 @@ public function section(int $id, Session $session, ForumCounters $forumCounters,
$session->remove(['fsort_id', 'fsort_users']);

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

$this->metaTagManager->setTitle($currentSection->name);
$this->metaTagManager->setPageTitle($currentSection->name);
Expand Down
50 changes: 35 additions & 15 deletions modules/johncms/forum/src/ForumUtils.php
Expand Up @@ -14,42 +14,62 @@

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Johncms\Forum\Models\ForumMessage;
use Johncms\Forum\Models\ForumSection;
use Johncms\Forum\Models\ForumTopic;
use Johncms\Http\Request;
use Johncms\NavChain;
use Johncms\System\Legacy\Tools;
use Johncms\Users\User;
use Johncms\View\Render;

class ForumUtils
{
private NavChain $navChain;

public function __construct(NavChain $navChain)
{
$this->navChain = $navChain;
}

/**
* Building breadcrumbs
*
* @param int $parent
* @param string $current_item_name
* @param string $current_item_url
* @param string $currentItemName
* @param string $currentItemUrl
*/
public static function buildBreadcrumbs(int $parent = 0, string $current_item_name = '', string $current_item_url = ''): void
public function buildBreadcrumbs(int $parent = 0, string $currentItemName = '', string $currentItemUrl = ''): void
{
/** @var Tools $tools */
$tools = di(Tools::class);
/** @var NavChain $nav_chain */
$nav_chain = di(NavChain::class);

$tree = [];
$tools->getSections($tree, $parent);
foreach ($tree as $item) {
$nav_chain->add($item['name'], '/forum/?' . ($item['section_type'] === 1 ? 'type=topics&' : '') . 'id=' . $item['id']);
if ($parent) {
$chain = $this->getSectionsChain($parent);
foreach ($chain as $section) {
$this->navChain->add($section->name, $section->url);
}
}

if (! empty($current_item_name)) {
$nav_chain->add($current_item_name, $current_item_url);
if (! empty($currentItemName)) {
$this->navChain->add($currentItemName, $currentItemUrl);
}
}

/**
* @param int $sectionId
* @param array $sections
* @return ForumSection[]
*/
public function getSectionsChain(int $sectionId, array &$sections = []): array
{
$section = ForumSection::query()->find($sectionId);
$sections[] = $section;
if ($section->parent) {
$this->getSectionsChain($section->parent, $sections);
}
krsort($sections);
return $sections;
}

/**
* Page not found
*
* @return no-return
*/
public static function notFound()
Expand Down

0 comments on commit e0290f6

Please sign in to comment.