2022-10-30 03:52:17 +08:00
|
|
|
<?php
|
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
namespace BookStack\Users\Queries;
|
2022-10-30 03:52:17 +08:00
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Users\Models\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') {
|
2023-07-05 04:52:46 +08:00
|
|
|
$sort = 'roles.created_at';
|
2022-10-30 03:52:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|