шаг 4 завершен

This commit is contained in:
Alisa
2026-06-04 01:30:45 +03:00
parent 62ce8c4d01
commit 238831ad3a
26 changed files with 1703 additions and 70 deletions
+45 -11
View File
@@ -4,20 +4,54 @@ 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): ?>
<li>
<a href="/catalog/<?= e($category['slug']) ?>"><?= e($category['name']) ?></a>
<?php if (!empty($category['children'])): ?>
<?php
$previousItems = $items;
$items = $category['children'];
$depth++;
require __DIR__ . '/category-tree.php';
$depth--;
$items = $previousItems;
?>
<?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; ?>