Added a new option for albums - private albums. These are only visible (and accessible) to owners.

This commit is contained in:
Andy Heathershaw 2016-09-09 16:59:13 +01:00
parent d8859848f3
commit 3ed309ec01
12 changed files with 73 additions and 27 deletions

View File

@ -19,7 +19,7 @@ class Album extends Model
* @var array
*/
protected $fillable = [
'name', 'description', 'url_alias'
'name', 'description', 'url_alias', 'is_private', 'user_id'
];
/**

33
app/Helpers/DbHelper.php Normal file
View File

@ -0,0 +1,33 @@
<?php
namespace App\Helpers;
use App\Album;
use App\Facade\UserConfig;
use Illuminate\Support\Facades\Auth;
class DbHelper
{
public static function getAlbumsForCurrentUser()
{
$user = Auth::user();
$userId = is_null($user) ? 0 : $user->id;
$albums = Album::where('is_private', false)
->orWhere(function ($query) use ($userId)
{
$query->where('is_private', true)
->where('user_id', $userId);
})
->orderBy('name')
->withCount('photos')
->paginate(UserConfig::get('items_per_page'));
return $albums;
}
public static function loadAlbumByUrlAlias($urlAlias)
{
return Album::where('url_alias', $urlAlias)->first();
}
}

View File

@ -12,6 +12,7 @@ use App\Services\PhotoService;
use App\Upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class AlbumController extends Controller
@ -143,6 +144,10 @@ class AlbumController extends Controller
$album = new Album();
$album->fill($request->only(['name', 'description']));
$album->is_private = (strtolower($request->get('is_private')) == 'on');
$album->user_id = Auth::user()->id;
$album->generateAlias();
$album->save();

View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers\Gallery;
use App\Album;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Helpers\DbHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Http\Request;
@ -14,7 +15,15 @@ class AlbumController extends Controller
{
public function index($albumUrlAlias)
{
$album = AlbumController::loadAlbum($albumUrlAlias);
$album = DbHelper::loadAlbumByUrlAlias($albumUrlAlias);
if (is_null($album))
{
App::abort(404);
return null;
}
$this->authorize('album.view', $album);
$photos = $album->photos()
->orderBy(DB::raw('COALESCE(taken_at, created_at)'))
->paginate(UserConfig::get('items_per_page_admin'));
@ -31,13 +40,6 @@ class AlbumController extends Controller
*/
private static function loadAlbum($urlAlias)
{
$album = Album::where('url_alias', $urlAlias)->first();
if (is_null($album))
{
App::abort(404);
return null;
}
return $album;
}
}

View File

@ -5,16 +5,16 @@ namespace App\Http\Controllers\Gallery;
use App\Album;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Helpers\DbHelper;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class DefaultController extends Controller
{
public function index(Request $request)
{
$albums = Album::orderBy('name')
->withCount('photos')
->paginate(UserConfig::get('items_per_page'));
$albums = DbHelper::getAlbumsForCurrentUser();
return Theme::render('gallery.index', [
'albums' => $albums,

View File

@ -2,6 +2,7 @@
namespace App\Providers;
use App\Album;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
@ -25,6 +26,10 @@ class AuthServiceProvider extends ServiceProvider
{
$this->registerPolicies();
Gate::define('album.view', function ($user, Album $album)
{
return (!$album->is_private || $album->user_id == $user->id);
});
Gate::define('admin-access', function ($user) {
return $user->is_admin;
});

View File

@ -19,7 +19,6 @@ return [
'edit_album' => 'Edit photo album: :album_name',
'edit_album_intro' => 'Photo albums contain individual photographs together in the same way as a physical photo album or memory book.',
'edit_album_intro2' => 'Complete the form below to edit the properties of the album: :album_name.',
'list_albums_name_column' => 'Album name',
'manage_widget' => [
'panel_header' => 'Manage'
],

View File

@ -10,6 +10,7 @@ return [
'name_label' => 'Name:',
'password_label' => 'Password:',
'password_confirm_label' => 'Confirm password:',
'private_album_label' => 'Private album (only visible to me)',
'realname_label' => 'Your name:',
'register_action' => 'Create account',
'remember_me_label' => 'Remember me',

View File

@ -44,8 +44,15 @@
{!! Form::textarea('description', old('description'), ['class' => 'form-control']) !!}
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="is_private">
<strong>@lang('forms.private_album_label')</strong>
</label>
</div>
<div class="form-actions">
<a href="{{ route('admin') }}" class="btn btn-default">@lang('forms.cancel_action')</a>
<a href="{{ route('albums.index') }}" class="btn btn-default">@lang('forms.cancel_action')</a>
{!! Form::submit(trans('forms.create_action'), ['class' => 'btn btn-success']) !!}
</div>
{!! Form::close() !!}

View File

@ -27,19 +27,13 @@
</div>
@else
<table class="table table-hover table-striped">
<thead>
<tr>
<th>@lang('admin.list_albums_name_column')</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach ($albums as $album)
<tr>
<td>
<span style="font-size: 1.3em;"><a href="{{ route('albums.show', ['id' => $album->id]) }}">{{ $album->name }}</a></span><br/>
<span style="font-size: 1.3em;"><a href="{{ route('albums.show', ['id' => $album->id]) }}">{{ $album->name }}</a>@if ($album->is_private) <i class="fa fa-fw fa-lock"></i>@endif</span><br/>
<p>{{ $album->description }}</p>
<p style="margin-bottom: 0;"><b>{{ $album->photos_count }}</b> {{ trans_choice('admin.stats_photos', $album->photos_count) }}</p>
<p style="margin-bottom: 0;"><b>{{ $album->photos_count }}</b> {{ trans_choice('admin.stats_widget.photos', $album->photos_count) }}</p>
</td>
<td class="text-right">
<a href="{{ route('albums.edit', ['id' => $album->id]) }}" class="btn btn-default">@lang('forms.edit_action')</a>
@ -54,8 +48,8 @@
{{ $albums->links() }}
</div>
<div style="margin-top: 10px;">
<a href="{{ route('albums.create') }}" class="btn btn-success"><i class="fa fa-fw fa-plus"></i> @lang('admin.create_album_link')</a>
<div class="pull-right" style="margin-top: 10px;">
<a href="{{ route('albums.create') }}" class="btn btn-success"><i class="fa fa-fw fa-plus"></i> @lang('admin.actions_widget.create_album_link')</a>
</div>
@endif
</div>

View File

@ -26,8 +26,8 @@
@foreach ($photos as $photo)
<div class="col-xs-12 col-sm-4 photo">
<div class="panel panel-default">
<div class="panel-body" style="padding: 0;">
<a href="{{ $photo->url() }}"><img src="{{ $photo->thumbnailUrl('preview') }}" alt="" class="img-responsive"/></a>
<div class="panel-body" style="padding: 4px;">
<a href="{{ $photo->url() }}"><img src="{{ $photo->thumbnailUrl('preview') }}" alt="" class="img-responsive img-rounded"/></a>
</div>
<div class="panel-footer"><b><a href="{{ $photo->url() }}">{{ $photo->name }}</a></b></div>
</div>

View File

@ -12,7 +12,7 @@
<p class="text-center">
@php($albumUrl = $album->thumbnailUrl('preview'))
@if (strlen($albumUrl) > 0)
<a href="{{ $album->url() }}"><img class="img-responsive" src="{{ $albumUrl }}"/></a>
<a href="{{ $album->url() }}"><img class="img-responsive img-rounded" src="{{ $albumUrl }}"/></a>
@endif
</p>
<p>{{ $album->description }}</p>