koel/app/Models/Album.php

194 lines
5.9 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
2022-06-10 10:47:46 +00:00
use Carbon\Carbon;
use Illuminate\Contracts\Database\Query\Builder as BuilderContract;
2020-09-06 18:21:39 +00:00
use Illuminate\Database\Eloquent\Builder;
2016-08-03 10:42:11 +00:00
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2015-12-13 04:42:28 +00:00
use Illuminate\Database\Eloquent\Model;
2017-08-05 18:55:02 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
2022-06-10 10:47:46 +00:00
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Facades\DB;
2020-12-23 10:53:00 +00:00
use Laravel\Scout\Searchable;
2015-12-13 04:42:28 +00:00
/**
2020-12-23 10:53:00 +00:00
* @property string $cover The album cover's file name
* @property string|null $cover_path The absolute path to the cover file
* @property bool $has_cover If the album has a non-default cover image
2020-12-22 20:11:22 +00:00
* @property int $id
2020-12-23 10:53:00 +00:00
* @property string $name Name of the album
2020-12-22 20:11:22 +00:00
* @property bool $is_compilation If the album is a compilation from multiple artists
2020-12-23 10:53:00 +00:00
* @property Artist $artist The album's artist
2020-12-22 20:11:22 +00:00
* @property int $artist_id
* @property Collection $songs
2020-12-23 10:53:00 +00:00
* @property bool $is_unknown If the album is the Unknown Album
2020-09-06 21:20:42 +00:00
* @property string|null $thumbnail_name The file name of the album's thumbnail
2020-12-22 20:11:22 +00:00
* @property string|null $thumbnail_path The full path to the thumbnail.
* Notice that this doesn't guarantee the thumbnail exists.
2020-12-23 10:53:00 +00:00
* @property string|null $thumbnail The public URL to the album's thumbnail
2022-06-10 10:47:46 +00:00
* @property Carbon $created_at
2019-08-05 10:57:36 +00:00
*
2019-11-29 21:23:30 +00:00
* @method static self firstOrCreate(array $where, array $params = [])
2020-06-12 15:05:18 +00:00
* @method static self|null find(int $id)
2020-09-06 18:21:39 +00:00
* @method static Builder where(...$params)
* @method static self first()
* @method static Builder whereArtistIdAndName(int $id, string $name)
2021-06-05 10:47:56 +00:00
* @method static orderBy(...$params)
2022-06-10 10:47:46 +00:00
* @method static Builder latest()
2015-12-13 04:42:28 +00:00
*/
class Album extends Model
{
use HasFactory;
2020-12-23 10:53:00 +00:00
use Searchable;
2016-09-26 07:32:16 +00:00
use SupportsDeleteWhereIDsNotIn;
2020-12-22 20:11:22 +00:00
public const UNKNOWN_ID = 1;
public const UNKNOWN_NAME = 'Unknown Album';
public const UNKNOWN_COVER = 'unknown-album.png';
2015-12-13 04:42:28 +00:00
protected $guarded = ['id'];
protected $hidden = ['updated_at'];
2016-05-19 15:31:02 +00:00
protected $casts = ['artist_id' => 'integer'];
2017-04-23 16:01:02 +00:00
protected $appends = ['is_compilation'];
2015-12-13 04:42:28 +00:00
/**
* Get an album using some provided information.
2018-08-24 15:27:19 +00:00
* If such is not found, a new album will be created using the information.
2015-12-13 04:42:28 +00:00
*/
public static function getOrCreate(Artist $artist, ?string $name = null, bool $isCompilation = false): self
2015-12-13 04:42:28 +00:00
{
2016-04-17 15:38:06 +00:00
// If this is a compilation album, its artist must be "Various Artists"
if ($isCompilation) {
2017-06-03 23:21:50 +00:00
$artist = Artist::getVariousArtist();
2016-04-17 15:38:06 +00:00
}
2015-12-13 04:42:28 +00:00
2018-08-24 15:27:19 +00:00
return static::firstOrCreate([
2015-12-13 04:42:28 +00:00
'artist_id' => $artist->id,
'name' => trim($name) ?: self::UNKNOWN_NAME,
2015-12-13 04:42:28 +00:00
]);
}
2021-06-05 10:47:56 +00:00
public function artist(): BelongsTo
{
return $this->belongsTo(Artist::class);
}
public function songs(): HasMany
{
return $this->hasMany(Song::class);
}
public function getIsUnknownAttribute(): bool
{
return $this->id === self::UNKNOWN_ID;
}
2018-08-24 15:27:19 +00:00
public function setCoverAttribute(?string $value): void
2015-12-13 04:42:28 +00:00
{
$this->attributes['cover'] = $value ?: self::UNKNOWN_COVER;
}
2018-08-24 15:27:19 +00:00
public function getCoverAttribute(?string $value): string
2015-12-13 04:42:28 +00:00
{
2020-06-13 12:19:24 +00:00
return album_cover_url($value ?: self::UNKNOWN_COVER);
2015-12-13 04:42:28 +00:00
}
2018-08-24 15:27:19 +00:00
public function getHasCoverAttribute(): bool
2015-12-13 04:42:28 +00:00
{
2018-02-05 08:52:14 +00:00
$cover = array_get($this->attributes, 'cover');
if (!$cover) {
2018-02-04 15:53:40 +00:00
return false;
}
2018-02-05 08:52:14 +00:00
if ($cover === self::UNKNOWN_COVER) {
2018-02-04 15:53:40 +00:00
return false;
}
2020-06-13 12:19:24 +00:00
return file_exists(album_cover_path($cover));
2015-12-13 04:42:28 +00:00
}
2018-08-29 06:15:11 +00:00
public function getCoverPathAttribute(): ?string
{
$cover = array_get($this->attributes, 'cover');
2020-06-13 12:19:24 +00:00
return $cover ? album_cover_path($cover) : null;
2018-08-29 06:15:11 +00:00
}
2015-12-13 04:42:28 +00:00
/**
* Sometimes the tags extracted from getID3 are HTML entity encoded.
* This makes sure they are always sane.
*/
2018-08-24 15:27:19 +00:00
public function getNameAttribute(string $value): string
2015-12-13 04:42:28 +00:00
{
2015-12-16 05:03:48 +00:00
return html_entity_decode($value);
2015-12-13 04:42:28 +00:00
}
2016-05-19 15:31:02 +00:00
2018-08-24 15:27:19 +00:00
public function getIsCompilationAttribute(): bool
2016-05-19 15:31:02 +00:00
{
return $this->artist_id === Artist::VARIOUS_ID;
}
public function getThumbnailNameAttribute(): ?string
{
if (!$this->has_cover) {
return null;
}
$parts = pathinfo($this->cover_path);
return sprintf('%s_thumb.%s', $parts['filename'], $parts['extension']);
}
public function getThumbnailPathAttribute(): ?string
{
2020-06-13 12:19:24 +00:00
return $this->thumbnail_name ? album_cover_path($this->thumbnail_name) : null;
}
public function getThumbnailAttribute(): ?string
{
2020-06-13 12:19:24 +00:00
return $this->thumbnail_name ? album_cover_url($this->thumbnail_name) : null;
}
2020-12-23 10:53:00 +00:00
2022-06-10 10:47:46 +00:00
public function scopeIsStandard(Builder $query): Builder
{
return $query->whereNot('albums.id', self::UNKNOWN_ID);
}
public static function withMeta(User $scopedUser): BuilderContract
{
return static::query()
->with('artist')
->leftJoin('songs', 'albums.id', '=', 'songs.album_id')
->leftJoin('interactions', static function (JoinClause $join) use ($scopedUser): void {
$join->on('songs.id', '=', 'interactions.song_id')
->where('interactions.user_id', $scopedUser->id);
})
->groupBy('albums.id')
->select(
'albums.*',
DB::raw('CAST(SUM(interactions.play_count) AS INTEGER) AS play_count')
)
->withCount('songs AS song_count')
->withSum('songs AS length', 'length');
}
2020-12-23 10:53:00 +00:00
/** @return array<mixed> */
public function toSearchableArray(): array
{
$array = [
'id' => $this->id,
'name' => $this->name,
];
if (!$this->artist->is_unknown && !$this->artist->is_various) {
$array['artist'] = $this->artist->name;
}
return $array;
}
2015-12-13 04:42:28 +00:00
}