Pagination of last events.

This commit is contained in:
Alexey Skobkin 2016-12-11 02:55:28 +03:00
parent 65cc08d91f
commit 74b39d7918
5 changed files with 53 additions and 73 deletions

View file

@ -5,16 +5,25 @@ namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class EventsController extends Controller class EventsController extends Controller
{ {
public function lastAction() public function lastAction(Request $request)
{ {
/** @var EntityManager $em */ /** @var EntityManager $em */
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$paginator = $this->get('knp_paginator');
$eventsPagination = $paginator->paginate(
$em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->createLastSubscriptionEventsQuery(),
$request->query->getInt('page', 1),
20
);
return $this->render('SkobkinPointToolsBundle:Events:last.html.twig', [ return $this->render('SkobkinPointToolsBundle:Events:last.html.twig', [
'last_events' => $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getLastSubscriptionEvents(20), 'last_events' => $eventsPagination,
]); ]);
} }
} }

View file

@ -43,7 +43,6 @@ class MainController extends Controller
'subscribers_count' => $em->getRepository('SkobkinPointToolsBundle:Subscription')->getUserSubscribersCountById($this->container->getParameter('point_id')), 'subscribers_count' => $em->getRepository('SkobkinPointToolsBundle:Subscription')->getUserSubscribersCountById($this->container->getParameter('point_id')),
'events_count' => $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getLastDayEventsCount(), 'events_count' => $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getLastDayEventsCount(),
'service_login' => $this->container->getParameter('point_login'), 'service_login' => $this->container->getParameter('point_login'),
'last_events' => $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getLastSubscriptionEvents(10),
]); ]);
} }

View file

@ -48,27 +48,19 @@ class SubscriptionEventRepository extends EntityRepository
} }
/** /**
* Get last $limit subscriptions * Get last subscriptions QueryBuilder for pagination
* *
* @param integer $limit * @return QueryBuilder
* @return SubscriptionEvent[]
*/ */
public function getLastSubscriptionEvents($limit) public function createLastSubscriptionEventsQuery()
{ {
if (!is_int($limit)) {
throw new \InvalidArgumentException('$limit must be an integer');
}
$qb = $this->createQueryBuilder('se'); $qb = $this->createQueryBuilder('se');
return $qb return $qb
->select() ->select(['se', 'a', 's'])
->innerJoin('se.author', 'a')
->innerJoin('se.subscriber', 's')
->orderBy('se.date', 'desc') ->orderBy('se.date', 'desc')
->setMaxResults($limit)
->getQuery()
->setFetchMode('SkobkinPointToolsBundle:SubscriptionEvent', 'author', ClassMetadata::FETCH_EAGER)
->setFetchMode('SkobkinPointToolsBundle:SubscriptionEvent', 'subscriber', ClassMetadata::FETCH_EAGER)
->getResult()
; ;
} }
} }

View file

@ -1,11 +0,0 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>Symfony2 is great</source>
<target>J'aime Symfony2</target>
</trans-unit>
</body>
</file>
</xliff>

View file

@ -4,52 +4,43 @@
{# TODO classes #} {# TODO classes #}
<div class="last-subscriptions-log"> <div class="last-subscriptions-log">
{% if last_events|length > 0 %} {% if last_events|length > 0 %}
<div class="panel-group" id="accordion-log"> <h3>{{ 'Last events'|trans }}</h3>
<div class="panel panel-default">
<div class="panel-heading" id="heading-subscriptions-log"> <table class="table table-striped">
<h4 class="panel-title"> <thead>
<a data-toggle="collapse" data-parent="#accordion-log" aria-expanded="true" href="#collapse-log"> <tr>
<span class="glyphicon glyphicon-collapse-down"></span> {{ 'Last events'|trans }} <td>{{ 'Subscriber'|trans }}</td>
</a> <td>{{ 'Author'|trans }}</td>
</h4> <td>{{ 'Action'|trans }}</td>
</div> <td>{{ 'Date'|trans }}</td>
<div id="collapse-log" class="panel-collapse collapse in" aria-labelledby="heading-subscriptions-log"> </tr>
<div class="panel-body"> </thead>
<table class="table table-striped"> <tbody>
<thead> {% for event in last_events %}
<tr> <tr>
<td>{{ 'Subscriber'|trans }}</td> <td>
<td>{{ 'Author'|trans }}</td> <a href="{{ url('user_show', {login: event.subscriber.login}) }}">@{{ event.subscriber.login }}</a>
<td>{{ 'Action'|trans }}</td> </td>
<td>{{ 'Date'|trans }}</td> <td>
</tr> <a href="{{ url('user_show', {login: event.author.login}) }}">@{{ event.author.login }}</a>
</thead> </td>
<tbody> <td>
{% for event in last_events %} <span class="glyphicon {% if event.action == 'subscribe' %}glyphicon-plus{% elseif event.action == 'unsubscribe' %}glyphicon-minus{% endif %}"></span>
<tr> </td>
<td> <td>
<a href="{{ url('user_show', {login: event.subscriber.login}) }}">@{{ event.subscriber.login }}</a> {# Use DateTime helper: https://sonata-project.org/bundles/intl/master/doc/reference/datetime.html #}
</td> {{ event.date|date('d F Y H:i:s') }}
<td> </td>
<a href="{{ url('user_show', {login: event.author.login}) }}">@{{ event.author.login }}</a> </tr>
</td> {% endfor %}
<td> </tbody>
<span class="glyphicon {% if event.action == 'subscribe' %}glyphicon-plus{% elseif event.action == 'unsubscribe' %}glyphicon-minus{% endif %}"></span> </table>
</td>
<td>
{# Use DateTime helper: https://sonata-project.org/bundles/intl/master/doc/reference/datetime.html #}
{{ event.date|date('d F Y H:i:s') }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{% else %} {% else %}
<div class="alert alert-warning" role="alert">{{ 'No log data found'|trans }}</div> <div class="alert alert-warning" role="alert">{{ 'No subscribers log data found'|trans }}</div>
{% endif %} {% endif %}
</div> </div>
<div class="navigation">
{{ knp_pagination_render(last_events) }}
</div>
{% endblock %} {% endblock %}