2015-05-30 06:50:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Controller;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
2017-11-04 21:30:32 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\DTO\{DailyEvents, TopUserDTO};
|
2015-05-31 02:48:12 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
2015-05-30 06:50:44 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
2015-06-26 15:05:26 +00:00
|
|
|
use Ob\HighchartsBundle\Highcharts\Highchart;
|
2017-11-04 21:30:32 +00:00
|
|
|
use Symfony\Component\HttpFoundation\{Request, Response};
|
2015-05-30 06:50:44 +00:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2015-05-31 02:48:12 +00:00
|
|
|
/**
|
2015-05-31 08:11:40 +00:00
|
|
|
* @param string $login
|
2015-05-31 02:48:12 +00:00
|
|
|
*/
|
2017-01-09 00:33:29 +00:00
|
|
|
public function showAction(Request $request, string $login): Response
|
2015-05-30 06:50:44 +00:00
|
|
|
{
|
2015-06-23 10:34:53 +00:00
|
|
|
/** @var EntityManager $em */
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
2015-05-31 08:11:40 +00:00
|
|
|
|
2015-06-23 10:34:53 +00:00
|
|
|
/** @var User $user */
|
|
|
|
$user = $em->getRepository('SkobkinPointToolsBundle:User')->findUserByLogin($login);
|
2015-05-31 08:11:40 +00:00
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
throw $this->createNotFoundException('User ' . $login . ' not found.');
|
|
|
|
}
|
|
|
|
|
2016-12-10 23:43:25 +00:00
|
|
|
$paginator = $this->get('knp_paginator');
|
|
|
|
|
|
|
|
$subscriberEventsPagination = $paginator->paginate(
|
|
|
|
$em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->createUserLastSubscribersEventsQuery($user),
|
|
|
|
$request->query->getInt('page', 1),
|
|
|
|
10
|
|
|
|
);
|
|
|
|
|
2015-05-31 02:48:12 +00:00
|
|
|
return $this->render('SkobkinPointToolsBundle:User:show.html.twig', [
|
|
|
|
'user' => $user,
|
2015-06-23 10:34:53 +00:00
|
|
|
'subscribers' => $em->getRepository('SkobkinPointToolsBundle:User')->findUserSubscribersById($user->getId()),
|
2016-12-10 23:43:25 +00:00
|
|
|
'subscriptions_log' => $subscriberEventsPagination,
|
2016-03-29 00:58:33 +00:00
|
|
|
'rename_log' => $em->getRepository('SkobkinPointToolsBundle:UserRenameEvent')->findBy(['user' => $user], ['date' => 'DESC'], 10),
|
2015-05-31 02:48:12 +00:00
|
|
|
]);
|
2015-05-30 06:50:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 00:33:29 +00:00
|
|
|
public function topAction(): Response
|
2015-05-30 06:50:44 +00:00
|
|
|
{
|
2017-01-10 01:21:57 +00:00
|
|
|
$userRepo = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:User');
|
|
|
|
$subscriptionsRecordsRepo = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:SubscriptionEvent');
|
|
|
|
$topUsers = $userRepo->getTopUsers();
|
|
|
|
$eventsByDay = $subscriptionsRecordsRepo->getLastEventsByDay();
|
2015-06-26 15:05:26 +00:00
|
|
|
|
2015-05-31 06:16:10 +00:00
|
|
|
return $this->render('@SkobkinPointTools/User/top.html.twig', [
|
2017-01-10 01:21:57 +00:00
|
|
|
'events_dynamic_chat' => $this->createEventsDynamicChart($eventsByDay),
|
|
|
|
'top_chart' => $this->createTopUsersGraph($topUsers),
|
2015-05-31 06:16:10 +00:00
|
|
|
]);
|
2015-05-30 06:50:44 +00:00
|
|
|
}
|
2015-05-31 07:02:00 +00:00
|
|
|
|
2015-06-26 15:05:26 +00:00
|
|
|
/**
|
2017-01-09 00:33:29 +00:00
|
|
|
* @todo move to the service
|
|
|
|
*
|
2017-01-10 01:21:57 +00:00
|
|
|
* @param DailyEvents[] $eventsByDay
|
|
|
|
*/
|
|
|
|
private function createEventsDynamicChart(array $eventsByDay = []): Highchart
|
|
|
|
{
|
2017-01-13 00:00:20 +00:00
|
|
|
$data = [];
|
2017-01-10 01:21:57 +00:00
|
|
|
|
2017-01-13 00:00:20 +00:00
|
|
|
foreach ($eventsByDay as $dailyEvents) {
|
|
|
|
$data[$dailyEvents->getDate()->format('d.m')] = $dailyEvents->getEventsCount();
|
2017-01-10 01:21:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 00:00:20 +00:00
|
|
|
return $this->createChart('eventschart', 'line', $data, 'Events by day', 'amount');
|
2017-01-10 01:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo move to the service
|
2017-01-09 00:33:29 +00:00
|
|
|
*
|
2017-01-10 01:21:57 +00:00
|
|
|
* @param TopUserDTO[] $topUsers
|
2015-06-26 15:05:26 +00:00
|
|
|
*/
|
2017-01-09 00:33:29 +00:00
|
|
|
private function createTopUsersGraph(array $topUsers = []): Highchart
|
2017-01-13 00:00:20 +00:00
|
|
|
{
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
foreach ($topUsers as $topUser) {
|
|
|
|
$data[$topUser->getLogin()] = $topUser->getSubscribersCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->createChart('topchart', 'bar', $data, 'Top users', 'amount');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createChart(string $blockId, string $type, array $data, string $bottomLabel, string $amountLabel): Highchart
|
2015-06-26 15:05:26 +00:00
|
|
|
{
|
2017-01-09 00:33:29 +00:00
|
|
|
$translator = $this->get('translator');
|
2015-06-26 15:05:26 +00:00
|
|
|
|
|
|
|
$chartData = [
|
2017-01-13 00:00:20 +00:00
|
|
|
'keys' => [],
|
|
|
|
'values' => [],
|
2015-06-26 15:05:26 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// Preparing chart data
|
2017-01-13 00:00:20 +00:00
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
$chartData['keys'][] = $key;
|
|
|
|
$chartData['values'][] = $value;
|
2015-06-26 15:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chart
|
|
|
|
$series = [[
|
2017-01-13 00:00:20 +00:00
|
|
|
'name' => $translator->trans($amountLabel),
|
|
|
|
'data' => $chartData['values'],
|
2015-06-26 15:05:26 +00:00
|
|
|
]];
|
|
|
|
|
|
|
|
// Initializing chart
|
|
|
|
$ob = new Highchart();
|
2017-01-13 00:00:20 +00:00
|
|
|
$ob->chart->renderTo($blockId);
|
|
|
|
$ob->chart->type($type);
|
|
|
|
$ob->title->text($translator->trans($bottomLabel));
|
2015-06-26 15:05:26 +00:00
|
|
|
$ob->xAxis->title(['text' => null]);
|
2017-01-13 00:00:20 +00:00
|
|
|
$ob->xAxis->categories($chartData['keys']);
|
|
|
|
$ob->yAxis->title(['text' => $translator->trans($amountLabel)]);
|
2015-06-26 15:05:26 +00:00
|
|
|
$ob->plotOptions->bar([
|
|
|
|
'dataLabels' => [
|
|
|
|
'enabled' => true
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
$ob->series($series);
|
|
|
|
|
|
|
|
return $ob;
|
|
|
|
}
|
2015-05-30 06:50:44 +00:00
|
|
|
}
|