Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a href="{{ path("homepage") }}">Domů</a>
<a href="{{ path("homepage", {"page":1}) }}">Domů</a>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asi bych takový argument nepředával - spíš pak někde uděláš chybu, navíc url /1 není nijak hezká

</li>
</ul>
</div>
Expand Down
11 changes: 11 additions & 0 deletions app/Resources/views/components/pagination.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="col-sm-12 col-md-12 col-lg-12">
{% if totalPages > 1 %}
<ul class="pagination pagination-md">
{% for i in 1..totalPages %}
<li {{ currentPage==i ? "class=\"active\"" }}>
<a href="{{ path("homepage", {"page":i}) }}">{{i}}</a>
</li>
{% endfor %}
</ul>
{% endif %}
</div>
1 change: 1 addition & 0 deletions app/Resources/views/homepage/homepage.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% block body %}
<div class="row">
{% include 'components/list.html.twig' %}
{% include 'components/pagination.html.twig' %}
</div>
{% endblock %}

36 changes: 36 additions & 0 deletions data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
# Dump of table category
# ------------------------------------------------------------

DROP TABLE IF EXISTS `category`;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Právě kvůli tomu slouží bin/console doctrine:schema:create a bin/console doctrine:schema:drop. To ti umožňuje vždy generovat správnou strukturu DB a tohle ti potom jen naplní daty tabulky.


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 */;

Expand Down Expand Up @@ -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 */;

Expand Down Expand Up @@ -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 */;

Expand Down
21 changes: 17 additions & 4 deletions src/AppBundle/Controller/HomepageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,36 @@
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 <jenik@klatys.cz>
*/
class HomepageController extends Controller
{
/**
* @Route("/", name="homepage")
* @Route("/{page}", name="homepage")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pozor na tohle, http://localhost:8000/a ti spadne

* @Template("homepage/homepage.html.twig")
*
* @param Request $request
* @return array
*/
public function homepageAction()
public function homepageAction(Request $request)
{
$perPage = 6;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tohle je hodnota, která by měla být spíše v konfiguraci

$currentPage = $request->attributes->get("page");
$totalProducts = $this->getDoctrine()->getRepository(Product::class)->findBy([]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tady pozor, taháš celé produkty jen na to abys je spočítal... teď jich je 44, když jich bude x tisíc, bude to drahé...

$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(
[
Expand All @@ -33,7 +44,9 @@ public function homepageAction()
"rank" => "desc",
]
),
"totalPages" => $totalPages,
"currentPage" => $currentPage,
];
}

}
}