diff --git a/app/Helpers/DbHelper.php b/app/Helpers/DbHelper.php index 8b0873e..326ba83 100644 --- a/app/Helpers/DbHelper.php +++ b/app/Helpers/DbHelper.php @@ -9,19 +9,26 @@ use Illuminate\Support\Facades\Auth; class DbHelper { + private static $allowedAlbumIDs = null; + public static function getAlbumIDsForCurrentUser() { - $query = self::getAlbumsForCurrentUser_NonPaged(); - $query->select('albums.id'); - - $ids = []; - - foreach ($query->get() as $album) + if (is_null(self::$allowedAlbumIDs)) { - $ids[] = $album->id; + $query = self::getAlbumsForCurrentUser_NonPaged(); + $query->select('albums.id'); + + $ids = []; + + foreach ($query->get() as $album) + { + $ids[] = $album->id; + } + + self::$allowedAlbumIDs = $ids; } - return $ids; + return self::$allowedAlbumIDs; } public static function getAlbumsForCurrentUser($parentID = -1) diff --git a/app/Http/Controllers/Gallery/LabelController.php b/app/Http/Controllers/Gallery/LabelController.php index 5f8ca44..1bca422 100644 --- a/app/Http/Controllers/Gallery/LabelController.php +++ b/app/Http/Controllers/Gallery/LabelController.php @@ -15,7 +15,14 @@ class LabelController extends Controller { public function index(Request $request) { - $labels = Label::withCount('photos')->orderBy('name')->get(); + $labels = Label::orderBy('name')->get(); + + /** @var Label $label */ + foreach ($labels as $label) + { + $label->photos_count = $label->photoCount(); + } + return Theme::render('gallery.labels', ['labels' => $labels]); } diff --git a/app/Http/Middleware/GlobalConfiguration.php b/app/Http/Middleware/GlobalConfiguration.php index f85358c..037efc0 100644 --- a/app/Http/Middleware/GlobalConfiguration.php +++ b/app/Http/Middleware/GlobalConfiguration.php @@ -69,9 +69,46 @@ class GlobalConfiguration { $NUMBER_TO_SHOW_IN_NAVBAR = 5; $labelCount = Label::count(); - $labels = Label::withCount('photos')->orderBy('photos_count', 'desc')->limit($NUMBER_TO_SHOW_IN_NAVBAR)->get(); + $labels = Label::all(); + $labelsToAdd = []; - View::share('g_labels', $labels); + /** @var Label $label */ + foreach ($labels as $label) + { + $label->photos_count = $label->photoCount(); + $labelsToAdd[] = $label; + } + + // Sort my photo count, then name + usort($labelsToAdd, function(Label $a, Label $b) + { + if ($a->photos_count == $b->photos_count) + { + if ($a->name == $b->name) + { + return 0; + } + else if ($a->name < $b->name) + { + return -1; + } + else if ($a->name > $b->name) + { + return 1; + } + } + else if ($a->photos_count < $b->photos_count) + { + return -1; + } + else if ($a->photos_count > $b->photos_count) + { + return 1; + } + }); + $labelsToAdd = array_slice(array_reverse($labelsToAdd), 0, $NUMBER_TO_SHOW_IN_NAVBAR); + + View::share('g_labels', $labelsToAdd); View::share('g_more_labels', $labelCount - $NUMBER_TO_SHOW_IN_NAVBAR); } diff --git a/app/Label.php b/app/Label.php index ae6fedc..6475af2 100644 --- a/app/Label.php +++ b/app/Label.php @@ -2,6 +2,7 @@ namespace App; +use App\Helpers\DbHelper; use App\Helpers\MiscHelper; use Illuminate\Database\Eloquent\Model; @@ -21,6 +22,11 @@ class Label extends Model $this->url_alias = preg_replace('/[^a-z0-9\-]/', '-', strtolower($this->name)); } + public function photoCount() + { + return $this->photos()->whereIn('album_id', DbHelper::getAlbumIDsForCurrentUser())->count(); + } + public function photos() { return $this->belongsToMany(Photo::class, 'photo_labels'); @@ -29,6 +35,7 @@ class Label extends Model public function thumbnailUrl($thumbnailName) { $photo = $this->photos() + ->whereIn('album_id', DbHelper::getAlbumIDsForCurrentUser()) ->inRandomOrder() ->first(); diff --git a/resources/views/themes/base/gallery/labels.blade.php b/resources/views/themes/base/gallery/labels.blade.php index 8212628..555864f 100644 --- a/resources/views/themes/base/gallery/labels.blade.php +++ b/resources/views/themes/base/gallery/labels.blade.php @@ -25,7 +25,7 @@ {{ $label->name }}
-

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

+

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

@endforeach diff --git a/resources/views/themes/base/partials/navbar.blade.php b/resources/views/themes/base/partials/navbar.blade.php index 68ac221..0bc8b6a 100644 --- a/resources/views/themes/base/partials/navbar.blade.php +++ b/resources/views/themes/base/partials/navbar.blade.php @@ -19,7 +19,7 @@ @endif - @if ($g_labels->count() > 0) + @if (count($g_labels) > 0)