]> BookStack Code Mirror - bookstack/blob - app/Actions/ViewService.php
Update german translation
[bookstack] / app / Actions / ViewService.php
1 <?php namespace BookStack\Actions;
2
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\Entity;
5
6 class ViewService
7 {
8     protected $view;
9     protected $permissionService;
10
11     /**
12      * ViewService constructor.
13      * @param \BookStack\Actions\View $view
14      * @param \BookStack\Auth\Permissions\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 Entity|false|array $filterModel
54      * @param string $action - used for permission checking
55      * @return
56      */
57     public function getPopular($count = 10, $page = 0, $filterModel = false, $action = 'view')
58     {
59         // TODO - Standardise input filter
60         $skipCount = $count * $page;
61         $query = $this->permissionService->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type', $action)
62             ->select('*', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count'))
63             ->groupBy('viewable_id', 'viewable_type')
64             ->orderBy('view_count', 'desc');
65
66         if ($filterModel && is_array($filterModel)) {
67             $query->whereIn('viewable_type', $filterModel);
68         } else if ($filterModel) {
69             $query->where('viewable_type', '=', $filterModel->getMorphClass());
70         }
71
72         return $query->with('viewable')->skip($skipCount)->take($count)->get()->pluck('viewable');
73     }
74
75     /**
76      * Get all recently viewed entities for the current user.
77      * @param int $count
78      * @param int $page
79      * @param Entity|bool $filterModel
80      * @return mixed
81      */
82     public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false)
83     {
84         $user = user();
85         if ($user === null || $user->isDefault()) {
86             return collect();
87         }
88
89         $query = $this->permissionService
90             ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type');
91
92         if ($filterModel) {
93             $query = $query->where('viewable_type', '=', $filterModel->getMorphClass());
94         }
95         $query = $query->where('user_id', '=', $user->id);
96
97         $viewables = $query->with('viewable')->orderBy('updated_at', 'desc')
98             ->skip($count * $page)->take($count)->get()->pluck('viewable');
99         return $viewables;
100     }
101
102     /**
103      * Reset all view counts by deleting all views.
104      */
105     public function resetAll()
106     {
107         $this->view->truncate();
108     }
109 }