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

Ukol 2 - Tomas Linhart #20

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions app/Resources/views/category/detail.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends 'base.html.twig' %}

{% block body %}
{% include ":components:breadcrumb.html.twig" %}
Copy link
Member

Choose a reason for hiding this comment

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

Kód by měl být vždy konzistentní napříč celým projektem, zde máš dva způsoby zápisu odkazu na šablonu: components/ a :components:

<div class="row">
{% include 'components/list.html.twig' %}
</div>
Expand Down
19 changes: 19 additions & 0 deletions app/Resources/views/components/breadcrumb.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<ol class="breadcrumb">
<li>
<a href="{{ path("homepage") }}">Domů</a>
</li>
{% for category in breadcrumbVO.categories %}
{% if loop.last and not breadcrumbVO.isProduct %}
<li class="active">{{ category.title }}</li>
{% else %}
<li>
<a href="{{ path("category_detail", {"slug": category.slug}) }}">
{{ category.title }}
</a>
</li>
{% endif %}
{% endfor %}
{% if breadcrumbVO.isProduct %}
<li class="active">{{ product.title }}</li>
{% endif %}
</ol>
4 changes: 2 additions & 2 deletions app/Resources/views/components/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<img src="{{ product.image }}" alt="{{ product.title }}">
<div class="caption">
<h4 class="pull-right">{{ product.price }} Kč</h4>
</h4>
<p>{{ product.title }}</p>
</div>
</div>
</a>
</div>
{% endfor %}
{% endfor %}
{% include ':components:paginator.html.twig' with {'paginatorVO': paginatorVO} %}
50 changes: 50 additions & 0 deletions app/Resources/views/components/paginator.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{% set routeName = paginatorVO.routeName %}
{% set thisPage = paginatorVO.page %}
{% set maxPages = paginatorVO.maxPages %}
{% set from = paginatorVO.from %}
{% set to = paginatorVO.to %}
{% set totalCount = paginatorVO.totalCount %}
{% if maxPages > 1 %}
<ul class="pagination pagination-sm">
<li>
<a href="{{ path(routeName, {page: 1}) }}">First</a>
</li>

{# `«` arrow #}
<li {{ thisPage == 1 ? 'class="disabled"' }}>
<a href="{{ path(routeName, {page: thisPage-1 < 1 ? 1 : thisPage-1}) }}">«</a>
</li>

{# Render each page number #}
{% if maxPages <= 6 %}
{% for i in 1..maxPages %}
<li {{ thisPage == i ? 'class="active"' }}>
<a href="{{ path(routeName, {page: i}) }}">{{ i }}</a>
</li>
{% endfor %}
{% else %}
{% set limit = 3 %}
{% for i in 1..limit %}
<li {{ thisPage == i ? 'class="active"' }}>
<a href="{{ path(routeName, {page: i}) }}">{{ i }}</a>
</li>
{% endfor %}
<li>...</li>
{% for i in maxPages-limit..maxPages %}
<li {{ thisPage == i ? 'class="active"' }}>
<a href="{{ path(routeName, {page: i}) }}">{{ i }}</a>
</li>
{% endfor %}
{% endif %}

{# `»` arrow #}
<li {{ thisPage == maxPages ? 'class="disabled"' }}>
<a href="{{ path(routeName, {page: thisPage+1 <= maxPages ? thisPage+1 : thisPage}) }}">»</a>
</li>

<li>
<a href="{{ path(routeName, {page: maxPages}) }}">Last</a>
</li>
<li><span class="label label-default">{{ from }} - {{ to }} / {{ totalCount }}</span></li>
</ul>
{% endif %}
2 changes: 1 addition & 1 deletion app/Resources/views/homepage/homepage.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block body %}
<div class="row">
{% include 'components/list.html.twig' %}
{% include ':components:list.html.twig' %}
</div>
{% endblock %}

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

{% block body %}
<ol class="breadcrumb">
<li>
<a href="{{ path("category_detail", {"slug": product.firstCategory.slug}) }}">
{{ product.firstCategory.title }}
</a>
</li>
<li class="active">{{ product.title }}</li>
</ol>
{% include ":components:breadcrumb.html.twig" %}
<h1> {{ product.title }}</h1>
<img src="{{ product.image }}" />
{% endblock %}
{% endblock %}
38 changes: 24 additions & 14 deletions src/AppBundle/Controller/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use AppBundle\Entity\Category;
use AppBundle\Entity\Product;
use AppBundle\Repository\ProductRepository;
use AppBundle\VO\BreadcrumbVO;
use AppBundle\VO\PaginatorVO;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
Expand All @@ -15,33 +18,40 @@
class CategoryController extends Controller
{
/**
* @Route("/vyber/{slug}", name="category_detail")
* @Route("/vyber/{slug}/{page}", name="category_detail", requirements={"page": "\d+"})
* @Template("category/detail.html.twig")
*
* @param Request $request
* @param string $slug
* @param int $page
* @return array
*/
public function categoryDetail(Request $request)
public function categoryDetail(Request $request, $slug, $page = 1)
{
$category = $this->getDoctrine()->getRepository(Category::class)->findOneBy([
"slug" => $request->attributes->get("slug"),
]);
$category = $this->getDoctrine()->getRepository(Category::class)->findOneBySlug($slug);

if (!$category) {
throw new NotFoundHttpException("Kategorie neexistuje");
}

$breadcrumb = $this->getDoctrine()->getRepository(Category::class)->findBreadCrumbPath($category);

$limit = ProductRepository::PRODUCTS_PER_PAGE;

$paginator = $this->getDoctrine()->getRepository(Product::class)->findByCategory(
$category,
$page,
$limit
);

$paginatorVO = PaginatorVO::create($paginator, $page, $limit, $request->get('_route'));

return [
"products" => $this->getDoctrine()->getRepository(Product::class)->findByCategory($category),
"categories" => $this->getDoctrine()->getRepository(Category::class)->findBy(
[
"parentCategory" => $category,
],
[
"rank" => "desc",
]
),
"category" => $category,
"categories" => $this->getDoctrine()->getRepository(Category::class)->findByParentCategory($category),
"products" => $paginator->getIterator(),
"paginatorVO" => $paginatorVO,
"breadcrumbVO" => BreadcrumbVO::create($breadcrumb),
];
}

Expand Down
36 changes: 19 additions & 17 deletions src/AppBundle/Controller/HomepageController.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php
namespace AppBundle\Controller;

use AppBundle\Entity\Category;
use AppBundle\Entity\Product;
use AppBundle\Repository\ProductRepository;
use AppBundle\VO\PaginatorVO;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
Expand All @@ -12,27 +16,25 @@
class HomepageController extends Controller
{
/**
* @Route("/", name="homepage")
* @Route("/{page}", name="homepage", requirements={"page": "\d+"})
* @Template("homepage/homepage.html.twig")
*
* @param Request $request
* @param int $page
* @return array
*/
public function homepageAction()
public function homepageAction(Request $request, $page = 1)
{
$limit = ProductRepository::PRODUCTS_PER_PAGE;

$paginator = $this->getDoctrine()->getRepository(Product::class)->findAllProducts($page, $limit);

$paginatorVO = PaginatorVO::create($paginator, $page, $limit, $request->get('_route'));

return [
"products" => $this->getDoctrine()->getRepository(Product::class)->findBy(
[],
[
"rank" => "desc"
],
21
),
"categories" => $this->getDoctrine()->getRepository(Category::class)->findBy(
[
"level" => 0,
],
[
"rank" => "desc",
]
),
"categories" => $this->getDoctrine()->getRepository(Category::class)->findTopCategories(),
"products" => $paginator->getIterator(),
"paginatorVO" => $paginatorVO,
];
}

Expand Down
25 changes: 11 additions & 14 deletions src/AppBundle/Controller/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace AppBundle\Controller;
use AppBundle\Entity\Category;
use AppBundle\Entity\Product;
use AppBundle\VO\BreadcrumbVO;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
Expand All @@ -19,29 +20,25 @@ class ProductController extends Controller
* @Route("/product/{slug}", name="product_detail")
* @Template("product/detail.html.twig")
*
* @param Request $request
* @param string $slug
* @return array
*/
public function productDetailAction(Request $request)
public function productDetailAction($slug)
{
$product = $this->getDoctrine()->getRepository(Product::class)->findOneBy([
"slug" => $request->attributes->get("slug"),
]);
$product = $this->getDoctrine()->getRepository(Product::class)->findOneBySlug($slug);
if (!$product) {
throw new NotFoundHttpException("Produkt neexistuje");
}

$category = $this->getDoctrine()->getRepository(Category::class)->findOneByProduct($product);

$breadcrumb = $this->getDoctrine()->getRepository(Category::class)->findBreadCrumbPath($category);

return [
"product" => $product,
"categories" => $this->getDoctrine()->getRepository(Category::class)->findBy(
[
"parentCategoryId" => $product->getFirstCategory() ? $product->getFirstCategory()->getId() : null,
],
[
"rank" => "desc",
]
),
"category" => $product->getFirstCategory(),
"category" => $category,
"categories" => $this->getDoctrine()->getRepository(Category::class)->findByParentCategory($category),
"breadcrumbVO" => BreadcrumbVO::create($breadcrumb, $product),
];

}
Expand Down
79 changes: 79 additions & 0 deletions src/AppBundle/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace AppBundle\Repository;

use AppBundle\Entity\Category;
use AppBundle\Entity\Product;
use Doctrine\ORM\EntityRepository;

/**
Expand All @@ -12,4 +14,81 @@
*/
class CategoryRepository extends EntityRepository
{

/**
* @param string $slug
* @return null|Category
*/
public function findOneBySlug($slug)
{
return $this->findOneBy(
[
"slug" => $slug
]
);
}

/**
* @return Category[]
*/
public function findTopCategories()
{
return $this->findBy(
[
"level" => 0,
],
[
"rank" => "desc",
]
);
}

/**
* @param Category $category
* @return Category[]
*/
public function findByParentCategory(Category $category = null)
{
return $this->findBy(
[
"parentCategory" => $category,
],
[
"rank" => "desc",
]
);
}

/**
* @param Product $product
* @return Category|null
*/
public function findOneByProduct(Product $product)
{
return $this->_em->createQuery('SELECT c
FROM AppBundle\Entity\ProductCategory pc
JOIN AppBundle\Entity\Category c WITH pc.category = c
WHERE pc.product = :product
ORDER BY c.rank DESC
')
->setParameter("product", $product)
->setMaxResults(1)
->getSingleResult();
}

/**
* @param Category $category
* @return Category[]
*/
public function findBreadCrumbPath(Category $category)
{
return $this->_em->createQuery('SELECT c
FROM AppBundle\Entity\Category c
WHERE :left BETWEEN c.left AND c.right
ORDER BY c.left ASC
')
->setParameter("left", $category->getLeft())
->getResult();
}

}
Loading