243 lines
6.9 KiB
PHP
243 lines
6.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Livewire;
|
||
|
|
|
||
|
|
use App\Models\Author;
|
||
|
|
use App\Models\Entry;
|
||
|
|
use App\Models\Game;
|
||
|
|
use App\Models\Language;
|
||
|
|
use App\Models\Modification;
|
||
|
|
use App\Models\Platform;
|
||
|
|
use App\Models\Status;
|
||
|
|
use Livewire\Component;
|
||
|
|
use Livewire\WithPagination;
|
||
|
|
|
||
|
|
class Database extends Component
|
||
|
|
{
|
||
|
|
use WithPagination;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* entry_title search
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
public string $search = '';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* type filter.
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
public array $types = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Games IDs filter.
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
public array $games = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Current game search
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
public string $gameSearch = '';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Platform IDs filter.
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
public array $platforms = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Status IDs filter.
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
public array $statuses = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Authors IDs filter.
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
public array $authors = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Authors mode and/or.
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
public string $authorsMode = 'or';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Current author search.
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
public string $authorSearch = '';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Languages IDs filter.
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
public array $languages = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Languages mode and/or
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
public string $languagesMode = 'or';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Modifications IDs filter.
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
public array $modifications = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Modifications mode and/or.
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
public string $modificationsMode = 'or';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Sort by field.
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
public string $sortBy = 'created_at';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* asc/desc
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
public string $sortDir = 'desc';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Translation of sort options key.
|
||
|
|
*/
|
||
|
|
public const array SORT_OPTIONS = [
|
||
|
|
'created_at' => 'Date added',
|
||
|
|
'release_date' => 'Release date',
|
||
|
|
'title' => 'Title'
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Translation of entries key.
|
||
|
|
*/
|
||
|
|
public const array ENTRY_TYPES = [
|
||
|
|
'translations' => 'Translations',
|
||
|
|
'romhacks' => 'Romhacks',
|
||
|
|
'homebrew' => 'Homebrew',
|
||
|
|
'utilities' => 'Utilities',
|
||
|
|
'documents' => 'Documents',
|
||
|
|
'lua-scripts' => 'Lua Scripts',
|
||
|
|
'tutorials' => 'Tutorials',
|
||
|
|
];
|
||
|
|
|
||
|
|
public const int PAGINATION = 30;
|
||
|
|
|
||
|
|
public function updatedSearch(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||
|
|
public function updatedTypes(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||
|
|
public function updatedGames(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||
|
|
public function updatedPlatforms(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||
|
|
public function updatedStatuses(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||
|
|
public function updatedAuthors(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||
|
|
public function updatedAuthorsMode(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||
|
|
public function updatedLanguages(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||
|
|
public function updatedLanguagesMode(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||
|
|
public function updatedModifications(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||
|
|
public function updatedModificationsMode(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||
|
|
|
||
|
|
public function clearFilters(): void
|
||
|
|
{
|
||
|
|
$this->reset([
|
||
|
|
'search', 'types', 'platforms', 'statuses', 'authors', 'authorsMode', 'languages', 'languagesMode', 'modifications', 'modificationsMode'
|
||
|
|
]);
|
||
|
|
$this->resetPage();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setSort(string $field): void
|
||
|
|
{
|
||
|
|
if( $this->sortBy === $field ) {
|
||
|
|
$this->sortDir = $this->sortDir === 'asc' ? 'desc' : 'asc';
|
||
|
|
} else {
|
||
|
|
$this->sortBy = $field;
|
||
|
|
$this->sortDir = 'asc';
|
||
|
|
}
|
||
|
|
$this->resetPage();
|
||
|
|
$this->dispatch('filters-updated');
|
||
|
|
}
|
||
|
|
|
||
|
|
private function buildQuery()
|
||
|
|
{
|
||
|
|
$query = Entry::query()->published()->with([
|
||
|
|
'game.platform', 'status', 'authors', 'languages'
|
||
|
|
]);
|
||
|
|
|
||
|
|
if( $this->search ) {
|
||
|
|
$query->where(function($q) {
|
||
|
|
$q->where('title', 'like', '%'.$this->search.'%');
|
||
|
|
$q->orWhere('complete_title', 'like', '%'.$this->search.'%');
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if( $this->types ) {
|
||
|
|
$query->whereIn('type', $this->types);
|
||
|
|
}
|
||
|
|
|
||
|
|
if( $this->platforms ) {
|
||
|
|
$query->where(function($q) {
|
||
|
|
$q->whereIn('platform_id', $this->platforms)
|
||
|
|
->orWhereHas('game', fn($q2) => $q2->whereIn('platform_id', $this->platforms) );
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if( $this->games ){
|
||
|
|
$query->whereIn('game_id', $this->games);
|
||
|
|
}
|
||
|
|
|
||
|
|
if( $this->statuses ) {
|
||
|
|
$query->whereIn('status_id', $this->statuses);
|
||
|
|
}
|
||
|
|
|
||
|
|
if( $this->authors ) {
|
||
|
|
if( $this->authorsMode === 'and' ) {
|
||
|
|
foreach ( $this->authors as $authorId ) {
|
||
|
|
$query->whereHas('authors', fn($q) => $q->where('authors.id', $authorId));
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$query->whereHas('authors', fn($q) => $q->whereIn('authors.id', $this->authors));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if( $this->languages ) {
|
||
|
|
if( $this->languagesMode === 'and' ) {
|
||
|
|
foreach ( $this->languages as $langId ) {
|
||
|
|
$query->whereHas('languages', fn($q) => $q->where('languages.id', $langId));
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$query->whereHas('languages', fn($q) => $q->whereIn('languages.id', $this->languages));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if( $this->modifications ) {
|
||
|
|
if( $this->modificationsMode === 'and' ) {
|
||
|
|
foreach ( $this->modifications as $modificationId ) {
|
||
|
|
$query->whereHas('modifications', fn($q) => $q->where('modifications.id', $modificationId));
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$query->whereHas('modifications', fn($q) => $q->whereIn('modifications.id', $this->modifications));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $query->orderBy($this->sortBy, $this->sortDir);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function render()
|
||
|
|
{
|
||
|
|
return view('livewire.database', [
|
||
|
|
'entries' => $this->buildQuery()->paginate(self::PAGINATION),
|
||
|
|
'allGames' => Game::orderBy('name')->get(),
|
||
|
|
'allPlatforms' => Platform::orderBy('name')->get(),
|
||
|
|
'allStatuses' => Status::orderBy('name')->get(),
|
||
|
|
'allAuthors' => Author::orderBy('name')->get(),
|
||
|
|
'allLanguages' => Language::orderBy('name')->get(),
|
||
|
|
'allModifications' => Modification::orderBy('name')->get(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|