66 lines
2.0 KiB
PHP
66 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\Contact;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
|
|
class ContactRepository extends BaseRepository implements ContactRepositoryInterface
|
|
{
|
|
public function __construct(Contact $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
/**
|
|
* Get paginated contacts
|
|
*/
|
|
public function paginate(int $perPage = 15, array $filters = []): LengthAwarePaginator
|
|
{
|
|
$query = $this->model->newQuery()->with('client');
|
|
|
|
// Apply filters
|
|
if (!empty($filters['search'])) {
|
|
$query->where(function ($q) use ($filters) {
|
|
$q->where('first_name', 'like', '%' . $filters['search'] . '%')
|
|
->orWhere('last_name', 'like', '%' . $filters['search'] . '%')
|
|
->orWhere('email', 'like', '%' . $filters['search'] . '%')
|
|
->orWhere('phone', 'like', '%' . $filters['search'] . '%')
|
|
->orWhere('mobile', 'like', '%' . $filters['search'] . '%')
|
|
->orWhere('position', 'like', '%' . $filters['search'] . '%');
|
|
});
|
|
}
|
|
|
|
if (isset($filters['is_primary'])) {
|
|
$query->where('is_primary', $filters['is_primary']);
|
|
}
|
|
|
|
if (!empty($filters['client_id'])) {
|
|
$query->where('client_id', $filters['client_id']);
|
|
}
|
|
|
|
// Apply sorting
|
|
$sortField = $filters['sort_by'] ?? 'created_at';
|
|
$sortDirection = $filters['sort_direction'] ?? 'desc';
|
|
|
|
// Special handling for name sorting
|
|
if ($sortField === 'name') {
|
|
$query->orderBy('last_name', $sortDirection)
|
|
->orderBy('first_name', $sortDirection);
|
|
} else {
|
|
$query->orderBy($sortField, $sortDirection);
|
|
}
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
public function getByClientId(int $clientId)
|
|
{
|
|
return $this->model->newQuery()
|
|
->where('client_id', $clientId)
|
|
->get();
|
|
}
|
|
}
|