]> BookStack Code Mirror - bookstack/blob - app/Services/ViewService.php
Set /app PHP code to PSR-2 standard
[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      */
55     public function getPopular($count = 10, $page = 0, $filterModel = false)
56     {
57         $skipCount = $count * $page;
58         $query = $this->permissionService->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type')
59             ->select('*', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count'))
60             ->groupBy('viewable_id', 'viewable_type')
61             ->orderBy('view_count', 'desc');
62
63         if ($filterModel && is_array($filterModel)) {
64             $query->whereIn('viewable_type', $filterModel);
65         } else if ($filterModel) {
66             $query->where('viewable_type', '=', get_class($filterModel));
67         }
68
69         return $query->with('viewable')->skip($skipCount)->take($count)->get()->pluck('viewable');
70     }
71
72     /**
73      * Get all recently viewed entities for the current user.
74      * @param int $count
75      * @param int $page
76      * @param Entity|bool $filterModel
77      * @return mixed
78      */
79     public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false)
80     {
81         $user = user();
82         if ($user === null || $user->isDefault()) {
83             return collect();
84         }
85
86         $query = $this->permissionService
87             ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type');
88
89         if ($filterModel) {
90             $query = $query->where('viewable_type', '=', get_class($filterModel));
91         }
92         $query = $query->where('user_id', '=', $user->id);
93
94         $viewables = $query->with('viewable')->orderBy('updated_at', 'desc')
95             ->skip($count * $page)->take($count)->get()->pluck('viewable');
96         return $viewables;
97     }
98
99     /**
100      * Reset all view counts by deleting all views.
101      */
102     public function resetAll()
103     {
104         $this->view->truncate();
105     }
106 }