Completed
Pull Request — master (#131)
by Deven
08:29
created

ReportsHelper::entriesFromIncidents()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 3
crap 3.009
1
<?php
2
/* vim: set expandtab sw=4 ts=4 sts=4: */
3
namespace app\View\Helper;
4
5
use App\Utility\Sanitize;
6
use App\View\Helper\AppHelper;
7
use App\View\Helper\IncidentsHelper;
8
use Cake\Utility\Inflector;
9
use Cake\View\View;
10
11
class ReportsHelper extends AppHelper {
12
    public $helpers = array('Incidents');
13
14 3
    public function __construct(View $view, $settings = array())
15
    {
16 3
        parent::__construct($view, $settings);
17 3
    }
18
19 1
    public function entriesFromIncidents($entries, $totalCount, $key)
20
    {
21
        //$entries = Sanitize::clean($entries);
22 1
        $values = array();
23 1
        foreach($entries as $entry) {
24 1
            $values[] = "$entry[$key] <span class='count'>("
25 1
                . $entry['count'] . ")</span>";
26
        }
27 1
        $fullString = implode(", ", $values);
28 1
        $remaining = $totalCount - count($values);
29 1
        if ($remaining) {
30
            $fullString .= " <small>and $remaining others</small>";
31
        }
32 1
        return $fullString;
33
    }
34
35
    public function createReportsLinks($reports)
36
    {
37
        $links = array();
38
        foreach ($reports as $report) {
39
            $links[] = $this->linkToReport($report);
40
        }
41
        $string = implode(", ", $links);
42
        return $string;
43
    }
44
45
    public function linkToReport($report)
46
    {
47
        $reportId = $report["id"];
48
        $link = "<a href=/" . BASE_DIR . "reports/view/$reportId>#$reportId</a>";
49
        return $link;
50
    }
51
52 1
    public function linkToReportFromIncident($incident)
53
    {
54 1
        $reportId = $incident["report_id"];
55 1
        $link = "<a href=/" . BASE_DIR . "reports/view/$reportId>#$reportId</a>";
56 1
        return $link;
57
    }
58
59 1
    public function getStacktracesForIncidents($incidents)
60
    {
61 1
        $count = 0;
62 1
        $html = '<div class="row">';
63 1
        foreach ($incidents as $incident) {
64 1
            $class = "well span5";
65
66 1
            if ($count % 2 == 1) {
67
                $class .= " ";
68
            } else {
69 1
                $html .= "</div><div class='row'>";
70
            }
71
72 1
            $html .= $this->Incidents->getStacktrace($incident, $class);
73 1
            $count++;
74
        }
75 1
        $html .= '</div>';
76 1
        return $html;
77
    }
78
79 1
    public function getChartArray($arrayName, $columns, $relatedEntries)
80
    {
81 1
        $html = "var $arrayName = [], chart = {};";
82 1
        foreach ($columns as $column) {
83 1
            $column = htmlspecialchars($column, ENT_QUOTES | ENT_HTML5);
84 1
            $html .= "chart = {};";
85 1
            $html .= "chart.name = '$column';";
86 1
            $html .= "chart.title = '" . Inflector::humanize($column) . "';";
87 1
            $html .= "chart.labels = []; chart.values = [];";
88 1
            foreach ($relatedEntries[$column] as $entry) {
89 1
                $count = $entry['count'];
90 1
                $html .= "chart.labels.push('$entry[$column] ($count)');";
91 1
                $html .= "chart.values.push($count);";
92
            }
93 1
            $html .= "$arrayName.push(chart);";
94
        }
95 1
        return $html;
96
    }
97
98
    public function getLineChartData($arrayName, $entries)
99
    {
100
        $html = "var $arrayName = [];";
101
        foreach ($entries as $entry) {
102
            $html .= "$arrayName.push(['" . $entry["date"] . "', "
103
                    . $entry["count"] . "]);";
104
        }
105
        return $html;
106
    }
107
}
108