-
Notifications
You must be signed in to change notification settings - Fork 13
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Symfony Standard Edition | ||
======================== | ||
|
||
test pull request |
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 %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
*/ | ||
|
@@ -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"); | ||
} | ||
} | ||
|
||
$paginator = new PaginatorHelper; | ||
$products = $this->getDoctrine()->getRepository(Product::class)->countByCategory($category); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 [ | ||
|
||
]; | ||
} */ | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
*/ | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. odsazení oproti zbytku kódu |
||
), | ||
"categories" => $this->getDoctrine()->getRepository(Category::class)->findBy( | ||
[ | ||
|
@@ -33,7 +45,9 @@ public function homepageAction() | |
"rank" => "desc", | ||
] | ||
), | ||
]; | ||
"paginator" => $paginator, | ||
"showPage" => $paginator->getPageOffer() | ||
]; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Zapomenutý debug výstup :) |
||
return $count = $query->getSingleScalarResult(); | ||
} | ||
} |
There was a problem hiding this comment.
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)