Changed repoClient funcionality. New filters added
testing/ogcore-api/pipeline/head This commit looks good
Details
testing/ogcore-api/pipeline/head This commit looks good
Details
parent
70bf6fc534
commit
a5d58fa78d
|
@ -20,7 +20,6 @@ class ClientRepository extends AbstractRepository
|
||||||
{
|
{
|
||||||
$entityManager = $this->getEntityManager();
|
$entityManager = $this->getEntityManager();
|
||||||
|
|
||||||
// Obtener el path de la unidad organizativa raíz
|
|
||||||
$query = $entityManager->createQuery(
|
$query = $entityManager->createQuery(
|
||||||
'SELECT o.path
|
'SELECT o.path
|
||||||
FROM App\Entity\OrganizationalUnit o
|
FROM App\Entity\OrganizationalUnit o
|
||||||
|
@ -34,7 +33,6 @@ class ClientRepository extends AbstractRepository
|
||||||
|
|
||||||
$path = $result['path'] . '/%';
|
$path = $result['path'] . '/%';
|
||||||
|
|
||||||
// Obtener las unidades organizativas descendientes
|
|
||||||
$query = $entityManager->createQuery(
|
$query = $entityManager->createQuery(
|
||||||
'SELECT o.id
|
'SELECT o.id
|
||||||
FROM App\Entity\OrganizationalUnit o
|
FROM App\Entity\OrganizationalUnit o
|
||||||
|
@ -45,17 +43,15 @@ class ClientRepository extends AbstractRepository
|
||||||
|
|
||||||
$unitIds = array_column($query->getArrayResult(), 'id');
|
$unitIds = array_column($query->getArrayResult(), 'id');
|
||||||
|
|
||||||
// Construcción de la consulta con filtros dinámicos
|
|
||||||
$qb = $entityManager->createQueryBuilder();
|
$qb = $entityManager->createQueryBuilder();
|
||||||
$qb->select('c')
|
$qb->select('c')
|
||||||
->from('App\Entity\Client', 'c')
|
->from('App\Entity\Client', 'c')
|
||||||
->where('c.organizationalUnit IN (:unitIds)')
|
->where('c.organizationalUnit IN (:unitIds)')
|
||||||
->setParameter('unitIds', $unitIds);
|
->setParameter('unitIds', $unitIds);
|
||||||
|
|
||||||
// Aplicar dinámicamente los filtros recibidos
|
|
||||||
foreach ($filters as $key => $value) {
|
foreach ($filters as $key => $value) {
|
||||||
if ($key === 'order' || $key === 'page' || $key === 'itemsPerPage' || $key === 'organizationalUnit.id') {
|
if ($key === 'order' || $key === 'page' || $key === 'itemsPerPage' || $key === 'organizationalUnit.id') {
|
||||||
continue; // No son filtros de búsqueda
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($key === 'exists') {
|
if ($key === 'exists') {
|
||||||
|
@ -66,13 +62,25 @@ class ClientRepository extends AbstractRepository
|
||||||
$qb->andWhere("c.$field IS NULL");
|
$qb->andWhere("c.$field IS NULL");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} elseif ($key === 'name') {
|
||||||
|
// Aplicar búsqueda con LIKE para el campo "name"
|
||||||
|
$qb->andWhere("c.name LIKE :name")
|
||||||
|
->setParameter('name', "%$value%");
|
||||||
|
} elseif ($key === 'query') {
|
||||||
|
// Búsqueda en múltiples campos (name, ip, mac)
|
||||||
|
$qb->andWhere($qb->expr()->orX(
|
||||||
|
$qb->expr()->like('c.name', ':searchQuery'),
|
||||||
|
$qb->expr()->like('c.ip', ':searchQuery'),
|
||||||
|
$qb->expr()->like('c.mac', ':searchQuery')
|
||||||
|
))->setParameter('searchQuery', "%$value%");
|
||||||
} else {
|
} else {
|
||||||
// Si es un campo normal, añadirlo como filtro
|
// Búsqueda exacta para otros campos
|
||||||
$qb->andWhere("c.$key = :$key")->setParameter($key, $value);
|
$qb->andWhere("c.$key = :$key")->setParameter($key, $value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Aplicar ordenación si existe
|
|
||||||
|
|
||||||
if (isset($filters['order']) && is_array($filters['order'])) {
|
if (isset($filters['order']) && is_array($filters['order'])) {
|
||||||
foreach ($filters['order'] as $field => $direction) {
|
foreach ($filters['order'] as $field => $direction) {
|
||||||
if (in_array(strtolower($direction), ['asc', 'desc'])) {
|
if (in_array(strtolower($direction), ['asc', 'desc'])) {
|
||||||
|
@ -81,7 +89,6 @@ class ClientRepository extends AbstractRepository
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Aplicar paginación
|
|
||||||
if (isset($filters['page']) && isset($filters['itemsPerPage'])) {
|
if (isset($filters['page']) && isset($filters['itemsPerPage'])) {
|
||||||
$page = max(1, (int) $filters['page']);
|
$page = max(1, (int) $filters['page']);
|
||||||
$itemsPerPage = max(1, (int) $filters['itemsPerPage']);
|
$itemsPerPage = max(1, (int) $filters['itemsPerPage']);
|
||||||
|
@ -108,7 +115,6 @@ class ClientRepository extends AbstractRepository
|
||||||
|
|
||||||
$path = $result['path'] . '/%';
|
$path = $result['path'] . '/%';
|
||||||
|
|
||||||
// Obtener las unidades organizativas descendientes
|
|
||||||
$query = $entityManager->createQuery(
|
$query = $entityManager->createQuery(
|
||||||
'SELECT o.id FROM App\Entity\OrganizationalUnit o WHERE o.id = :id OR o.path LIKE :path'
|
'SELECT o.id FROM App\Entity\OrganizationalUnit o WHERE o.id = :id OR o.path LIKE :path'
|
||||||
)
|
)
|
||||||
|
@ -117,17 +123,15 @@ class ClientRepository extends AbstractRepository
|
||||||
|
|
||||||
$unitIds = array_column($query->getArrayResult(), 'id');
|
$unitIds = array_column($query->getArrayResult(), 'id');
|
||||||
|
|
||||||
// Contar clientes sin paginación
|
|
||||||
$qb = $entityManager->createQueryBuilder();
|
$qb = $entityManager->createQueryBuilder();
|
||||||
$qb->select('COUNT(c.id)')
|
$qb->select('COUNT(c.id)')
|
||||||
->from('App\Entity\Client', 'c')
|
->from('App\Entity\Client', 'c')
|
||||||
->where('c.organizationalUnit IN (:unitIds)')
|
->where('c.organizationalUnit IN (:unitIds)')
|
||||||
->setParameter('unitIds', $unitIds);
|
->setParameter('unitIds', $unitIds);
|
||||||
|
|
||||||
// Aplicar los mismos filtros que en `findClientsByOrganizationalUnitAndDescendants`
|
|
||||||
foreach ($filters as $key => $value) {
|
foreach ($filters as $key => $value) {
|
||||||
if ($key === 'order' || $key === 'page' || $key === 'itemsPerPage' || $key === 'organizationalUnit.id') {
|
if ($key === 'order' || $key === 'page' || $key === 'itemsPerPage' || $key === 'organizationalUnit.id') {
|
||||||
continue; // No son filtros de búsqueda
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($key === 'exists') {
|
if ($key === 'exists') {
|
||||||
|
@ -138,6 +142,15 @@ class ClientRepository extends AbstractRepository
|
||||||
$qb->andWhere("c.$field IS NULL");
|
$qb->andWhere("c.$field IS NULL");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} elseif ($key === 'name') {
|
||||||
|
$qb->andWhere("c.name LIKE :name")
|
||||||
|
->setParameter('name', "%$value%");
|
||||||
|
} elseif ($key === 'query') {
|
||||||
|
$qb->andWhere($qb->expr()->orX(
|
||||||
|
$qb->expr()->like('c.name', ':searchQuery'),
|
||||||
|
$qb->expr()->like('c.ip', ':searchQuery'),
|
||||||
|
$qb->expr()->like('c.mac', ':searchQuery')
|
||||||
|
))->setParameter('searchQuery', "%$value%");
|
||||||
} else {
|
} else {
|
||||||
$qb->andWhere("c.$key = :$key")->setParameter($key, $value);
|
$qb->andWhere("c.$key = :$key")->setParameter($key, $value);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue