File: /home/imensosw/www/mpl.imenso.co/app/Models/Album.php
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image as ImageLibrary;
class Album extends Model
{
protected $casts = [
'release_date' => 'datetime',
];
public function songs()
{
return $this->morphMany(\App\Models\Song::class, 'songable')->orderBy('orderby');
}
public function artist()
{
return $this->belongsTo(\App\Models\User::class, 'artist_id');
}
public function genres()
{
return $this->belongsToMany(\App\Models\Genre::class, 'album_genres');
}
public function genreIds()
{
$ids = [];
foreach ($this->genres as $genre) {
$ids[] = $genre->id;
}
return $ids;
}
public function images()
{
return $this->morphMany(\App\Models\Image::class, 'imageable');
}
public function coverImage()
{
return $this->image(6);
}
public function image($image_type_id = 0)
{
if ($this->images()->where('type_id', $image_type_id)->count() > 0) {
return $this->images()->where('type_id', $image_type_id)->first();
} else {
$image = new Image;
$image->src = ImageType::findOrFail($image_type_id)->default_image;
return $image;
}
}
public function remove()
{
foreach ($this->songs as $song) {
$song->remove();
}
$this->delete();
}
public static function addNew(Request $request)
{
$album = new self;
$album->name = $request->name;
$album->label = $request->label;
$album->release_date = Carbon::createFromFormat('d/m/Y', $request->release_date);
$album->save();
$genre_ids = explode(':', $request->genres);
$album->setGenres($genre_ids);
return $album;
}
public function edit(Request $request)
{
$this->name = $request->name;
$this->label = $request->label;
$this->release_date = Carbon::createFromFormat('d/m/Y', $request->release_date);
$this->save();
$genre_ids = explode(':', $request->genres);
$this->setGenres($genre_ids);
return $this;
}
public function setGenres($genre_ids)
{
DB::table('album_genres')->where('album_id', $this->id)->delete();
foreach ($genre_ids as $genre_id) {
if (DB::table('album_genres')
->where('album_id', $this->id)
->where('genre_id', $genre_id)
->count() == 0) {
DB::table('album_genres')->insert([
'album_id' => $this->id,
'genre_id' => $genre_id,
]);
}
}
}
public function assignToArtist(User $user)
{
$this->artist_id = $user->id;
$this->save();
}
public function uploadArtworkImage($image)
{
$filename = $this->id.'-'.time().'-artwork.'.$image->getClientOriginalExtension();
$web_location = 'album/'.$filename;
$processed_image = ImageLibrary::make($image->getRealPath());
$image_type = ImageType::findOrFail(6);
$processed_image->resize($image_type->default_size_x, $image_type->default_size_y, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$save = $processed_image->save();
Storage::disk('s3')->put($web_location, $save->__toString());
$this->deleteImage($image_type->id);
$this->setImage($image_type->id, $web_location);
}
public function deleteImage($type_id)
{
if ($this->images()->where('type_id', $type_id)->count() > 0) {
$this->images()->where('type_id', $type_id)->first()->remove();
}
}
public function setImage($type_id, $location)
{
$image = new Image;
$image->src = $location;
$image->imageable_id = $this->id;
$image->imageable_type = self::class;
$image->type_id = $type_id;
$image->save();
}
public function addSong(Song $song)
{
$song->songable_id = $this->id;
$song->songable_type = self::class;
if ($this->songs->count() > 0) {
$last_song = $this->songs()->orderBy('orderby')->get()->last();
$song->orderby = $last_song->orderby + 10;
} else {
$song->orderby = 0;
}
$song->save();
}
public function genreString()
{
if ($this->genres()->count() > 0) {
$genres = [];
foreach ($this->genres as $genre) {
$genres[] = $genre->name;
}
return implode(', ', $genres);
} else {
return 'Not set';
}
}
}