<?php
namespace test\FilterBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use test\FilterBundle\Entity\Production;
use test\FilterBundle\Entity\TM;
use test\FilterBundle\Entity\type;
class filter
{
public function buildSql($request)
{
$get = $request->query->all();
return $get;
}
}
class FilterController extends Controller
{
public function indexAction(Request $request)
{
$get = new filter();
$get = $get->buildSql($request);
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();
$cookies = $request->cookies;
$cookies = $cookies->get('sort');
$qb
->select('c')
->from('test\FilterBundle\Entity\Production', 'c');
if (isset($get['TM']) and
count($get['TM']) > 1) { $qb
->where('c.TM IN (:value)')
->setParameter('value', $get['TM']);
} elseif (isset($get['TM']) and
count($get['TM']) == 1) { $qb
->where('c.TM = :value')
->setParameter('value', $get['TM']);
};
if (isset($get['type']) and
count($get['type']) > 1) { $qb
->andWhere('c.type IN (:value2)')
->setParameter('value2', $get['type']);
} elseif (isset($get['type']) and
count($get['type']) == 1) { $qb
->andWhere('c.type = :value2')
->setParameter('value2', $get['type']);
};
if ($cookies == 0) {
$cookies = 'ASC';
} elseif ($cookies == 1) {
$cookies = 'desc';
}
$qb
->addOrderBy('c.price', $cookies);
}
$result = $qb
->getQuery()
->getResult();
$type = $this->getDoctrine()
->getRepository('testFilterBundle:type')
->findAll();
foreach ($type as $key => $value) {
if (isset($get['type'])) { foreach ($get['type'] as $key2 => $value2) {
if ($value->id == $value2) {
$value->checked = 'checked';
}
}
}
}
$TM = $this->getDoctrine()
->getRepository('testFilterBundle:TM')
->findAll();
foreach ($TM as $key => $value) {
foreach ($get['TM'] as $key2 => $value2) {
if ($value->id == $value2) {
$value->checked = 'checked';
}
}
}
}
$url = $this->get('request_stack')->getMasterRequest();
return $this->render(
'testFilterBundle:Default:index.html.twig',
'products' => $result,
'get' => $get,
'type' => $type,
'TM' => $TM,
'qb' => $qb,
'cookie' => $cookies,
'url' => $url)
);
}
}