161 lines
5.2 KiB
PHP
161 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\User\Repositories;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Modules\Auth\Entities\User;
|
|
use Modules\User\Repositories\Contracts\UserRepositoryInterface;
|
|
|
|
class UserRepository implements UserRepositoryInterface
|
|
{
|
|
/**
|
|
* Get all Users with pagination and search
|
|
*/
|
|
public function getAllPaginated(int $perPage = 10, string $search = '')
|
|
{
|
|
$query = User::visibleTo(auth()->user())->excludeDevelopersUnlessDeveloper()->orderBy('name');
|
|
|
|
if (! empty($search)) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'ILIKE', "%{$search}%");
|
|
$q->orWhere('ic_number', 'ILIKE', "%{$search}%");
|
|
$q->orWhere('email', 'ILIKE', "%{$search}%");
|
|
$q->orWhere('phone_number', 'ILIKE', "%{$search}%");
|
|
$q->orWhereHas('roles', function ($roleQuery) use ($search) {
|
|
$roleQuery->whereRaw('LOWER(name) ILIKE ?', ['%' . strtolower($search) . '%']);
|
|
});
|
|
});
|
|
}
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
/**
|
|
* Get all Users with their relationships and pagination with search
|
|
*/
|
|
public function getAllWithRelationsPaginated(
|
|
int $perPage = 10,
|
|
string $search = '',
|
|
string $status = '',
|
|
string $sortBy = 'name',
|
|
string $sortOrder = 'asc'
|
|
) {
|
|
$allowedSortColumns = [
|
|
'id',
|
|
'name',
|
|
'email',
|
|
'position',
|
|
'status',
|
|
'ic_number',
|
|
'phone_number',
|
|
'created_at',
|
|
];
|
|
$sortBy = in_array($sortBy, $allowedSortColumns, true) ? $sortBy : 'name';
|
|
$sortOrder = strtolower($sortOrder) === 'desc' ? 'desc' : 'asc';
|
|
|
|
$query = User::visibleTo(auth()->user())->excludeDevelopersUnlessDeveloper()
|
|
->with([
|
|
'roles:id,name,guard_name'
|
|
])
|
|
->orderBy($sortBy, $sortOrder);
|
|
|
|
if (! empty($search)) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'ILIKE', "%{$search}%")
|
|
->orWhere('ic_number', 'ILIKE', "%{$search}%")
|
|
->orWhere('email', 'ILIKE', "%{$search}%")
|
|
->orWhere('phone_number', 'ILIKE', "%{$search}%")
|
|
->orWhereHas('roles', function ($roleQuery) use ($search) {
|
|
$roleQuery->whereRaw('LOWER(name) ILIKE ?', ['%' . strtolower($search) . '%']);
|
|
});
|
|
$q->orWhere('ic_number', 'ILIKE', "%{$search}%");
|
|
$q->orWhere('email', 'ILIKE', "%{$search}%");
|
|
$q->orWhere('phone_number', 'ILIKE', "%{$search}%");
|
|
$q->orWhereHas('roles', function ($roleQuery) use ($search) {
|
|
$roleQuery->whereRaw('LOWER(name) ILIKE ?', ['%' . strtolower($search) . '%']);
|
|
});
|
|
});
|
|
}
|
|
|
|
// Add status filter
|
|
if (! empty($status)) {
|
|
$query->where('status', $status);
|
|
}
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
/**
|
|
* Get all Users with their relationships and search
|
|
*/
|
|
public function getAllWithRelations(string $search = ''): Collection
|
|
{
|
|
$query = User::visibleTo(auth()->user())->excludeDevelopersUnlessDeveloper()->orderBy('name');
|
|
|
|
if (! empty($search)) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'ILIKE', "%{$search}%")
|
|
->orWhere('ic_number', 'ILIKE', "%{$search}%")
|
|
->orWhere('email', 'ILIKE', "%{$search}%")
|
|
->orWhere('phone_number', 'ILIKE', "%{$search}%")
|
|
->orWhereHas('roles', function ($roleQuery) use ($search) {
|
|
$roleQuery->whereRaw('LOWER(name) ILIKE ?', ['%' . strtolower($search) . '%']);
|
|
});
|
|
});
|
|
}
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
/**
|
|
* Create a new User
|
|
*/
|
|
public function create(array $data): User
|
|
{
|
|
return User::create($data);
|
|
}
|
|
|
|
/**
|
|
* Find User by ID
|
|
*/
|
|
public function findById(string $id): ?User
|
|
{
|
|
return User::with(['roles'])->find($id);
|
|
}
|
|
|
|
/**
|
|
* Delete User (soft delete)
|
|
*/
|
|
public function delete(string $id): bool
|
|
{
|
|
$User = User::find($id);
|
|
if ($User) {
|
|
return $User->delete();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get all Ranks
|
|
*/
|
|
public function all(string $search = ''): Collection
|
|
{
|
|
$query = User::visibleTo(auth()->user())->excludeDevelopersUnlessDeveloper()->orderBy('name');
|
|
|
|
if (! empty($search)) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'ILIKE', "%{$search}%")
|
|
->orWhere('ic_number', 'ILIKE', "%{$search}%")
|
|
->orWhere('email', 'ILIKE', "%{$search}%")
|
|
->orWhere('phone_number', 'ILIKE', "%{$search}%")
|
|
->orWhereHas('roles', function ($roleQuery) use ($search) {
|
|
$roleQuery->whereRaw('LOWER(name) ILIKE ?', ['%' . strtolower($search) . '%']);
|
|
});
|
|
});
|
|
}
|
|
|
|
return $query->get();
|
|
}
|
|
}
|