2016-09-01 16:23:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2019-09-10 15:11:53 +01:00
|
|
|
use App\AlbumSources\AlbumSourceBase;
|
2016-09-02 22:00:42 +01:00
|
|
|
use App\AlbumSources\IAlbumSource;
|
|
|
|
use App\AlbumSources\LocalFilesystemSource;
|
2017-04-17 21:31:45 +01:00
|
|
|
use App\Helpers\MiscHelper;
|
2016-09-01 16:23:39 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
2017-09-03 08:31:31 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
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 = [
|
2018-09-16 09:12:35 +01:00
|
|
|
'name', 'description', 'url_alias', 'user_id', 'storage_id', 'default_view', 'parent_album_id', 'url_path', 'is_permissions_inherited'
|
2016-09-01 16:23:39 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = [
|
|
|
|
];
|
|
|
|
|
2017-04-17 21:31:45 +01:00
|
|
|
/**
|
|
|
|
* Gets an array of parent items
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function albumParentTree()
|
|
|
|
{
|
|
|
|
$albums = [];
|
|
|
|
$current = $this;
|
|
|
|
|
|
|
|
while (!is_null($current))
|
|
|
|
{
|
|
|
|
$albums[] = $current;
|
|
|
|
|
|
|
|
$current = $current->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
$albums = array_reverse($albums);
|
|
|
|
return $albums;
|
|
|
|
}
|
|
|
|
|
2017-02-17 11:38:10 +00:00
|
|
|
public function anonymousPermissions()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Permission::class, 'album_anonymous_permissions');
|
|
|
|
}
|
|
|
|
|
2017-09-03 08:31:31 +01:00
|
|
|
public function cameras()
|
|
|
|
{
|
|
|
|
return DB::table('photos')
|
2017-09-05 21:43:58 +01:00
|
|
|
->where([
|
|
|
|
['album_id', $this->id],
|
|
|
|
['camera_make', '!=', ''],
|
|
|
|
['camera_model', '!=', '']
|
|
|
|
])
|
2017-09-03 08:31:31 +01:00
|
|
|
->groupBy('camera_make', 'camera_model', 'camera_software')
|
|
|
|
->select('camera_make', 'camera_model', 'camera_software', DB::raw('count(*) as photo_count'))
|
|
|
|
->orderBy('photo_count', 'desc')
|
|
|
|
->orderBy('camera_make')
|
|
|
|
->orderBy('camera_model')
|
|
|
|
->orderBy('camera_software')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
2017-04-17 17:11:59 +01:00
|
|
|
public function children()
|
|
|
|
{
|
2017-04-17 21:53:47 +01:00
|
|
|
return $this->hasMany(Album::class, 'parent_album_id')->withCount('photos');
|
2017-04-17 17:11:59 +01:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-21 21:48:55 +00:00
|
|
|
public function doesUserHavePermission($user, Permission $permission)
|
|
|
|
{
|
|
|
|
// User will be null for anonymous users
|
|
|
|
if (is_null($user))
|
|
|
|
{
|
|
|
|
return $this->anonymousPermissions()->where(['permission_id' => $permission->id])->count() > 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $this->userPermissions()->where([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'permission_id' => $permission->id
|
|
|
|
])->count() > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 22:11:53 +01:00
|
|
|
/**
|
|
|
|
* Try and locate the parent album ID that permissions are inherited from.
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function effectiveAlbumIDForPermissions()
|
|
|
|
{
|
|
|
|
$current = $this;
|
|
|
|
|
|
|
|
while (!is_null($current->parent_album_id))
|
|
|
|
{
|
|
|
|
if ($current->is_permissions_inherited)
|
|
|
|
{
|
|
|
|
$current = $current->parent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:03 +01:00
|
|
|
if (is_null($current->parent_album_id) && $current->is_permissions_inherited)
|
|
|
|
{
|
|
|
|
// Use default permissions list
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-16 22:11:53 +01:00
|
|
|
return $current->id;
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:27:50 +01:00
|
|
|
public function generateAlias()
|
|
|
|
{
|
2017-04-17 21:31:45 +01:00
|
|
|
$this->url_alias = MiscHelper::capitaliseWord(preg_replace('/[^a-z0-9\-]/', '-', strtolower($this->name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generateUrlPath()
|
|
|
|
{
|
|
|
|
$parts = [];
|
|
|
|
$current = $this;
|
|
|
|
|
|
|
|
while (!is_null($current))
|
|
|
|
{
|
|
|
|
$parts[] = $current->url_alias;
|
|
|
|
|
|
|
|
$current = $current->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts = array_reverse($parts);
|
|
|
|
$this->url_path = join('/', $parts);
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|
|
|
|
|
2016-09-02 22:00:42 +01:00
|
|
|
/**
|
|
|
|
* @return IAlbumSource
|
|
|
|
*/
|
|
|
|
public function getAlbumSource()
|
2016-09-02 21:27:50 +01:00
|
|
|
{
|
2019-09-10 15:11:53 +01:00
|
|
|
$source = AlbumSourceBase::make($this->storage->source);
|
2016-09-28 20:13:18 +01:00
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2017-04-17 17:11:59 +01:00
|
|
|
/**
|
|
|
|
* Returns true if this album is a descendant of the given album.
|
|
|
|
* @param Album $album
|
|
|
|
*/
|
|
|
|
public function isChildOf(Album $album)
|
|
|
|
{
|
|
|
|
$currentAlbum = $this;
|
|
|
|
while (!is_null($currentAlbum))
|
|
|
|
{
|
|
|
|
if ($currentAlbum->parent_album_id == $album->id)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$currentAlbum = Album::where('id', $currentAlbum->parent_album_id)->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parent()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Album::class, 'parent_album_id');
|
|
|
|
}
|
|
|
|
|
2016-09-03 22:13:05 +01:00
|
|
|
public function photos()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Photo::class);
|
|
|
|
}
|
2017-09-04 17:23:31 +01:00
|
|
|
|
|
|
|
public function redirects()
|
|
|
|
{
|
|
|
|
return $this->hasMany(AlbumRedirect::class);
|
|
|
|
}
|
2016-09-03 22:13:05 +01:00
|
|
|
|
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)
|
|
|
|
{
|
2018-10-13 04:47:48 +01:00
|
|
|
/** @var Photo $photo */
|
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))
|
|
|
|
{
|
2018-10-13 04:47:48 +01:00
|
|
|
return $photo->thumbnailUrl($thumbnailName);
|
2016-09-03 22:13:05 +01:00
|
|
|
}
|
2016-10-06 16:21:27 +01:00
|
|
|
|
2017-09-29 13:57:45 +01:00
|
|
|
// See if any child albums have an image
|
|
|
|
$images = [];
|
|
|
|
|
|
|
|
/** @var Album $childAlbum */
|
|
|
|
foreach ($this->children as $childAlbum)
|
|
|
|
{
|
|
|
|
if ($childAlbum->photos()->count() > 0)
|
|
|
|
{
|
|
|
|
$images[] = $childAlbum->thumbnailUrl($thumbnailName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($images) == 0)
|
|
|
|
{
|
|
|
|
// 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')
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-10-06 16:21:27 +01:00
|
|
|
return $images[rand(0, count($images) - 1)];
|
2016-09-03 22:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function url()
|
|
|
|
{
|
2017-04-17 21:31:45 +01:00
|
|
|
if (is_null($this->url_path))
|
2017-04-17 17:15:15 +01:00
|
|
|
{
|
2017-04-17 21:31:45 +01:00
|
|
|
$this->generateUrlPath();
|
2017-09-03 08:40:39 +01:00
|
|
|
$this->save();
|
2017-04-17 17:15:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-17 21:31:45 +01:00
|
|
|
return route('viewAlbum', $this->url_path);
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|
2017-03-21 21:48:55 +00:00
|
|
|
|
2018-08-11 09:20:40 +01:00
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
2017-03-21 21:48:55 +00:00
|
|
|
public function userPermissions()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Permission::class, 'album_user_permissions');
|
|
|
|
}
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|