From 3254ca1500f60c243b8ef5c950eb643650a7ad18 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Sun, 10 Sep 2017 13:21:45 +0100 Subject: [PATCH] #29: Added a new /labels endpoint to display labels and a preview of their photos --- .../Controllers/Gallery/LabelController.php | 8 +++- app/Http/Middleware/GlobalConfiguration.php | 12 ++++++ app/Label.php | 20 ++++++++++ resources/lang/en/gallery.php | 3 ++ resources/lang/en/navigation.php | 1 + .../themes/base/gallery/labels.blade.php | 37 +++++++++++++++++++ .../views/themes/base/gallery/photo.blade.php | 2 +- .../themes/base/partials/navbar.blade.php | 28 ++++++++++++-- routes/web.php | 3 +- 9 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 resources/views/themes/base/gallery/labels.blade.php diff --git a/app/Http/Controllers/Gallery/LabelController.php b/app/Http/Controllers/Gallery/LabelController.php index f476324..5f8ca44 100644 --- a/app/Http/Controllers/Gallery/LabelController.php +++ b/app/Http/Controllers/Gallery/LabelController.php @@ -13,7 +13,13 @@ use Symfony\Component\HttpFoundation\Request; class LabelController extends Controller { - public function index(Request $request, $labelAlias) + public function index(Request $request) + { + $labels = Label::withCount('photos')->orderBy('name')->get(); + return Theme::render('gallery.labels', ['labels' => $labels]); + } + + public function show(Request $request, $labelAlias) { $label = Label::where('url_alias', $labelAlias)->first(); if (is_null($label)) diff --git a/app/Http/Middleware/GlobalConfiguration.php b/app/Http/Middleware/GlobalConfiguration.php index 32c7884..f85358c 100644 --- a/app/Http/Middleware/GlobalConfiguration.php +++ b/app/Http/Middleware/GlobalConfiguration.php @@ -6,6 +6,7 @@ use App\Album; use App\Facade\Theme; use App\Facade\UserConfig; use App\Helpers\DbHelper; +use App\Label; use Closure; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Foundation\Application; @@ -49,6 +50,7 @@ class GlobalConfiguration { $this->addThemeInfoToView(); $this->addAlbumsToView(); + $this->addLabelsToView(); } // Set the default mail configuration as per user's requirements @@ -63,6 +65,16 @@ class GlobalConfiguration View::share('albums', $albums); } + private function addLabelsToView() + { + $NUMBER_TO_SHOW_IN_NAVBAR = 5; + $labelCount = Label::count(); + $labels = Label::withCount('photos')->orderBy('photos_count', 'desc')->limit($NUMBER_TO_SHOW_IN_NAVBAR)->get(); + + View::share('g_labels', $labels); + View::share('g_more_labels', $labelCount - $NUMBER_TO_SHOW_IN_NAVBAR); + } + private function addThemeInfoToView() { $themeInfo = Theme::info(); diff --git a/app/Label.php b/app/Label.php index dee259a..ae6fedc 100644 --- a/app/Label.php +++ b/app/Label.php @@ -26,6 +26,26 @@ class Label extends Model return $this->belongsToMany(Photo::class, 'photo_labels'); } + public function thumbnailUrl($thumbnailName) + { + $photo = $this->photos() + ->inRandomOrder() + ->first(); + + if (!is_null($photo)) + { + return $photo->album->getAlbumSource()->getUrlToPhoto($photo, $thumbnailName); + } + + // 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') + ]; + return $images[rand(0, count($images) - 1)]; + } + public function url() { return route('viewLabel', $this->url_alias); diff --git a/resources/lang/en/gallery.php b/resources/lang/en/gallery.php index c54d758..770f3e6 100644 --- a/resources/lang/en/gallery.php +++ b/resources/lang/en/gallery.php @@ -14,11 +14,14 @@ return [ '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:', + 'labels_title' => 'Labels', 'manage_album_link' => 'Manage', 'manage_album_link_2' => 'Manage Album', 'open_album_link' => 'Open Album', 'other_albums_description' => 'You may also be interested in the following albums.', 'other_albums_heading' => 'More Albums in :album_name', + 'photos' => 'photo|photos', + 'show_more_labels' => '... and :count other|... and :count others', 'statistics' => [ 'album_by_photos' => 'Top 10 largest albums - number of photos', 'album_by_size' => 'Top 10 largest albums - photo size (MB)', diff --git a/resources/lang/en/navigation.php b/resources/lang/en/navigation.php index 76020c1..101a800 100644 --- a/resources/lang/en/navigation.php +++ b/resources/lang/en/navigation.php @@ -27,6 +27,7 @@ return [ 'admin' => 'Manage', 'albums' => 'Albums', 'change_password' => 'Change password', + 'labels' => 'Labels', 'login' => 'Login', 'logout' => 'Logout', 'register' => 'Register', diff --git a/resources/views/themes/base/gallery/labels.blade.php b/resources/views/themes/base/gallery/labels.blade.php new file mode 100644 index 0000000..8212628 --- /dev/null +++ b/resources/views/themes/base/gallery/labels.blade.php @@ -0,0 +1,37 @@ +@extends('themes.base.layout') +@section('title', trans('gallery.labels_title')) + +@section('breadcrumb') + + +@endsection + +@section('content') +
+
+
+

@lang('gallery.labels_title')

+
+
+ + + + @foreach ($labels as $label) + + + + + @endforeach + +
+ + + + {{ $label->name }} +
+

{{ $label->photos_count }} {{ trans_choice('gallery.photos', $label->photos_count) }}

+
+
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/themes/base/gallery/photo.blade.php b/resources/views/themes/base/gallery/photo.blade.php index d469817..c31c96c 100644 --- a/resources/views/themes/base/gallery/photo.blade.php +++ b/resources/views/themes/base/gallery/photo.blade.php @@ -39,7 +39,7 @@