<?php
namespace App\Controller;
use App\Entity\ItemSpecTypes;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class AjaxController extends AbstractController
{
#[Route(path: '/api/item-spec-types/search', name: 'api-item-spec-type')]
public function itemSpecTypes(Request $request, EntityManagerInterface $em, PaginatorInterface $paginator): Response
{
$search = $request->get('term');
$items = $em->getRepository(ItemSpecTypes::class)->createQueryBuilder('i');
if($search) {
$items
->where('lower(i.name) LIKE :search or lower(i.adminName) LIKE :search')->setParameter('search', strtolower('%'.$search.'%'));
}
$paginator = $paginator->paginate($items, $request->query->getInt('page', 1), 20);
$return = [];
foreach ($paginator->getItems() as $item) {
$return[] = [
'text' => $item->getAdminName(),
'id' => $item->getId(),
'value' => $item->getId(),
];
}
return new JsonResponse([
'results' => $return,
]);
}
}