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

Code pagination #11

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
39 changes: 22 additions & 17 deletions src/Govnokod/CodeBundle/Controller/CodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,40 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Govnokod\CodeBundle\Entity\Code;
use Govnokod\CommentBundle\Entity\Thread;
use Symfony\Component\HttpFoundation\Request;

class CodeController extends Controller
{
public function listAction($category = null)
public function listAction(Request $request, $category = null)
{
$em = $this->getDoctrine()->getManager();

$criteria = array();
if (!is_null($category)) {
$categoryRepository = $em->getRepository("GovnokodCodeBundle:Category");
$categoryEntity = $categoryRepository->findByName($category);
/* var $codeService \Govnokod\CodeBundle\Service\CodeService */
$codeService = $this->container->get('code_service');
/* var $categoryService \Govnokod\CodeBundle\Service\CategoryService */
$categoryService = $this->container->get('category_service');
$categoryEntity = null;

if ($category) {
$categoryEntity = $categoryService->getEntity($category);
if (!$categoryEntity) {
throw $this->createNotFoundException("Category «{$category}» doesn't exists");
}
$criteria['category'] = $categoryEntity;
}

$codeRepository = $em->getRepository('GovnokodCodeBundle:Code');
$codes = $codeRepository->findBy($criteria, array('created_at' => 'DESC'));
$page = $request->query->get('page', 0);
$pageSize = $this->container->getParameter('code_controller.page_size');

return $this->render('GovnokodCodeBundle:Code:list.html.twig', array(
'codes' => $codes
));
list($codes, $pageCount, $actualPage) = $codeService->paginate($page, $pageSize, $categoryEntity);

return $this->render('GovnokodCodeBundle:Code:list.html.twig', [
'codes' => $codes,
'currentPage' => $actualPage,
'pageCount' => $pageCount,
'current_filters' => ['category' => $category]
]);
}

public function viewAction($id)
public function viewAction(Request $request, $id)
{
$request = $this->getRequest();
$em = $this->getDoctrine()->getManager();

$codeRepository = $em->getRepository('GovnokodCodeBundle:Code');
Expand Down Expand Up @@ -64,9 +70,8 @@ public function viewAction($id)
));
}

public function saveAction($id = null)
public function saveAction(Request $request, $id = null)
{
$request = $this->getRequest();
$em = $this->getDoctrine()->getManager();

$codeRepository = $em->getRepository('GovnokodCodeBundle:Code');
Expand Down
4 changes: 3 additions & 1 deletion src/Govnokod/CodeBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ code_view_legacy:
id: \d+

code_list:
pattern: /{category}/
pattern: /{category}
defaults: { _controller: GovnokodCodeBundle:Code:list }
requirements:
category: "[a-z]*"
14 changes: 13 additions & 1 deletion src/Govnokod/CodeBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
parameters:
# govnokod_code.example.class: Govnokod\CodeBundle\Example
code_service.class: Govnokod\CodeBundle\Service\CodeService
category_service.class: Govnokod\CodeBundle\Service\CategoryService
code_controller.page_size: 20

services:
sonata.admin.govnokod:
class: Govnokod\CodeBundle\Admin\CodeCategoryAdmin
arguments: [null, 'Govnokod\CodeBundle\Entity\Category', 'SonataAdminBundle:CRUD']
tags:
- { name: sonata.admin, manager_type: orm, label: Категории говнокода }

code_service:
class: %code_service.class%
arguments:
entityManager: "@doctrine.orm.entity_manager"

category_service:
class: %category_service.class%
arguments:
entityManager: "@doctrine.orm.entity_manager"
98 changes: 75 additions & 23 deletions src/Govnokod/CodeBundle/Resources/views/Code/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,85 @@

{% block content %}
<ol class="posts hatom">
{% for code in codes %}
<li class="hentry">
<h2>
{% for code in codes %}
<li class="hentry">
<h2>
<a rel="chapter" href="{{ path('code_list', { 'category': code.category.name}) }}">{{ code.category.title }}</a>
/
/
<a rel="bookmark" class="entry-title" href="{{ path('code_view', { 'category': code.category.name, 'id': code.id }) }}">Говнокод #{{ code.id }}</a>
</h2>
<p class="vote">
</h2>
<p class="vote">
<strong class="just-rating{% if code.rating < 0 %} bad{% endif %}" title="{{ code.votesOn }} за и {{ code.votesAgainst }} против">{% if code.rating > 0 %}+{% elseif code.rating < 0 %}&minus;{% endif %}{{ code.rating|abs }}</strong>
</p>
<div class="entry-content">
<pre><code>{{ code.body }}</code></pre>
</div>
<p class="description">
{{ code.description|nl2br }}
</p>
{#
<p class="author">
Запостил: <a href=""><img src="" alt="" class="avatar" /></a> <a href="">{{ code.user.username }}</a>,
<abbr title="{{ code.created|date('c') }}">{{ code.created|date('d F Y') }}</abbr>
</p>
#}
<div class="entry-comments">
</p>
<div class="entry-content">
<pre><code>{{ code.body }}</code></pre>
</div>
<p class="description">
{{ code.description|nl2br }}
</p>

<div class="entry-comments">
<span class="comments-icon">&nbsp;</span><a href="{{ path('code_view', { 'category': code.category.name, 'id': code.id }) }}" data-load-url="{{ path('code_comments_list', { 'code_id': code.id }) }}" class="entry-comments-load" title="Показать комментарии">Комментарии</a> <span class="entry-comments-count">({{ code.commentsCount }})</span>
{#<span class="comments-icon<?php if ($toolkit->getUser()->isLoggedIn() && $quote->getNewCommentsCount() > 0) { echo ' comments-new" title="Есть новые комментарии!'; } ?>">&nbsp;</span><a href="<?php echo htmlspecialchars($url . '/' . $quote->getId()); ?>" class="entry-comments-load">Комментарии</a> <span class="entry-comments-count">(<?php echo $quote->getCommentsCount(); if ($toolkit->getUser()->isLoggedIn() && $quote->getNewCommentsCount() > 0) { ?>, <span title="Новые комментарии" class="entry-comments-new">+<?php echo $quote->getNewCommentsCount(); ?></span><?php } ?>)</span>#}
</div>
</li>
{% endfor %}
</div>
</li>
{% endfor %}
</ol>

{% set paginationPath = 'code_list' %}
{% set nearbyPagesLimit = 4 %}
{% set nearbyHigh = currentPage + nearbyPagesLimit %}
{% set nearbyLow = currentPage - nearbyPagesLimit %}

<ul class="pagination">
{% if currentPage < pageCount %}
<li>
<a href="{{ path(paginationPath, current_filters|merge({page: currentPage+1})) }}">← влево</a>
</li>
{% endif %}

{% if currentPage > 1 %}
<li>
<a href="{{ path(paginationPath, current_filters|merge({page: currentPage-1})) }}">вправо →</a>
<li>
{% endif %}
</ul>

<ul class="pagination numbered">
{% if pageCount > 1 %}
<li>
{% if currentPage < pageCount %}
<a href="{{ path(paginationPath, current_filters|merge({page: pageCount})) }}">{{ pageCount }}</a>
{% else %}
{{ pageCount }}
{% endif %}
</li>

{% if nearbyHigh < pageCount - 1 %}
<li>...</li>
{% endif %}

{% for i in range(nearbyHigh, nearbyLow) if (i < pageCount and i > 1) %}
<li>
{% if i == currentPage %}
{{ currentPage }}
{% else %}
<a href="{{ path(paginationPath, current_filters|merge({page: i})) }}">{{ i }}</a>
{% endif %}
</li>
{% endfor %}

{% if nearbyLow > 2 %}
<li>...</li>
{% endif %}

<li>
{% if currentPage > 1 %}
<a href="{{ path(paginationPath, current_filters|merge({page: 1})) }}">1</a>
{% else %}
1
{% endif %}
</li>
{% endif %}
</ul>
{% endblock %}
27 changes: 27 additions & 0 deletions src/Govnokod/CodeBundle/Service/CategoryService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Govnokod\CodeBundle\Service;

use Doctrine\Common\Persistence\ObjectManager;

class CategoryService
{
/**
* @var ObjectManager
*/
private $entityManager;

public function __construct(ObjectManager $entityManager)
{
$this->entityManager = $entityManager;
}

public function getEntity($category)
{
$categoryRepository = $this->entityManager->getRepository("GovnokodCodeBundle:Category");
$entity = $categoryRepository->findOneByName($category);

return $entity;
}

}
55 changes: 55 additions & 0 deletions src/Govnokod/CodeBundle/Service/CodeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Govnokod\CodeBundle\Service;

use Doctrine\Common\Persistence\ObjectManager;

class CodeService
{
/**
* @var ObjectManager
*/
private $entityManager;

public function __construct(ObjectManager $entityManager)
{
$this->entityManager = $entityManager;
}

public function paginate($page, $pageSize, $categoryEntity)
{
/* @var $codeRepository \Doctrine\ORM\EntityRepository */
$codeRepository = $this->entityManager->getRepository('GovnokodCodeBundle:Code');

$countQuery = $codeRepository->createQueryBuilder('c')
->select('count(c.id) as _count');

if ($categoryEntity) {
$countQuery->where('c.category = :category')
->setParameter('category', $categoryEntity);
}

$count = $countQuery->getQuery()->execute()[0]['_count'];
$pageCount = (int) ceil($count / $pageSize);

if ($page < 1 || $page > $pageCount) {
$page = $pageCount;
}

$itemQuery = $codeRepository->createQueryBuilder('c')
->select(['c', 'cat'])
->join('c.category', 'cat')
->orderBy('c.created_at', 'DESC')
->setFirstResult(($pageCount - $page) * $pageSize)
->setMaxResults($pageSize);

if ($categoryEntity) {
$itemQuery->where('c.category = :category')
->setParameter('category', $categoryEntity);
}

$codes = $itemQuery->getQuery()->execute();

return [$codes, $pageCount, $page];
}
}