1
|
|
|
<?php namespace VojtaSvoboda\UserAccessLog\ReportWidgets; |
2
|
|
|
|
3
|
|
|
use App; |
4
|
|
|
use ApplicationException; |
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Exception; |
7
|
|
|
use Backend\Classes\ReportWidgetBase; |
8
|
|
|
use VojtaSvoboda\UserAccessLog\Models\AccessLog; |
9
|
|
|
use RainLab\User\Models\User; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* AccessLogChartLineAggregated overview widget. |
13
|
|
|
* |
14
|
|
|
* @package namespace VojtaSvoboda\UserAccessLog\ReportWidgets |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
class AccessLogChartLineAggregated extends ReportWidgetBase |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Renders the widget. |
20
|
|
|
*/ |
21
|
|
|
public function render() |
22
|
|
|
{ |
23
|
|
|
try { |
24
|
|
|
$this->vars['all'] = $this->loadData(); |
25
|
|
|
} |
26
|
|
|
catch (Exception $ex) { |
27
|
|
|
$this->vars['error'] = $ex->getMessage(); |
28
|
|
|
$this->vars['all'] = ''; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $this->makePartial('widget'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Define widget properties |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function defineProperties() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
'title' => [ |
43
|
|
|
'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.chartlineaggregated.title', |
44
|
|
|
'default' => 'Access statistics in time', |
45
|
|
|
'type' => 'string', |
46
|
|
|
'validationPattern' => '^.+$', |
47
|
|
|
'validationMessage' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.chartlineaggregated.title_validation', |
48
|
|
|
], |
49
|
|
|
'days' => [ |
50
|
|
|
'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.chartlineaggregated.days_title', |
51
|
|
|
'default' => '30', |
52
|
|
|
'type' => 'string', |
53
|
|
|
'validationPattern' => '^[0-9]+$', |
54
|
|
|
] |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function loadData() |
59
|
|
|
{ |
60
|
|
|
$days = $this->property('days'); |
61
|
|
|
if (!$days) { |
62
|
|
|
throw new ApplicationException('Invalid days value: ' . $days); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// all accesses for last month |
66
|
|
|
$items = AccessLog::where('created_at', '>=', Carbon::now()->subDays($days)->format('Y-m-d'))->get(); |
67
|
|
|
|
68
|
|
|
// parse data |
69
|
|
|
$all = $this->sortItemsToDays($items); |
70
|
|
|
|
71
|
|
|
// we need at least two days, to display chart |
72
|
|
|
if (sizeof($all) == 1) { |
73
|
|
|
$day = reset($all); |
74
|
|
|
$date = Carbon::createFromFormat('Y-m-d', $day['date'])->subDays(1); |
75
|
|
|
$dateFormated = $date->format('Y-m-d'); |
76
|
|
|
$all[$dateFormated] = [ |
77
|
|
|
'timestamp' => $date->timestamp * 1000, |
78
|
|
|
'date' => $dateFormated, |
79
|
|
|
'count' => 0, |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// count accessess for each day |
84
|
|
|
$all_render = []; |
85
|
|
|
foreach ($all as $a) { |
86
|
|
|
$all_render[] = [$a['timestamp'], $a['count']]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $all_render; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sort items by day. |
94
|
|
|
* |
95
|
|
|
* @param $items |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
private function sortItemsToDays($items) |
100
|
|
|
{ |
101
|
|
|
$all = []; |
102
|
|
|
|
103
|
|
|
foreach ($items as $item) |
104
|
|
|
{ |
105
|
|
|
// date |
106
|
|
|
$timestamp = strtotime($item->created_at) * 1000; |
107
|
|
|
$day = Carbon::createFromFormat('Y-m-d H:i:s', $item->created_at)->format('Y-m-d'); |
108
|
|
|
|
109
|
|
|
// init empty day |
110
|
|
|
if (!isset($all[$day])) { |
111
|
|
|
$all[$day] = [ |
112
|
|
|
'timestamp' => $timestamp, |
113
|
|
|
'date' => $day, |
114
|
|
|
'count' => 0, |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// increase count |
119
|
|
|
$all[$day]['count']++; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $all; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.