2016-09-09 16:59:13 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
|
|
|
use App\Album;
|
|
|
|
use App\Facade\UserConfig;
|
2017-03-21 21:48:55 +00:00
|
|
|
use Illuminate\Database\Query\Builder;
|
2016-09-09 16:59:13 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
class DbHelper
|
|
|
|
{
|
2017-09-10 14:01:20 +01:00
|
|
|
private static $allowedAlbumIDs = null;
|
|
|
|
|
2017-09-10 11:24:44 +01:00
|
|
|
public static function getAlbumIDsForCurrentUser()
|
|
|
|
{
|
2017-09-10 14:01:20 +01:00
|
|
|
if (is_null(self::$allowedAlbumIDs))
|
|
|
|
{
|
|
|
|
$query = self::getAlbumsForCurrentUser_NonPaged();
|
|
|
|
$query->select('albums.id');
|
2017-09-10 11:24:44 +01:00
|
|
|
|
2017-09-10 14:01:20 +01:00
|
|
|
$ids = [];
|
2017-09-10 11:24:44 +01:00
|
|
|
|
2017-09-10 14:01:20 +01:00
|
|
|
foreach ($query->get() as $album)
|
|
|
|
{
|
|
|
|
$ids[] = $album->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$allowedAlbumIDs = $ids;
|
2017-09-10 11:24:44 +01:00
|
|
|
}
|
|
|
|
|
2017-09-10 14:01:20 +01:00
|
|
|
return self::$allowedAlbumIDs;
|
2017-09-10 11:24:44 +01:00
|
|
|
}
|
2017-09-16 08:26:05 +01:00
|
|
|
|
2017-04-17 17:11:59 +01:00
|
|
|
public static function getAlbumsForCurrentUser($parentID = -1)
|
2017-04-10 17:29:45 +01:00
|
|
|
{
|
2017-09-29 13:57:45 +01:00
|
|
|
$query = self::getAlbumsForCurrentUser_NonPaged('list', $parentID);
|
2017-04-17 17:11:59 +01:00
|
|
|
|
|
|
|
return $query->paginate(UserConfig::get('items_per_page'));
|
2017-04-10 17:29:45 +01:00
|
|
|
}
|
|
|
|
|
2017-09-29 13:57:45 +01:00
|
|
|
public static function getAlbumsForCurrentUser_NonPaged($permission = 'list', $parentAlbumID = -1)
|
2016-09-09 16:59:13 +01:00
|
|
|
{
|
2017-03-21 21:48:55 +00:00
|
|
|
$albumsQuery = Album::query();
|
2016-09-09 16:59:13 +01:00
|
|
|
$user = Auth::user();
|
|
|
|
|
2017-03-21 21:48:55 +00:00
|
|
|
if (!is_null($user) && $user->is_admin)
|
|
|
|
{
|
|
|
|
/* Admin users always get everything, therefore no filters are necessary */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-16 08:41:36 +01:00
|
|
|
$helper = new PermissionsHelper();
|
|
|
|
$albumIDs = $helper->getAlbumIDs($permission, $user);
|
|
|
|
//dd($albumIDs->toArray());
|
|
|
|
$albumsQuery->whereIn('albums.id', $albumIDs);
|
|
|
|
//
|
2017-03-21 21:48:55 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 13:57:45 +01:00
|
|
|
$parentAlbumID = intval($parentAlbumID);
|
|
|
|
if ($parentAlbumID == 0)
|
|
|
|
{
|
|
|
|
$albumsQuery->where('albums.parent_album_id', null);
|
|
|
|
}
|
|
|
|
else if ($parentAlbumID > 0)
|
|
|
|
{
|
|
|
|
$albumsQuery->where('albums.parent_album_id', $parentAlbumID);
|
|
|
|
}
|
|
|
|
|
2017-03-21 21:48:55 +00:00
|
|
|
return $albumsQuery->select('albums.*')
|
|
|
|
->distinct()
|
2016-09-09 16:59:13 +01:00
|
|
|
->orderBy('name')
|
2017-04-10 17:29:45 +01:00
|
|
|
->withCount('photos');
|
2016-09-09 16:59:13 +01:00
|
|
|
}
|
2017-04-09 09:07:18 +01:00
|
|
|
|
2017-09-03 08:54:49 +01:00
|
|
|
public static function getAlbumById($albumID)
|
|
|
|
{
|
|
|
|
return Album::where('id', $albumID)->first();
|
|
|
|
}
|
|
|
|
|
2017-04-17 21:31:45 +01:00
|
|
|
public static function getAlbumByPath($urlPath)
|
2017-04-09 09:07:18 +01:00
|
|
|
{
|
2017-04-17 21:31:45 +01:00
|
|
|
return Album::where('url_path', $urlPath)->first();
|
2017-04-09 09:07:18 +01:00
|
|
|
}
|
2017-09-29 13:57:45 +01:00
|
|
|
|
|
|
|
public static function getChildAlbumsCount(Album $album)
|
|
|
|
{
|
|
|
|
return self::getAlbumsForCurrentUser_NonPaged('list', $album->id)->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getChildAlbums(Album $album)
|
|
|
|
{
|
|
|
|
return self::getAlbumsForCurrentUser_NonPaged('list', $album->id)->get();
|
|
|
|
}
|
2016-09-09 16:59:13 +01:00
|
|
|
}
|