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