koel/app/Models/Song.php

265 lines
7.6 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
2016-03-05 09:01:12 +00:00
use App\Events\LibraryChanged;
2017-08-05 16:56:11 +00:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2015-12-13 04:42:28 +00:00
use Illuminate\Database\Eloquent\Model;
2017-08-05 16:56:11 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
2022-06-10 10:47:46 +00:00
use Illuminate\Database\Query\JoinClause;
2018-08-29 06:30:39 +00:00
use Illuminate\Support\Collection;
2020-12-23 10:53:00 +00:00
use Laravel\Scout\Searchable;
2015-12-13 04:42:28 +00:00
/**
2020-12-22 20:11:22 +00:00
* @property string $path
* @property string $title
* @property Album $album
* @property Artist $artist
* @property float $length
* @property string $lyrics
* @property int $track
* @property int $disc
* @property int $album_id
* @property string $id
* @property int $artist_id
* @property int $mtime
2020-12-22 23:01:49 +00:00
* @property int $contributing_artist_id
2019-08-05 10:57:36 +00:00
*
2019-08-05 10:56:48 +00:00
* @method static self updateOrCreate(array $where, array $params)
* @method static Builder select(string $string)
* @method static Builder inDirectory(string $path)
* @method static self first()
2021-06-05 10:47:56 +00:00
* @method static Builder orderBy(...$args)
2020-09-06 18:21:39 +00:00
* @method static int count()
* @method static self|Collection|null find($id)
* @method static Builder take(int $count)
2022-06-10 10:47:46 +00:00
* @method static float|int sum(string $column)
2015-12-13 04:42:28 +00:00
*/
class Song extends Model
{
use HasFactory;
2020-12-23 10:53:00 +00:00
use Searchable;
2016-09-26 07:32:16 +00:00
use SupportsDeleteWhereIDsNotIn;
use SupportsS3;
2016-09-26 07:32:16 +00:00
2021-06-05 10:47:56 +00:00
public $incrementing = false;
2015-12-13 04:42:28 +00:00
protected $guarded = [];
/**
* Attributes to be hidden from JSON outputs.
* Here we specify to hide lyrics as well to save some bandwidth (actually, lots of it).
* Lyrics can then be queried on demand.
*/
protected $hidden = ['lyrics', 'updated_at', 'path', 'mtime'];
2015-12-13 04:42:28 +00:00
protected $casts = [
2015-12-21 02:18:00 +00:00
'length' => 'float',
2016-03-22 08:22:39 +00:00
'mtime' => 'int',
2016-03-28 13:18:09 +00:00
'track' => 'int',
'disc' => 'int',
];
2020-09-06 18:21:39 +00:00
protected $keyType = 'string';
2016-03-05 09:01:12 +00:00
/**
* Update song info.
*
2020-12-22 20:11:22 +00:00
* @param array<string> $ids
* @param array<string> $data the data array, with these supported fields:
* - title
* - artistName
* - albumName
* - lyrics
* All of these are optional, in which case the info will not be changed
* (except for lyrics, which will be emptied)
*
* @return Collection|array<Song>
2016-03-05 09:01:12 +00:00
*/
2018-08-29 06:30:39 +00:00
public static function updateInfo(array $ids, array $data): Collection
2016-03-05 09:01:12 +00:00
{
2016-03-06 04:11:28 +00:00
/*
2017-04-23 16:01:02 +00:00
* A collection of the updated songs.
2016-03-05 09:01:12 +00:00
*
2017-08-05 16:56:11 +00:00
* @var Collection
2016-03-05 09:01:12 +00:00
*/
2017-04-23 16:01:02 +00:00
$updatedSongs = collect();
2016-03-05 09:01:12 +00:00
2016-05-27 03:32:52 +00:00
$ids = (array) $ids;
// If we're updating only one song, take into account the title, lyrics, and track number.
$single = count($ids) === 1;
foreach ($ids as $id) {
2020-12-22 23:01:49 +00:00
/** @var Song|null $song */
2019-06-30 14:22:53 +00:00
$song = self::with('album', 'album.artist')->find($id);
if (!$song) {
2016-03-05 09:01:12 +00:00
continue;
}
2017-04-23 16:01:02 +00:00
$updatedSongs->push($song->updateSingle(
2016-05-27 03:32:52 +00:00
$single ? trim($data['title']) : $song->title,
2016-04-24 04:37:04 +00:00
trim($data['albumName'] ?: $song->album->name),
trim($data['artistName']) ?: $song->artist->name,
$single ? trim($data['lyrics']) : $song->lyrics,
2016-08-16 15:12:35 +00:00
$single ? (int) $data['track'] : $song->track,
(int) $data['compilationState']
2017-04-23 16:01:02 +00:00
));
2016-03-05 09:01:12 +00:00
}
// Our library may have been changed. Broadcast an event to tidy it up if need be.
2017-04-23 16:01:02 +00:00
if ($updatedSongs->count()) {
2016-03-05 09:01:12 +00:00
event(new LibraryChanged());
}
2018-08-29 06:30:39 +00:00
return $updatedSongs;
2016-03-05 09:01:12 +00:00
}
2018-08-24 15:27:19 +00:00
public function updateSingle(
string $title,
string $albumName,
string $artistName,
string $lyrics,
int $track,
int $compilationState
): self {
2016-04-24 04:37:04 +00:00
if ($artistName === Artist::VARIOUS_NAME) {
2017-04-23 16:01:02 +00:00
// If the artist name is "Various Artists", it's a compilation song no matter what.
2016-04-24 04:37:04 +00:00
$compilationState = 1;
2017-04-23 16:01:02 +00:00
// and since we can't determine the real contributing artist, it's "Unknown"
$artistName = Artist::UNKNOWN_NAME;
2016-04-24 04:37:04 +00:00
}
$artist = Artist::getOrCreate($artistName);
2017-04-23 16:01:02 +00:00
switch ($compilationState) {
case 1: // ALL, or forcing compilation status to be Yes
$isCompilation = true;
break;
2020-12-22 20:11:22 +00:00
2017-04-23 16:01:02 +00:00
case 2: // Keep current compilation status
$isCompilation = $this->album->artist_id === Artist::VARIOUS_ID;
break;
2020-12-22 20:11:22 +00:00
2017-04-23 16:01:02 +00:00
default:
$isCompilation = false;
break;
2016-04-24 04:37:04 +00:00
}
$album = Album::getOrCreate($artist, $albumName, $isCompilation);
2016-04-24 04:37:04 +00:00
$this->artist_id = $artist->id;
2016-04-24 04:37:04 +00:00
$this->album_id = $album->id;
2016-05-27 02:41:46 +00:00
$this->title = $title;
2016-04-24 04:37:04 +00:00
$this->lyrics = $lyrics;
$this->track = $track;
$this->save();
2017-04-23 16:01:02 +00:00
// Clean up unnecessary data from the object
unset($this->album);
unset($this->artist);
// and make sure the lyrics is shown
$this->makeVisible('lyrics');
2016-04-24 04:37:04 +00:00
2017-04-23 16:01:02 +00:00
return $this;
2016-04-24 04:37:04 +00:00
}
2021-06-05 10:47:56 +00:00
public function artist(): BelongsTo
{
return $this->belongsTo(Artist::class);
}
public function album(): BelongsTo
{
return $this->belongsTo(Album::class);
}
public function playlists(): BelongsToMany
{
return $this->belongsToMany(Playlist::class);
}
public function interactions(): HasMany
{
return $this->hasMany(Interaction::class);
}
2016-02-02 07:47:00 +00:00
/**
* Scope a query to only include songs in a given directory.
*/
2018-08-24 15:27:19 +00:00
public function scopeInDirectory(Builder $query, string $path): Builder
2016-02-02 07:47:00 +00:00
{
// Make sure the path ends with a directory separator.
2020-12-22 20:11:22 +00:00
$path = rtrim(trim($path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
2016-02-02 07:47:00 +00:00
return $query->where('path', 'LIKE', "$path%");
}
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 setTitleAttribute(string $value): void
2015-12-13 04:42:28 +00:00
{
$this->attributes['title'] = html_entity_decode($value);
}
/**
* Some songs don't have a title.
* Fall back to the file name (without extension) for such.
*/
2018-08-24 15:27:19 +00:00
public function getTitleAttribute(?string $value): string
2015-12-13 04:42:28 +00:00
{
return $value ?: pathinfo($this->path, PATHINFO_FILENAME);
}
2022-06-10 10:47:46 +00:00
public static function withMeta(User $scopedUser, ?Builder $query = null): Builder
{
$query ??= static::query();
return $query
->with('artist', 'album', 'album.artist')
->leftJoin('interactions', static function (JoinClause $join) use ($scopedUser): void {
$join->on('interactions.song_id', '=', 'songs.id')
->where('interactions.user_id', $scopedUser->id);
})
->join('albums', 'songs.album_id', '=', 'albums.id')
->join('artists', 'songs.artist_id', '=', 'artists.id')
->select(
'songs.*',
'albums.name',
'artists.name',
'interactions.liked',
'interactions.play_count'
);
}
public function scopeWithMeta(Builder $query, User $scopedUser): Builder
2015-12-13 04:42:28 +00:00
{
2022-06-10 10:47:46 +00:00
return static::withMeta($scopedUser, $query);
2015-12-13 04:42:28 +00:00
}
2016-04-17 15:38:06 +00:00
2020-12-23 10:53:00 +00:00
/** @return array<mixed> */
public function toSearchableArray(): array
{
$array = [
'id' => $this->id,
'title' => $this->title,
];
if (!$this->artist->is_unknown && !$this->artist->is_various) {
$array['artist'] = $this->artist->name;
}
return $array;
}
2020-12-22 20:11:22 +00:00
public function __toString(): string
2017-06-24 20:46:55 +00:00
{
return $this->id;
}
2015-12-13 04:42:28 +00:00
}