59 lines
1.9 KiB
PHP
59 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/** @var array<int, array<string, mixed>> $items */
|
|
/** @var int $depth */
|
|
/** @var string $currentSlug */
|
|
|
|
if (!function_exists('category_tree_has_current')) {
|
|
/**
|
|
* @param array<string, mixed> $category
|
|
*/
|
|
function category_tree_has_current(array $category, string $currentSlug): bool
|
|
{
|
|
if ($currentSlug === '') {
|
|
return false;
|
|
}
|
|
|
|
if (($category['slug'] ?? '') === $currentSlug) {
|
|
return true;
|
|
}
|
|
|
|
foreach (($category['children'] ?? []) as $child) {
|
|
if (category_tree_has_current($child, $currentSlug)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
?>
|
|
<ul class="category-list depth-<?= e((string) $depth) ?>">
|
|
<?php foreach ($items as $category): ?>
|
|
<?php $isCurrent = ($currentSlug ?? '') === $category['slug']; ?>
|
|
<?php $hasChildren = !empty($category['children']); ?>
|
|
<?php $shouldOpen = category_tree_has_current($category, $currentSlug ?? ''); ?>
|
|
<li class="<?= $isCurrent ? 'is-current' : '' ?>">
|
|
<?php if ($hasChildren): ?>
|
|
<details class="category-details" <?= $shouldOpen ? 'open' : '' ?>>
|
|
<summary>
|
|
<a class="<?= $isCurrent ? 'is-current-link' : '' ?>" href="/catalog/<?= e($category['slug']) ?>"><?= e($category['name']) ?></a>
|
|
</summary>
|
|
<?php
|
|
$previousItems = $items;
|
|
$items = $category['children'];
|
|
$depth++;
|
|
require __DIR__ . '/category-tree.php';
|
|
$depth--;
|
|
$items = $previousItems;
|
|
?>
|
|
</details>
|
|
<?php else: ?>
|
|
<a href="/catalog/<?= e($category['slug']) ?>"><?= e($category['name']) ?></a>
|
|
<?php endif; ?>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|