mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-11-13 09:38:48 +01:00
228 lines
6.8 KiB
PHP
228 lines
6.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models\Wiki;
|
|
|
|
use App\Concerns\Models\SoftDeletes;
|
|
use App\Contracts\Models\HasResources;
|
|
use App\Contracts\Models\SoftDeletable;
|
|
use App\Events\Wiki\Song\SongCreated;
|
|
use App\Events\Wiki\Song\SongDeleted;
|
|
use App\Events\Wiki\Song\SongForceDeleting;
|
|
use App\Events\Wiki\Song\SongRestored;
|
|
use App\Events\Wiki\Song\SongUpdated;
|
|
use App\Models\BaseModel;
|
|
use App\Pivots\Morph\Resourceable;
|
|
use Database\Factories\Wiki\SongFactory;
|
|
use Deprecated;
|
|
use Illuminate\Database\Eloquent\Attributes\Table;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Laravel\Scout\Searchable;
|
|
use OwenIt\Auditing\Auditable as HasAudits;
|
|
use OwenIt\Auditing\Contracts\Auditable;
|
|
use RuntimeException;
|
|
|
|
/**
|
|
* @property Collection<int, Theme> $animethemes
|
|
* @property Collection<int, Artist> $artists
|
|
* @property Collection<int, Performance> $performances
|
|
* @property Collection<int, ExternalResource> $resources
|
|
* @property int $song_id
|
|
* @property string|null $title
|
|
* @property string|null $title_native
|
|
*
|
|
* @method static SongFactory factory(...$parameters)
|
|
*/
|
|
#[Table(Song::TABLE, Song::ATTRIBUTE_ID)]
|
|
class Song extends BaseModel implements Auditable, HasResources, SoftDeletable
|
|
{
|
|
use HasAudits;
|
|
use HasFactory;
|
|
use Searchable;
|
|
use SoftDeletes;
|
|
|
|
final public const string TABLE = 'songs';
|
|
|
|
final public const string ATTRIBUTE_ID = 'song_id';
|
|
|
|
final public const string ATTRIBUTE_TITLE = 'title';
|
|
|
|
final public const string ATTRIBUTE_TITLE_NATIVE = 'title_native';
|
|
|
|
final public const string RELATION_ANIME = 'animethemes.anime';
|
|
|
|
final public const string RELATION_ANIMETHEMES = 'animethemes';
|
|
|
|
final public const string RELATION_ARTISTS = 'artists';
|
|
|
|
final public const string RELATION_PERFORMANCES = 'performances';
|
|
|
|
final public const string RELATION_PERFORMANCES_ARTIST = 'performances.artist';
|
|
|
|
final public const string RELATION_PERFORMANCES_MEMBER = 'performances.member';
|
|
|
|
final public const string RELATION_RESOURCES = 'resources';
|
|
|
|
final public const string RELATION_STAFF = 'staff';
|
|
|
|
final public const string RELATION_THEMES = 'themes';
|
|
|
|
final public const string RELATION_THEME_GROUPS = 'themes.group';
|
|
|
|
final public const string RELATION_VIDEOS = 'animethemes.animethemeentries.videos';
|
|
|
|
/**
|
|
* The event map for the model.
|
|
*
|
|
* Allows for object-based events for native Eloquent events.
|
|
*
|
|
* @var array<string, class-string>
|
|
*/
|
|
protected $dispatchesEvents = [
|
|
'created' => SongCreated::class,
|
|
'deleted' => SongDeleted::class,
|
|
'forceDeleting' => SongForceDeleting::class,
|
|
'restored' => SongRestored::class,
|
|
'updated' => SongUpdated::class,
|
|
];
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
Song::ATTRIBUTE_TITLE,
|
|
Song::ATTRIBUTE_TITLE_NATIVE,
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
Song::ATTRIBUTE_TITLE => 'string',
|
|
Song::ATTRIBUTE_TITLE_NATIVE => 'string',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get the indexable data array for the model.
|
|
*/
|
|
public function toSearchableArray(): array
|
|
{
|
|
return match ($driver = Config::get('scout.driver')) {
|
|
'collection',
|
|
'typesense' => $this->toTypesenseArray(),
|
|
default => throw new RuntimeException("Unsupported {$driver} search driver configured."),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function toTypesenseArray(): array
|
|
{
|
|
return [
|
|
'id' => (string) $this->getKey(),
|
|
'title' => $this->title,
|
|
// So TypeSense does not boost when alternative titles are the same.
|
|
'title_native' => $this->title_native !== $this->title ? $this->title_native : null,
|
|
'created_at' => $this->created_at->timestamp,
|
|
];
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
if (blank($this->title)) {
|
|
return strval($this->getKey());
|
|
}
|
|
|
|
return $this->title;
|
|
}
|
|
|
|
public function getSubtitle(): string
|
|
{
|
|
if ($this->themes()->count() !== 0 && $this->themes->first()->anime !== null) {
|
|
return "{$this->themes->first()->anime->getName()} {$this->themes->first()->slug}";
|
|
}
|
|
|
|
return $this->title_native ?? strval($this->getKey());
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<Theme, $this>
|
|
*/
|
|
#[Deprecated('Use themes() instead. Required for JSON:API.')]
|
|
public function animethemes(): HasMany
|
|
{
|
|
return $this->themes();
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<Theme, $this>
|
|
*/
|
|
public function themes(): HasMany
|
|
{
|
|
return $this->hasMany(Theme::class, Theme::ATTRIBUTE_SONG);
|
|
}
|
|
|
|
#[Deprecated]
|
|
public function artists(): BelongsToMany
|
|
{
|
|
$sub = Performance::query()
|
|
->selectRaw('MIN(id) as song_staff_id')
|
|
->groupBy(Performance::ATTRIBUTE_SONG, Performance::ATTRIBUTE_ARTIST);
|
|
|
|
return $this->belongsToMany(Artist::class, Performance::TABLE, Performance::ATTRIBUTE_SONG, Performance::ATTRIBUTE_ARTIST)
|
|
->joinSub($sub, 'unique_performances', function ($join): void {
|
|
$join->on('song_staff.id', '=', 'unique_performances.song_staff_id');
|
|
})
|
|
->as('artistsong')
|
|
->withPivot([
|
|
Performance::ATTRIBUTE_ID,
|
|
Performance::ATTRIBUTE_ALIAS,
|
|
Performance::ATTRIBUTE_AS,
|
|
Performance::ATTRIBUTE_ROLE,
|
|
])
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<Performance, $this>
|
|
*/
|
|
public function performances(): HasMany
|
|
{
|
|
return $this->hasMany(Performance::class, Performance::ATTRIBUTE_SONG);
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<SongStaff, $this>
|
|
*/
|
|
public function staff(): HasMany
|
|
{
|
|
return $this->hasMany(SongStaff::class, SongStaff::ATTRIBUTE_SONG);
|
|
}
|
|
|
|
/**
|
|
* @return MorphToMany<ExternalResource, $this, Resourceable, 'songresource'>
|
|
*/
|
|
public function resources(): MorphToMany
|
|
{
|
|
return $this->morphToMany(ExternalResource::class, Resourceable::RELATION_RESOURCEABLE, Resourceable::TABLE, Resourceable::ATTRIBUTE_RESOURCEABLE_ID, Resourceable::ATTRIBUTE_RESOURCE)
|
|
->using(Resourceable::class)
|
|
->as('songresource')
|
|
->withPivot([Resourceable::ATTRIBUTE_ID, Resourceable::ATTRIBUTE_AS])
|
|
->withTimestamps();
|
|
}
|
|
}
|