koel/app/Models/Album.php

106 lines
2.9 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
2016-09-26 07:32:16 +00:00
use App\Traits\SupportsDeleteWhereIDsNotIn;
2016-08-03 10:42:11 +00:00
use Illuminate\Database\Eloquent\Collection;
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;
2015-12-13 04:42:28 +00:00
/**
2016-04-17 15:38:06 +00:00
* @property string cover The path to the album's cover
* @property bool has_cover If the album has a cover image
2015-12-13 04:42:28 +00:00
* @property int id
2016-04-17 15:38:06 +00:00
* @property string name Name of the album
* @property bool is_compilation If the album is a compilation from multiple artists
* @property Artist artist The album's artist
2016-06-04 14:17:24 +00:00
* @property int artist_id
2016-08-03 10:42:11 +00:00
* @property Collection songs
2017-06-03 23:21:50 +00:00
* @property bool is_unknown
2015-12-13 04:42:28 +00:00
*/
class Album extends Model
{
2016-09-26 07:32:16 +00:00
use SupportsDeleteWhereIDsNotIn;
2015-12-13 04:42:28 +00:00
const UNKNOWN_ID = 1;
const UNKNOWN_NAME = 'Unknown Album';
const UNKNOWN_COVER = 'unknown-album.png';
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
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
*/
2018-08-24 15:27:19 +00:00
public static function get(Artist $artist, string $name, 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,
2016-04-17 15:38:06 +00:00
'name' => $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
{
return app()->staticUrl('public/img/covers/'.($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;
}
2018-02-05 08:52:14 +00:00
return file_exists(public_path("/public/img/covers/$cover"));
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;
}
2015-12-13 04:42:28 +00:00
}