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