/** * @Route("/", name="homepage") * @param Request $request * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response */ public function indexAction(Request $request) { $em = $this->getDoctrine()->getManager(); // Persist filters when navigation from page to page and create the initial query $arrayParams = ParamUtil::createArrayParams($request->query->all()); $query = $em->getRepository('AppBundle:Person')->findFiltered($arrayParams); $form = $this->createForm(PersonType::class, $arrayParams); $form->handleRequest($request); if ($form->isValid()) { // Create parameters and redirect showing confirmation message $arrayParams = ParamUtil::createArrayParams($form->getData(), 'form'); if ($form->get('filter')->isClicked()) { $this->get('session')->getFlashBag()->add('success', 'Filter applied'); return $this->redirectToRoute('homepage', $arrayParams); } else { $this->get('session')->getFlashBag()->add('notice', 'Filters cleared'); return $this->redirectToRoute('homepage'); } } $this->get('session')->getFlashBag()->add('info', $query->getDql()); // Prepare pagination & add parameters for the construction of links $pagination = $this->get('knp_paginator')->paginate($query, $request->query->getInt('page', 1), 10); $pagination = ParamUtil::setParams($pagination, $arrayParams); return $this->render('default/index.html.twig', array('form' => $form->createView(), 'pagination' => $pagination)); }
public function testParametersCreation() { // empty query from URL, no params created $params_array = array(); $params = ParamUtil::createArrayParams($params_array); $this->assertEquals(0, count($params)); // One parameter created from URL $params_array = array('firstName' => 'Sergiu'); $params = ParamUtil::createArrayParams($params_array); $this->assertEquals(1, count($params)); // Two parameters created from URL $params_array = array('firstName' => 'Sergiu', 'lastName' => 'Popa'); $params = ParamUtil::createArrayParams($params_array); $this->assertEquals(2, count($params)); // 1 boolean parameters from URL converted to TRUE $params_array = array("blind" => "1"); $params = ParamUtil::createArrayParams($params_array); $this->assertTrue($params["blind"] === true); // 0 boolean parameters from URL converted to FALSE $params_array = array("blind" => "0"); $params = ParamUtil::createArrayParams($params_array); $this->assertTrue($params["blind"] === false); // One string parameters and one boolean $params_array = array('firstName' => 'Sergiu', "blind" => "0"); $params = ParamUtil::createArrayParams($params_array); $this->assertTrue($params["blind"] === false && count($params) === 2); // True parameter from FORM remains true $params_array = array("blind" => true); $params = ParamUtil::createArrayParams($params_array, "form"); $this->assertTrue($params["blind"] === true); // One string parameter and one false boolean from FORM $params_array = array("firstName" => "Sergiu", "blind" => false); $params = ParamUtil::createArrayParams($params_array, "form"); $this->assertTrue($params["blind"] === false && count($params) === 2); // Remove one parameter if comes empty from FORM $params_array = array("firstName" => "Sergiu", "lastName" => ""); $params = ParamUtil::createArrayParams($params_array, "form"); $this->assertTrue(count($params) === 1); // Remove one boolean parameter if comes empty from FORM $params_array = array("firstName" => "Sergiu", "blind" => ""); $params = ParamUtil::createArrayParams($params_array, "form"); $this->assertTrue(count($params) === 1); }