forge/app/Livewire/ModIndex.php

72 lines
2.1 KiB
PHP
Raw Normal View History

2024-08-09 10:15:05 -04:00
<?php
namespace App\Livewire;
use App\Models\Mod;
use App\Models\SptVersion;
2024-08-09 11:19:07 -04:00
use Illuminate\Support\Str;
2024-08-09 10:15:05 -04:00
use Livewire\Component;
use Livewire\WithPagination;
class ModIndex extends Component
{
use WithPagination;
public string $modSearch = '';
2024-08-09 11:19:07 -04:00
public string $sectionFilter = 'featured';
2024-08-12 11:20:03 -04:00
public int $versionFilter = -1;
2024-08-09 10:15:05 -04:00
public function render()
{
2024-08-09 11:45:37 -04:00
// 'featured' section is default
$section = 'featured';
2024-08-09 11:19:07 -04:00
switch ($this->sectionFilter) {
2024-08-09 11:45:37 -04:00
case 'new':
$section = 'created_at';
break;
2024-08-09 11:19:07 -04:00
case 'most_downloaded':
$section = 'total_downloads';
break;
case 'recently_updated':
$section = 'updated_at';
break;
case 'top_rated':
// probably use some kind of 'likes' or something
// not implemented yet afaik -waffle
break;
}
2024-08-09 10:15:05 -04:00
$mods = Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'created_at'])
->withTotalDownloads()
2024-08-12 11:20:03 -04:00
->with(['users:id,name'])
->where('name', 'like', '%'.Str::trim($this->modSearch).'%');
if ($this->versionFilter === -1) {
$mods = $mods
->with(['latestVersion', 'latestVersion.sptVersion'])
->whereHas('latestVersion.sptVersion');
} else {
$mods = $mods->with(['latestVersion' => function ($query) {
$query->where('spt_version_id', $this->versionFilter);
}, 'latestVersion.sptVersion'])
->whereHas('latestVersion.sptVersion', function ($query) {
$query->where('spt_version_id', $this->versionFilter);
});
}
$mods = $mods->orderByDesc($section)->paginate(12);
2024-08-09 10:15:05 -04:00
$sptVersions = SptVersion::select(['id', 'version', 'color_class'])->orderByDesc('version')->get();
return view('livewire.mod-index', ['mods' => $mods, 'sptVersions' => $sptVersions]);
2024-08-09 10:15:05 -04:00
}
public function changeSection($section): void
{
$this->sectionFilter = $section;
}
}