blue-twilight/app/Album.php

77 lines
1.7 KiB
PHP
Raw 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'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
public function fromRequest(Request $request)
{
$this->name = $request->get('name');
$this->description = $request->get('description');
$this->generateAlias();
return $this;
}
public function generateAlias()
{
$this->url_alias = ucfirst(preg_replace('/[^a-z0-9\-]/', '-', strtolower($this->name)));
}
/**
* @return IAlbumSource
*/
public function getAlbumSource()
{
// TODO allow albums to specify different storage locations - e.g. Amazon S3, SFTP/FTP, OpenStack
return new LocalFilesystemSource(dirname(__DIR__) . '/storage/app/albums');
}
public function photos()
{
return $this->hasMany(Photo::class);
}
public function thumbnailUrl($thumbnailName)
{
$photo = Photo::where('album_id', $this->id)->first();
if (!is_null($photo))
{
return $this->getAlbumSource()->getUrlToPhoto($this, $photo, $thumbnailName);
}
return '';
}
public function url()
{
return route('viewAlbum', $this->url_alias);
}
}