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;
|
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-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
|
|
|
|
* @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)
|
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)
|
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
|
|
|
|
{
|
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';
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-07-06 13:08:40 +00:00
|
|
|
public static function getOrCreate(Artist $artist, ?string $name = null): self
|
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
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
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-05 09:03:11 +00:00
|
|
|
return Attribute::get(fn () => $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-05 09:03:11 +00:00
|
|
|
return Attribute::get(static fn (?string $value) => $value ? 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
|
|
|
{
|
2022-07-05 09:03:11 +00:00
|
|
|
return Attribute::get(function () {
|
|
|
|
$cover = array_get($this->attributes, 'cover');
|
2018-02-05 08:52:14 +00:00
|
|
|
|
2022-07-05 09:03:11 +00:00
|
|
|
return $cover && file_exists(album_cover_path($cover));
|
|
|
|
});
|
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 () {
|
|
|
|
$cover = array_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-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
|
|
|
}
|