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 RainLab\User\Models\User; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Registrations overview widget. |
12
|
|
|
* |
13
|
|
|
* @package namespace VojtaSvoboda\UserAccessLog\ReportWidgets |
14
|
|
|
*/ |
15
|
|
View Code Duplication |
class Registrations extends ReportWidgetBase |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Renders the widget. |
19
|
|
|
*/ |
20
|
|
|
public function render() |
21
|
|
|
{ |
22
|
|
|
try { |
23
|
|
|
$this->vars['registrations'] = $this->loadData(); |
24
|
|
|
|
25
|
|
|
} catch (Exception $ex) { |
26
|
|
|
$this->vars['error'] = $ex->getMessage(); |
27
|
|
|
$this->vars['registrations'] = []; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $this->makePartial('widget'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function defineProperties() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
'title' => [ |
37
|
|
|
'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.registrations.title', |
38
|
|
|
'default' => 'New registrations', |
39
|
|
|
'type' => 'string', |
40
|
|
|
'validationPattern' => '^.+$', |
41
|
|
|
'validationMessage' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.registrations.title_validation', |
42
|
|
|
], |
43
|
|
|
'days' => [ |
44
|
|
|
'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.registrations.days_title', |
45
|
|
|
'default' => '30', |
46
|
|
|
'type' => 'string', |
47
|
|
|
'validationPattern' => '^[0-9]+$', |
48
|
|
|
] |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function loadData() |
53
|
|
|
{ |
54
|
|
|
$days = $this->property('days'); |
55
|
|
|
if (!$days) { |
56
|
|
|
throw new ApplicationException('Invalid days value: ' . $days); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// all accesses for last month |
60
|
|
|
$items = User::where('created_at', '>=', Carbon::now()->subDays($days)->format('Y-m-d'))->get(); |
61
|
|
|
|
62
|
|
|
// parse data |
63
|
|
|
$all = $this->sortItemsToDays($items); |
64
|
|
|
|
65
|
|
|
// we need at least two days, to display chart |
66
|
|
|
if (sizeof($all) == 1) { |
67
|
|
|
$day = reset($all); |
68
|
|
|
$date = Carbon::createFromFormat('Y-m-d', $day['date'])->subDays(1); |
69
|
|
|
$dateFormated = $date->format('Y-m-d'); |
70
|
|
|
$all[$dateFormated] = [ |
71
|
|
|
'timestamp' => $date->timestamp * 1000, |
72
|
|
|
'date' => $dateFormated, |
73
|
|
|
'count' => 0, |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// count all |
78
|
|
|
$all_render = []; |
79
|
|
|
foreach ($all as $a) { |
80
|
|
|
$all_render[] = [$a['timestamp'], $a['count']]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $all_render; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sort items by days. |
88
|
|
|
* |
89
|
|
|
* @param $items |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
private function sortItemsToDays($items) |
94
|
|
|
{ |
95
|
|
|
$all = []; |
96
|
|
|
|
97
|
|
|
foreach ($items as $item) |
98
|
|
|
{ |
99
|
|
|
// date |
100
|
|
|
$timestamp = strtotime($item->created_at) * 1000; |
101
|
|
|
$day = Carbon::createFromFormat('Y-m-d H:i:s', $item->created_at)->format('Y-m-d'); |
102
|
|
|
|
103
|
|
|
// init empty day |
104
|
|
|
if (!isset($all[$day])) { |
105
|
|
|
$all[$day] = [ |
106
|
|
|
'timestamp' => $timestamp, |
107
|
|
|
'date' => $day, |
108
|
|
|
'count' => 0, |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// increase count |
113
|
|
|
$all[$day]['count']++; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $all; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.