Skip to content

Commit

Permalink
Add source configuration UI
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Mar 28, 2024
1 parent 0691496 commit 9f4bacc
Show file tree
Hide file tree
Showing 8 changed files with 647 additions and 9 deletions.
62 changes: 62 additions & 0 deletions application/controllers/SourceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/* Icinga Notifications Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Notifications\Controllers;

use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Forms\SourceForm;
use Icinga\Module\Notifications\Model\Source;
use Icinga\Web\Notification;
use ipl\Html\Contract\FormSubmitElement;
use ipl\Stdlib\Filter;
use ipl\Web\Compat\CompatController;

/** @method never httpNotFound(string $message) */
class SourceController extends CompatController
{
public function init()
{
$this->assertPermission('config/modules');
}

public function indexAction(): void
{
/** @var int $sourceId */
$sourceId = $this->params->getRequired('id');

/** @var ?Source $source */
$source = Source::on(Database::get())
->filter(Filter::equal('id', $sourceId))
->first();
if ($source === null) {
$this->httpNotFound($this->translate('Source not found'));
}

$form = (new SourceForm(Database::get(), $sourceId))
->populate($source)
->on(SourceForm::ON_SUCCESS, function (SourceForm $form) {
/** @var string $sourceName */
$sourceName = $form->getValue('name');

/** @var FormSubmitElement $pressedButton */
$pressedButton = $form->getPressedSubmitElement();
if ($pressedButton->getName() === 'delete') {
Notification::success(sprintf(
$this->translate('Deleted source "%s" successfully'),
$sourceName
));
} else {
Notification::success(sprintf(
$this->translate('Updated source "%s" successfully'),
$sourceName
));
}

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

$this->addTitleTab(sprintf($this->translate('Source: %s'), $source->name));
$this->addContent($form);
}
}
164 changes: 164 additions & 0 deletions application/controllers/SourcesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

/* Icinga Notifications Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Notifications\Controllers;

use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Forms\SourceForm;
use Icinga\Module\Notifications\Model\Source;
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
use Icinga\Module\Notifications\Widget\ItemList\SourceList;
use Icinga\Web\Notification;
use Icinga\Web\Widget\Tab;
use Icinga\Web\Widget\Tabs;
use ipl\Html\ValidHtml;
use ipl\Web\Common\BaseItemList;
use ipl\Web\Compat\CompatController;
use ipl\Web\Compat\SearchControls;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\SortControl;
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use ipl\Web\Widget\ButtonLink;

class SourcesController extends CompatController
{
use SearchControls;

public function init()
{
$this->assertPermission('config/modules');
}

public function indexAction(): void
{
$sources = Source::on(Database::get())
->columns(['id', 'type', 'name']);

$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($sources);
$sortControl = $this->createSortControl(
$sources,
[
'name' => t('Name'),
'type' => t('Type')
]
);

$searchBar = $this->createSearchBar(
$sources,
[
$limitControl->getLimitParam(),
$sortControl->getSortParam()
]
);

if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
if ($searchBar->hasBeenSubmitted()) {
$filter = QueryString::parse((string) $this->params);
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();
return;
}
} else {
$filter = $searchBar->getFilter();
}

$sources->filter($filter);

$this->addControl($paginationControl);
$this->addControl($sortControl);
$this->addControl($limitControl);
$this->addControl($searchBar);
$this->addContent(
(new ButtonLink(
t('Add Source'),
Url::fromPath('notifications/sources/add'),
'plus'
))->setBaseTarget('_next')
->addAttributes(['class' => 'add-new-component'])
);

$this->mergeTabs($this->Module()->getConfigTabs());
$this->getTabs()->activate('sources');
$this->addContent(new SourceList($sources->execute()));

if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
$this->sendMultipartUpdate();
}
}

public function addAction(): void
{
$form = (new SourceForm(Database::get()))
->on(SourceForm::ON_SUCCESS, function (SourceForm $form) {
/** @var string $sourceName */
$sourceName = $form->getValue('name');
Notification::success(sprintf(t('Added new source %s has successfully'), $sourceName));
$this->switchToSingleColumnLayout();
})
->handleRequest($this->getServerRequest());

$this->addTitleTab($this->translate('Add Source'));
$this->addContent($form);
}

public function completeAction(): void
{
$suggestions = new ObjectSuggestions();
$suggestions->setModel(Source::class);
$suggestions->forRequest($this->getServerRequest());
$this->getDocument()->add($suggestions);
}

public function searchEditorAction(): void
{
$editor = $this->createSearchEditor(
Source::on(Database::get()),
[
LimitControl::DEFAULT_LIMIT_PARAM,
SortControl::DEFAULT_SORT_PARAM,
]
);

$this->setTitle($this->translate('Adjust Filter'));
$this->getDocument()->add($editor);
}

/**
* Add attribute 'class' => 'full-width' if the content is an instance of BaseItemList
*
* @param ValidHtml $content
*
* @return $this
*/
protected function addContent(ValidHtml $content)
{
if ($content instanceof BaseItemList) {
$this->content->getAttributes()->add('class', 'full-width');
}

return parent::addContent($content);
}

/**
* Merge tabs with other tabs contained in this tab panel
*
* @param Tabs $tabs
*
* @return void
*/
protected function mergeTabs(Tabs $tabs): void
{
/** @var Tab $tab */
foreach ($tabs->getTabs() as $tab) {
/** @var string $name */
$name = $tab->getName();
if ($name) {
$this->tabs->add($name, $tab);
}
}
}
}
Loading

0 comments on commit 9f4bacc

Please sign in to comment.