]> BookStack Code Mirror - bookstack/blob - app/Services/ViewService.php
Added basic system tests for markdown editor, Added extra test helpers
[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     protected $restrictionService;
13
14     /**
15      * ViewService constructor.
16      * @param View $view
17      * @param RestrictionService $restrictionService
18      */
19     public function __construct(View $view, RestrictionService $restrictionService)
20     {
21         $this->view = $view;
22         $this->user = auth()->user();
23         $this->restrictionService = $restrictionService;
24     }
25
26     /**
27      * Add a view to the given entity.
28      * @param Entity $entity
29      * @return int
30      */
31     public function add(Entity $entity)
32     {
33         if ($this->user === null) return 0;
34         $view = $entity->views()->where('user_id', '=', $this->user->id)->first();
35         // Add view if model exists
36         if ($view) {
37             $view->increment('views');
38             return $view->views;
39         }
40
41         // Otherwise create new view count
42         $entity->views()->save($this->view->create([
43             'user_id' => $this->user->id,
44             'views' => 1
45         ]));
46
47         return 1;
48     }
49
50
51     /**
52      * Get the entities with the most views.
53      * @param int $count
54      * @param int $page
55      * @param bool|false $filterModel
56      */
57     public function getPopular($count = 10, $page = 0, $filterModel = false)
58     {
59         $skipCount = $count * $page;
60         $query = $this->restrictionService->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type')
61             ->select('id', '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) $query->where('viewable_type', '=', get_class($filterModel));
66
67         $views = $query->with('viewable')->skip($skipCount)->take($count)->get();
68         $viewedEntities = $views->map(function ($item) {
69             return $item->viewable()->getResults();
70         });
71         return $viewedEntities;
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         if ($this->user === null) return collect();
84         $skipCount = $count * $page;
85         $query = $this->restrictionService
86             ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type');
87
88         if ($filterModel) $query = $query->where('viewable_type', '=', get_class($filterModel));
89         $query = $query->where('user_id', '=', auth()->user()->id);
90
91         $views = $query->with('viewable')->orderBy('updated_at', 'desc')->skip($skipCount)->take($count)->get();
92         $viewedEntities = $views->map(function ($item) {
93             return $item->viewable;
94         });
95         return $viewedEntities;
96     }
97
98
99     /**
100      * Reset all view counts by deleting all views.
101      */
102     public function resetAll()
103     {
104         $this->view->truncate();
105     }
106
107
108 }