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-10-05 14:49:44 +01:00
|
|
|
'name', 'description', 'url_alias', 'is_private', 'user_id', 'storage_id', 'default_view'
|
2016-09-01 16:23:39 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = [
|
|
|
|
];
|
|
|
|
|
2017-02-17 11:38:10 +00:00
|
|
|
public function anonymousPermissions()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Permission::class, 'album_anonymous_permissions');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doesAnonymousHavePermission(Permission $permission)
|
|
|
|
{
|
|
|
|
return $this->anonymousPermissions()->where(['permission_id' => $permission->id])->count() > 0;
|
|
|
|
}
|
|
|
|
|
2017-02-16 17:32:01 +00:00
|
|
|
public function doesGroupHavePermission(Group $group, Permission $permission)
|
|
|
|
{
|
|
|
|
return $this->groupPermissions()->where([
|
|
|
|
'group_id' => $group->id,
|
|
|
|
'permission_id' => $permission->id
|
|
|
|
])->count() > 0;
|
|
|
|
}
|
|
|
|
|
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-28 20:13:18 +01:00
|
|
|
$fullClassName = sprintf('App\AlbumSources\%s', $this->storage->source);
|
|
|
|
|
|
|
|
/** @var IAlbumSource $source */
|
|
|
|
$source = new $fullClassName;
|
|
|
|
$source->setAlbum($this);
|
|
|
|
$source->setConfiguration($this->storage);
|
|
|
|
|
|
|
|
return $source;
|
2016-09-03 22:13:05 +01:00
|
|
|
}
|
|
|
|
|
2017-02-16 17:32:01 +00:00
|
|
|
public function groupPermissions()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Permission::class, 'album_group_permissions');
|
|
|
|
}
|
|
|
|
|
2016-09-03 22:13:05 +01:00
|
|
|
public function photos()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Photo::class);
|
|
|
|
}
|
|
|
|
|
2016-09-28 20:13:18 +01:00
|
|
|
public function storage()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Storage::class);
|
|
|
|
}
|
|
|
|
|
2016-09-03 22:13:05 +01:00
|
|
|
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
|
|
|
}
|
2016-10-06 16:21:27 +01:00
|
|
|
|
|
|
|
// 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)];
|
2016-09-03 22:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|