]> BookStack Code Mirror - bookstack/blob - app/Services/ViewService.php
Added auto-suggestions to tag names and values
[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 $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) $query->where('viewable_type', '=', get_class($filterModel));
64
65         return $query->with('viewable')->skip($skipCount)->take($count)->get()->pluck('viewable');
66     }
67
68     /**
69      * Get all recently viewed entities for the current user.
70      * @param int $count
71      * @param int $page
72      * @param Entity|bool $filterModel
73      * @return mixed
74      */
75     public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false)
76     {
77         if ($this->user === null) return collect();
78
79         $query = $this->permissionService
80             ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type');
81
82         if ($filterModel) $query = $query->where('viewable_type', '=', get_class($filterModel));
83         $query = $query->where('user_id', '=', auth()->user()->id);
84
85         $viewables = $query->with('viewable')->orderBy('updated_at', 'desc')
86             ->skip($count * $page)->take($count)->get()->pluck('viewable');
87         return $viewables;
88     }
89
90     /**
91      * Reset all view counts by deleting all views.
92      */
93     public function resetAll()
94     {
95         $this->view->truncate();
96     }
97
98 }