2016-09-01 16:23:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2016-09-02 22:00:42 +01:00
|
|
|
use App\AlbumSources\IAlbumSource;
|
|
|
|
use App\AlbumSources\LocalFilesystemSource;
|
2016-09-01 16:23:39 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
2016-09-04 21:59:32 +01:00
|
|
|
use Illuminate\Support\Facades\Route;
|
2016-09-01 16:23:39 +01:00
|
|
|
|
|
|
|
class Album extends Model
|
|
|
|
{
|
|
|
|
use Notifiable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
2016-09-09 16:59:13 +01:00
|
|
|
'name', 'description', 'url_alias', 'is_private', 'user_id'
|
2016-09-01 16:23:39 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = [
|
|
|
|
];
|
|
|
|
|
2016-09-02 21:27:50 +01:00
|
|
|
public function generateAlias()
|
|
|
|
{
|
|
|
|
$this->url_alias = ucfirst(preg_replace('/[^a-z0-9\-]/', '-', strtolower($this->name)));
|
|
|
|
}
|
|
|
|
|
2016-09-02 22:00:42 +01:00
|
|
|
/**
|
|
|
|
* @return IAlbumSource
|
|
|
|
*/
|
|
|
|
public function getAlbumSource()
|
2016-09-02 21:27:50 +01:00
|
|
|
{
|
2016-09-02 22:00:42 +01:00
|
|
|
// TODO allow albums to specify different storage locations - e.g. Amazon S3, SFTP/FTP, OpenStack
|
2016-09-08 23:22:29 +01:00
|
|
|
return new LocalFilesystemSource($this, dirname(__DIR__) . '/storage/app/albums');
|
2016-09-03 22:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function photos()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Photo::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function thumbnailUrl($thumbnailName)
|
|
|
|
{
|
2016-09-05 14:06:41 +01:00
|
|
|
$photo = $this->photos()
|
|
|
|
->inRandomOrder()
|
|
|
|
->first();
|
2016-09-03 22:13:05 +01:00
|
|
|
|
|
|
|
if (!is_null($photo))
|
|
|
|
{
|
2016-09-08 23:22:29 +01:00
|
|
|
return $this->getAlbumSource()->getUrlToPhoto($photo, $thumbnailName);
|
2016-09-03 22:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function url()
|
|
|
|
{
|
2016-09-04 21:59:32 +01:00
|
|
|
return route('viewAlbum', $this->url_alias);
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|