-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves #147 requires Icinga/ipl-web#214
- Loading branch information
Showing
10 changed files
with
666 additions
and
27 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
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'); | ||
|
||
/** @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); | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.