]> BookStack Code Mirror - bookstack/blob - app/Actions/ViewService.php
Added owner field to DummyContentSeeder
[bookstack] / app / Actions / ViewService.php
1 <?php namespace BookStack\Actions;
2
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Models\Entity;
6 use BookStack\Entities\EntityProvider;
7 use DB;
8 use Illuminate\Support\Collection;
9
10 class ViewService
11 {
12     protected $view;
13     protected $permissionService;
14     protected $entityProvider;
15
16     /**
17      * ViewService constructor.
18      * @param View $view
19      * @param PermissionService $permissionService
20      * @param EntityProvider $entityProvider
21      */
22     public function __construct(View $view, PermissionService $permissionService, EntityProvider $entityProvider)
23     {
24         $this->view = $view;
25         $this->permissionService = $permissionService;
26         $this->entityProvider = $entityProvider;
27     }
28
29     /**
30      * Add a view to the given entity.
31      * @param \BookStack\Entities\Models\Entity $entity
32      * @return int
33      */
34     public function add(Entity $entity)
35     {
36         $user = user();
37         if ($user === null || $user->isDefault()) {
38             return 0;
39         }
40         $view = $entity->views()->where('user_id', '=', $user->id)->first();
41         // Add view if model exists
42         if ($view) {
43             $view->increment('views');
44             return $view->views;
45         }
46
47         // Otherwise create new view count
48         $entity->views()->save($this->view->newInstance([
49             'user_id' => $user->id,
50             'views' => 1
51         ]));
52
53         return 1;
54     }
55
56     /**
57      * Get the entities with the most views.
58      * @param int $count
59      * @param int $page
60      * @param string|array $filterModels
61      * @param string $action - used for permission checking
62      * @return Collection
63      */
64     public function getPopular(int $count = 10, int $page = 0, array $filterModels = null, string $action = 'view')
65     {
66         $skipCount = $count * $page;
67         $query = $this->permissionService
68             ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type', $action)
69             ->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count'))
70             ->groupBy('viewable_id', 'viewable_type')
71             ->orderBy('view_count', 'desc');
72
73         if ($filterModels) {
74             $query->whereIn('viewable_type', $this->entityProvider->getMorphClasses($filterModels));
75         }
76
77         return $query->with('viewable')->skip($skipCount)->take($count)->get()->pluck('viewable');
78     }
79
80     /**
81      * Get all recently viewed entities for the current user.
82      */
83     public function getUserRecentlyViewed(int $count = 10, int $page = 1)
84     {
85         $user = user();
86         if ($user === null || $user->isDefault()) {
87             return collect();
88         }
89
90         $all = collect();
91         /** @var Entity $instance */
92         foreach ($this->entityProvider->all() as $name => $instance) {
93             $items = $instance::visible()->withLastView()
94                 ->orderBy('last_viewed_at', 'desc')
95                 ->skip($count * ($page - 1))
96                 ->take($count)
97                 ->get();
98             $all = $all->concat($items);
99         }
100
101         return $all->sortByDesc('last_viewed_at')->slice(0, $count);
102     }
103
104     /**
105      * Reset all view counts by deleting all views.
106      */
107     public function resetAll()
108     {
109         $this->view->truncate();
110     }
111 }