diff --git a/.gitignore b/.gitignore index dc9c2f2..32eb536 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ /vendor/ /web/bundles/ composer.lock +workspace.xml +composer.phar +.idea diff --git a/app/Resources/views/components/breadcrumbs.html.twig b/app/Resources/views/components/breadcrumbs.html.twig new file mode 100644 index 0000000..4232b69 --- /dev/null +++ b/app/Resources/views/components/breadcrumbs.html.twig @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/app/Resources/views/components/pagination.html.twig b/app/Resources/views/components/pagination.html.twig new file mode 100644 index 0000000..7c9c4e7 --- /dev/null +++ b/app/Resources/views/components/pagination.html.twig @@ -0,0 +1,20 @@ +{% if maxPages > 1 %} + +{% endif %} \ No newline at end of file diff --git a/app/Resources/views/homepage/homepage.html.twig b/app/Resources/views/homepage/homepage.html.twig index 13b9283..4abdef8 100644 --- a/app/Resources/views/homepage/homepage.html.twig +++ b/app/Resources/views/homepage/homepage.html.twig @@ -2,7 +2,16 @@ {% block body %}
+
+ {% include 'components/breadcrumbs.html.twig' %} +
+
+ {% include 'components/pagination.html.twig' with { 'page': page, 'maxPages': maxPages } %} +
{% include 'components/list.html.twig' %} +
+ {% include 'components/pagination.html.twig' with { 'page': page, 'maxPages': maxPages } %} +
{% endblock %} diff --git a/app/Resources/views/product/detail.html.twig b/app/Resources/views/product/detail.html.twig index eb279ca..5d33918 100644 --- a/app/Resources/views/product/detail.html.twig +++ b/app/Resources/views/product/detail.html.twig @@ -1,12 +1,9 @@ {% extends 'base.html.twig' %} {% block body %} - +
+ {% include 'components/breadcrumbs.html.twig' %} +

{{ product.title }}

{% endblock %} \ No newline at end of file diff --git a/app/config/services.yml b/app/config/services.yml index 5c76fc5..cfbd9fc 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -7,3 +7,13 @@ services: # service_name: # class: AppBundle\Directory\ClassName # arguments: ["@another_service_name", "plain_value", "%parameter_name%"] + product_repository: + class: AppBundle\Repository\ProductRepository + factory: ["@doctrine.orm.entity_manager", getRepository] + arguments: + - AppBundle\Entity\ProductRepository + category_repository: + class: AppBundle\Repository\CategoryRepository + factory: ["@doctrine.orm.entity_manager", getRepository] + arguments: + - AppBundle\Entity\CategoryRepository \ No newline at end of file diff --git a/src/AppBundle/Controller/HomepageController.php b/src/AppBundle/Controller/HomepageController.php index 16d2edb..5496cbf 100644 --- a/src/AppBundle/Controller/HomepageController.php +++ b/src/AppBundle/Controller/HomepageController.php @@ -2,9 +2,11 @@ namespace AppBundle\Controller; use AppBundle\Entity\Category; use AppBundle\Entity\Product; +use AppBundle\Repository\ProductRepository; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; +use Doctrine\ORM\Tools\Pagination\Paginator; /** * @author Jan Klat @@ -19,34 +21,60 @@ class HomepageController extends Controller */ public function homepageAction(Request $request) { + $page = 1; + if ($request->query->has('page')) { + $page = $request->query->get('page'); + } + + $productRepository = $this->container->get('doctrine.orm.entity_manager')->getRepository + ('AppBundle\Entity\Product'); + + $limit = 9; + $paginatedProducts = $this->paginate($productRepository->getAllProducts(), $page, $limit); + $maxPages = ceil($paginatedProducts->count() / $limit); + + $categoryRepository = $this->container->get('doctrine.orm.entity_manager')->getRepository + ('AppBundle\Entity\Category'); + + $breadcrumbs = [ + "homepage" => ["title" => "Úvodní stránka"] + ]; + return $this->render("homepage/homepage.html.twig", [ - "products" => $this->getDoctrine()->getRepository(Product::class)->findBy( - [], - [ - "rank" => "desc" - ], - 20 - ), - "categories" => $this->getDoctrine()->getRepository(Category::class)->findBy( - [], - [ - "rank" => "desc", - ] - ), + "page" => $page, + "maxPages" => $maxPages, + "products" => $paginatedProducts, + "categories" => $categoryRepository->getAllCategories(), + "breadcrumbs" => $breadcrumbs ]); } - /** - * @Route("/hello", name="hello") - * @param Request $request - * @return \Symfony\Component\HttpFoundation\Response + * Paginator Helper + * Source: https://anil.io/blog/symfony/doctrine/symfony-and-doctrine-pagination-with-twig/ + * + * Pass through a query object, current page & limit + * the offset is calculated from the page and limit + * returns an `Paginator` instance, which you can call the following on: + * + * $paginator->getIterator()->count() # Total fetched (ie: `5` posts) + * $paginator->count() # Count of ALL posts (ie: `20` posts) + * $paginator->getIterator() # ArrayIterator + * + * @param Doctrine\ORM\Query $dql DQL Query Object + * @param integer $page Current page (defaults to 1) + * @param integer $limit The total number per page (defaults to 5) + * + * @return \Doctrine\ORM\Tools\Pagination\Paginator */ - public function helloAction(Request $request) + private function paginate($dql, $page = 1, $limit = 9) { - return $this->render("homepage/hello.html.twig", [ - "who" => "world", - ]); - } + $paginator = new Paginator($dql); + + $paginator->getQuery() + ->setFirstResult($limit * ($page - 1)) // Offset + ->setMaxResults($limit); // Limit + return $paginator; + } } \ No newline at end of file diff --git a/src/AppBundle/Controller/ProductController.php b/src/AppBundle/Controller/ProductController.php index ce54c46..ae5b207 100644 --- a/src/AppBundle/Controller/ProductController.php +++ b/src/AppBundle/Controller/ProductController.php @@ -28,7 +28,14 @@ public function productDetailAction(Request $request) throw new NotFoundHttpException("Produkt neexistuje"); } + $breadcrumbs = [ + "homepage" => ["title" => "Úvodní stránka"], + "#notyet" => ["title" => $product->getCategory()->getTitle()], + "product_detail" => ["title" => $product->getTitle(), "slug" => $request->attributes->get("slug")] + ]; + return $this->render("product/detail.html.twig", [ + "breadcrumbs" => $breadcrumbs, "product" => $product, "categories" => $this->getDoctrine()->getRepository(Category::class)->findBy( [], diff --git a/src/AppBundle/Repository/CategoryRepository.php b/src/AppBundle/Repository/CategoryRepository.php index f9b2633..33fdfbe 100644 --- a/src/AppBundle/Repository/CategoryRepository.php +++ b/src/AppBundle/Repository/CategoryRepository.php @@ -12,4 +12,13 @@ */ class CategoryRepository extends EntityRepository { + public function getAllCategories() + { + return $this->findBy( + [], + [ + "rank" => "desc", + ] + ); + } } diff --git a/src/AppBundle/Repository/ProductRepository.php b/src/AppBundle/Repository/ProductRepository.php index 74463f1..ab5c6d3 100644 --- a/src/AppBundle/Repository/ProductRepository.php +++ b/src/AppBundle/Repository/ProductRepository.php @@ -11,4 +11,12 @@ class ProductRepository extends EntityRepository { + public function getAllProducts() + { + $query = $this->createQueryBuilder('p') + ->orderBy('p.rank', 'DESC') + ->getQuery(); + + return $query; + } }