diff --git a/app/Resources/views/base.html.twig b/app/Resources/views/base.html.twig index 5448960..b803b51 100644 --- a/app/Resources/views/base.html.twig +++ b/app/Resources/views/base.html.twig @@ -44,7 +44,7 @@ diff --git a/app/Resources/views/components/pagination.html.twig b/app/Resources/views/components/pagination.html.twig new file mode 100644 index 0000000..39e84f6 --- /dev/null +++ b/app/Resources/views/components/pagination.html.twig @@ -0,0 +1,11 @@ +
+ {% if totalPages > 1 %} + + {% endif %} +
diff --git a/app/Resources/views/homepage/homepage.html.twig b/app/Resources/views/homepage/homepage.html.twig index 13b9283..b8fc7e1 100644 --- a/app/Resources/views/homepage/homepage.html.twig +++ b/app/Resources/views/homepage/homepage.html.twig @@ -3,6 +3,7 @@ {% block body %}
{% include 'components/list.html.twig' %} + {% include 'components/pagination.html.twig' %}
{% endblock %} diff --git a/data.sql b/data.sql index 70239af..9be8bc8 100644 --- a/data.sql +++ b/data.sql @@ -23,6 +23,21 @@ # Dump of table category # ------------------------------------------------------------ +DROP TABLE IF EXISTS `category`; + +CREATE TABLE `category` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `parent_category_id` int(11) NOT NULL, + `top_category_id` int(11) NOT NULL, + `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, + `slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL, + `rank` int(11) NOT NULL, + `left` int(11) NOT NULL, + `right` int(11) NOT NULL, + `level` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + LOCK TABLES `category` WRITE; /*!40000 ALTER TABLE `category` DISABLE KEYS */; @@ -54,6 +69,19 @@ UNLOCK TABLES; # Dump of table product # ------------------------------------------------------------ +DROP TABLE IF EXISTS `product`; + +CREATE TABLE `product` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, + `image` varchar(255) COLLATE utf8_unicode_ci NOT NULL, + `slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL, + `description` varchar(255) COLLATE utf8_unicode_ci NOT NULL, + `price` double NOT NULL, + `rank` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + LOCK TABLES `product` WRITE; /*!40000 ALTER TABLE `product` DISABLE KEYS */; @@ -111,6 +139,14 @@ UNLOCK TABLES; # Dump of table product_category # ------------------------------------------------------------ +DROP TABLE IF EXISTS `product_category`; + +CREATE TABLE `product_category` ( + `product_id` int(11) NOT NULL, + `category_id` int(11) NOT NULL, + PRIMARY KEY (`product_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + LOCK TABLES `product_category` WRITE; /*!40000 ALTER TABLE `product_category` DISABLE KEYS */; diff --git a/src/AppBundle/Controller/HomepageController.php b/src/AppBundle/Controller/HomepageController.php index 1abe442..d955dd6 100644 --- a/src/AppBundle/Controller/HomepageController.php +++ b/src/AppBundle/Controller/HomepageController.php @@ -5,6 +5,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\HttpFoundation\Request; /** * @author Jan Klat @@ -12,18 +13,28 @@ class HomepageController extends Controller { /** - * @Route("/", name="homepage") + * @Route("/{page}", name="homepage") * @Template("homepage/homepage.html.twig") + * + * @param Request $request + * @return array */ - public function homepageAction() + public function homepageAction(Request $request) { + $perPage = 6; + $currentPage = $request->attributes->get("page"); + $totalProducts = $this->getDoctrine()->getRepository(Product::class)->findBy([]); + $totalPages = ceil(count($totalProducts)/$perPage); + + return [ "products" => $this->getDoctrine()->getRepository(Product::class)->findBy( [], [ "rank" => "desc" ], - 21 + $perPage, // limit of products per page + $perPage * ($currentPage - 1) // offset of products ), "categories" => $this->getDoctrine()->getRepository(Category::class)->findBy( [ @@ -33,7 +44,9 @@ public function homepageAction() "rank" => "desc", ] ), + "totalPages" => $totalPages, + "currentPage" => $currentPage, ]; } -} +} \ No newline at end of file