]> BookStack Code Mirror - bookstack/blobdiff - app/Services/ViewService.php
Adds overflow:auto to popup content to allow it to scroll in lower res.
[bookstack] / app / Services / ViewService.php
index 55e32fe1c89b190f962b0f57c5ab8c76e09fdf77..770a9e39ffef3387ba10efc975b8749e662e5a5c 100644 (file)
@@ -1,23 +1,22 @@
 <?php namespace BookStack\Services;
 
-
 use BookStack\Entity;
 use BookStack\View;
 
 class ViewService
 {
-
     protected $view;
-    protected $user;
+    protected $permissionService;
 
     /**
      * ViewService constructor.
-     * @param $view
+     * @param View $view
+     * @param PermissionService $permissionService
      */
-    public function __construct(View $view)
+    public function __construct(View $view, PermissionService $permissionService)
     {
         $this->view = $view;
-        $this->user = auth()->user();
+        $this->permissionService = $permissionService;
     }
 
     /**
@@ -27,7 +26,9 @@ class ViewService
      */
     public function add(Entity $entity)
     {
-        $view = $entity->views()->where('user_id', '=', $this->user->id)->first();
+        $user = user();
+        if ($user === null || $user->isDefault()) return 0;
+        $view = $entity->views()->where('user_id', '=', $user->id)->first();
         // Add view if model exists
         if ($view) {
             $view->increment('views');
@@ -36,34 +37,58 @@ class ViewService
 
         // Otherwise create new view count
         $entity->views()->save($this->view->create([
-            'user_id' => $this->user->id,
+            'user_id' => $user->id,
             'views' => 1
         ]));
 
         return 1;
     }
 
+    /**
+     * Get the entities with the most views.
+     * @param int $count
+     * @param int $page
+     * @param bool|false|array $filterModel
+     */
+    public function getPopular($count = 10, $page = 0, $filterModel = false)
+    {
+        $skipCount = $count * $page;
+        $query = $this->permissionService->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type')
+            ->select('*', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count'))
+            ->groupBy('viewable_id', 'viewable_type')
+            ->orderBy('view_count', 'desc');
+
+        if ($filterModel && is_array($filterModel)) {
+            $query->whereIn('viewable_type', $filterModel);
+        } else if ($filterModel) {
+            $query->where('viewable_type', '=', get_class($filterModel));
+        }
+
+        return $query->with('viewable')->skip($skipCount)->take($count)->get()->pluck('viewable');
+    }
+
     /**
      * Get all recently viewed entities for the current user.
-     * @param int         $count
-     * @param int         $page
+     * @param int $count
+     * @param int $page
      * @param Entity|bool $filterModel
      * @return mixed
      */
     public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false)
     {
-        $skipCount = $count * $page;
-        $query = $this->view->where('user_id', '=', auth()->user()->id);
+        $user = user();
+        if ($user === null || $user->isDefault()) return collect();
 
-        if ($filterModel) $query->where('viewable_type', '=', get_class($filterModel));
+        $query = $this->permissionService
+            ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type');
 
-        $views = $query->with('viewable')->orderBy('updated_at', 'desc')->skip($skipCount)->take($count)->get();
-        $viewedEntities = $views->map(function ($item) {
-            return $item->viewable()->getResults();
-        });
-        return $viewedEntities;
-    }
+        if ($filterModel) $query = $query->where('viewable_type', '=', get_class($filterModel));
+        $query = $query->where('user_id', '=', $user->id);
 
+        $viewables = $query->with('viewable')->orderBy('updated_at', 'desc')
+            ->skip($count * $page)->take($count)->get()->pluck('viewable');
+        return $viewables;
+    }
 
     /**
      * Reset all view counts by deleting all views.
@@ -73,5 +98,4 @@ class ViewService
         $this->view->truncate();
     }
 
-
 }
\ No newline at end of file