92 lines
2.9 KiB
PHP
92 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\GeneralConfigType;
|
|
use App\Models\Actu;
|
|
use App\Models\Tag;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class ActuController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index()
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Tag> */
|
|
$tags = Tag::query()->whereHas('actus', function (Builder $q) {
|
|
$q->where('status', true);
|
|
})->get();
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Actu> */
|
|
$actus = Actu::query()->where('status', true)
|
|
->with('tags:id')
|
|
->orderBy('created_at', 'DESC')
|
|
->get(['id'])
|
|
->map(function ($actu) {
|
|
return [
|
|
'id' => $actu->id,
|
|
'tags' => $actu->tags->map(function ($tag) {
|
|
return $tag->id;
|
|
})->all()
|
|
];
|
|
})->all();
|
|
|
|
$generalConfig = app()->view->getShared()['GeneralConfigGlobal'] ?? null;
|
|
$generalConfig = $generalConfig ? $generalConfig->where('type', GeneralConfigType::actu())->first() : null;
|
|
return view('front.actu.index', [
|
|
'generalConfig' => $generalConfig,
|
|
'paginate' => 9,
|
|
'actus' => $actus,
|
|
'tags' => $tags
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Display a news
|
|
*
|
|
* @param \App\Models\Actu $actu
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function view(Actu $actu)
|
|
{
|
|
$relatedActus = Actu::whereHas('tags', function ($q) use ($actu) {
|
|
$q->whereIn('id', $actu->tags->pluck('id'));
|
|
})->where('id', '!=', $actu->id)->whereStatus(true)
|
|
->inRandomOrder()->limit(3)->get();
|
|
$generalConfig = app()->view->getShared()['GeneralConfigGlobal'] ?? null;
|
|
$generalConfig = $generalConfig ? $generalConfig->where('type', GeneralConfigType::actu())->first() : null;
|
|
return \view('front.actu.show', [
|
|
'generalConfig' => $generalConfig,
|
|
'actu' => $actu,
|
|
'relatedActus' => $relatedActus
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Render a news for JS usage (ajax)
|
|
*
|
|
* @param \App\Models\Actu $actu
|
|
* @return string Base64 rendered view.
|
|
*/
|
|
public function renderActu(Actu $actu)
|
|
{
|
|
if (!request()->wantsJson()) {
|
|
abort(404);
|
|
}
|
|
return response()->json(
|
|
Cache::rememberForever(
|
|
"ActuRendered_$actu->id",
|
|
function () use ($actu) {
|
|
return \base64_encode(\view(
|
|
'front.actu.card',
|
|
['actu' => $actu]
|
|
)->render());
|
|
}
|
|
)
|
|
);
|
|
}
|
|
}
|