2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2016-09-26 07:32:16 +00:00
|
|
|
use App\Traits\SupportsDeleteWhereIDsNotIn;
|
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;
|
2020-11-14 16:57:25 +00:00
|
|
|
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;
|
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
|
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)
|
2020-09-07 20:43:23 +00:00
|
|
|
* @method static self first()
|
2020-11-14 16:57:25 +00:00
|
|
|
* @method static Builder whereArtistIdAndName(int $id, string $name)
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
class Album extends Model
|
|
|
|
{
|
2020-11-14 16:57:25 +00:00
|
|
|
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'];
|
2016-08-07 12:33:46 +00:00
|
|
|
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
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function artist(): BelongsTo
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Artist::class);
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function songs(): HasMany
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
return $this->hasMany(Song::class);
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function getIsUnknownAttribute(): bool
|
2016-02-10 17:01:44 +00:00
|
|
|
{
|
|
|
|
return $this->id === self::UNKNOWN_ID;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2020-11-14 16:57:25 +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,
|
2020-11-14 16:57:25 +00:00
|
|
|
'name' => trim($name) ?: self::UNKNOWN_NAME,
|
2015-12-13 04:42:28 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2020-06-13 11:00:51 +00:00
|
|
|
|
|
|
|
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;
|
2020-06-13 11:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getThumbnailAttribute(): ?string
|
|
|
|
{
|
2020-06-13 12:19:24 +00:00
|
|
|
return $this->thumbnail_name ? album_cover_url($this->thumbnail_name) : null;
|
2020-06-13 11:00:51 +00:00
|
|
|
}
|
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
|
|
|
}
|