ldc-groupe/app/View/Components/Front/News/IndexNewsComponent.php

114 lines
3.6 KiB
PHP

<?php
namespace App\View\Components\Front\News;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
use App\Enums\News\NewsYearsEnum;
use App\Helpers\BladeComponentHelper;
use App\Models\News;
use App\Models\NewsCategory;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
class IndexNewsComponent extends Component
{
public $categories;
public $years;
public $newsModels;
public $postContainer;
/**
* 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(NewsCategory::all())
->map(fn($categoryLocal) => [
'title' => $categoryLocal->name,
'tag' => $categoryLocal->slug,
'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' => trans($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);
/**
* Cette section est à revoir mais c'est pour coller et être iso au site actuellement en prod
*/
$currentRoute = Route::currentRouteName();
// Common values
$this->postContainer = [
'secondaryClass' => 'fw-container no-bottom-padding',
'tertiaryClass' => 'tagged-posts',
'list_type' => 'news',
'id' => 'news',
'post_id' => '9', // FIXME: probablement plus utile
'slug' => 'news',
'url' => route('front.news.index'),
'localized' => 'ajax_load_more_news_vars', // FIXME: probablement pas utile
'object' => 'ajax_load_more_news', // FIXME: probablement pas utile
];
switch ($currentRoute) {
case 'front.news.index':
$this->postContainer['load_more'] = ucfirst(__('front.buttons.see_more_news'));
break;
case 'front.dynamic-pages.espace-presse':
$this->postContainer = array_merge($this->postContainer, [
'class' => 'fw-container no-top-padding',
'load_more' => ucfirst(__('front.buttons.see_more_docs')),
'type_button' => true
]);
break;
default:
$this->postContainer['load_more'] = __('front.buttons.load_more');
break;
}
}
/**
* 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,
'postContainer' => $this->postContainer
]);
}
}