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 Jan 16, 2024
1 parent 2d13cb9 commit f56d03a
Show file tree
Hide file tree
Showing 6 changed files with 565 additions and 9 deletions.
54 changes: 54 additions & 0 deletions application/controllers/SourceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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\Stdlib\Filter;
use ipl\Web\Compat\CompatController;

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

public function indexAction()

Check failure on line 21 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourceController::indexAction() has no return type specified.

Check failure on line 21 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourceController::indexAction() has no return type specified.

Check failure on line 21 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourceController::indexAction() has no return type specified.

Check failure on line 21 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourceController::indexAction() has no return type specified.

Check failure on line 21 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourceController::indexAction() has no return type specified.

Check failure on line 21 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourceController::indexAction() has no return type specified.
{
$sourceId = $this->params->getRequired('id');

/** @var ?Source $source */
$source = Source::on(Database::get())
->filter(Filter::equal('id', $sourceId))

Check failure on line 27 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

Check failure on line 27 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

Check failure on line 27 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

Check failure on line 27 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

Check failure on line 27 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

Check failure on line 27 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.
->first();
if ($source === null) {
$this->httpNotFound($this->translate('Source not found'));
}

$form = (new SourceForm(Database::get(), $sourceId))

Check failure on line 33 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #2 $sourceId of class Icinga\Module\Notifications\Forms\SourceForm constructor expects int|null, mixed given.

Check failure on line 33 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #2 $sourceId of class Icinga\Module\Notifications\Forms\SourceForm constructor expects int|null, mixed given.

Check failure on line 33 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #2 $sourceId of class Icinga\Module\Notifications\Forms\SourceForm constructor expects int|null, mixed given.

Check failure on line 33 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #2 $sourceId of class Icinga\Module\Notifications\Forms\SourceForm constructor expects int|null, mixed given.

Check failure on line 33 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #2 $sourceId of class Icinga\Module\Notifications\Forms\SourceForm constructor expects int|null, mixed given.

Check failure on line 33 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #2 $sourceId of class Icinga\Module\Notifications\Forms\SourceForm constructor expects int|null, mixed given.
->populate($source)

Check failure on line 34 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #1 $values of method Icinga\Module\Notifications\Forms\SourceForm::populate() expects iterable<string, mixed>, Icinga\Module\Notifications\Model\Source|null given.

Check failure on line 34 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #1 $values of method Icinga\Module\Notifications\Forms\SourceForm::populate() expects iterable<string, mixed>, Icinga\Module\Notifications\Model\Source|null given.

Check failure on line 34 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #1 $values of method Icinga\Module\Notifications\Forms\SourceForm::populate() expects iterable<string, mixed>, Icinga\Module\Notifications\Model\Source|null given.

Check failure on line 34 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #1 $values of method Icinga\Module\Notifications\Forms\SourceForm::populate() expects iterable<string, mixed>, Icinga\Module\Notifications\Model\Source|null given.

Check failure on line 34 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #1 $values of method Icinga\Module\Notifications\Forms\SourceForm::populate() expects iterable<string, mixed>, Icinga\Module\Notifications\Model\Source|null given.

Check failure on line 34 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #1 $values of method Icinga\Module\Notifications\Forms\SourceForm::populate() expects iterable<string, mixed>, Icinga\Module\Notifications\Model\Source|null given.
->on(SourceForm::ON_SUCCESS, function (SourceForm $form) {
if ($form->getPressedSubmitElement()->getName() === 'delete') {

Check failure on line 36 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Cannot call method getName() on ipl\Html\Contract\FormSubmitElement|null.

Check failure on line 36 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Cannot call method getName() on ipl\Html\Contract\FormSubmitElement|null.

Check failure on line 36 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Cannot call method getName() on ipl\Html\Contract\FormSubmitElement|null.

Check failure on line 36 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Cannot call method getName() on ipl\Html\Contract\FormSubmitElement|null.

Check failure on line 36 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Cannot call method getName() on ipl\Html\Contract\FormSubmitElement|null.

Check failure on line 36 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Cannot call method getName() on ipl\Html\Contract\FormSubmitElement|null.
Notification::success(sprintf(
$this->translate('Deleted source "%s" successfully'),
$form->getValue('name')

Check failure on line 39 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 39 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 39 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 39 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 39 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 39 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.
));
} else {
Notification::success(sprintf(
$this->translate('Updated source "%s" successfully'),
$form->getValue('name')

Check failure on line 44 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 44 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 44 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 44 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 44 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 44 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.
));
}

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

$this->addTitleTab(sprintf($this->translate('Source: %s'), $source->name));

Check failure on line 51 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Cannot access property $name on Icinga\Module\Notifications\Model\Source|null.

Check failure on line 51 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Cannot access property $name on Icinga\Module\Notifications\Model\Source|null.

Check failure on line 51 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Cannot access property $name on Icinga\Module\Notifications\Model\Source|null.

Check failure on line 51 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Cannot access property $name on Icinga\Module\Notifications\Model\Source|null.

Check failure on line 51 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Cannot access property $name on Icinga\Module\Notifications\Model\Source|null.

Check failure on line 51 in application/controllers/SourceController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Cannot access property $name on Icinga\Module\Notifications\Model\Source|null.
$this->addContent($form);
}
}
161 changes: 161 additions & 0 deletions application/controllers/SourcesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?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()

Check failure on line 34 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::indexAction() has no return type specified.

Check failure on line 34 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::indexAction() has no return type specified.

Check failure on line 34 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::indexAction() has no return type specified.

Check failure on line 34 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::indexAction() has no return type specified.

Check failure on line 34 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::indexAction() has no return type specified.

Check failure on line 34 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::indexAction() has no return type specified.
{
$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()

Check failure on line 93 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::addAction() has no return type specified.

Check failure on line 93 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::addAction() has no return type specified.

Check failure on line 93 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::addAction() has no return type specified.

Check failure on line 93 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::addAction() has no return type specified.

Check failure on line 93 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::addAction() has no return type specified.

Check failure on line 93 in application/controllers/SourcesController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Method Icinga\Module\Notifications\Controllers\SourcesController::addAction() has no return type specified.
{
$form = (new SourceForm(Database::get()))
->on(SourceForm::ON_SUCCESS, function (SourceForm $form) {
Notification::success(sprintf(
t('Added new source %s has successfully'),
$form->getValue('name')
));
$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) {
$this->tabs->add($tab->getName(), $tab);
}
}
}
Loading

0 comments on commit f56d03a

Please sign in to comment.