Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add source configuration UI #156

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions application/controllers/SourceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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;

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

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Cannot cast mixed to int.

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Cannot cast mixed to int.

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Cannot cast mixed to int.

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Cannot cast mixed to int.

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Cannot cast mixed to int.

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Cannot cast mixed to int.

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Cannot cast mixed to int.

/** @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)

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

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

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

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 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\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
{
foreach ($tabs->getTabs() as $tab) {
$name = $tab->getName();
if ($name) {
$this->tabs->add($name, $tab);
}
}
}
}
Loading
Loading