From 652ad0db7ec0608198925b2967ca34452bd113a8 Mon Sep 17 00:00:00 2001 From: Krzysiej Date: Fri, 23 Jan 2026 09:47:07 +0100 Subject: [PATCH 1/5] Star working on category tree --- src/Controller/CategoryController.php | 64 +++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php index 27c190b..bceb0ae 100644 --- a/src/Controller/CategoryController.php +++ b/src/Controller/CategoryController.php @@ -11,10 +11,11 @@ final class CategoryController extends BaseController #[Route('/category/{category}', name: 'app_category')] public function __invoke(string $category): Response { - if($this->cache->getItem('list_category_'.$category)->isHit()) { - return $this->render('productList.html.twig', ['listType' => 'category_'.$category]); - } +// if($this->cache->getItem('list_category_'.$category)->isHit()) { +// return $this->render('productList.html.twig', ['listType' => 'category_'.$category]); +// } + /** @var Product[] $products */ $products = Product::with(['price', 'lowestPrice']) ->selectRaw('products.*') ->distinct('products.id') @@ -24,6 +25,61 @@ final class CategoryController extends BaseController ->orderByDesc('created_by') ->get(); - return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_'.$category]); + $categoriesTree = []; + + foreach ($products as $product) { + $categoriesTree = $this->addToTree($product->categories, $categoriesTree); + dump($categoriesTree); + } + + dd($categoriesTree); + + dd(); + + return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_' . $category]); } + + private function addToTree(array $categories, mixed $categoriesTree) + { +// $tree = [ +// "Elektronarzędzia" => [ +// "Maszyny i narzędzia do cięcia" => [ +// "Pilarki tarczowe" => [] +// +// ] +// ] +// ]; +// dump($tree); +// $test = &$tree['Elektronarzędzia']; +// $test = &$test['Maszyny i narzędzia do cięcia']; +// $test = &$test['Maszyny i narzędzia do cięcia2']; +// $test = &$test['Maszyny i narzędzia do cięcia3']; +// $test['node'] = []; +// dump( +// $tree +// ); +// +// $tree2 = []; +// $tree2['Elektronarzędzia'] = []; +// $tree2['Elektronarzędzia']['Maszyny i narzędzia do cięcia'] = []; + + + $tmp = &$categoriesTree; + foreach ($categories as $category) { + + if (empty($tmp[$category])) { + $tmp[$category] = []; + $tmp = &$tmp[$category]; + } else { + $tmp = &$tmp[$category]; + } + } + + dump($categoriesTree); + + + return $categoriesTree; + } + + } -- 2.52.0 From f3169bcb32ee61c434517885e91c452ab67a2ca5 Mon Sep 17 00:00:00 2001 From: Krzysiej Date: Fri, 23 Jan 2026 11:24:46 +0100 Subject: [PATCH 2/5] Star working on category tree --- src/Controller/CategoryController.php | 41 ++------------------------- 1 file changed, 3 insertions(+), 38 deletions(-) diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php index bceb0ae..e600fd8 100644 --- a/src/Controller/CategoryController.php +++ b/src/Controller/CategoryController.php @@ -29,57 +29,22 @@ final class CategoryController extends BaseController foreach ($products as $product) { $categoriesTree = $this->addToTree($product->categories, $categoriesTree); - dump($categoriesTree); } - dd($categoriesTree); - - dd(); - return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_' . $category]); } private function addToTree(array $categories, mixed $categoriesTree) { -// $tree = [ -// "Elektronarzędzia" => [ -// "Maszyny i narzędzia do cięcia" => [ -// "Pilarki tarczowe" => [] -// -// ] -// ] -// ]; -// dump($tree); -// $test = &$tree['Elektronarzędzia']; -// $test = &$test['Maszyny i narzędzia do cięcia']; -// $test = &$test['Maszyny i narzędzia do cięcia2']; -// $test = &$test['Maszyny i narzędzia do cięcia3']; -// $test['node'] = []; -// dump( -// $tree -// ); -// -// $tree2 = []; -// $tree2['Elektronarzędzia'] = []; -// $tree2['Elektronarzędzia']['Maszyny i narzędzia do cięcia'] = []; - - $tmp = &$categoriesTree; foreach ($categories as $category) { - if (empty($tmp[$category])) { - $tmp[$category] = []; - $tmp = &$tmp[$category]; - } else { - $tmp = &$tmp[$category]; + $tmp[$category] = ['count' => 0]; } + $tmp = &$tmp[$category]; + $tmp['count']++; } - dump($categoriesTree); - - return $categoriesTree; } - - } -- 2.52.0 From 1e4815d47a696176dc23b514b755ce1865f736fd Mon Sep 17 00:00:00 2001 From: Krzysiej Date: Sat, 24 Jan 2026 08:42:16 +0100 Subject: [PATCH 3/5] Star working on category tree --- src/Controller/CategoryController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php index e600fd8..7f5669d 100644 --- a/src/Controller/CategoryController.php +++ b/src/Controller/CategoryController.php @@ -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]); + return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_' . $category, 'categoryTree' => $categoriesTree]); } private function addToTree(array $categories, mixed $categoriesTree) -- 2.52.0 From 53b0bd6894d8c462a81ddb5e86bbb49afd473bd6 Mon Sep 17 00:00:00 2001 From: Krzysiej Date: Sun, 25 Jan 2026 11:33:58 +0100 Subject: [PATCH 4/5] Star working on category tree --- src/Twig/AppExtension.php | 34 ++++++++++++++++++++++++++++++--- templates/productList.html.twig | 9 +++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index 1a08554..3208b73 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -2,11 +2,11 @@ namespace Krzysiej\RyobiCrawler\Twig; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Krzysiej\RyobiCrawler\Models\Price; use Krzysiej\RyobiCrawler\Models\Product; use Krzysiej\RyobiCrawler\Models\Stock; +use Symfony\Component\Routing\RouterInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; use Twig\TwigFunction; @@ -15,6 +15,10 @@ use function Symfony\Component\Clock\now; class AppExtension extends AbstractExtension { + public function __construct(public RouterInterface $route) + { + } + public function getFunctions(): array { return [ @@ -23,6 +27,7 @@ class AppExtension extends AbstractExtension new TwigFunction('newCount', [$this, 'newCount']), new TwigFunction('discontinuedCount', [$this, 'discontinuedCount']), new TwigFunction('lowestPriceCount', [$this, 'lowestPriceCount']), + new TwigFunction('renderCategoryTree', [$this, 'renderCategoryTree']), ]; } @@ -56,7 +61,7 @@ class AppExtension extends AbstractExtension public function lowestPriceCount(): int { return Product::whereRaw('priceCurrent = priceLowest') - ->whereRaw('lastSeen = "'.now()->format('Y-m-d').'"') + ->whereRaw('lastSeen = "' . now()->format('Y-m-d') . '"') ->whereRaw('priceCurrent < productStandardPrice') ->count(); } @@ -65,4 +70,27 @@ class AppExtension extends AbstractExtension { return $items->first(fn($item) => str_starts_with($item->created_at, $date)); } -} \ No newline at end of file + + public function renderCategoryTree($categories, $level = 0): string + { + $tree = ''; + if ($level == 0) { + $tree .= '
    '; + } + foreach ($categories as $categoryName => $category) { + $tree .= '' . $categoryName . ' ' . $category['count'] . ''; + + unset($category['count']); + if (is_array($category) && count($category) >= 1) { + foreach ($category as $subcategoryName => $subCategory) { + $tree .= $this->renderCategoryTree([$subcategoryName => $subCategory], $level + 1); + } + } + } + if ($level == 0) { + $tree .= '
'; + } + return $tree; + + } +} diff --git a/templates/productList.html.twig b/templates/productList.html.twig index 1d349f5..71d1c92 100644 --- a/templates/productList.html.twig +++ b/templates/productList.html.twig @@ -1,7 +1,12 @@ {% extends "template.html.twig" %} {% block content %} -{% cache 'list_' ~ listType %} +{#{% cache 'list_' ~ listType %}#} + +{% if listType starts with 'category_' %} + {{ renderCategoryTree(categoryTree) | raw }} +{% endif %} +
@@ -66,5 +71,5 @@ {% endfor %}
-{% endcache %} +{#{% endcache %}#} {% endblock %} -- 2.52.0 From eb2eee53bda5271ece5a39a0fb66ca2646e1da88 Mon Sep 17 00:00:00 2001 From: Krzysiej Date: Mon, 26 Jan 2026 08:58:16 +0100 Subject: [PATCH 5/5] Render category tree on category pages --- src/Controller/CategoryController.php | 2 +- src/Twig/AppExtension.php | 9 +++++---- templates/productList.html.twig | 6 +++--- templates/template.html.twig | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php index 7f5669d..3e872cb 100644 --- a/src/Controller/CategoryController.php +++ b/src/Controller/CategoryController.php @@ -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) diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index 3208b73..22bc991 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -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 .= '
    '; + $tree .= '
      '; } foreach ($categories as $categoryName => $category) { - $tree .= '' . $categoryName . ' ' . $category['count'] . ''; + $currentClass = $categoryName == $current ? 'list-group-item-primary' : ''; + $tree .= '' . $categoryName . ' ' . $category['count'] . ''; 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); } } } diff --git a/templates/productList.html.twig b/templates/productList.html.twig index 71d1c92..92436e6 100644 --- a/templates/productList.html.twig +++ b/templates/productList.html.twig @@ -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 %}
      @@ -71,5 +71,5 @@ {% endfor %}
      -{#{% endcache %}#} +{% endcache %} {% endblock %} diff --git a/templates/template.html.twig b/templates/template.html.twig index da00920..e7b7508 100644 --- a/templates/template.html.twig +++ b/templates/template.html.twig @@ -1,5 +1,5 @@ - + -- 2.52.0