diff --git a/app/AppKernel.php b/app/AppKernel.php index 7206f61e9..4f3c7dad1 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -22,6 +22,7 @@ public function registerBundles() new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(), new CodeExplorerBundle\CodeExplorerBundle(), + new Lexik\Bundle\FormFilterBundle\LexikFormFilterBundle(), new AppBundle\AppBundle(), ); diff --git a/app/Resources/views/admin/blog/index.html.twig b/app/Resources/views/admin/blog/index.html.twig index 7a0442a50..089288ce2 100644 --- a/app/Resources/views/admin/blog/index.html.twig +++ b/app/Resources/views/admin/blog/index.html.twig @@ -5,6 +5,11 @@ {% block main %}

{{ 'title.post_list'|trans }}

+
+ {{ form_rest(form) }} + +
+ diff --git a/composer.json b/composer.json index 3abc14476..0dc268165 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "symfony/monolog-bundle" : "~2.7", "symfony/swiftmailer-bundle" : "~2.3", "symfony/symfony" : "~2.8", - "twig/extensions" : "~1.2" + "twig/extensions" : "~1.2", + "lexik/form-filter-bundle": "dev-master" }, "require-dev": { "sensio/generator-bundle": "~3.0" diff --git a/src/AppBundle/Controller/Admin/BlogController.php b/src/AppBundle/Controller/Admin/BlogController.php index b2be81c3a..e53a21891 100644 --- a/src/AppBundle/Controller/Admin/BlogController.php +++ b/src/AppBundle/Controller/Admin/BlogController.php @@ -11,6 +11,7 @@ namespace AppBundle\Controller\Admin; +use AppBundle\Filter\PostFilterType; use AppBundle\Form\PostType; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -50,12 +51,29 @@ class BlogController extends Controller * @Route("/", name="admin_post_index") * @Method("GET") */ - public function indexAction() + public function indexAction(Request $request) { - $entityManager = $this->getDoctrine()->getManager(); - $posts = $entityManager->getRepository('AppBundle:Post')->findAll(); + $form = $this->get('form.factory')->create(new PostFilterType()); + + // initialize a query builder + $filterBuilder = $this->get('doctrine.orm.entity_manager') + ->getRepository('AppBundle:Post') + ->createQueryBuilder('p'); + + if ($request->query->has($form->getName())) { + // manually bind values from the request + $form->submit($request->query->get($form->getName())); + + // build the query from the given form object + $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder); + } + + $posts = $filterBuilder->getQuery()->getResult(); - return $this->render('admin/blog/index.html.twig', array('posts' => $posts)); + return $this->render('admin/blog/index.html.twig', array( + 'form' => $form->createView(), + 'posts' => $posts + )); } /** diff --git a/src/AppBundle/Filter/PostFilterType.php b/src/AppBundle/Filter/PostFilterType.php new file mode 100644 index 000000000..b375e36e8 --- /dev/null +++ b/src/AppBundle/Filter/PostFilterType.php @@ -0,0 +1,29 @@ +add('title', 'filter_text', array( + 'condition_pattern' => FilterOperands::STRING_CONTAINS)); + } + + public function getName() + { + return 'post_filter'; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'csrf_protection' => false, + 'validation_groups' => array('filtering') // avoid NotBlank() constraint-related message + )); + } +} \ No newline at end of file