diff --git a/app/Helpers/DbHelper.php b/app/Helpers/DbHelper.php index d2aaaff..8b0873e 100644 --- a/app/Helpers/DbHelper.php +++ b/app/Helpers/DbHelper.php @@ -9,6 +9,21 @@ use Illuminate\Support\Facades\Auth; class DbHelper { + public static function getAlbumIDsForCurrentUser() + { + $query = self::getAlbumsForCurrentUser_NonPaged(); + $query->select('albums.id'); + + $ids = []; + + foreach ($query->get() as $album) + { + $ids[] = $album->id; + } + + return $ids; + } + public static function getAlbumsForCurrentUser($parentID = -1) { $query = self::getAlbumsForCurrentUser_NonPaged(); diff --git a/app/Http/Controllers/Gallery/LabelController.php b/app/Http/Controllers/Gallery/LabelController.php new file mode 100644 index 0000000..4900d28 --- /dev/null +++ b/app/Http/Controllers/Gallery/LabelController.php @@ -0,0 +1,66 @@ +first(); + if (is_null($label)) + { + App::abort(404); + } + + $validViews = UserConfig::allowedAlbumViews(); + $requestedView = strtolower($request->get('view')); + if (!in_array($requestedView, $validViews)) + { + $requestedView = $validViews[0]; + } + + $allowedAlbumIDs = DbHelper::getAlbumIDsForCurrentUser(); + + if ($label->photos()->count() == 0) + { + $requestedView = 'empty'; + $photos = []; + } + else if ($requestedView != 'slideshow') + { + $photos = $label->photos() + ->where('album_id', $allowedAlbumIDs) + ->orderBy(DB::raw('COALESCE(photos.taken_at, photos.created_at)')) + ->paginate(UserConfig::get('items_per_page')); + } + else + { + // The slideshow view needs access to all photos, not paged + $photos = $label->photos() + ->where('album_id', $allowedAlbumIDs) + ->orderBy(DB::raw('COALESCE(photos.taken_at, photos.created_at)')) + ->get(); + } + + if (count($photos) == 0) + { + $requestedView = 'empty'; + } + + return Theme::render(sprintf('gallery.label_%s', $requestedView), [ + 'allowed_views' => $validViews, + 'current_view' => $requestedView, + 'label' => $label, + 'photos' => $photos + ]); + } +} \ No newline at end of file diff --git a/app/Label.php b/app/Label.php index 9a1d6a4..c638d0a 100644 --- a/app/Label.php +++ b/app/Label.php @@ -2,6 +2,7 @@ namespace App; +use App\Helpers\MiscHelper; use Illuminate\Database\Eloquent\Model; class Label extends Model @@ -12,9 +13,14 @@ class Label extends Model * @var array */ protected $fillable = [ - 'name' + 'name', 'url_alias' ]; + public function generateAlias() + { + $this->url_alias = preg_replace('/[^a-z0-9\-]/', '-', strtolower($this->name)); + } + public function photos() { return $this->belongsToMany(Photo::class, 'photo_labels'); diff --git a/app/ModelObservers/LabelObserver.php b/app/ModelObservers/LabelObserver.php new file mode 100644 index 0000000..52fb755 --- /dev/null +++ b/app/ModelObservers/LabelObserver.php @@ -0,0 +1,13 @@ +generateAlias(); + } +} \ No newline at end of file diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index d7e3d17..1c98c7d 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -8,7 +8,9 @@ use App\Helpers\ImageHelper; use App\Helpers\MiscHelper; use App\Helpers\ThemeHelper; use App\Helpers\ValidationHelper; +use App\Label; use App\ModelObservers\AlbumObserver; +use App\ModelObservers\LabelObserver; use Illuminate\Database\QueryException; use Illuminate\Mail\Mailer; use Illuminate\Pagination\LengthAwarePaginator; @@ -47,6 +49,7 @@ class AppServiceProvider extends ServiceProvider // Model observers Album::observe(AlbumObserver::class); + Label::observe(LabelObserver::class); // Configure our default pager if (MiscHelper::isAppInstalled()) diff --git a/database/migrations/2017_09_10_080152_create_photo_labels_table.php b/database/migrations/2017_09_10_080152_create_photo_labels_table.php index 1225264..61d000d 100644 --- a/database/migrations/2017_09_10_080152_create_photo_labels_table.php +++ b/database/migrations/2017_09_10_080152_create_photo_labels_table.php @@ -16,6 +16,7 @@ class CreatePhotoLabelsTable extends Migration Schema::create('labels', function ($table) { $table->increments('id'); $table->string('name'); + $table->string('url_alias'); $table->timestamps(); }); diff --git a/resources/lang/en/gallery.php b/resources/lang/en/gallery.php index 9e1453f..c54d758 100644 --- a/resources/lang/en/gallery.php +++ b/resources/lang/en/gallery.php @@ -10,6 +10,10 @@ return [ 'back_to_album' => 'Back to :name', 'index_no_results_heading' => 'Start something amazing', 'index_no_results_text' => 'This gallery is currently empty. If you are the owner of this gallery, you can create new albums and upload photos using the :admin_link.', + 'label_intro' => 'All photos tagged with the label ":name".', + 'label_no_results_text' => 'No photos are tagged with the label ":name".', + 'label_no_results_text_2' => 'If you are an admin of this gallery, you can upload and tag photos in the :admin_link.', + 'labels' => 'Labels:', 'manage_album_link' => 'Manage', 'manage_album_link_2' => 'Manage Album', 'open_album_link' => 'Open Album', diff --git a/resources/views/themes/base/gallery/label_default.blade.php b/resources/views/themes/base/gallery/label_default.blade.php new file mode 100644 index 0000000..4e9af3c --- /dev/null +++ b/resources/views/themes/base/gallery/label_default.blade.php @@ -0,0 +1,46 @@ +@extends('themes.base.layout') +@section('title', $label->name) + +@section('breadcrumb') + + +@endsection + +@section('content') +
+
+
+
+ @include(\App\Facade\Theme::viewName('partials.album_view_selector')) +
+ +

{{ $label->name }}

+

@lang('gallery.label_intro', ['name' => $label->name])

+
+
+
+
+ +
+ @foreach ($photos as $photo) +
+
+ + + +
+
+ @endforeach + +
+
+ {{ $photos->links() }} +
+
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/themes/base/gallery/label_empty.blade.php b/resources/views/themes/base/gallery/label_empty.blade.php new file mode 100644 index 0000000..6c77618 --- /dev/null +++ b/resources/views/themes/base/gallery/label_empty.blade.php @@ -0,0 +1,21 @@ +@extends('themes.base.layout') +@section('title', $label->name) + +@section('breadcrumb') + + +@endsection + +@section('content') +
+
+
+

@lang('gallery.album_no_results_heading')

+

@lang('gallery.label_no_results_text', ['name' => $label->name])

+

@lang('gallery.label_no_results_text_2', ['admin_link' => sprintf('%s', route('admin'), trans('admin.title'))])

+ + +
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/themes/base/gallery/label_slideshow.blade.php b/resources/views/themes/base/gallery/label_slideshow.blade.php new file mode 100644 index 0000000..df3fcda --- /dev/null +++ b/resources/views/themes/base/gallery/label_slideshow.blade.php @@ -0,0 +1,76 @@ +@extends('themes.base.layout') +@section('title', $label->name) + +@section('breadcrumb') + + +@endsection + +@section('content') +
+
+
+
+ @include(\App\Facade\Theme::viewName('partials.album_view_selector')) +
+ +

{{ $label->name }}

+

@lang('gallery.label_intro', ['name' => $label->name])

+
+
+
+
+ +
+
+
+
+ + +
+
+ +
+ + + +
+
+
+ +
+
+
+ @foreach ($photos as $photo) + + {{ $photo->name }} + + @endforeach +
+
+
+
+@endsection + +@push('scripts') + +@endpush diff --git a/resources/views/themes/base/gallery/photo.blade.php b/resources/views/themes/base/gallery/photo.blade.php index d9e27d7..d469817 100644 --- a/resources/views/themes/base/gallery/photo.blade.php +++ b/resources/views/themes/base/gallery/photo.blade.php @@ -29,10 +29,23 @@ @if ($is_original_allowed) @endif - + @if ($is_original_allowed) @endif + + @if ($photo->labels()->count() > 0) +

@lang('gallery.labels')

+ + @endif
diff --git a/routes/web.php b/routes/web.php index 129a62c..bafad61 100644 --- a/routes/web.php +++ b/routes/web.php @@ -88,4 +88,7 @@ Route::get('p/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@show') ->where('albumUrlAlias', '.*'); Route::get('i/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@download') ->name('downloadPhoto') - ->where('albumUrlAlias', '.*'); \ No newline at end of file + ->where('albumUrlAlias', '.*'); +Route::get('label/{labelAlias}', 'Gallery\LabelController@index') + ->name('viewLabel') + ->where('labelAlias', '.*'); \ No newline at end of file