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 - Aleš Kúdela #23

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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Symfony Standard Edition
========================

test pull request
2 changes: 1 addition & 1 deletion app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="/bootstrap/css/shop-homepage.css" rel="stylesheet">

<link href="/bootstrap/css/paginator.css" rel="stylesheet" type="text/css"/>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
Expand Down
4 changes: 4 additions & 0 deletions app/Resources/views/category/detail.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
<div class="row">
{% include 'components/list.html.twig' %}
</div>

<div class="row">
{% include 'components/paginator.html.twig' with {'actualPageLink': 'category_detail'} %}
</div>
{% endblock %}

58 changes: 58 additions & 0 deletions app/Resources/views/components/paginator.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{% if attribute(paginator,'getPageCount') > 1 %}
<nav>
{% if category is defined %}
<ul class="pagination">
{% if not attribute(paginator,'isFirst') %}
<li>
<a href="{{ path(actualPageLink, {"page": attribute(paginator,'getPage')-1, "slug": category.slug} )}}">
<span aria-hidden="true">Zpět</span>
</a>
</li>
{% endif %}
{% for i in showPage %}
{% if i == 'gap' %}
<span class="gap">...</span>
{% else %}
<li class="{% if i == attribute(paginator,'getPage') %}active{% endif %}">
<a href="{{ path(actualPageLink, {"page": i, "slug": category.slug} )}}">{{ i }}</a>
</li>
{% endif %}
{% endfor %}
{% if not attribute(paginator,'isLast') %}
<li>
<a href="{{ path(actualPageLink, {"page": attribute(paginator,'getPage')+1, "slug": category.slug} )}}">
<span>Další</span>
</a>
</li>
{% endif %}
</ul>
{% else %}
<ul class="pagination">
{% if not attribute(paginator,'isFirst') %}
<li>
<a href="{{ path(actualPageLink, {"page": attribute(paginator,'getPage')-1} )}}">
<span aria-hidden="true">Zpět</span>
</a>
</li>
{% endif %}
{% for i in showPage %}
{% if i == 'gap' %}
<span class="gap">...</span>
{% else %}
<li class="{% if i == attribute(paginator,'getPage') %}active{% endif %}">
<a href="{{ path(actualPageLink, {"page": i} )}}">{{ i }}</a>
</li>
{% endif %}
{% endfor %}
{% if not attribute(paginator,'isLast') %}
<li>
<a href="{{ path(actualPageLink, {"page": attribute(paginator,'getPage')+1} )}}">
<span>Další</span>
</a>
</li>
{% endif %}
</ul>
{% endif%}

</nav>
{% endif %}
3 changes: 3 additions & 0 deletions app/Resources/views/homepage/homepage.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
<div class="row">
{% include 'components/list.html.twig' %}
</div>
<div class="row">
{% include 'components/paginator.html.twig' with {'actualPageLink': 'homepage'} %}
</div>
{% endblock %}

28 changes: 23 additions & 5 deletions src/AppBundle/Controller/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

use AppBundle\Utils\PaginatorHelper;
/**
* @author Jan Klat <jenik@klatys.cz>
*/
Expand All @@ -23,16 +23,24 @@ class CategoryController extends Controller
*/
public function categoryDetail(Request $request)
{
$page = $request->query->get('page');
$category = $this->getDoctrine()->getRepository(Category::class)->findOneBy([
"slug" => $request->attributes->get("slug"),
]);

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

Choose a reason for hiding this comment

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

Ty ponechané whitespacy, pryč (zde a :30)


$paginator = new PaginatorHelper;
$products = $this->getDoctrine()->getRepository(Product::class)->countByCategory($category);
Copy link
Member

Choose a reason for hiding this comment

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

odsazení oproti zbytku kódu

$paginator->setItemCount($products);
$itemsPerPage = 2;
$paginator->setItemsPerPage($itemsPerPage);
$paginator->setPage($page);
dump($products);
return [
"products" => $this->getDoctrine()->getRepository(Product::class)->findByCategory($category),
"products" => $this->getDoctrine()->getRepository(Product::class)->findByCategoryAndLimit($category, $paginator->getLength(), $paginator->getOffset()),
"categories" => $this->getDoctrine()->getRepository(Category::class)->findBy(
[
"parentCategory" => $category,
Expand All @@ -42,7 +50,17 @@ public function categoryDetail(Request $request)
]
),
"category" => $category,
"paginator" => $paginator,
"showPage" => $paginator->getPageOffer()
];
}
}

/* public function renderShow($id, $page, $more = null, $order = null) {
Copy link
Member

Choose a reason for hiding this comment

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

Zakomentovaný kód většinou necommitujeme, nebo píšeme proč je zakomentovaný. Za půl roku nebudeš vědět proč tu je :)



return [

];
} */

}
22 changes: 18 additions & 4 deletions src/AppBundle/Controller/HomepageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;

use Symfony\Component\HttpFoundation\Request;
use AppBundle\Utils\PaginatorHelper;
/**
* @author Jan Klat <jenik@klatys.cz>
*/
Expand All @@ -14,16 +15,27 @@ class HomepageController extends Controller
/**
* @Route("/", name="homepage")
* @Template("homepage/homepage.html.twig")
* @param Request $request
*/
public function homepageAction()
public function homepageAction(Request $request)
{
$page = $request->query->get('page');

$paginator = new PaginatorHelper;
$products = $this->getDoctrine()->getRepository(Product::class)->countAll();
Copy link
Member

Choose a reason for hiding this comment

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

odsazení oproti zbytku kódu

$paginator->setItemCount($products);
$itemsPerPage = 2;
$paginator->setItemsPerPage($itemsPerPage);
$paginator->setPage($page);

return [
"products" => $this->getDoctrine()->getRepository(Product::class)->findBy(
[],
[
"rank" => "desc"
],
21
$paginator->getLength(),
$paginator->getOffset()
Copy link
Member

Choose a reason for hiding this comment

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

odsazení oproti zbytku kódu

),
"categories" => $this->getDoctrine()->getRepository(Category::class)->findBy(
[
Expand All @@ -33,7 +45,9 @@ public function homepageAction()
"rank" => "desc",
]
),
];
"paginator" => $paginator,
"showPage" => $paginator->getPageOffer()
];
}

}
40 changes: 40 additions & 0 deletions src/AppBundle/Repository/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use AppBundle\Entity\Category;
use AppBundle\Entity\Product;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;

class ProductRepository extends EntityRepository
{
Expand All @@ -26,5 +27,44 @@ public function findByCategory(Category $category)
->setParameter("rgt", $category->getRight())
->getResult();
}

/**
* @param Category $category
* @return Product[]
*/
public function findByCategoryAndLimit(Category $category, $length, $offset)
{
return $query = $this->_em->createQuery('SELECT p
FROM AppBundle\Entity\Product p
JOIN AppBundle\Entity\ProductCategory pc WITH p = pc.product
JOIN AppBundle\Entity\Category c WITH pc.category = c
WHERE c.left >= :lft and c.right <= :rgt
GROUP BY p
')
->setParameter("lft", $category->getLeft())
->setParameter("rgt", $category->getRight())
->setFirstResult($offset)
->setMaxResults($length)
->getResult();
return $paginator = new Paginator($query, $fetchJoinCollection = true);
}

public function countAll() {
$query = $this->_em->createQuery('SELECT COUNT(p.id) FROM AppBundle\Entity\Product p');
return $count = $query->getSingleScalarResult();
}

public function countByCategory(Category $category) {
$query = $this->_em->createQuery('SELECT COUNT(p.id)
FROM AppBundle\Entity\Product p
LEFT JOIN AppBundle\Entity\ProductCategory pc WITH p = pc.product
LEFT JOIN AppBundle\Entity\Category c WITH pc.category = c
WHERE c.left >= :lft and c.right <= :rgt

')
->setParameter("lft", $category->getLeft())
->setParameter("rgt", $category->getRight());
dump($query->getScalarResult());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Zapomenutý debug výstup :)

return $count = $query->getSingleScalarResult();
}
}
Loading