src/Controller/SearchController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\SearchFormType;
  4. use Doctrine\DBAL\Driver\Exception;
  5. use Doctrine\DBAL\Types\Type;
  6. use Pimcore\Controller\FrontendController;
  7. use Pimcore\Model\DataObject\ProductGroup;
  8. use Pimcore\Model\DataObject\ProductSubgroup;
  9. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  10. use Symfony\Component\Form\Extension\Core\Type\TextType;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Pimcore\Model\DataObject\ProductArticle;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Pimcore\Model\DataObject;
  16. use Symfony\Component\Validator\Constraints\Length;
  17. class SearchController extends FrontendController
  18. {
  19.     /**
  20.      * @Route("{_locale}/navbarsearch", name="navBarSearch")
  21.      * @param Request $request
  22.      */
  23.     public function NavBarSearch(Request $request)
  24.     {
  25.         $form $this->createFormBuilder()
  26.             ->setAction($this->generateUrl('handleSearch'))
  27.             ->add('query'TextType::class, [
  28.                 'required' => false,
  29.                 'attr' => [
  30.                     'class' => 'form-control rounded m-1',
  31.                      'placeholder' => 'Ik ben op zoek naar',
  32.                 ],
  33.                 'constraints' => [
  34.                     new Length([
  35.                         'min' => 3,
  36.                         'minMessage' => 'min 3 ***',
  37.                     ]),
  38.                 ],
  39.             ])
  40.             ->add('search'SubmitType::class, [
  41.                 'label'=> 'Zoeken',
  42.                 'attr' => [
  43.                     'class' => 'btn btn-primary btn-sm rounded m-1',
  44.                 ]
  45.             ])
  46.             ->getForm();
  47.         return $this->render('layouts/navigation/searchbar.html.twig', [
  48.             'form' => $form->createView()
  49.         ]);
  50.     }
  51.     /**
  52.      * @Route("{_locale}/handlesearch", name="handleSearch")
  53.      * @param Request $request
  54.      * @throws \Exception
  55.      * @throws Exception
  56.      */
  57.     public function handleSearch(Request $request)
  58.     {
  59.         $query $request->request->get('form')['query'];
  60.         if ($query && strlen($query) >= 3) {
  61.             $array = [];
  62.             $classIdProduct DataObject\ProductArticle::classId();
  63.             // Get the database connection
  64.             $connection $this->getDoctrine()->getConnection();
  65.             // Prepare the SQL statement
  66.             $statement $connection->prepare("SELECT object_query_".$classIdProduct.".oo_id FROM  object_query_".$classIdProduct." WHERE `oms` LIKE :query OR `barcode` LIKE :query  OR `code` LIKE :query ORDER BY `oms` LIMIT 100");
  67.             // Bind the variable to the prepared statement
  68.             $statement->bindValue('query''%' $query '%');
  69.             // Execute the statement
  70.             $statement->execute();
  71.             // Fetch the results
  72.             $ProductsIds $statement->fetchAll();
  73.             foreach ($ProductsIds as $ProductId) {
  74.                 if (ProductArticle::getById($ProductId['oo_id'])?->isPublished()){
  75.                     $array[] = ProductArticle::getById($ProductId['oo_id']);
  76.                 }
  77.             }
  78.             return $this->render('search-objects/products/overview.html.twig', [
  79.                 'productDetail' => $array,
  80.                 'query'=> $query,
  81.             ]);
  82.         }
  83.         return $this->redirect('/');
  84.     }
  85.     /**
  86.      * @Route("{_locale}/handlesearch/detailpage", name="handleSearchDetail")
  87.      * @param Request $request
  88.      * @return Response
  89.      * @throws \Exception
  90.      */
  91.     public function handleSearchDetail(Request $request): Response
  92.     {
  93.         $product ProductArticle::getById($request->get('id'));
  94.         return $this->render('search-objects/products/detail.html.twig', [
  95.             'product' => $product,
  96.         ]);
  97.     }
  98. }