feature/category-tree #55

Merged
krzysiej merged 5 commits from feature/category-tree into master 2026-01-26 08:59:58 +01:00
4 changed files with 10 additions and 9 deletions
Showing only changes of commit eb2eee53bd - Show all commits

View File

@@ -31,7 +31,7 @@ final class CategoryController extends BaseController
$categoriesTree = $this->addToTree($product->categories, $categoriesTree);
}
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_' . $category, 'categoryTree' => $categoriesTree]);
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_' . $category, 'category' => $category, 'categoryTree' => $categoriesTree]);
}
private function addToTree(array $categories, mixed $categoriesTree)

View File

@@ -71,19 +71,20 @@ class AppExtension extends AbstractExtension
return $items->first(fn($item) => str_starts_with($item->created_at, $date));
}
public function renderCategoryTree($categories, $level = 0): string
public function renderCategoryTree($categories, $current, $level = 0): string
{
$tree = '';
if ($level == 0) {
$tree .= '<ul class="list-group">';
$tree .= '<ul class="list-group list-group-flush">';
}
foreach ($categories as $categoryName => $category) {
$tree .= '<a class="list-group-item list-group-item-action text-decoration-none ms-' . ($level * 2) . '" href="' . $this->route->generate('app_category', ['category' => $categoryName]) . '">' . $categoryName . ' <span class="badge bg-primary rounded-pill">' . $category['count'] . '</span></a>';
$currentClass = $categoryName == $current ? 'list-group-item-primary' : '';
$tree .= '<a class="list-group-item list-group-item-action '.$currentClass.' text-decoration-none ms-' . ($level * 2) . '" href="' . $this->route->generate('app_category', ['category' => $categoryName]) . '">' . $categoryName . ' <span class="badge bg-primary rounded-pill">' . $category['count'] . '</span></a>';
unset($category['count']);
if (is_array($category) && count($category) >= 1) {
foreach ($category as $subcategoryName => $subCategory) {
$tree .= $this->renderCategoryTree([$subcategoryName => $subCategory], $level + 1);
$tree .= $this->renderCategoryTree([$subcategoryName => $subCategory], $current, $level + 1);
}
}
}

View File

@@ -1,10 +1,10 @@
{% extends "template.html.twig" %}
{% block content %}
{#{% cache 'list_' ~ listType %}#}
{% cache 'list_' ~ listType %}
{% if listType starts with 'category_' %}
{{ renderCategoryTree(categoryTree) | raw }}
{{ renderCategoryTree(categoryTree, category) | raw }}
{% endif %}
<div class="table-responsive">
@@ -71,5 +71,5 @@
{% endfor %}
</table>
</div>
{#{% endcache %}#}
{% endcache %}
{% endblock %}

View File

@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en">
<html lang="en" data-bs-theme="light">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">