<?php
namespace App\Controller;
use App\Form\SearchFormType;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Types\Type;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\ProductGroup;
use Pimcore\Model\DataObject\ProductSubgroup;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Pimcore\Model\DataObject\ProductArticle;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Pimcore\Model\DataObject;
use Symfony\Component\Validator\Constraints\Length;
class SearchController extends FrontendController
{
/**
* @Route("{_locale}/navbarsearch", name="navBarSearch")
* @param Request $request
*/
public function NavBarSearch(Request $request)
{
$form = $this->createFormBuilder()
->setAction($this->generateUrl('handleSearch'))
->add('query', TextType::class, [
'required' => false,
'attr' => [
'class' => 'form-control rounded m-1',
'placeholder' => 'Ik ben op zoek naar',
],
'constraints' => [
new Length([
'min' => 3,
'minMessage' => 'min 3 ***',
]),
],
])
->add('search', SubmitType::class, [
'label'=> 'Zoeken',
'attr' => [
'class' => 'btn btn-primary btn-sm rounded m-1',
]
])
->getForm();
return $this->render('layouts/navigation/searchbar.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("{_locale}/handlesearch", name="handleSearch")
* @param Request $request
* @throws \Exception
* @throws Exception
*/
public function handleSearch(Request $request)
{
$query = $request->request->get('form')['query'];
if ($query && strlen($query) >= 3) {
$array = [];
$classIdProduct = DataObject\ProductArticle::classId();
// Get the database connection
$connection = $this->getDoctrine()->getConnection();
// Prepare the SQL statement
$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");
// Bind the variable to the prepared statement
$statement->bindValue('query', '%' . $query . '%');
// Execute the statement
$statement->execute();
// Fetch the results
$ProductsIds = $statement->fetchAll();
foreach ($ProductsIds as $ProductId) {
if (ProductArticle::getById($ProductId['oo_id'])?->isPublished()){
$array[] = ProductArticle::getById($ProductId['oo_id']);
}
}
return $this->render('search-objects/products/overview.html.twig', [
'productDetail' => $array,
'query'=> $query,
]);
}
return $this->redirect('/');
}
/**
* @Route("{_locale}/handlesearch/detailpage", name="handleSearchDetail")
* @param Request $request
* @return Response
* @throws \Exception
*/
public function handleSearchDetail(Request $request): Response
{
$product = ProductArticle::getById($request->get('id'));
return $this->render('search-objects/products/detail.html.twig', [
'product' => $product,
]);
}
}