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