blue-twilight/app/Helpers/DbHelper.php

94 lines
2.4 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Helpers;
use App\Album;
use App\Facade\UserConfig;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Auth;
class DbHelper
{
private static $allowedAlbumIDs = null;
public static function getAlbumIDsForCurrentUser()
{
if (is_null(self::$allowedAlbumIDs))
{
$query = self::getAlbumsForCurrentUser_NonPaged();
$query->select('albums.id');
$ids = [];
foreach ($query->get() as $album)
{
$ids[] = $album->id;
}
self::$allowedAlbumIDs = $ids;
}
return self::$allowedAlbumIDs;
}
public static function getAlbumsForCurrentUser($parentID = -1)
{
$query = self::getAlbumsForCurrentUser_NonPaged('list', $parentID);
return $query->paginate(UserConfig::get('items_per_page'));
}
public static function getAlbumsForCurrentUser_NonPaged($permission = 'list', $parentAlbumID = -1)
{
$albumsQuery = Album::query();
$user = Auth::user();
if (!is_null($user) && $user->is_admin)
{
/* Admin users always get everything, therefore no filters are necessary */
}
else
{
$helper = new PermissionsHelper();
$albumIDs = $helper->getAlbumIDs($permission, $user);
//dd($albumIDs->toArray());
$albumsQuery->whereIn('albums.id', $albumIDs);
//
}
$parentAlbumID = intval($parentAlbumID);
if ($parentAlbumID == 0)
{
$albumsQuery->where('albums.parent_album_id', null);
}
else if ($parentAlbumID > 0)
{
$albumsQuery->where('albums.parent_album_id', $parentAlbumID);
}
return $albumsQuery->select('albums.*')
->distinct()
->orderBy('name')
->withCount('photos');
}
public static function getAlbumById($albumID)
{
return Album::where('id', $albumID)->first();
}
public static function getAlbumByPath($urlPath)
{
return Album::where('url_path', $urlPath)->first();
}
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();
}
}