]> BookStack Code Mirror - bookstack/blob - app/Services/ViewService.php
First spanish translation effort
[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     protected $view;
9     protected $permissionService;
10
11     /**
12      * ViewService constructor.
13      * @param View $view
14      * @param 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()) return 0;
31         $view = $entity->views()->where('user_id', '=', $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' => $user->id,
41             'views' => 1
42         ]));
43
44         return 1;
45     }
46
47     /**
48      * Get the entities with the most views.
49      * @param int $count
50      * @param int $page
51      * @param bool|false|array $filterModel
52      */
53     public function getPopular($count = 10, $page = 0, $filterModel = false)
54     {
55         $skipCount = $count * $page;
56         $query = $this->permissionService->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type')
57             ->select('*', '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 && is_array($filterModel)) {
62             $query->whereIn('viewable_type', $filterModel);
63         } else if ($filterModel) {
64             $query->where('viewable_type', '=', get_class($filterModel));
65         };
66
67         return $query->with('viewable')->skip($skipCount)->take($count)->get()->pluck('viewable');
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         $user = user();
80         if ($user === null || $user->isDefault()) return collect();
81
82         $query = $this->permissionService
83             ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type');
84
85         if ($filterModel) $query = $query->where('viewable_type', '=', get_class($filterModel));
86         $query = $query->where('user_id', '=', $user->id);
87
88         $viewables = $query->with('viewable')->orderBy('updated_at', 'desc')
89             ->skip($count * $page)->take($count)->get()->pluck('viewable');
90         return $viewables;
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 }