'float', 'mtime' => 'int', 'track' => 'int', 'disc' => 'int', ]; protected $keyType = 'string'; /** * Update song info. * * @param array $ids * @param array $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 */ public static function updateInfo(array $ids, array $data): Collection { /* * A collection of the updated songs. * * @var Collection */ $updatedSongs = collect(); $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) { /** @var Song|null $song */ $song = self::with('album', 'album.artist')->find($id); if (!$song) { continue; } $updatedSongs->push($song->updateSingle( $single ? trim($data['title']) : $song->title, trim($data['albumName'] ?: $song->album->name), trim($data['artistName']) ?: $song->artist->name, $single ? trim($data['lyrics']) : $song->lyrics, $single ? (int) $data['track'] : $song->track, (int) $data['compilationState'] )); } // Our library may have been changed. Broadcast an event to tidy it up if need be. if ($updatedSongs->count()) { event(new LibraryChanged()); } return $updatedSongs; } public function updateSingle( string $title, string $albumName, string $artistName, string $lyrics, int $track, int $compilationState ): self { if ($artistName === Artist::VARIOUS_NAME) { // If the artist name is "Various Artists", it's a compilation song no matter what. $compilationState = 1; // and since we can't determine the real contributing artist, it's "Unknown" $artistName = Artist::UNKNOWN_NAME; } $artist = Artist::getOrCreate($artistName); switch ($compilationState) { case 1: // ALL, or forcing compilation status to be Yes $isCompilation = true; break; case 2: // Keep current compilation status $isCompilation = $this->album->artist_id === Artist::VARIOUS_ID; break; default: $isCompilation = false; break; } $album = Album::getOrCreate($artist, $albumName, $isCompilation); $this->artist_id = $artist->id; $this->album_id = $album->id; $this->title = $title; $this->lyrics = $lyrics; $this->track = $track; $this->save(); // Clean up unnecessary data from the object unset($this->album); unset($this->artist); // and make sure the lyrics is shown $this->makeVisible('lyrics'); return $this; } 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); } /** * Scope a query to only include songs in a given directory. */ public function scopeInDirectory(Builder $query, string $path): Builder { // Make sure the path ends with a directory separator. $path = rtrim(trim($path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; return $query->where('path', 'LIKE', "$path%"); } /** * Sometimes the tags extracted from getID3 are HTML entity encoded. * This makes sure they are always sane. */ public function setTitleAttribute(string $value): void { $this->attributes['title'] = html_entity_decode($value); } /** * Some songs don't have a title. * Fall back to the file name (without extension) for such. */ public function getTitleAttribute(?string $value): string { return $value ?: pathinfo($this->path, PATHINFO_FILENAME); } 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 { return static::withMeta($scopedUser, $query); } /** @return array */ 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; } public function __toString(): string { return $this->id; } }