Skip to content

Commit 6392744

Browse files
committed
WIP
1 parent bb59df7 commit 6392744

14 files changed

+359
-311
lines changed

application/controllers/ReportController.php

+48-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
use Icinga\Application\Hook;
99
use Icinga\Module\Pdfexport\ProvidedHook\Pdfexport;
1010
use Icinga\Module\Reporting\Database;
11+
use Icinga\Module\Reporting\Model;
1112
use Icinga\Module\Reporting\Report;
13+
use Icinga\Module\Reporting\Reportlet;
14+
use Icinga\Module\Reporting\Schedule;
1215
use Icinga\Module\Reporting\Web\Controller;
1316
use Icinga\Module\Reporting\Web\Forms\ReportForm;
1417
use Icinga\Module\Reporting\Web\Forms\ScheduleForm;
1518
use Icinga\Module\Reporting\Web\Forms\SendForm;
1619
use Icinga\Module\Reporting\Web\Widget\CompatDropdown;
1720
use ipl\Html\Error;
21+
use ipl\Stdlib\Filter;
1822
use ipl\Web\Url;
1923
use ipl\Web\Widget\ActionBar;
2024
use Icinga\Util\Environment;
@@ -28,7 +32,50 @@ class ReportController extends Controller
2832

2933
public function init()
3034
{
31-
$this->report = Report::fromDb($this->params->getRequired('id'));
35+
$rs = Model\Report::on($this->getDb())
36+
->with([
37+
'timeframe',
38+
'template',
39+
'reportlet',
40+
'reportlet.config',
41+
'schedule'
42+
])
43+
->filter(Filter::equal('id', $this->params->getRequired('id')));
44+
45+
$result = $rs->first();
46+
47+
$report = Report::fromModel($result);
48+
49+
$reportlet = new Reportlet();
50+
51+
$reportlet->class = $result->reportlet->class;
52+
$reportlet->id = $result->reportlet->id;
53+
54+
$row = $rs
55+
->filter(Filter::equal('reportlet.config.reportlet_id', $report->reportlet->id));
56+
57+
$config = [];
58+
foreach ($row as $r) {
59+
$config[$r->reportlet->config->name] = $r->reportlet->config->value;
60+
}
61+
62+
$reportlet->config = $config;
63+
64+
$report->setReportlets([$reportlet]);
65+
66+
if ($result->schedule->config !== null) {
67+
$schedule = new Schedule();
68+
69+
$schedule
70+
->setId($result->schedule->id)
71+
->setStart((new \DateTime())->setTimestamp((int) $result->schedule->start / 1000))
72+
->setFrequency($result->schedule->frequency)
73+
->setAction($result->schedule->action)
74+
->setConfig(json_decode($result->schedule->config, true));
75+
76+
$report->setSchedule($schedule);
77+
}
78+
$this->report = $report;
3279
}
3380

3481
public function indexAction()

application/controllers/ReportsController.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
use GuzzleHttp\Psr7\ServerRequest;
88
use Icinga\Module\Reporting\Database;
9+
use Icinga\Module\Reporting\Model\Report;
910
use Icinga\Module\Reporting\Web\Controller;
1011
use Icinga\Module\Reporting\Web\Forms\ReportForm;
1112
use Icinga\Module\Reporting\Web\ReportsTimeframesAndTemplatesTabs;
1213
use ipl\Html\Html;
13-
use ipl\Sql\Select;
1414
use ipl\Web\Url;
1515
use ipl\Web\Widget\ButtonLink;
1616
use ipl\Web\Widget\Icon;
@@ -35,19 +35,24 @@ public function indexAction()
3535

3636
$tableRows = [];
3737

38-
$select = (new Select())
39-
->from('report r')
40-
->columns(['r.*', 'timeframe' => 't.name'])
41-
->join('timeframe t', 'r.timeframe_id = t.id')
42-
->orderBy('r.mtime', SORT_DESC);
38+
$reports = Report::on($this->getDb())
39+
->withColumns(['report.timeframe.name']);
4340

44-
foreach ($this->getDb()->select($select) as $report) {
41+
$sortControl = $this->createSortControl(
42+
$reports,
43+
[
44+
'name' => t('Name'),
45+
'author' => t('Author')
46+
]
47+
);
48+
49+
foreach ($reports as $report) {
4550
$url = Url::fromPath('reporting/report', ['id' => $report->id])->getAbsoluteUrl('&');
4651

4752
$tableRows[] = Html::tag('tr', ['href' => $url], [
4853
Html::tag('td', null, $report->name),
4954
Html::tag('td', null, $report->author),
50-
Html::tag('td', null, $report->timeframe),
55+
Html::tag('td', null, $report->timeframe->name),
5156
Html::tag('td', null, date('Y-m-d H:i', $report->ctime / 1000)),
5257
Html::tag('td', null, date('Y-m-d H:i', $report->mtime / 1000)),
5358
Html::tag('td', ['class' => 'icon-col'], [
@@ -88,6 +93,7 @@ public function indexAction()
8893
} else {
8994
$this->addContent(Html::tag('p', null, 'No reports created yet.'));
9095
}
96+
$this->addControl($sortControl);
9197
}
9298

9399
public function newAction()

application/controllers/TemplateController.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
use DateTime;
88
use GuzzleHttp\Psr7\ServerRequest;
99
use Icinga\Module\Reporting\Database;
10+
use Icinga\Module\Reporting\Model;
1011
use Icinga\Module\Reporting\Web\Controller;
1112
use Icinga\Module\Reporting\Web\Forms\TemplateForm;
12-
use Icinga\Module\Reporting\Web\Widget\Template;
1313
use ipl\Sql\Select;
14+
use ipl\Stdlib\Filter;
1415

1516
class TemplateController extends Controller
1617
{
@@ -20,7 +21,9 @@ public function indexAction()
2021
{
2122
$this->createTabs()->activate('preview');
2223

23-
$template = Template::fromDb($this->params->getRequired('id'));
24+
$template = Model\Template::on($this->getDb())
25+
->filter(Filter::equal('id', $this->params->getRequired('id')))
26+
->first();
2427

2528
if ($template === null) {
2629
throw new \Exception('Template not found');

application/controllers/TimeframeController.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
use GuzzleHttp\Psr7\ServerRequest;
88
use Icinga\Module\Reporting\Database;
9+
use Icinga\Module\Reporting\Model;
910
use Icinga\Module\Reporting\Timeframe;
1011
use Icinga\Module\Reporting\Web\Controller;
1112
use Icinga\Module\Reporting\Web\Forms\TimeframeForm;
13+
use ipl\Stdlib\Filter;
1214

1315
class TimeframeController extends Controller
1416
{
@@ -19,7 +21,9 @@ class TimeframeController extends Controller
1921

2022
public function init()
2123
{
22-
$this->timeframe = Timeframe::fromDb($this->params->getRequired('id'));
24+
$this->timeframe = Model\Timeframe::on($this->getDb())
25+
->filter(Filter::equal('id', $this->params->getRequired('id')))
26+
->first();
2327
}
2428

2529
public function editAction()
@@ -28,14 +32,14 @@ public function editAction()
2832
$this->addTitleTab($this->translate('Edit Time Frame'));
2933

3034
$values = [
31-
'name' => $this->timeframe->getName(),
32-
'start' => $this->timeframe->getStart(),
33-
'end' => $this->timeframe->getEnd()
35+
'name' => $this->timeframe->name,
36+
'start' => $this->timeframe->start,
37+
'end' => $this->timeframe->end
3438
];
3539

3640

3741
$form = (new TimeframeForm())
38-
->setId($this->timeframe->getId());
42+
->setId($this->timeframe->id);
3943

4044
$form->populate($values);
4145

library/Reporting/Model/Config.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Icinga\Module\Reporting\Model;
4+
5+
use ipl\Orm\Model;
6+
use ipl\Orm\Relations;
7+
8+
class Config extends Model
9+
{
10+
public function getTableName()
11+
{
12+
return 'config';
13+
}
14+
15+
public function getKeyName()
16+
{
17+
return 'id';
18+
}
19+
20+
public function getColumns()
21+
{
22+
return [
23+
'reportlet_id',
24+
'name',
25+
'value',
26+
'ctime',
27+
'mtime'
28+
];
29+
}
30+
31+
public function createRelations(Relations $relations)
32+
{
33+
$relations->belongsTo('reportlet', Reportlet::class);
34+
}
35+
}

library/Reporting/Model/Report.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Icinga\Module\Reporting\Model;
4+
5+
use ipl\Orm\Model;
6+
use ipl\Orm\Relations;
7+
8+
class Report extends Model
9+
{
10+
public function getTableName()
11+
{
12+
return 'report';
13+
}
14+
15+
public function getKeyName()
16+
{
17+
return 'id';
18+
}
19+
20+
public function getColumns()
21+
{
22+
return [
23+
'timeframe_id',
24+
'template_id',
25+
'author',
26+
'name',
27+
'ctime',
28+
'mtime'
29+
];
30+
}
31+
public function createRelations(Relations $relations)
32+
{
33+
$relations->belongsTo('timeframe', Timeframe::class);
34+
$relations->belongsTo('template', Template::class)
35+
->setJoinType('LEFT');
36+
37+
$relations->hasOne('schedule', Schedule::class)
38+
->setJoinType('LEFT');
39+
$relations->hasMany('reportlet', Reportlet::class)
40+
->setJoinType('LEFT');
41+
}
42+
}

library/Reporting/Model/Reportlet.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Icinga\Module\Reporting\Model;
4+
5+
use ipl\Orm\Model;
6+
use ipl\Orm\Relations;
7+
8+
class Reportlet extends Model
9+
{
10+
public function getTableName()
11+
{
12+
return 'reportlet';
13+
}
14+
15+
public function getKeyName()
16+
{
17+
return 'id';
18+
}
19+
20+
public function getColumns()
21+
{
22+
return [
23+
'report_id',
24+
'class',
25+
'ctime',
26+
'mtime'
27+
];
28+
}
29+
30+
public function createRelations(Relations $relations)
31+
{
32+
$relations->belongsTo('report', Report::class);
33+
34+
$relations->hasMany('config', Config::class)
35+
->setJoinType('LEFT');
36+
}
37+
}

library/Reporting/Model/Schedule.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Icinga\Module\Reporting\Model;
4+
5+
use ipl\Orm\Model;
6+
use ipl\Orm\Relations;
7+
8+
class Schedule extends Model
9+
{
10+
public function getTableName()
11+
{
12+
return 'schedule';
13+
}
14+
15+
public function getKeyName()
16+
{
17+
return 'id';
18+
}
19+
20+
public function getColumns()
21+
{
22+
return [
23+
'report_id',
24+
'author',
25+
'start',
26+
'frequency',
27+
'action',
28+
'config',
29+
'ctime',
30+
'mtime'
31+
];
32+
}
33+
34+
public function createRelations(Relations $relations)
35+
{
36+
$relations->belongsTo('report', Report::class)
37+
->setJoinType('LEFT');
38+
}
39+
}

library/Reporting/Model/Template.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Icinga\Module\Reporting\Model;
4+
5+
use ipl\Orm\Model;
6+
7+
class Template extends Model
8+
{
9+
public function getTableName()
10+
{
11+
return 'template';
12+
}
13+
14+
public function getKeyName()
15+
{
16+
return 'id';
17+
}
18+
19+
public function getColumns()
20+
{
21+
return [
22+
'author',
23+
'name',
24+
'settings',
25+
'ctime',
26+
'mtime'
27+
];
28+
}
29+
}

0 commit comments

Comments
 (0)