feature/category-tree #55

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

View File

@@ -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));
}
public function renderCategoryTree($categories, $level = 0): string
{
$tree = '';
if ($level == 0) {
$tree .= '<ul class="list-group">';
}
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>';
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 .= '</ul>';
}
return $tree;
}
}

View File

@@ -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 %}
<div class="table-responsive">
<table class='table table-hover'>
<thead>
@@ -66,5 +71,5 @@
{% endfor %}
</table>
</div>
{% endcache %}
{#{% endcache %}#}
{% endblock %}