|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\View\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Utility\Inflector; |
|
6
|
|
|
use const ENT_HTML5; |
|
7
|
|
|
use const ENT_QUOTES; |
|
8
|
|
|
use function count; |
|
9
|
|
|
use function htmlspecialchars; |
|
10
|
|
|
use function implode; |
|
11
|
|
|
|
|
12
|
|
|
class ReportsHelper extends AppHelper |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
public $helpers = ['Incidents']; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param mixed $entries Entries |
|
19
|
|
|
* @param int $totalCount Total count |
|
20
|
|
|
* @param string $key Key |
|
21
|
|
|
* @return string HTML |
|
22
|
|
|
*/ |
|
23
|
8 |
|
public function entriesFromIncidents($entries, int $totalCount, string $key): string |
|
24
|
|
|
{ |
|
25
|
|
|
//$entries = Sanitize::clean($entries); |
|
26
|
8 |
|
$values = []; |
|
27
|
8 |
|
foreach ($entries as $entry) { |
|
28
|
8 |
|
$values[] = $entry . '[' . $key . '] <span class="count">(' |
|
29
|
8 |
|
. $entry['count'] . ')</span>'; |
|
30
|
|
|
} |
|
31
|
8 |
|
$fullString = implode(', ', $values); |
|
32
|
8 |
|
$remaining = $totalCount - count($values); |
|
33
|
8 |
|
if ($remaining) { |
|
34
|
|
|
$fullString .= ' <small>and ' . $remaining . ' others</small>'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
8 |
|
return $fullString; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param mixed $reports The reports |
|
42
|
|
|
* @return string comma separated list |
|
43
|
|
|
*/ |
|
44
|
4 |
|
public function createReportsLinks($reports): string |
|
45
|
|
|
{ |
|
46
|
4 |
|
$links = []; |
|
47
|
4 |
|
foreach ($reports as $report) { |
|
48
|
4 |
|
$links[] = $this->linkToReport($report); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
4 |
|
return implode(', ', $links); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param mixed $report The report |
|
56
|
|
|
* @return string HTML <a> link |
|
57
|
|
|
*/ |
|
58
|
12 |
|
public function linkToReport($report): string |
|
59
|
|
|
{ |
|
60
|
12 |
|
$reportId = $report['id']; |
|
61
|
|
|
|
|
62
|
12 |
|
return '<a href="/' . BASE_DIR . 'reports/view/' . $reportId . '">#' . $reportId . '</a>'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param mixed $incident The incident |
|
67
|
|
|
* @return string HTML <a> link |
|
68
|
|
|
*/ |
|
69
|
4 |
|
public function linkToReportFromIncident($incident): string |
|
70
|
|
|
{ |
|
71
|
4 |
|
$reportId = $incident['report_id']; |
|
72
|
|
|
|
|
73
|
4 |
|
return '<a href="/' . BASE_DIR . 'reports/view/' . $reportId . '">#' . $reportId . '</a>'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param mixed $incidents The incidents |
|
78
|
|
|
* @return string HTML |
|
79
|
|
|
*/ |
|
80
|
4 |
|
public function getStacktracesForIncidents($incidents): string |
|
81
|
|
|
{ |
|
82
|
4 |
|
$count = 0; |
|
83
|
4 |
|
$html = '<div class="row">'; |
|
84
|
4 |
|
foreach ($incidents as $incident) { |
|
85
|
4 |
|
$class = 'well span5'; |
|
86
|
|
|
|
|
87
|
4 |
|
if ($count % 2 === 1) { |
|
88
|
|
|
$class .= ' '; |
|
89
|
|
|
} else { |
|
90
|
4 |
|
$html .= '</div><div class="row">'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
$html .= $this->Incidents->getStacktrace($incident, $class); |
|
|
|
|
|
|
94
|
4 |
|
++$count; |
|
95
|
|
|
} |
|
96
|
4 |
|
$html .= '</div>'; |
|
97
|
|
|
|
|
98
|
4 |
|
return $html; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param mixed $columns |
|
103
|
|
|
* @param mixed $relatedEntries |
|
104
|
|
|
* @return string HTML code |
|
105
|
|
|
*/ |
|
106
|
4 |
|
public function getChartArray(string $arrayName, $columns, $relatedEntries): string |
|
107
|
|
|
{ |
|
108
|
4 |
|
$html = 'var ' . $arrayName . ' = [], chart = {};'; |
|
109
|
4 |
|
foreach ($columns as $column) { |
|
110
|
4 |
|
$column = htmlspecialchars($column, ENT_QUOTES | ENT_HTML5); |
|
111
|
4 |
|
$html .= 'chart = {};'; |
|
112
|
4 |
|
$html .= 'chart.name = "' . $column . '";'; |
|
113
|
4 |
|
$html .= 'chart.title = "' . Inflector::humanize($column) . '";'; |
|
114
|
4 |
|
$html .= 'chart.labels = []; chart.values = [];'; |
|
115
|
4 |
|
foreach ($relatedEntries[$column] as $entry) { |
|
116
|
4 |
|
$count = $entry['count']; |
|
117
|
4 |
|
$html .= 'chart.labels.push("' . $entry[$column] . ' (' . $count . ')");'; |
|
118
|
4 |
|
$html .= 'chart.values.push(' . $count . ');'; |
|
119
|
|
|
} |
|
120
|
4 |
|
$html .= $arrayName . '.push(chart);'; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
4 |
|
return $html; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param mixed $entries |
|
128
|
|
|
* @return string HTML |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getLineChartData(string $arrayName, $entries): string |
|
131
|
|
|
{ |
|
132
|
|
|
$html = 'var $arrayName = [];'; |
|
133
|
|
|
foreach ($entries as $entry) { |
|
134
|
|
|
$html .= $arrayName . '.push(["' . $entry['date'] . '", ' |
|
135
|
|
|
. $entry['count'] . ']);'; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $html; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|