]> BookStack Code Mirror - bookstack/blob - app/Auth/Queries/RolesAllPaginatedAndSorted.php
9ee4f6c249c56689849c7cb97add4ee6fbafeb25
[bookstack] / app / Auth / Queries / RolesAllPaginatedAndSorted.php
1 <?php
2
3 namespace BookStack\Auth\Queries;
4
5 use BookStack\Auth\Role;
6 use BookStack\Util\SimpleListOptions;
7 use Illuminate\Pagination\LengthAwarePaginator;
8
9 /**
10  * Get all the roles in the system in a paginated format.
11  */
12 class RolesAllPaginatedAndSorted
13 {
14     public function run(int $count, SimpleListOptions $listOptions): LengthAwarePaginator
15     {
16         $sort = $listOptions->getSort();
17         if ($sort === 'created_at') {
18             $sort = 'users.created_at';
19         }
20
21         $query = Role::query()->select(['*'])
22             ->withCount(['users', 'permissions'])
23             ->orderBy($sort, $listOptions->getOrder());
24
25         if ($listOptions->getSearch()) {
26             $term = '%' . $listOptions->getSearch() . '%';
27             $query->where(function ($query) use ($term) {
28                 $query->where('display_name', 'like', $term)
29                     ->orWhere('description', 'like', $term);
30             });
31         }
32
33         return $query->paginate($count);
34     }
35 }