Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Úkol 2 - Lubomír Jiřišta #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
/vendor/
/web/bundles/
composer.lock
workspace.xml
composer.phar
.idea
11 changes: 11 additions & 0 deletions app/Resources/views/components/breadcrumbs.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<ol class="breadcrumb" style="margin-bottom: 0;">
{% for key, value in breadcrumbs %}
{% if key starts with '#' %}
<li><a href="#" {% if loop.last %}class="active"{% endif %}>{{ value.title }}</a></li>
{% elseif value.slug is defined %}
<li><a href="{{ path(key, {slug: value.slug}) }}" {% if loop.last %}class="active"{% endif %}>{{ value.title }}</a></li>
{% else %}
<li><a href="{{ path(key) }}" {% if loop.last %}class="active"{% endif %}>{{ value.title }}</a></li>
{% endif %}
{% endfor %}
</ol>
20 changes: 20 additions & 0 deletions app/Resources/views/components/pagination.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% if maxPages > 1 %}
<ul class="pagination pagination-sm pull-right">
{# `«` arrow #}
<li {{ page == 1 ? 'class="disabled"' }}>
<a href="{{ path('homepage', {page: page-1 < 1 ? 1 : page-1}) }}">«</a>
</li>

{# Render each page number #}
{% for i in 1..maxPages %}
<li {{ page == i ? 'class="active"' }}>
<a href="{{ path('homepage', {page: i}) }}">{{ i }}</a>
</li>
{% endfor %}

{# `»` arrow #}
<li {{ page == maxPages ? 'class="disabled"' }}>
<a href="{{ path('homepage', {page: page+1 <= maxPages ? page+1 : page}) }}">»</a>
</li>
</ul>
{% endif %}
9 changes: 9 additions & 0 deletions app/Resources/views/homepage/homepage.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

{% block body %}
<div class="row">
<div class="col-md-12">
{% include 'components/breadcrumbs.html.twig' %}
</div>
<div class="col-md-12">
{% include 'components/pagination.html.twig' with { 'page': page, 'maxPages': maxPages } %}
</div>
{% include 'components/list.html.twig' %}
<div class="col-md-12">
{% include 'components/pagination.html.twig' with { 'page': page, 'maxPages': maxPages } %}
</div>
</div>
{% endblock %}

9 changes: 3 additions & 6 deletions app/Resources/views/product/detail.html.twig
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
{% extends 'base.html.twig' %}

{% block body %}
<ol class="breadcrumb">
{% if product.category %}
<li><a href="#">{{ product.category.title }}</a></li>
{% endif %}
<li class="active">{{ product.title }}</li>
</ol>
<div class="col-md-12">
{% include 'components/breadcrumbs.html.twig' %}
</div>
<h1> {{ product.title }}</h1>
<img src="{{ product.image }}" />
{% endblock %}
10 changes: 10 additions & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ services:
# service_name:
# class: AppBundle\Directory\ClassName
# arguments: ["@another_service_name", "plain_value", "%parameter_name%"]
product_repository:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Pecka super :)

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
72 changes: 50 additions & 22 deletions src/AppBundle/Controller/HomepageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand All @@ -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;
}
}
7 changes: 7 additions & 0 deletions src/AppBundle/Controller/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[],
Expand Down
9 changes: 9 additions & 0 deletions src/AppBundle/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@
*/
class CategoryRepository extends EntityRepository
{
public function getAllCategories()
{
return $this->findBy(
[],
[
"rank" => "desc",
]
);
}
}
8 changes: 8 additions & 0 deletions src/AppBundle/Repository/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@
class ProductRepository extends EntityRepository
{

public function getAllProducts()
{
$query = $this->createQueryBuilder('p')
->orderBy('p.rank', 'DESC')
->getQuery();

return $query;
}
}