2021-05-22 15:05:28 +02:00
|
|
|
<?php namespace BookStack\Entities\Queries;
|
|
|
|
|
|
|
|
use BookStack\Actions\View;
|
|
|
|
use Illuminate\Database\Query\JoinClause;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
class TopFavourites extends EntityQuery
|
|
|
|
{
|
2021-05-23 14:34:08 +02:00
|
|
|
public function run(int $count, int $skip = 0)
|
2021-05-22 15:05:28 +02:00
|
|
|
{
|
|
|
|
$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')
|
2021-05-23 14:34:08 +02:00
|
|
|
->skip($skip)
|
2021-05-22 15:05:28 +02:00
|
|
|
->take($count)
|
|
|
|
->get()
|
|
|
|
->pluck('viewable')
|
|
|
|
->filter();
|
|
|
|
}
|
2021-05-23 14:34:08 +02:00
|
|
|
}
|