2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2015-12-17 17:56:48 +00:00
|
|
|
use App\Facades\Util;
|
2016-09-26 07:32:16 +00:00
|
|
|
use App\Traits\SupportsDeleteWhereIDsNotIn;
|
2017-08-05 18:55:02 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2015-12-17 18:00:42 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-08-05 18:55:02 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
/**
|
2015-12-19 16:36:44 +00:00
|
|
|
* @property int id The model ID
|
|
|
|
* @property string name The artist name
|
2016-06-04 14:17:24 +00:00
|
|
|
* @property string image
|
2017-06-03 23:21:50 +00:00
|
|
|
* @property bool is_unknown
|
|
|
|
* @property bool is_various
|
2017-08-05 18:55:02 +00:00
|
|
|
* @property Collection songs
|
2018-02-04 15:53:40 +00:00
|
|
|
* @property bool has_image
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
class Artist 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 Artist';
|
2016-04-17 15:38:06 +00:00
|
|
|
const VARIOUS_ID = 2;
|
|
|
|
const VARIOUS_NAME = 'Various Artists';
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
|
|
|
|
protected $hidden = ['created_at', 'updated_at'];
|
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
/**
|
|
|
|
* An artist can have many albums.
|
|
|
|
*
|
|
|
|
* @return HasMany
|
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
public function albums()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Album::class);
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
/**
|
|
|
|
* An artist can have many songs.
|
|
|
|
* Unless he is Rick Astley.
|
|
|
|
*
|
|
|
|
* @return HasManyThrough
|
|
|
|
*/
|
2016-06-04 11:57:27 +00:00
|
|
|
public function songs()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(Song::class, Album::class);
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
/**
|
|
|
|
* Indicate if the artist is unknown.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-06-03 23:21:50 +00:00
|
|
|
public function getIsUnknownAttribute()
|
2015-12-21 13:49:00 +00:00
|
|
|
{
|
|
|
|
return $this->id === self::UNKNOWN_ID;
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
/**
|
2017-08-05 18:55:53 +00:00
|
|
|
* Indicate if the artist is the special "Various Artists".
|
2017-08-05 18:55:02 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-06-09 23:29:01 +00:00
|
|
|
public function getIsVariousAttribute()
|
2016-04-17 15:38:06 +00:00
|
|
|
{
|
|
|
|
return $this->id === self::VARIOUS_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the "Various Artists" object.
|
|
|
|
*
|
|
|
|
* @return Artist
|
|
|
|
*/
|
2017-06-03 23:21:50 +00:00
|
|
|
public static function getVariousArtist()
|
2016-04-17 15:38:06 +00:00
|
|
|
{
|
|
|
|
return self::find(self::VARIOUS_ID);
|
|
|
|
}
|
|
|
|
|
2015-12-16 05:03:48 +00:00
|
|
|
/**
|
|
|
|
* Sometimes the tags extracted from getID3 are HTML entity encoded.
|
|
|
|
* This makes sure they are always sane.
|
|
|
|
*
|
|
|
|
* @param $value
|
2015-12-17 17:56:48 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2015-12-16 05:03:48 +00:00
|
|
|
*/
|
2015-12-14 13:22:39 +00:00
|
|
|
public function getNameAttribute($value)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2015-12-16 05:03:48 +00:00
|
|
|
return html_entity_decode($value ?: self::UNKNOWN_NAME);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an Artist object from their name.
|
|
|
|
* If such is not found, a new artist will be created.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return Artist
|
|
|
|
*/
|
|
|
|
public static function get($name)
|
|
|
|
{
|
2015-12-17 17:56:48 +00:00
|
|
|
// Remove the BOM from UTF-8/16/32, as it will mess up the database constraints.
|
|
|
|
if ($encoding = Util::detectUTFEncoding($name)) {
|
2015-12-18 02:11:32 +00:00
|
|
|
$name = mb_convert_encoding($name, 'UTF-8', $encoding);
|
2015-12-17 17:56:48 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
$name = trim($name) ?: self::UNKNOWN_NAME;
|
|
|
|
|
|
|
|
return self::firstOrCreate(compact('name'), compact('name'));
|
|
|
|
}
|
2015-12-19 16:36:44 +00:00
|
|
|
|
2015-12-22 09:53:03 +00:00
|
|
|
/**
|
|
|
|
* Turn the image name into its absolute URL.
|
2016-02-10 16:59:29 +00:00
|
|
|
*
|
2015-12-22 09:54:19 +00:00
|
|
|
* @param mixed $value
|
2016-02-10 16:59:29 +00:00
|
|
|
*
|
2015-12-22 09:53:03 +00:00
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getImageAttribute($value)
|
|
|
|
{
|
2018-02-04 15:53:40 +00:00
|
|
|
return $value ? app()->staticUrl("public/img/artists/$value") : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHasImageAttribute()
|
|
|
|
{
|
2018-02-05 08:52:14 +00:00
|
|
|
$image = array_get($this->attributes, 'image');
|
|
|
|
|
|
|
|
if (!$image) {
|
2018-02-04 15:53:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-05 08:52:14 +00:00
|
|
|
if (!file_exists(public_path("public/img/artists/$image"))) {
|
2018-02-04 15:53:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-12-22 09:53:03 +00:00
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|