feature/category-tree #55
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 %}
|
||||
|
||||
@@ -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">
|
||||
|
||||
Reference in New Issue
Block a user