| 
									
										
										
										
											2015-11-22 01:22:14 +08:00
										 |  |  | <?php namespace BookStack\Services; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Entity; | 
					
						
							|  |  |  | use BookStack\View; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ViewService | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $view; | 
					
						
							|  |  |  |     protected $user; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * ViewService constructor. | 
					
						
							|  |  |  |      * @param $view | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(View $view) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->view = $view; | 
					
						
							|  |  |  |         $this->user = auth()->user(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Add a view to the given entity. | 
					
						
							|  |  |  |      * @param Entity $entity | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function add(Entity $entity) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-02 05:14:39 +08:00
										 |  |  |         if($this->user === null) return 0; | 
					
						
							| 
									
										
										
										
											2015-11-22 01:22:14 +08:00
										 |  |  |         $view = $entity->views()->where('user_id', '=', $this->user->id)->first(); | 
					
						
							|  |  |  |         // Add view if model exists
 | 
					
						
							|  |  |  |         if ($view) { | 
					
						
							|  |  |  |             $view->increment('views'); | 
					
						
							|  |  |  |             return $view->views; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Otherwise create new view count
 | 
					
						
							|  |  |  |         $entity->views()->save($this->view->create([ | 
					
						
							|  |  |  |             'user_id' => $this->user->id, | 
					
						
							|  |  |  |             'views' => 1 | 
					
						
							|  |  |  |         ])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 04:22:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the entities with the most views. | 
					
						
							|  |  |  |      * @param int        $count | 
					
						
							|  |  |  |      * @param int        $page | 
					
						
							|  |  |  |      * @param bool|false $filterModel | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getPopular($count = 10, $page = 0, $filterModel = false) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $skipCount = $count * $page; | 
					
						
							|  |  |  |         $query = $this->view->select('id', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count')) | 
					
						
							|  |  |  |             ->groupBy('viewable_id', 'viewable_type') | 
					
						
							|  |  |  |             ->orderBy('view_count', 'desc'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if($filterModel) $query->where('viewable_type', '=', get_class($filterModel)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $views = $query->with('viewable')->skip($skipCount)->take($count)->get(); | 
					
						
							|  |  |  |         $viewedEntities = $views->map(function ($item) { | 
					
						
							|  |  |  |             return $item->viewable()->getResults(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         return $viewedEntities; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 01:22:14 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get all recently viewed entities for the current user. | 
					
						
							|  |  |  |      * @param int         $count | 
					
						
							|  |  |  |      * @param int         $page | 
					
						
							|  |  |  |      * @param Entity|bool $filterModel | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-02 05:14:39 +08:00
										 |  |  |         if($this->user === null) return collect(); | 
					
						
							| 
									
										
										
										
											2015-11-22 01:22:14 +08:00
										 |  |  |         $skipCount = $count * $page; | 
					
						
							|  |  |  |         $query = $this->view->where('user_id', '=', auth()->user()->id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($filterModel) $query->where('viewable_type', '=', get_class($filterModel)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $views = $query->with('viewable')->orderBy('updated_at', 'desc')->skip($skipCount)->take($count)->get(); | 
					
						
							|  |  |  |         $viewedEntities = $views->map(function ($item) { | 
					
						
							|  |  |  |             return $item->viewable()->getResults(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         return $viewedEntities; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Reset all view counts by deleting all views. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function resetAll() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->view->truncate(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |