forge/app/View/Components/ModListSection.php

105 lines
3.0 KiB
PHP
Raw Normal View History

2024-05-17 23:54:03 -04:00
<?php
namespace App\View\Components;
use App\Models\Mod;
use App\Models\ModVersion;
2024-05-17 23:54:03 -04:00
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
2024-05-17 23:54:03 -04:00
use Illuminate\View\Component;
class ModListSection extends Component
{
2024-05-21 21:02:49 -04:00
public Collection $modsFeatured;
2024-05-22 01:00:37 -04:00
2024-05-17 23:54:03 -04:00
public Collection $modsLatest;
2024-05-22 01:00:37 -04:00
2024-05-17 23:54:03 -04:00
public Collection $modsUpdated;
public function __construct()
{
2024-05-21 21:02:49 -04:00
$this->modsFeatured = $this->fetchFeaturedMods();
2024-05-17 23:54:03 -04:00
$this->modsLatest = $this->fetchLatestMods();
$this->modsUpdated = $this->fetchUpdatedMods();
}
2024-05-21 21:02:49 -04:00
private function fetchFeaturedMods(): Collection
2024-05-17 23:54:03 -04:00
{
return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'downloads'])
->with([
'latestVersion',
'latestVersion.latestSptVersion:id,version,color_class',
'users:id,name',
'license:id,name,link',
])
->whereFeatured(true)
->inRandomOrder()
->limit(6)
->get();
2024-05-17 23:54:03 -04:00
}
private function fetchLatestMods(): Collection
{
return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'created_at', 'downloads'])
->with([
'latestVersion',
'latestVersion.latestSptVersion:id,version,color_class',
'users:id,name',
'license:id,name,link',
])
->latest()
->limit(6)
->get();
2024-05-17 23:54:03 -04:00
}
private function fetchUpdatedMods(): Collection
{
return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'downloads'])
->with([
'lastUpdatedVersion',
'lastUpdatedVersion.latestSptVersion:id,version,color_class',
'users:id,name',
'license:id,name,link',
])
->joinSub(
ModVersion::select('mod_id', DB::raw('MAX(updated_at) as latest_updated_at'))->groupBy('mod_id'),
'latest_versions',
'mods.id',
'=',
'latest_versions.mod_id'
)
->orderByDesc('latest_versions.latest_updated_at')
->limit(6)
->get();
2024-05-17 23:54:03 -04:00
}
public function render(): View
{
return view('components.mod-list-section', [
'sections' => $this->getSections(),
]);
}
2024-05-17 23:54:03 -04:00
public function getSections(): array
{
return [
[
'title' => __('Featured Mods'),
2024-05-21 21:02:49 -04:00
'mods' => $this->modsFeatured,
'versionScope' => 'latestVersion',
2024-05-17 23:54:03 -04:00
],
[
'title' => __('Newest Mods'),
2024-05-17 23:54:03 -04:00
'mods' => $this->modsLatest,
'versionScope' => 'latestVersion',
2024-05-17 23:54:03 -04:00
],
[
'title' => __('Recently Updated Mods'),
2024-05-17 23:54:03 -04:00
'mods' => $this->modsUpdated,
'versionScope' => 'lastUpdatedVersion',
2024-05-17 23:54:03 -04:00
],
];
}
}