-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Icinga/feature/manage-timeframes
Allow to manage time frames
- Loading branch information
Showing
12 changed files
with
378 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} |
Oops, something went wrong.