Skip to content

Commit 5d04b50

Browse files
committed
Use ipl orm
1 parent 9630f63 commit 5d04b50

19 files changed

+493
-422
lines changed

application/clicommands/DownloadCommand.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
namespace Icinga\Module\Reporting\Clicommands;
66

7-
use InvalidArgumentException;
7+
use Icinga\Exception\NotFoundError;
88
use Icinga\Module\Pdfexport\ProvidedHook\Pdfexport;
99
use Icinga\Module\Reporting\Cli\Command;
10+
use Icinga\Module\Reporting\Model;
1011
use Icinga\Module\Reporting\Report;
12+
use InvalidArgumentException;
13+
use ipl\Stdlib\Filter;
1114

1215
class DownloadCommand extends Command
1316
{
@@ -44,7 +47,17 @@ public function defaultAction()
4447
$this->fail($this->translate('Argument id is mandatory'));
4548
}
4649

47-
$report = Report::fromDb($id);
50+
$report = Model\Report::on($this->getDb())
51+
->with('timeframe')
52+
->filter(Filter::equal('id', $id))
53+
->first();
54+
55+
if ($report === null) {
56+
throw new NotFoundError('Report not found');
57+
}
58+
59+
$report = Report::fromModel($report);
60+
4861
$format = strtolower($this->params->get('format', 'pdf'));
4962
switch ($format) {
5063
case 'pdf':

application/controllers/ReportController.php

+14-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,18 @@ 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+
$report = Model\Report::on($this->getDb())
36+
->with(['timeframe'])
37+
->filter(Filter::equal('id', $reportId))
38+
->first();
39+
40+
if ($report === null) {
41+
$this->httpNotFound($this->translate('Report not found'));
42+
}
43+
44+
$this->report = Report::fromModel($report);
3245
}
3346

3447
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

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

77
use DateTime;
8+
use Exception;
89
use GuzzleHttp\Psr7\ServerRequest;
910
use Icinga\Module\Reporting\Database;
11+
use Icinga\Module\Reporting\Model;
1012
use Icinga\Module\Reporting\Web\Controller;
1113
use Icinga\Module\Reporting\Web\Forms\TemplateForm;
1214
use Icinga\Module\Reporting\Web\Widget\Template;
13-
use ipl\Sql\Select;
15+
use ipl\Stdlib\Filter;
1416

1517
class TemplateController extends Controller
1618
{
@@ -20,13 +22,15 @@ public function indexAction()
2022
{
2123
$this->createTabs()->activate('preview');
2224

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

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

29-
$template
33+
$template = Template::fromModel($template)
3034
->setMacros([
3135
'date' => (new DateTime())->format('jS M, Y'),
3236
'time_frame' => 'Time Frame',
@@ -44,15 +48,12 @@ public function editAction()
4448

4549
$this->createTabs()->activate('edit');
4650

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();
51+
$template = Model\Template::on($this->getDb())
52+
->filter(Filter::equal('id', $this->params->getRequired('id')))
53+
->first();
5354

5455
if ($template === false) {
55-
throw new \Exception('Template not found');
56+
throw new Exception('Template not found');
5657
}
5758

5859
$template->settings = json_decode($template->settings, true);

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
@@ -4,11 +4,14 @@
44

55
namespace Icinga\Module\Reporting\Controllers;
66

7+
use Exception;
78
use Icinga\Module\Reporting\Database;
9+
use Icinga\Module\Reporting\Model;
810
use Icinga\Module\Reporting\Timeframe;
911
use Icinga\Module\Reporting\Web\Controller;
1012
use Icinga\Module\Reporting\Web\Forms\TimeframeForm;
1113
use ipl\Web\Url;
14+
use ipl\Stdlib\Filter;
1215

1316
class TimeframeController extends Controller
1417
{
@@ -19,7 +22,15 @@ class TimeframeController extends Controller
1922

2023
public function init()
2124
{
22-
$this->timeframe = Timeframe::fromDb($this->params->getRequired('id'));
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
@@ -5,11 +5,11 @@
55
namespace Icinga\Module\Reporting\Controllers;
66

77
use Icinga\Module\Reporting\Database;
8+
use Icinga\Module\Reporting\Model;
89
use Icinga\Module\Reporting\Web\Controller;
910
use Icinga\Module\Reporting\Web\Forms\TimeframeForm;
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\Link;
@@ -39,11 +39,9 @@ public function indexAction()
3939

4040
$tableRows = [];
4141

42-
$select = (new Select())
43-
->from('timeframe t')
44-
->columns('*');
42+
$timeframes = Model\Timeframe::on($this->getDb());
4543

46-
foreach ($this->getDb()->select($select) as $timeframe) {
44+
foreach ($timeframes as $timeframe) {
4745
$subject = $timeframe->name;
4846

4947
if ($canManage) {
@@ -64,8 +62,8 @@ public function indexAction()
6462
Html::tag('td', null, $subject),
6563
Html::tag('td', null, $timeframe->start),
6664
Html::tag('td', null, $timeframe->end),
67-
Html::tag('td', null, date('Y-m-d H:i', $timeframe->ctime / 1000)),
68-
Html::tag('td', null, date('Y-m-d H:i', $timeframe->mtime / 1000))
65+
Html::tag('td', null, $timeframe->ctime->format('Y-m-d H:i')),
66+
Html::tag('td', null, $timeframe->mtime->format('Y-m-d H:i'))
6967
]);
7068
}
7169

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'];
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+
}

0 commit comments

Comments
 (0)