2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Queries;
|
2021-05-22 09:05:28 -04:00
|
|
|
|
2021-05-23 09:34:36 -04:00
|
|
|
use BookStack\Actions\Favourite;
|
2021-05-22 09:05:28 -04:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
|
|
|
|
|
|
|
class TopFavourites extends EntityQuery
|
|
|
|
{
|
2021-05-23 08:34:08 -04:00
|
|
|
public function run(int $count, int $skip = 0)
|
2021-05-22 09:05:28 -04:00
|
|
|
{
|
|
|
|
$user = user();
|
2021-05-23 09:34:36 -04:00
|
|
|
if (is_null($user) || $user->isDefault()) {
|
2021-05-22 09:05:28 -04:00
|
|
|
return collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->permissionService()
|
2022-07-16 14:54:25 -04:00
|
|
|
->restrictEntityRelationQuery(Favourite::query(), 'favourites', 'favouritable_id', 'favouritable_type')
|
2021-05-23 09:34:36 -04:00
|
|
|
->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 09:05:28 -04:00
|
|
|
})
|
2021-05-23 09:34:36 -04:00
|
|
|
->orderBy('views.views', 'desc')
|
|
|
|
->where('favourites.user_id', '=', user()->id);
|
2021-05-22 09:05:28 -04:00
|
|
|
|
2021-05-23 09:34:36 -04:00
|
|
|
return $query->with('favouritable')
|
2021-05-23 08:34:08 -04:00
|
|
|
->skip($skip)
|
2021-05-22 09:05:28 -04:00
|
|
|
->take($count)
|
|
|
|
->get()
|
2021-05-23 09:34:36 -04:00
|
|
|
->pluck('favouritable')
|
2021-05-22 09:05:28 -04:00
|
|
|
->filter();
|
|
|
|
}
|
2021-05-23 08:34:08 -04:00
|
|
|
}
|