| 
									
										
										
										
											2022-10-30 03:52:17 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Auth\Queries; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Auth\Role; | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  | use BookStack\Util\SimpleListOptions; | 
					
						
							| 
									
										
										
										
											2022-10-30 03:52:17 +08:00
										 |  |  | use Illuminate\Pagination\LengthAwarePaginator; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Get all the roles in the system in a paginated format. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-10-30 20:02:06 +08:00
										 |  |  | class RolesAllPaginatedAndSorted | 
					
						
							| 
									
										
										
										
											2022-10-30 03:52:17 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |     public function run(int $count, SimpleListOptions $listOptions): LengthAwarePaginator | 
					
						
							| 
									
										
										
										
											2022-10-30 03:52:17 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |         $sort = $listOptions->getSort(); | 
					
						
							| 
									
										
										
										
											2022-10-30 03:52:17 +08:00
										 |  |  |         if ($sort === 'created_at') { | 
					
						
							|  |  |  |             $sort = 'users.created_at'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $query = Role::query()->select(['*']) | 
					
						
							|  |  |  |             ->withCount(['users', 'permissions']) | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |             ->orderBy($sort, $listOptions->getOrder()); | 
					
						
							| 
									
										
										
										
											2022-10-30 03:52:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |         if ($listOptions->getSearch()) { | 
					
						
							|  |  |  |             $term = '%' . $listOptions->getSearch() . '%'; | 
					
						
							| 
									
										
										
										
											2022-10-30 03:52:17 +08:00
										 |  |  |             $query->where(function ($query) use ($term) { | 
					
						
							|  |  |  |                 $query->where('display_name', 'like', $term) | 
					
						
							|  |  |  |                     ->orWhere('description', 'like', $term); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $query->paginate($count); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |