Skip to content

Commit

Permalink
Merge pull request #25 from Icinga/feature/manage-timeframes
Browse files Browse the repository at this point in the history
Allow to manage time frames
  • Loading branch information
lippserd authored Jun 13, 2019
2 parents 1a05027 + 4539875 commit a7964a3
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 15 deletions.
6 changes: 3 additions & 3 deletions application/controllers/ReportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
namespace Icinga\Module\Reporting\Controllers;

use GuzzleHttp\Psr7\ServerRequest;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Web\Controller;
use Icinga\Module\Reporting\Web\Forms\ReportForm;
use Icinga\Module\Reporting\Web\ReportsAndTimeframesTabs;
use ipl\Html\Html;
use ipl\Sql\Expression;
use ipl\Sql\Select;
use reportingipl\Web\Url;
use reportingipl\Web\Widget\ButtonLink;

class ReportsController extends Controller
{
use Database;
use ReportsAndTimeframesTabs;

public function indexAction()
{
$this->setTitle($this->translate('Reports'));
$this->createTabs()->activate('reports');

$newReport = new ButtonLink(
$this->translate('New Report'),
Expand Down
46 changes: 46 additions & 0 deletions application/controllers/TimeframeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
// Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting\Controllers;

use GuzzleHttp\Psr7\ServerRequest;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Timeframe;
use Icinga\Module\Reporting\Web\Controller;
use Icinga\Module\Reporting\Web\Forms\TimeframeForm;

class TimeframeController extends Controller
{
use Database;

/** @var Timeframe */
protected $timeframe;

public function init()
{
$this->timeframe = Timeframe::fromDb($this->params->getRequired('id'));
}

public function editAction()
{
$this->setTitle($this->translate('Edit Time Frame'));

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


$form = (new TimeframeForm())
->setId($this->timeframe->getId());

$form->populate($values);

$form->handleRequest(ServerRequest::fromGlobals());

$this->redirectForm($form, 'reporting/timeframes');

$this->addContent($form);
}
}
92 changes: 92 additions & 0 deletions application/controllers/TimeframesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
// Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting\Controllers;

use GuzzleHttp\Psr7\ServerRequest;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Web\Controller;
use Icinga\Module\Reporting\Web\Forms\TimeframeForm;
use Icinga\Module\Reporting\Web\ReportsAndTimeframesTabs;
use ipl\Html\Html;
use ipl\Sql\Select;
use reportingipl\Web\Url;
use reportingipl\Web\Widget\ButtonLink;

class TimeframesController extends Controller
{
use Database;
use ReportsAndTimeframesTabs;

public function indexAction()
{
$this->createTabs()->activate('timeframes');

$new = new ButtonLink(
$this->translate('New Timerfame'),
Url::fromPath('reporting/timeframes/new')->getAbsoluteUrl('&'),
'plus'
);

$this->addControl($new);

$tableRows = [];

$select = (new Select())
->from('timeframe t')
->columns('*');

foreach ($this->getDb()->select($select) as $timeframe) {
$url = Url::fromPath('reporting/timeframe/edit', ['id' => $timeframe->id])->getAbsoluteUrl('&');

$tableRows[] = Html::tag('tr', ['href' => $url], [
Html::tag('td', null, $timeframe->name),
Html::tag('td', null, $timeframe->start),
Html::tag('td', null, $timeframe->end),
Html::tag('td', null, date('Y-m-d H:i', $timeframe->ctime / 1000)),
Html::tag('td', null, date('Y-m-d H:i', $timeframe->mtime / 1000))
]);
}

if (! empty($tableRows)) {
$table = Html::tag(
'table',
['class' => 'common-table table-row-selectable', 'data-base-target' => '_next'],
[
Html::tag(
'thead',
null,
Html::tag(
'tr',
null,
[
Html::tag('th', null, 'Name'),
Html::tag('th', null, 'Start'),
Html::tag('th', null, 'End'),
Html::tag('th', null, 'Date Created'),
Html::tag('th', null, 'Date Modified')
]
)
),
Html::tag('tbody', null, $tableRows)
]
);

$this->addContent($table);
} else {
$this->addContent(Html::tag('p', null, 'No timeframes created yet.'));
}
}

public function newAction()
{
$this->setTitle($this->translate('New Timeframe'));

$form = new TimeframeForm();
$form->handleRequest(ServerRequest::fromGlobals());

$this->redirectForm($form, 'reporting/timeframes');

$this->addContent($form);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting;
namespace Icinga\Module\Reporting\Web;

use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
Expand Down Expand Up @@ -70,7 +70,12 @@ protected function assemble()

$this->add([
$this->assembleLabel(),
$this->formElement,
$this->formElement
]);

$this->afterFormElement();

$this->add([
$this->assembleDescription(),
$this->assembleErrors()
]);
Expand Down Expand Up @@ -118,4 +123,9 @@ protected function assembleErrors()

return null;
}

protected function afterFormElement()
{

}
}
25 changes: 25 additions & 0 deletions library/Reporting/Web/Flatpickr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
// Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting\Web;

use ipl\Html\FormElement\BaseFormElement;
use ipl\Html\Html;

class Flatpickr extends DivDecorator
{
protected $defaultAttributes = ['class' => 'reporting-flatpickr'];

public function decorate(BaseFormElement $input)
{
parent::decorate($input);

$input->getAttributes()->set('data-input', true);
}

protected function afterFormElement()
{
$this->add(Html::tag('button', ['type' => 'button', 'class' => 'icon-calendar', 'data-toggle' => true]));
$this->add(Html::tag('button', ['type' => 'button', 'class' => 'icon-cancel', 'data-clear' => true]));
}
}
17 changes: 17 additions & 0 deletions library/Reporting/Web/Forms/DecoratedElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
// Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting\Web\Forms;

use ipl\Html\FormDecorator\DecoratorInterface;

trait DecoratedElement
{
protected function addDecoratedElement(DecoratorInterface $decorator, $type, $name, array $attributes)
{
$element = $this->createElement($type, $name, $attributes);
$decorator->decorate($element);
$this->registerElement($element);
$this->add($element);
}
}
2 changes: 1 addition & 1 deletion library/Reporting/Web/Forms/ReportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

use Icinga\Authentication\Auth;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\DivDecorator;
use Icinga\Module\Reporting\ProvidedReports;
use Icinga\Module\Reporting\Web\DivDecorator;
use ipl\Html\Form;
use ipl\Html\FormElement\SubmitElementInterface;

Expand Down
10 changes: 6 additions & 4 deletions library/Reporting/Web/Forms/ScheduleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class ScheduleForm extends Form
{
use Database;
use DecoratedElement;
use ProvidedActions;

/** @var Report */
Expand Down Expand Up @@ -62,10 +63,11 @@ protected function assemble()
'monthly' => 'Monthly'
];

$this->addElement('text', 'start', [
'required' => true,
'label' => 'Start',
'class' => 'flatpickr'
$this->addDecoratedElement(new Flatpickr(), 'text', 'start', [
'required' => true,
'label' => 'Start',
'placeholder' => 'Choose date and time',
'data-enable-time' => true
]);

$this->addElement('select', 'frequency', [
Expand Down
2 changes: 1 addition & 1 deletion library/Reporting/Web/Forms/SendForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

use Icinga\Module\Reporting\Actions\SendMail;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\DivDecorator;
use Icinga\Module\Reporting\ProvidedReports;
use Icinga\Module\Reporting\Report;
use Icinga\Module\Reporting\Web\DivDecorator;
use ipl\Html\Form;

class SendForm extends Form
Expand Down
109 changes: 109 additions & 0 deletions library/Reporting/Web/Forms/TimeframeForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
// Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting\Web\Forms;

use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Web\DivDecorator;
use Icinga\Module\Reporting\Web\Flatpickr;
use ipl\Html\Form;
use ipl\Html\FormElement\SubmitElementInterface;

class TimeframeForm extends Form
{
use Database;
use DecoratedElement;

protected $id;

public function setId($id)
{
$this->id = $id;

return $this;
}

protected function assemble()
{
$this->setDefaultElementDecorator(new DivDecorator());

$this->addElement('text', 'name', [
'required' => true,
'label' => 'Name'
]);

$flatpickr = new Flatpickr();

$this->addDecoratedElement($flatpickr, 'text', 'start', [
'required' => true,
'label' => 'Start',
'placeholder' => 'Select a start date or provide a textual datetime description',
'data-allow-input' => true,
'data-enable-time' => true,
'data-enable-seconds' => true,
'data-default-hour' => '00'
]);

$this->addDecoratedElement($flatpickr, 'text', 'end', [
'required' => true,
'label' => 'End',
'placeholder' => 'Select a end date or provide a textual datetime description',
'data-allow-input' => true,
'data-enable-time' => true,
'data-enable-seconds' => true,
'data-default-hour' => '23',
'data-default-minute' => '59',
'data-default-seconds' => '59'
]);

$this->addElement('submit', 'submit', [
'label' => $this->id === null ? 'Create Time Frame' : 'Update Time Frame'
]);

if ($this->id !== null) {
$this->addElement('submit', 'remove', [
'label' => 'Remove Time Frame',
'class' => 'remove-button',
'formnovalidate' => true
]);

/** @var SubmitElementInterface $remove */
$remove = $this->getElement('remove');
if ($remove->hasBeenPressed()) {
$this->getDb()->delete('timeframe', ['id = ?' => $this->id]);

// Stupid cheat because ipl/html is not capable of multiple submit buttons
$this->getSubmitButton()->setValue($this->getSubmitButton()->getButtonLabel());
$this->valid = true;

return;
}
}
}

public function onSuccess()
{
$db = $this->getDb();

$values = $this->getValues();

$now = time() * 1000;

if ($this->id === null) {
$db->insert('timeframe', [
'name' => $values['name'],
'start' => $values['start'],
'end' => $values['end'],
'ctime' => $now,
'mtime' => $now
]);
} else {
$db->update('timeframe', [
'name' => $values['name'],
'start' => $values['start'],
'end' => $values['end'],
'mtime' => $now
], ['id = ?' => $this->id]);
}
}
}
Loading

0 comments on commit a7964a3

Please sign in to comment.