1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Skobkin\Bundle\PointToolsBundle\DTO\DailyEvents; |
7
|
|
|
use Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO; |
8
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
10
|
|
|
use Ob\HighchartsBundle\Highcharts\Highchart; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
|
14
|
|
|
class UserController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param string $login |
18
|
|
|
*/ |
19
|
3 |
|
public function showAction(Request $request, string $login): Response |
20
|
|
|
{ |
21
|
|
|
/** @var EntityManager $em */ |
22
|
3 |
|
$em = $this->getDoctrine()->getManager(); |
23
|
|
|
|
24
|
|
|
/** @var User $user */ |
25
|
3 |
|
$user = $em->getRepository('SkobkinPointToolsBundle:User')->findUserByLogin($login); |
26
|
|
|
|
27
|
3 |
|
if (!$user) { |
28
|
|
|
throw $this->createNotFoundException('User ' . $login . ' not found.'); |
29
|
|
|
} |
30
|
|
|
|
31
|
3 |
|
$paginator = $this->get('knp_paginator'); |
32
|
|
|
|
33
|
3 |
|
$subscriberEventsPagination = $paginator->paginate( |
34
|
3 |
|
$em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->createUserLastSubscribersEventsQuery($user), |
35
|
3 |
|
$request->query->getInt('page', 1), |
36
|
3 |
|
10 |
37
|
|
|
); |
38
|
|
|
|
39
|
3 |
|
return $this->render('SkobkinPointToolsBundle:User:show.html.twig', [ |
40
|
|
|
'user' => $user, |
41
|
3 |
|
'subscribers' => $em->getRepository('SkobkinPointToolsBundle:User')->findUserSubscribersById($user->getId()), |
42
|
3 |
|
'subscriptions_log' => $subscriberEventsPagination, |
43
|
3 |
|
'rename_log' => $em->getRepository('SkobkinPointToolsBundle:UserRenameEvent')->findBy(['user' => $user], ['date' => 'DESC'], 10), |
44
|
3 |
|
]); |
45
|
3 |
|
} |
46
|
3 |
|
|
47
|
|
|
public function topAction(): Response |
48
|
|
|
{ |
49
|
|
|
$userRepo = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:User'); |
50
|
1 |
|
$subscriptionsRecordsRepo = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:SubscriptionEvent'); |
51
|
|
|
$topUsers = $userRepo->getTopUsers(); |
52
|
1 |
|
$eventsByDay = $subscriptionsRecordsRepo->getLastEventsByDay(); |
53
|
|
|
|
54
|
1 |
|
return $this->render('@SkobkinPointTools/User/top.html.twig', [ |
55
|
|
|
'events_dynamic_chat' => $this->createEventsDynamicChart($eventsByDay), |
56
|
1 |
|
'top_chart' => $this->createTopUsersGraph($topUsers), |
57
|
1 |
|
]); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @todo move to the service |
62
|
|
|
* |
63
|
|
|
* @param DailyEvents[] $eventsByDay |
64
|
|
|
*/ |
65
|
|
|
private function createEventsDynamicChart(array $eventsByDay = []): Highchart |
66
|
|
|
{ |
67
|
|
|
$data = []; |
68
|
|
|
|
69
|
1 |
|
foreach ($eventsByDay as $dailyEvents) { |
70
|
|
|
$data[$dailyEvents->getDate()->format('d.m')] = $dailyEvents->getEventsCount(); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
return $this->createChart('eventschart', 'line', $data, 'Events by day', 'amount'); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @todo move to the service |
78
|
|
|
* |
79
|
1 |
|
* @param TopUserDTO[] $topUsers |
80
|
1 |
|
*/ |
81
|
1 |
|
private function createTopUsersGraph(array $topUsers = []): Highchart |
82
|
|
|
{ |
83
|
|
|
$data = []; |
84
|
|
|
|
85
|
|
|
foreach ($topUsers as $topUser) { |
86
|
1 |
|
$data[$topUser->getLogin()] = $topUser->getSubscribersCount(); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
return $this->createChart('topchart', 'bar', $data, 'Top users', 'amount'); |
90
|
|
|
} |
91
|
1 |
|
|
92
|
1 |
|
private function createChart(string $blockId, string $type, array $data, string $bottomLabel, string $amountLabel): Highchart |
93
|
1 |
|
{ |
94
|
1 |
|
$translator = $this->get('translator'); |
95
|
1 |
|
|
96
|
1 |
|
$chartData = [ |
97
|
1 |
|
'keys' => [], |
98
|
1 |
|
'values' => [], |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
// Preparing chart data |
102
|
1 |
|
foreach ($data as $key => $value) { |
103
|
1 |
|
$chartData['keys'][] = $key; |
104
|
|
|
$chartData['values'][] = $value; |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
// Chart |
108
|
|
|
$series = [[ |
109
|
|
|
'name' => $translator->trans($amountLabel), |
110
|
|
|
'data' => $chartData['values'], |
111
|
|
|
]]; |
112
|
|
|
|
113
|
|
|
// Initializing chart |
114
|
|
|
$ob = new Highchart(); |
115
|
|
|
$ob->chart->renderTo($blockId); |
116
|
|
|
$ob->chart->type($type); |
117
|
|
|
$ob->title->text($translator->trans($bottomLabel)); |
118
|
|
|
$ob->xAxis->title(['text' => null]); |
119
|
|
|
$ob->xAxis->categories($chartData['keys']); |
120
|
|
|
$ob->yAxis->title(['text' => $translator->trans($amountLabel)]); |
121
|
|
|
$ob->plotOptions->bar([ |
122
|
|
|
'dataLabels' => [ |
123
|
|
|
'enabled' => true |
124
|
|
|
] |
125
|
|
|
]); |
126
|
|
|
$ob->series($series); |
127
|
|
|
|
128
|
|
|
return $ob; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|