ldc-groupe/app/View/Components/Front/News/IndexNewsComponent.php
2025-11-12 16:48:20 +01:00

69 lines
2.2 KiB
PHP

<?php
namespace App\View\Components\Front\News;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
use App\Enums\News\NewsCategoryEnum;
use App\Enums\News\NewsYearsEnum;
use App\Helpers\BladeComponentHelper;
use App\Models\News;
use Illuminate\Support\Str;
class IndexNewsComponent extends Component
{
public $categories;
public $years;
public $newsModels;
/**
* Create a new component instance.
*/
public function __construct()
{
// Rajouter ici un filtre pour ne récupérer que le status published
$bladeHelper = new BladeComponentHelper();
$this->categories = collect(NewsCategoryEnum::toArray())
->map(fn($category) => NewsCategoryEnum::make((int) $category->value))
->filter(fn($categoryLocal) => $categoryLocal->categoriesWithLocal())
->map(fn($categoryLocal) => [
'title' => __($categoryLocal->label()),
'tag' => Str::slug($categoryLocal->value),
'category' => 'news',
])
->values()
->toArray();
$this->years = collect(NewsYearsEnum::toArray())
->map(fn($year) => NewsYearsEnum::make((int) $year->value))
->filter(fn($yearLocal) => $yearLocal->yearsWithLocal())
->map(fn($yearLocal) => [
'title' => __($yearLocal->label()),
'tag' => Str::slug($yearLocal->value),
'category' => 'news',
])
->values()
->toArray();
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\News> $query */
$query = News::query()->with('categories');
/** Sort columns with a query */
$bladeHelper->runSortQuery($query);
/** Custom pagination */
$this->newsModels = $bladeHelper->runPaginate($query);
}
/**
* Get the view / contents that represent the component.
*
* @return View|Closure|string
*/
public function render(): View|Closure|string
{
return view('components.front.news.index-news-component', [
'categories' => $this->categories,
'years' => $this->years,
'newsModels' => $this->newsModels
]);
}
}