2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
use App\Builders\AlbumBuilder;
|
2024-02-23 18:36:02 +00:00
|
|
|
use App\Models\Concerns\SupportsDeleteWhereValueNotIn;
|
2022-06-10 10:47:46 +00:00
|
|
|
use Carbon\Carbon;
|
2022-07-05 09:03:11 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
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;
|
2022-07-18 11:00:37 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2024-01-10 00:47:09 +00:00
|
|
|
use Illuminate\Support\Facades\File;
|
2020-12-23 10:53:00 +00:00
|
|
|
use Laravel\Scout\Searchable;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
/**
|
2024-02-24 15:37:01 +00:00
|
|
|
* @property string $cover The album cover's URL
|
2020-12-23 10:53:00 +00:00
|
|
|
* @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
|
|
|
|
* @property Artist $artist The album's artist
|
2020-12-22 20:11:22 +00:00
|
|
|
* @property int $artist_id
|
2024-04-18 11:27:07 +00:00
|
|
|
* @property Collection<array-key, Song> $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
|
2022-07-27 15:32:36 +00:00
|
|
|
* @property float|string $length Total length of the album in seconds (dynamically calculated)
|
|
|
|
* @property int|string $play_count Total number of times the album's songs have been played (dynamically calculated)
|
|
|
|
* @property int|string $song_count Total number of songs on the album (dynamically calculated)
|
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;
|
2022-08-01 10:42:33 +00:00
|
|
|
use SupportsDeleteWhereValueNotIn;
|
2016-09-26 07:32:16 +00:00
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
public const UNKNOWN_ID = 1;
|
|
|
|
public const UNKNOWN_NAME = 'Unknown Album';
|
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'];
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
protected $with = ['artist'];
|
|
|
|
|
2022-07-28 07:37:48 +00:00
|
|
|
/** @deprecated */
|
|
|
|
protected $appends = ['is_compilation'];
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
public static function query(): AlbumBuilder
|
|
|
|
{
|
|
|
|
return parent::query();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function newEloquentBuilder($query): AlbumBuilder
|
|
|
|
{
|
|
|
|
return new AlbumBuilder($query);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2022-08-09 18:45:11 +00:00
|
|
|
public static function getOrCreate(Artist $artist, ?string $name = null): static
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
return static::query()->firstOrCreate([ // @phpstan-ignore-line
|
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
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-07-05 09:03:11 +00:00
|
|
|
protected function isUnknown(): Attribute
|
2021-06-05 10:47:56 +00:00
|
|
|
{
|
2022-07-18 11:00:37 +00:00
|
|
|
return Attribute::get(fn (): bool => $this->id === self::UNKNOWN_ID);
|
2021-06-05 10:47:56 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 09:03:11 +00:00
|
|
|
protected function cover(): Attribute
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-07-18 11:00:37 +00:00
|
|
|
return Attribute::get(static fn (?string $value): ?string => album_cover_url($value));
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 09:03:11 +00:00
|
|
|
protected function hasCover(): Attribute
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2023-04-17 19:45:43 +00:00
|
|
|
return Attribute::get(fn (): bool => $this->cover_path
|
2024-01-10 00:47:09 +00:00
|
|
|
&& (app()->runningUnitTests() || File::exists($this->cover_path)));
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 09:03:11 +00:00
|
|
|
protected function coverPath(): Attribute
|
2018-08-29 06:15:11 +00:00
|
|
|
{
|
2022-07-05 09:03:11 +00:00
|
|
|
return Attribute::get(function () {
|
2022-07-18 11:00:37 +00:00
|
|
|
$cover = Arr::get($this->attributes, 'cover');
|
2018-08-29 06:15:11 +00:00
|
|
|
|
2022-07-05 09:03:11 +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.
|
|
|
|
*/
|
2022-07-05 09:03:11 +00:00
|
|
|
protected function name(): Attribute
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-07-05 09:03:11 +00:00
|
|
|
return Attribute::get(static fn (string $value) => html_entity_decode($value));
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
2016-05-19 15:31:02 +00:00
|
|
|
|
2022-07-05 09:03:11 +00:00
|
|
|
protected function thumbnailName(): Attribute
|
2016-05-19 15:31:02 +00:00
|
|
|
{
|
2022-07-05 09:03:11 +00:00
|
|
|
return Attribute::get(function (): ?string {
|
|
|
|
if (!$this->has_cover) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-06-13 11:00:51 +00:00
|
|
|
|
2022-07-05 09:03:11 +00:00
|
|
|
$parts = pathinfo($this->cover_path);
|
2020-06-13 11:00:51 +00:00
|
|
|
|
2022-07-05 09:03:11 +00:00
|
|
|
return sprintf('%s_thumb.%s', $parts['filename'], $parts['extension']);
|
|
|
|
});
|
2020-06-13 11:00:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 09:03:11 +00:00
|
|
|
protected function thumbnailPath(): Attribute
|
2020-06-13 11:00:51 +00:00
|
|
|
{
|
2022-07-05 09:03:11 +00:00
|
|
|
return Attribute::get(fn () => $this->thumbnail_name ? album_cover_path($this->thumbnail_name) : null);
|
2020-06-13 11:00:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 09:03:11 +00:00
|
|
|
protected function thumbnail(): Attribute
|
2020-06-13 11:00:51 +00:00
|
|
|
{
|
2022-07-05 09:03:11 +00:00
|
|
|
return Attribute::get(fn () => $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
|
|
|
|
2022-07-28 07:37:48 +00:00
|
|
|
/** @deprecated Only here for backward compat with mobile apps */
|
|
|
|
protected function isCompilation(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::get(fn () => $this->artist_id === Artist::VARIOUS_ID);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|