| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | namespace BookStack\Entities\Queries; | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Actions\View; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Popular extends EntityQuery | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-13 22:23:03 +08:00
										 |  |  |     public function run(int $count, int $page, array $filterModels = null) | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $query = $this->permissionService() | 
					
						
							| 
									
										
										
										
											2022-07-17 02:54:25 +08:00
										 |  |  |             ->restrictEntityRelationQuery(View::query(), 'views', 'viewable_id', 'viewable_type') | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  |             ->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count')) | 
					
						
							|  |  |  |             ->groupBy('viewable_id', 'viewable_type') | 
					
						
							|  |  |  |             ->orderBy('view_count', 'desc'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($filterModels) { | 
					
						
							|  |  |  |             $query->whereIn('viewable_type', $this->entityProvider()->getMorphClasses($filterModels)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $query->with('viewable') | 
					
						
							|  |  |  |             ->skip($count * ($page - 1)) | 
					
						
							|  |  |  |             ->take($count) | 
					
						
							|  |  |  |             ->get() | 
					
						
							|  |  |  |             ->pluck('viewable') | 
					
						
							|  |  |  |             ->filter(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |