mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-11-26 02:10:19 +08:00
af0b4fa851
Primarily intended to show parent book for chapters when moving/copying pages, since the default parent selector interfaces, which used the entity-selector search endpoint, would run this popular query when no term was present as a default backup. For #4264
47 lines
1.5 KiB
PHP
47 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Entities\Queries;
|
|
|
|
use BookStack\Activity\Models\View;
|
|
use BookStack\Entities\Models\BookChild;
|
|
use BookStack\Entities\Models\Entity;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class Popular extends EntityQuery
|
|
{
|
|
public function run(int $count, int $page, array $filterModels = null)
|
|
{
|
|
$query = $this->permissionService()
|
|
->restrictEntityRelationQuery(View::query(), 'views', 'viewable_id', 'viewable_type')
|
|
->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count'))
|
|
->groupBy('viewable_id', 'viewable_type')
|
|
->orderBy('view_count', 'desc');
|
|
|
|
if ($filterModels) {
|
|
$query->whereIn('viewable_type', $this->entityProvider()->getMorphClasses($filterModels));
|
|
}
|
|
|
|
$entities = $query->with('viewable')
|
|
->skip($count * ($page - 1))
|
|
->take($count)
|
|
->get()
|
|
->pluck('viewable')
|
|
->filter();
|
|
|
|
$this->loadBooksForChildren($entities);
|
|
|
|
return $entities;
|
|
}
|
|
|
|
protected function loadBooksForChildren(Collection $entities)
|
|
{
|
|
$bookChildren = $entities->filter(fn(Entity $entity) => $entity instanceof BookChild);
|
|
$eloquent = (new \Illuminate\Database\Eloquent\Collection($bookChildren));
|
|
$eloquent->load(['book' => function (BelongsTo $query) {
|
|
$query->scopes('visible');
|
|
}]);
|
|
}
|
|
}
|