blue-twilight/app/Album.php

87 lines
2.0 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App;
use App\AlbumSources\IAlbumSource;
use App\AlbumSources\LocalFilesystemSource;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Route;
class Album extends Model
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'description', 'url_alias', 'is_private', 'user_id', 'storage_id', 'default_view'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
public function generateAlias()
{
$this->url_alias = ucfirst(preg_replace('/[^a-z0-9\-]/', '-', strtolower($this->name)));
}
/**
* @return IAlbumSource
*/
public function getAlbumSource()
{
$fullClassName = sprintf('App\AlbumSources\%s', $this->storage->source);
/** @var IAlbumSource $source */
$source = new $fullClassName;
$source->setAlbum($this);
$source->setConfiguration($this->storage);
return $source;
}
public function photos()
{
return $this->hasMany(Photo::class);
}
public function storage()
{
return $this->belongsTo(Storage::class);
}
public function thumbnailUrl($thumbnailName)
{
$photo = $this->photos()
->inRandomOrder()
->first();
if (!is_null($photo))
{
return $this->getAlbumSource()->getUrlToPhoto($photo, $thumbnailName);
}
// Rotate standard images
$images = [
asset('themes/base/images/empty-album-1.jpg'),
asset('themes/base/images/empty-album-2.jpg'),
asset('themes/base/images/empty-album-3.jpg')
];
return $images[rand(0, count($images) - 1)];
}
public function url()
{
return route('viewAlbum', $this->url_alias);
}
}