]> BookStack Code Mirror - bookstack/blob - app/Services/ViewService.php
Fixes a corner case with exclamation in the ID.
[bookstack] / app / Services / ViewService.php
1 <?php namespace BookStack\Services;
2
3 use BookStack\Entity;
4 use BookStack\View;
5
6 class ViewService
7 {
8     protected $view;
9     protected $permissionService;
10
11     /**
12      * ViewService constructor.
13      * @param View $view
14      * @param PermissionService $permissionService
15      */
16     public function __construct(View $view, PermissionService $permissionService)
17     {
18         $this->view = $view;
19         $this->permissionService = $permissionService;
20     }
21
22     /**
23      * Add a view to the given entity.
24      * @param Entity $entity
25      * @return int
26      */
27     public function add(Entity $entity)
28     {
29         $user = user();
30         if ($user === null || $user->isDefault()) {
31             return 0;
32         }
33         $view = $entity->views()->where('user_id', '=', $user->id)->first();
34         // Add view if model exists
35         if ($view) {
36             $view->increment('views');
37             return $view->views;
38         }
39
40         // Otherwise create new view count
41         $entity->views()->save($this->view->create([
42             'user_id' => $user->id,
43             'views' => 1
44         ]));
45
46         return 1;
47     }
48
49     /**
50      * Get the entities with the most views.
51      * @param int $count
52      * @param int $page
53      * @param bool|false|array $filterModel
54      * @param string $action - used for permission checking
55      * @return
56      */
57     public function getPopular($count = 10, $page = 0, $filterModel = false, $action = 'view')
58     {
59         $skipCount = $count * $page;
60         $query = $this->permissionService->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type', $action)
61             ->select('*', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count'))
62             ->groupBy('viewable_id', 'viewable_type')
63             ->orderBy('view_count', 'desc');
64
65         if ($filterModel && is_array($filterModel)) {
66             $query->whereIn('viewable_type', $filterModel);
67         } else if ($filterModel) {
68             $query->where('viewable_type', '=', get_class($filterModel));
69         }
70
71         return $query->with('viewable')->skip($skipCount)->take($count)->get()->pluck('viewable');
72     }
73
74     /**
75      * Get all recently viewed entities for the current user.
76      * @param int $count
77      * @param int $page
78      * @param Entity|bool $filterModel
79      * @return mixed
80      */
81     public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false)
82     {
83         $user = user();
84         if ($user === null || $user->isDefault()) {
85             return collect();
86         }
87
88         $query = $this->permissionService
89             ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type');
90
91         if ($filterModel) {
92             $query = $query->where('viewable_type', '=', get_class($filterModel));
93         }
94         $query = $query->where('user_id', '=', $user->id);
95
96         $viewables = $query->with('viewable')->orderBy('updated_at', 'desc')
97             ->skip($count * $page)->take($count)->get()->pluck('viewable');
98         return $viewables;
99     }
100
101     /**
102      * Reset all view counts by deleting all views.
103      */
104     public function resetAll()
105     {
106         $this->view->truncate();
107     }
108 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.