]> BookStack Code Mirror - bookstack/blob - app/Services/ViewService.php
Fixed image tests after amends to url system
[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
9     protected $view;
10     protected $user;
11     protected $permissionService;
12
13     /**
14      * ViewService constructor.
15      * @param View $view
16      * @param PermissionService $permissionService
17      */
18     public function __construct(View $view, PermissionService $permissionService)
19     {
20         $this->view = $view;
21         $this->user = auth()->user();
22         $this->permissionService = $permissionService;
23     }
24
25     /**
26      * Add a view to the given entity.
27      * @param Entity $entity
28      * @return int
29      */
30     public function add(Entity $entity)
31     {
32         if ($this->user === null) return 0;
33         $view = $entity->views()->where('user_id', '=', $this->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' => $this->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         if ($this->user === null) return collect();
82
83         $query = $this->permissionService
84             ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type');
85
86         if ($filterModel) $query = $query->where('viewable_type', '=', get_class($filterModel));
87         $query = $query->where('user_id', '=', auth()->user()->id);
88
89         $viewables = $query->with('viewable')->orderBy('updated_at', 'desc')
90             ->skip($count * $page)->take($count)->get()->pluck('viewable');
91         return $viewables;
92     }
93
94     /**
95      * Reset all view counts by deleting all views.
96      */
97     public function resetAll()
98     {
99         $this->view->truncate();
100     }
101
102 }