1 <?php namespace BookStack\Actions;
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\Models\Entity;
5 use BookStack\Entities\EntityProvider;
7 use Illuminate\Support\Collection;
12 protected $permissionService;
13 protected $entityProvider;
16 * ViewService constructor.
18 * @param PermissionService $permissionService
19 * @param EntityProvider $entityProvider
21 public function __construct(View $view, PermissionService $permissionService, EntityProvider $entityProvider)
24 $this->permissionService = $permissionService;
25 $this->entityProvider = $entityProvider;
29 * Get the entities with the most views.
32 * @param string|array $filterModels
33 * @param string $action - used for permission checking
36 public function getPopular(int $count = 10, int $page = 0, array $filterModels = null, string $action = 'view')
38 $skipCount = $count * $page;
39 $query = $this->permissionService
40 ->filterRestrictedEntityRelations($this->view->newQuery(), 'views', 'viewable_id', 'viewable_type', $action)
41 ->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count'))
42 ->groupBy('viewable_id', 'viewable_type')
43 ->orderBy('view_count', 'desc');
46 $query->whereIn('viewable_type', $this->entityProvider->getMorphClasses($filterModels));
49 return $query->with('viewable')
58 * Get all recently viewed entities for the current user.
60 public function getUserRecentlyViewed(int $count = 10, int $page = 1)
63 if ($user === null || $user->isDefault()) {
68 /** @var Entity $instance */
69 foreach ($this->entityProvider->all() as $name => $instance) {
70 $items = $instance::visible()->withLastView()
71 ->having('last_viewed_at', '>', 0)
72 ->orderBy('last_viewed_at', 'desc')
73 ->skip($count * ($page - 1))
76 $all = $all->concat($items);
79 return $all->sortByDesc('last_viewed_at')->slice(0, $count);