Skip to content

Commit 56871a4

Browse files
committed
Use ipl orm
1 parent 9b4a3d1 commit 56871a4

20 files changed

+482
-416
lines changed

application/controllers/ReportController.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
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;
1213
use Icinga\Module\Reporting\Web\Controller;
1314
use Icinga\Module\Reporting\Web\Forms\ReportForm;
1415
use Icinga\Module\Reporting\Web\Forms\ScheduleForm;
1516
use Icinga\Module\Reporting\Web\Forms\SendForm;
1617
use Icinga\Module\Reporting\Web\Widget\CompatDropdown;
1718
use ipl\Html\Error;
19+
use ipl\Stdlib\Filter;
1820
use ipl\Web\Url;
1921
use ipl\Web\Widget\ActionBar;
2022
use Icinga\Util\Environment;
@@ -28,7 +30,22 @@ 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([
38+
'timeframe',
39+
'template'
40+
])
41+
->filter(Filter::equal('id', $reportId))
42+
->first();
43+
44+
if ($report === null) {
45+
$this->httpNotFound(sprintf('Report with id %d not found', $reportId));
46+
}
47+
48+
$this->report = Report::fromModel($report);
3249
}
3350

3451
public function indexAction()

application/controllers/ReportsController.php

+6-9
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,21 +35,18 @@ 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+
foreach ($reports as $report) {
4542
$url = Url::fromPath('reporting/report', ['id' => $report->id])->getAbsoluteUrl('&');
4643

4744
$tableRows[] = Html::tag('tr', ['href' => $url], [
4845
Html::tag('td', null, $report->name),
4946
Html::tag('td', null, $report->author),
5047
Html::tag('td', null, $report->timeframe),
51-
Html::tag('td', null, date('Y-m-d H:i', $report->ctime / 1000)),
52-
Html::tag('td', null, date('Y-m-d H:i', $report->mtime / 1000)),
48+
Html::tag('td', null, $report->ctime->format('Y-m-d H:i')),
49+
Html::tag('td', null, $report->mtime->format('Y-m-d H:i')),
5350
Html::tag('td', ['class' => 'icon-col'], [
5451
new Link(
5552
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-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\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,10 @@ public function indexAction()
3434
));
3535
}
3636

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

42-
foreach ($this->getDb()->select($select) as $template) {
40+
foreach ($templates as $template) {
4341
if ($canManage) {
4442
// Edit URL
4543
$subjectUrl = Url::fromPath(
@@ -57,8 +55,8 @@ public function indexAction()
5755
$tableRows[] = Html::tag('tr', null, [
5856
Html::tag('td', null, new Link($template->name, $subjectUrl)),
5957
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))
58+
Html::tag('td', null, $template->ctime->format('Y-m-d H:i')),
59+
Html::tag('td', null, $template->mtime->format('Y-m-d H:i'))
6260
]);
6361
}
6462

application/controllers/TimeframeController.php

+8-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,12 @@ 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+
$this->timeframe = Timeframe::fromModel($timeframe);
2330
}
2431

2532
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

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 createBehaviors(Behaviors $behaviors)
37+
{
38+
$behaviors->add(new MillisecondTimestamp([
39+
'ctime',
40+
'mtime'
41+
]));
42+
}
43+
44+
public function createRelations(Relations $relations)
45+
{
46+
$relations->belongsTo('timeframe', Timeframe::class);
47+
$relations->belongsTo('template', Template::class)
48+
->setJoinType('LEFT');
49+
50+
$relations->hasOne('schedule', Schedule::class)
51+
->setJoinType('LEFT');
52+
$relations->hasMany('reportlets', Reportlet::class);
53+
}
54+
}

library/Reporting/Model/Reportlet.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
->setJoinType('LEFT');
48+
}
49+
}

0 commit comments

Comments
 (0)