diff --git a/src/Govnokod/CodeBundle/Controller/CodeController.php b/src/Govnokod/CodeBundle/Controller/CodeController.php index aea5057..7c27ec6 100644 --- a/src/Govnokod/CodeBundle/Controller/CodeController.php +++ b/src/Govnokod/CodeBundle/Controller/CodeController.php @@ -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'); @@ -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'); diff --git a/src/Govnokod/CodeBundle/Resources/config/routing.yml b/src/Govnokod/CodeBundle/Resources/config/routing.yml index 045ae5b..21c2985 100644 --- a/src/Govnokod/CodeBundle/Resources/config/routing.yml +++ b/src/Govnokod/CodeBundle/Resources/config/routing.yml @@ -21,5 +21,7 @@ code_view_legacy: id: \d+ code_list: - pattern: /{category}/ + pattern: /{category} defaults: { _controller: GovnokodCodeBundle:Code:list } + requirements: + category: "[a-z]*" diff --git a/src/Govnokod/CodeBundle/Resources/config/services.yml b/src/Govnokod/CodeBundle/Resources/config/services.yml index 8ee016c..d293619 100644 --- a/src/Govnokod/CodeBundle/Resources/config/services.yml +++ b/src/Govnokod/CodeBundle/Resources/config/services.yml @@ -1,5 +1,7 @@ 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: @@ -7,3 +9,13 @@ services: 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" diff --git a/src/Govnokod/CodeBundle/Resources/views/Code/list.html.twig b/src/Govnokod/CodeBundle/Resources/views/Code/list.html.twig index 28a9f8d..10e915a 100644 --- a/src/Govnokod/CodeBundle/Resources/views/Code/list.html.twig +++ b/src/Govnokod/CodeBundle/Resources/views/Code/list.html.twig @@ -2,33 +2,85 @@ {% block content %}
    -{% for code in codes %} -
  1. -

    + {% for code in codes %} +
  2. +

    {{ code.category.title }} - / + / Говнокод #{{ code.id }} -

    -

    +

  3. +

    {% if code.rating > 0 %}+{% elseif code.rating < 0 %}−{% endif %}{{ code.rating|abs }} -

    -
    -
    {{ code.body }}
    -
    -

    - {{ code.description|nl2br }} -

    -{# -

    - Запостил: {{ code.user.username }}, - {{ code.created|date('d F Y') }} -

    -#} -
    +

    +
    +
    {{ code.body }}
    +
    +

    + {{ code.description|nl2br }} +

    + +
     Комментарии ({{ code.commentsCount }}) {# Комментарии (getCommentsCount(); if ($toolkit->getUser()->isLoggedIn() && $quote->getNewCommentsCount() > 0) { ?>, +getNewCommentsCount(); ?>)#} -
    -
  4. -{% endfor %} + + + {% endfor %}
+ + {% set paginationPath = 'code_list' %} + {% set nearbyPagesLimit = 4 %} + {% set nearbyHigh = currentPage + nearbyPagesLimit %} + {% set nearbyLow = currentPage - nearbyPagesLimit %} + + + + {% endblock %} diff --git a/src/Govnokod/CodeBundle/Service/CategoryService.php b/src/Govnokod/CodeBundle/Service/CategoryService.php new file mode 100644 index 0000000..5adbb45 --- /dev/null +++ b/src/Govnokod/CodeBundle/Service/CategoryService.php @@ -0,0 +1,27 @@ +entityManager = $entityManager; + } + + public function getEntity($category) + { + $categoryRepository = $this->entityManager->getRepository("GovnokodCodeBundle:Category"); + $entity = $categoryRepository->findOneByName($category); + + return $entity; + } + +} diff --git a/src/Govnokod/CodeBundle/Service/CodeService.php b/src/Govnokod/CodeBundle/Service/CodeService.php new file mode 100644 index 0000000..2884273 --- /dev/null +++ b/src/Govnokod/CodeBundle/Service/CodeService.php @@ -0,0 +1,55 @@ +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]; + } +}