2020-08-05 16:10:39 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-05 16:10:39 +00:00
|
|
|
/**
|
|
|
|
* Generates and renders a breadcrumb based on the current url segments
|
|
|
|
*
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2021 Ad Aures
|
2020-08-05 16:10:39 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Libraries;
|
|
|
|
|
|
|
|
class Breadcrumb
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* List of breadcrumb links.
|
|
|
|
*
|
2021-05-19 16:35:13 +00:00
|
|
|
* $links = [ 'text' => 'Example Link', 'href' => 'https://example.com/', ]
|
2021-05-14 17:59:35 +00:00
|
|
|
*
|
|
|
|
* @var array<array<string, string>>
|
2020-08-05 16:10:39 +00:00
|
|
|
*/
|
2021-05-18 17:16:36 +00:00
|
|
|
protected array $links = [];
|
2020-08-05 16:10:39 +00:00
|
|
|
|
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* Initializes the Breadcrumb object using the segments from current_url by populating the $links property with text
|
|
|
|
* and href data
|
2020-08-05 16:10:39 +00:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$uri = '';
|
|
|
|
foreach (current_url(true)->getSegments() as $segment) {
|
|
|
|
$uri .= '/' . $segment;
|
2024-05-09 17:55:41 +00:00
|
|
|
$link = [
|
2020-08-05 16:10:39 +00:00
|
|
|
'text' => is_numeric($segment)
|
|
|
|
? $segment
|
|
|
|
: lang('Breadcrumb.' . $segment),
|
|
|
|
'href' => base_url($uri),
|
2021-05-06 14:00:48 +00:00
|
|
|
];
|
2024-05-09 17:55:41 +00:00
|
|
|
|
|
|
|
if (is_numeric($segment)) {
|
|
|
|
$this->links[] = $link;
|
|
|
|
} else {
|
|
|
|
$this->links[$segment] = $link;
|
|
|
|
}
|
2020-08-05 16:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* Replaces all numeric text in breadcrumb's $link property with new params at same position
|
2020-08-05 16:10:39 +00:00
|
|
|
*
|
2021-05-19 16:35:13 +00:00
|
|
|
* Given a breadcrumb with numeric params, this function replaces them with the values provided in $newParams
|
2020-08-05 16:10:39 +00:00
|
|
|
*
|
2024-05-09 17:55:41 +00:00
|
|
|
* Example with `Home / podcasts / 1 / episodes / 1 / foo`
|
2020-08-05 16:10:39 +00:00
|
|
|
*
|
2024-05-09 17:55:41 +00:00
|
|
|
* $newParams = [ 0 => 'bar', 1 => 'baz', 'foo' => 'I Pity The Foo' ] replaceParams($newParams);
|
2020-08-05 16:10:39 +00:00
|
|
|
*
|
2024-05-09 17:55:41 +00:00
|
|
|
* The breadcrumb is now `Home / podcasts / foo / episodes / bar / I Pity The Foo`
|
2021-05-14 17:59:35 +00:00
|
|
|
*
|
2024-05-13 16:17:18 +00:00
|
|
|
* @param array<string|int,string> $newParams
|
2020-08-05 16:10:39 +00:00
|
|
|
*/
|
2021-05-06 14:00:48 +00:00
|
|
|
public function replaceParams(array $newParams): void
|
2020-08-05 16:10:39 +00:00
|
|
|
{
|
2024-05-09 17:55:41 +00:00
|
|
|
foreach ($newParams as $key => $newValue) {
|
|
|
|
if (array_key_exists($key, $this->links)) {
|
|
|
|
$this->links[$key]['text'] = $newValue;
|
2020-08-05 16:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the breadcrumb object as an accessible html breadcrumb nav
|
|
|
|
*/
|
2021-05-14 17:59:35 +00:00
|
|
|
public function render(string $class = null): string
|
2020-08-05 16:10:39 +00:00
|
|
|
{
|
|
|
|
$listItems = '';
|
|
|
|
$keys = array_keys($this->links);
|
|
|
|
foreach ($this->links as $key => $link) {
|
2021-05-19 16:35:13 +00:00
|
|
|
if (end($keys) === $key) {
|
2020-08-05 16:10:39 +00:00
|
|
|
$listItem =
|
|
|
|
'<li class="breadcrumb-item active" aria-current="page">' .
|
|
|
|
$link['text'] .
|
|
|
|
'</li>';
|
|
|
|
} else {
|
|
|
|
$listItem =
|
|
|
|
'<li class="breadcrumb-item">' .
|
|
|
|
anchor($link['href'], $link['text']) .
|
|
|
|
'</li>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$listItems .= $listItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '<nav aria-label="' .
|
|
|
|
lang('Breadcrumb.label') .
|
2021-09-23 14:59:30 +00:00
|
|
|
'" class="' . $class . '"><ol class="breadcrumb">' .
|
2020-08-05 16:10:39 +00:00
|
|
|
$listItems .
|
|
|
|
'</ol></nav>';
|
|
|
|
}
|
|
|
|
}
|