81 lines
3.0 KiB
PHP
81 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/** @var array<int, array<string, mixed>> $items */
|
|
/** @var int $depth */
|
|
/** @var string $currentSlug */
|
|
|
|
require_once __DIR__ . '/category-icons.php';
|
|
|
|
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 if ($depth === 0): ?>
|
|
<li class="category-special-link is-all">
|
|
<a href="/catalog"><span>Весь каталог</span></a>
|
|
</li>
|
|
<li class="category-special-link">
|
|
<a href="/promo"><span>Акции</span></a>
|
|
</li>
|
|
<li class="category-special-link">
|
|
<a href="/new"><span>Новинки</span></a>
|
|
</li>
|
|
<?php endif; ?>
|
|
<?php foreach ($items as $category): ?>
|
|
<?php $isCurrent = ($currentSlug ?? '') === $category['slug']; ?>
|
|
<?php $hasChildren = !empty($category['children']); ?>
|
|
<?php $shouldOpen = category_tree_has_current($category, $currentSlug ?? ''); ?>
|
|
<?php $iconName = $depth === 0 ? category_tree_icon((string) $category['name']) : ''; ?>
|
|
<li class="<?= $isCurrent ? 'is-current' : '' ?><?= $hasChildren ? ' has-children' : '' ?>">
|
|
<?php if ($hasChildren): ?>
|
|
<details class="category-details" <?= $shouldOpen ? 'open' : '' ?>>
|
|
<summary>
|
|
<?php if ($depth === 0): ?>
|
|
<span class="category-icon is-<?= e($iconName) ?>" aria-hidden="true"><?= category_tree_icon_svg($iconName) ?></span>
|
|
<?php endif; ?>
|
|
<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']) ?>">
|
|
<?php if ($depth === 0): ?>
|
|
<span class="category-icon is-<?= e($iconName) ?>" aria-hidden="true"><?= category_tree_icon_svg($iconName) ?></span>
|
|
<?php endif; ?>
|
|
<span><?= e($category['name']) ?></span>
|
|
</a>
|
|
<?php endif; ?>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|