#29: Number of improvements to Labels to show the count and thumbnail correctly based on the allowed albums

This commit is contained in:
Andy Heathershaw 2017-09-10 14:01:20 +01:00
parent 3254ca1500
commit d9d43e9c29
6 changed files with 71 additions and 13 deletions

View File

@ -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)

View File

@ -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]);
}

View File

@ -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);
}

View File

@ -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();

View File

@ -25,7 +25,7 @@
<span style="font-size: 1.3em;">
<a href="{{ $label->url() }}">{{ $label->name }}</a>
</span><br/>
<p style="margin-bottom: 0;"><b>{{ $label->photos_count }}</b> {{ trans_choice('gallery.photos', $label->photos_count) }}</p>
<p style="margin-bottom: 0;"><b>{{ number_format($label->photos_count, 0) }}</b> {{ trans_choice('gallery.photos', $label->photos_count) }}</p>
</td>
</tr>
@endforeach

View File

@ -19,7 +19,7 @@
</li>
@endif
@if ($g_labels->count() > 0)
@if (count($g_labels) > 0)
<li class="nav-item dropdown ml-2">
<a class="nav-link dropdown-toggle" href="{{ url('/') }}" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fa fa-tags"></i> @lang('navigation.navbar.labels')