BookStack/app/Entities/Queries/TopFavourites.php
Dan Brown d0ff79ea60
Revamped some complex queries, added favourites to home
- Removed old view system and started use of new query classes instead.
- Finished off RelationMultiModelQuery but found it was less efficient
than x-many queries due to the amount of tables being scanned.
Adding now for history but will delete as not used.
- Updated recently viewed to use same query system as popular items
  rather than running and joining x-entities queries.
- Added "Most Viewed Faviourites" listing to homepages.
2021-05-22 14:05:28 +01:00

36 lines
No EOL
1.2 KiB
PHP

<?php namespace BookStack\Entities\Queries;
use BookStack\Actions\View;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Facades\DB;
class TopFavourites extends EntityQuery
{
public function run(int $count, int $page)
{
$user = user();
if ($user === null || $user->isDefault()) {
return collect();
}
$query = $this->permissionService()
->filterRestrictedEntityRelations(View::query(), 'views', 'viewable_id', 'viewable_type', 'view')
->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count'))
->groupBy('viewable_id', 'viewable_type')
->rightJoin('favourites', function (JoinClause $join) {
$join->on('views.viewable_id', '=', 'favourites.favouritable_id');
$join->on('views.viewable_type', '=', 'favourites.favouritable_type');
$join->where('favourites.user_id', '=', user()->id);
})
->orderBy('view_count', 'desc');
return $query->with('viewable')
->skip($count * ($page - 1))
->take($count)
->get()
->pluck('viewable')
->filter();
}
}