2021-06-26 17:23:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Queries;
|
2021-05-22 15:05:28 +02:00
|
|
|
|
2021-05-23 15:34:36 +02:00
|
|
|
use BookStack\Actions\Favourite;
|
2021-05-22 15:05:28 +02:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
|
|
|
|
|
|
|
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();
|
2021-05-23 15:34:36 +02:00
|
|
|
if (is_null($user) || $user->isDefault()) {
|
2021-05-22 15:05:28 +02:00
|
|
|
return collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->permissionService()
|
2021-05-23 15:34:36 +02:00
|
|
|
->filterRestrictedEntityRelations(Favourite::query(), 'favourites', 'favouritable_id', 'favouritable_type', 'view')
|
|
|
|
->select('favourites.*')
|
|
|
|
->leftJoin('views', function (JoinClause $join) {
|
|
|
|
$join->on('favourites.favouritable_id', '=', 'views.viewable_id');
|
|
|
|
$join->on('favourites.favouritable_type', '=', 'views.viewable_type');
|
|
|
|
$join->where('views.user_id', '=', user()->id);
|
2021-05-22 15:05:28 +02:00
|
|
|
})
|
2021-05-23 15:34:36 +02:00
|
|
|
->orderBy('views.views', 'desc')
|
|
|
|
->where('favourites.user_id', '=', user()->id);
|
2021-05-22 15:05:28 +02:00
|
|
|
|
2021-05-23 15:34:36 +02:00
|
|
|
return $query->with('favouritable')
|
2021-05-23 14:34:08 +02:00
|
|
|
->skip($skip)
|
2021-05-22 15:05:28 +02:00
|
|
|
->take($count)
|
|
|
|
->get()
|
2021-05-23 15:34:36 +02:00
|
|
|
->pluck('favouritable')
|
2021-05-22 15:05:28 +02:00
|
|
|
->filter();
|
|
|
|
}
|
2021-05-23 14:34:08 +02:00
|
|
|
}
|