Skip to content

Commit a8460ca

Browse files
committed
Use ipl orm
1 parent 449f1f5 commit a8460ca

20 files changed

+495
-424
lines changed

application/controllers/ReportController.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
use Icinga\Application\Hook;
88
use Icinga\Module\Pdfexport\ProvidedHook\Pdfexport;
99
use Icinga\Module\Reporting\Database;
10+
use Icinga\Module\Reporting\Model;
1011
use Icinga\Module\Reporting\Report;
1112
use Icinga\Module\Reporting\Web\Controller;
1213
use Icinga\Module\Reporting\Web\Forms\ReportForm;
1314
use Icinga\Module\Reporting\Web\Forms\ScheduleForm;
1415
use Icinga\Module\Reporting\Web\Forms\SendForm;
1516
use Icinga\Module\Reporting\Web\Widget\CompatDropdown;
1617
use ipl\Html\Error;
18+
use ipl\Stdlib\Filter;
1719
use ipl\Web\Url;
1820
use ipl\Web\Widget\ActionBar;
1921
use Icinga\Util\Environment;
@@ -28,7 +30,19 @@ class ReportController extends Controller
2830

2931
public function init()
3032
{
31-
$this->report = Report::fromDb($this->params->getRequired('id'));
33+
$reportId = $this->params->getRequired('id');
34+
35+
/** @var $report Model\Report */
36+
$report = Model\Report::on($this->getDb())
37+
->with(['timeframe'])
38+
->filter(Filter::equal('id', $reportId))
39+
->first();
40+
41+
if ($report === null) {
42+
$this->httpNotFound(sprintf('Report with id %d not found', $reportId));
43+
}
44+
45+
$this->report = Report::fromModel($report);
3246
}
3347

3448
public function indexAction()

application/controllers/ReportsController.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
namespace Icinga\Module\Reporting\Controllers;
66

77
use Icinga\Module\Reporting\Database;
8+
use Icinga\Module\Reporting\Model\Report;
89
use Icinga\Module\Reporting\Web\Controller;
910
use Icinga\Module\Reporting\Web\Forms\ReportForm;
1011
use Icinga\Module\Reporting\Web\ReportsTimeframesAndTemplatesTabs;
1112
use ipl\Html\Html;
12-
use ipl\Sql\Select;
1313
use ipl\Web\Url;
1414
use ipl\Web\Widget\ButtonLink;
1515
use ipl\Web\Widget\Icon;
@@ -38,21 +38,18 @@ public function indexAction()
3838

3939
$tableRows = [];
4040

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

47-
foreach ($this->getDb()->select($select) as $report) {
44+
foreach ($reports as $report) {
4845
$url = Url::fromPath('reporting/report', ['id' => $report->id])->getAbsoluteUrl('&');
4946

5047
$tableRows[] = Html::tag('tr', ['href' => $url], [
5148
Html::tag('td', null, $report->name),
5249
Html::tag('td', null, $report->author),
53-
Html::tag('td', null, $report->timeframe),
54-
Html::tag('td', null, date('Y-m-d H:i', $report->ctime / 1000)),
55-
Html::tag('td', null, date('Y-m-d H:i', $report->mtime / 1000)),
50+
Html::tag('td', null, $report->timeframe->name),
51+
Html::tag('td', null, $report->ctime->format('Y-m-d H:i')),
52+
Html::tag('td', null, $report->mtime->format('Y-m-d H:i')),
5653
Html::tag('td', ['class' => 'icon-col'], [
5754
new Link(
5855
new Icon('edit'),

application/controllers/TemplateController.php

+11-8
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;
1213
use Icinga\Module\Reporting\Web\Widget\Template;
13-
use ipl\Sql\Select;
14+
use ipl\Stdlib\Filter;
1415

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

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

2529
if ($template === null) {
2630
throw new \Exception('Template not found');
2731
}
2832

33+
$template = Template::fromModel($template);
34+
2935
$template
3036
->setMacros([
3137
'date' => (new DateTime())->format('jS M, Y'),
@@ -44,12 +50,9 @@ public function editAction()
4450

4551
$this->createTabs()->activate('edit');
4652

47-
$select = (new Select())
48-
->from('template')
49-
->columns(['id', 'settings'])
50-
->where(['id = ?' => $this->params->getRequired('id')]);
51-
52-
$template = $this->getDb()->select($select)->fetch();
53+
$template = Model\Template::on($this->getDb())
54+
->filter(Filter::equal('id', $this->params->getRequired('id')))
55+
->first();
5356

5457
if ($template === false) {
5558
throw new \Exception('Template not found');

application/controllers/TemplatesController.php

+5-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;
910
use Icinga\Module\Reporting\Web\Controller;
1011
use Icinga\Module\Reporting\Web\Forms\TemplateForm;
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\Link;
@@ -34,12 +34,9 @@ public function indexAction()
3434
));
3535
}
3636

37-
$select = (new Select())
38-
->from('template')
39-
->columns(['id', 'name', 'author', 'ctime', 'mtime'])
40-
->orderBy('mtime', SORT_DESC);
37+
$templates = Model\Template::on($this->getDb());
4138

42-
foreach ($this->getDb()->select($select) as $template) {
39+
foreach ($templates as $template) {
4340
if ($canManage) {
4441
// Edit URL
4542
$subjectUrl = Url::fromPath(
@@ -57,8 +54,8 @@ public function indexAction()
5754
$tableRows[] = Html::tag('tr', null, [
5855
Html::tag('td', null, new Link($template->name, $subjectUrl)),
5956
Html::tag('td', null, $template->author),
60-
Html::tag('td', null, date('Y-m-d H:i', $template->ctime / 1000)),
61-
Html::tag('td', null, date('Y-m-d H:i', $template->mtime / 1000))
57+
Html::tag('td', null, $template->ctime->format('Y-m-d H:i')),
58+
Html::tag('td', null, $template->mtime->format('Y-m-d H:i'))
6259
]);
6360
}
6461

application/controllers/TimeframeController.php

+12-1
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,16 @@ class TimeframeController extends Controller
1921

2022
public function init()
2123
{
22-
$this->timeframe = Timeframe::fromDb($this->params->getRequired('id'));
24+
/** @var $timeframe Model\Timeframe */
25+
$timeframe = Model\Timeframe::on($this->getDb())
26+
->filter(Filter::equal('id', $this->params->getRequired('id')))
27+
->first();
28+
29+
if ($timeframe === null) {
30+
throw new \Exception('Timeframe not found');
31+
}
32+
33+
$this->timeframe = Timeframe::fromModel($timeframe);
2334
}
2435

2536
public function editAction()

application/controllers/TimeframesController.php

+5-7
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;
910
use Icinga\Module\Reporting\Web\Controller;
1011
use Icinga\Module\Reporting\Web\Forms\TimeframeForm;
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\Link;
@@ -36,11 +36,9 @@ public function indexAction()
3636

3737
$tableRows = [];
3838

39-
$select = (new Select())
40-
->from('timeframe t')
41-
->columns('*');
39+
$timeframes = Model\Timeframe::on($this->getDb());
4240

43-
foreach ($this->getDb()->select($select) as $timeframe) {
41+
foreach ($timeframes as $timeframe) {
4442
$subject = $timeframe->name;
4543

4644
if ($canManage) {
@@ -54,8 +52,8 @@ public function indexAction()
5452
Html::tag('td', null, $subject),
5553
Html::tag('td', null, $timeframe->start),
5654
Html::tag('td', null, $timeframe->end),
57-
Html::tag('td', null, date('Y-m-d H:i', $timeframe->ctime / 1000)),
58-
Html::tag('td', null, date('Y-m-d H:i', $timeframe->mtime / 1000))
55+
Html::tag('td', null, $timeframe->ctime->format('Y-m-d H:i')),
56+
Html::tag('td', null, $timeframe->mtime->format('Y-m-d H:i'))
5957
]);
6058
}
6159

library/Reporting/Model/Config.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
// Icinga Reporting | (c) 2023 Icinga GmbH | GPLv2
4+
5+
namespace Icinga\Module\Reporting\Model;
6+
7+
use ipl\Orm\Behavior\MillisecondTimestamp;
8+
use ipl\Orm\Behaviors;
9+
use ipl\Orm\Model;
10+
use ipl\Orm\Relations;
11+
12+
class Config extends Model
13+
{
14+
public function getTableName()
15+
{
16+
return 'config';
17+
}
18+
19+
public function getKeyName()
20+
{
21+
return 'id';
22+
}
23+
24+
public function getColumns()
25+
{
26+
return [
27+
'reportlet_id',
28+
'name',
29+
'value',
30+
'ctime',
31+
'mtime'
32+
];
33+
}
34+
35+
public function createBehaviors(Behaviors $behaviors)
36+
{
37+
$behaviors->add(new MillisecondTimestamp([
38+
'ctime',
39+
'mtime'
40+
]));
41+
}
42+
43+
public function createRelations(Relations $relations)
44+
{
45+
$relations->belongsTo('reportlet', Reportlet::class);
46+
}
47+
}

library/Reporting/Model/Report.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
// Icinga Reporting | (c) 2023 Icinga GmbH | GPLv2
4+
5+
namespace Icinga\Module\Reporting\Model;
6+
7+
use ipl\Orm\Behavior\MillisecondTimestamp;
8+
use ipl\Orm\Behaviors;
9+
use ipl\Orm\Model;
10+
use ipl\Orm\Relations;
11+
12+
class Report extends Model
13+
{
14+
public function getTableName()
15+
{
16+
return 'report';
17+
}
18+
19+
public function getKeyName()
20+
{
21+
return 'id';
22+
}
23+
24+
public function getColumns()
25+
{
26+
return [
27+
'timeframe_id',
28+
'template_id',
29+
'author',
30+
'name',
31+
'ctime',
32+
'mtime'
33+
];
34+
}
35+
36+
public function getDefaultSort()
37+
{
38+
return ['name ASC'];
39+
}
40+
41+
public function createBehaviors(Behaviors $behaviors)
42+
{
43+
$behaviors->add(new MillisecondTimestamp([
44+
'ctime',
45+
'mtime'
46+
]));
47+
}
48+
49+
public function createRelations(Relations $relations)
50+
{
51+
$relations->belongsTo('timeframe', Timeframe::class);
52+
$relations->belongsTo('template', Template::class)
53+
->setJoinType('LEFT');
54+
55+
$relations->hasOne('schedule', Schedule::class)
56+
->setJoinType('LEFT');
57+
$relations->hasMany('reportlets', Reportlet::class);
58+
}
59+
}

library/Reporting/Model/Reportlet.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
// Icinga Reporting | (c) 2023 Icinga GmbH | GPLv2
4+
5+
namespace Icinga\Module\Reporting\Model;
6+
7+
use ipl\Orm\Behavior\MillisecondTimestamp;
8+
use ipl\Orm\Behaviors;
9+
use ipl\Orm\Model;
10+
use ipl\Orm\Relations;
11+
12+
class Reportlet extends Model
13+
{
14+
public function getTableName()
15+
{
16+
return 'reportlet';
17+
}
18+
19+
public function getKeyName()
20+
{
21+
return 'id';
22+
}
23+
24+
public function getColumns()
25+
{
26+
return [
27+
'report_id',
28+
'class',
29+
'ctime',
30+
'mtime'
31+
];
32+
}
33+
34+
public function createBehaviors(Behaviors $behaviors)
35+
{
36+
$behaviors->add(new MillisecondTimestamp([
37+
'ctime',
38+
'mtime'
39+
]));
40+
}
41+
42+
public function createRelations(Relations $relations)
43+
{
44+
$relations->belongsTo('report', Report::class);
45+
46+
$relations->hasMany('config', Config::class);
47+
}
48+
}

0 commit comments

Comments
 (0)