File: TimeframeController.php

package info (click to toggle)
icingaweb2-module-reporting 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 552 kB
  • sloc: php: 3,469; sql: 289; sh: 54; xml: 18; makefile: 15
file content (64 lines) | stat: -rw-r--r-- 2,041 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

// Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting\Controllers;

use Exception;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Model;
use Icinga\Module\Reporting\Timeframe;
use Icinga\Module\Reporting\Web\Controller;
use Icinga\Module\Reporting\Web\Forms\TimeframeForm;
use Icinga\Web\Notification;
use ipl\Html\Form;
use ipl\Web\Url;
use ipl\Stdlib\Filter;

class TimeframeController extends Controller
{
    /** @var Timeframe */
    protected $timeframe;

    public function init(): void
    {
        /** @var Model\Timeframe $timeframe */
        $timeframe = Model\Timeframe::on(Database::get())
            ->filter(Filter::equal('id', $this->params->getRequired('id')))
            ->first();

        if ($timeframe === null) {
            throw new Exception('Timeframe not found');
        }

        $this->timeframe = Timeframe::fromModel($timeframe);
    }

    public function editAction(): void
    {
        $this->assertPermission('reporting/timeframes');
        $this->addTitleTab($this->translate('Edit Time Frame'));

        $values = [
            'name'  => $this->timeframe->getName(),
            'start' => $this->timeframe->getStart(),
            'end'   => $this->timeframe->getEnd()
        ];

        $form = TimeframeForm::fromId($this->timeframe->getId())
            ->setAction((string) Url::fromRequest())
            ->populate($values)
            ->on(TimeframeForm::ON_SUCCESS, function (Form $form) {
                $pressedButton = $form->getPressedSubmitElement();
                if ($pressedButton && $pressedButton->getName() === 'remove') {
                    Notification::success($this->translate('Removed timeframe successfully'));
                } else {
                    Notification::success($this->translate('Update timeframe successfully'));
                }

                $this->switchToSingleColumnLayout();
            })->handleRequest($this->getServerRequest());

        $this->addContent($form);
    }
}