Skip to content
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
2 changes: 1 addition & 1 deletion application/controllers/ChannelsController.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

/* Icinga Notifications Web | (c) 2023 Icinga GmbH | GPLv2 */
Expand Down Expand Up @@ -118,7 +118,7 @@
$form->getValue('name')
)
);
$this->redirectNow(Links::channels());
$this->switchToSingleColumnLayout();
})
->handleRequest($this->getServerRequest());

Expand Down Expand Up @@ -172,7 +172,7 @@
{
/** @var Tab $tab */
foreach ($tabs->getTabs() as $tab) {
$this->tabs->add($tab->getName(), $tab);

Check failure on line 175 in application/controllers/ChannelsController.php

View workflow job for this annotation

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

Parameter #1 $name of method ipl\Web\Widget\Tabs::add() expects string, string|null given.

Check failure on line 175 in application/controllers/ChannelsController.php

View workflow job for this annotation

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

Parameter #1 $name of method ipl\Web\Widget\Tabs::add() expects string, string|null given.

Check failure on line 175 in application/controllers/ChannelsController.php

View workflow job for this annotation

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

Parameter #1 $name of method ipl\Web\Widget\Tabs::add() expects string, string|null given.
}
}
}
63 changes: 63 additions & 0 deletions application/controllers/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

namespace Icinga\Module\Notifications\Controllers;

use Exception;
use Icinga\Application\Config;
use Icinga\Authentication\User\DomainAwareInterface;
use Icinga\Authentication\User\UserBackend;
use Icinga\Data\Selectable;
use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Web\Form\ContactForm;
use Icinga\Repository\Repository;
use Icinga\Web\Notification;
use ipl\Web\Compat\CompatController;
use ipl\Web\FormElement\SearchSuggestions;

class ContactController extends CompatController
{
Expand All @@ -21,7 +28,7 @@
$contactId = $this->params->getRequired('id');

$form = (new ContactForm(Database::get()))
->loadContact($contactId)

Check failure on line 31 in application/controllers/ContactController.php

View workflow job for this annotation

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

Parameter #1 $id of method Icinga\Module\Notifications\Web\Form\ContactForm::loadContact() expects int, mixed given.

Check failure on line 31 in application/controllers/ContactController.php

View workflow job for this annotation

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

Parameter #1 $id of method Icinga\Module\Notifications\Web\Form\ContactForm::loadContact() expects int, mixed given.

Check failure on line 31 in application/controllers/ContactController.php

View workflow job for this annotation

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

Parameter #1 $id of method Icinga\Module\Notifications\Web\Form\ContactForm::loadContact() expects int, mixed given.
->on(ContactForm::ON_SUCCESS, function (ContactForm $form) {
$form->editContact();
Notification::success(sprintf(
Expand All @@ -44,4 +51,60 @@

$this->addContent($form);
}

public function suggestIcingaWebUserAction(): void
{
$suggestions = new SearchSuggestions((function () use (&$suggestions) {
$userBackends = [];
foreach (Config::app('authentication') as $backendName => $backendConfig) {
$candidate = UserBackend::create($backendName, $backendConfig);
if ($candidate instanceof Selectable) {
$userBackends[] = $candidate;
}
}

$limit = 10;
while ($limit > 0 && ! empty($userBackends)) {
/** @var Repository $backend */
$backend = array_shift($userBackends);
$query = $backend->select()
->from('user', ['user_name'])
->where('user_name', $suggestions->getSearchTerm())

Check failure on line 72 in application/controllers/ContactController.php

View workflow job for this annotation

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

Cannot call method getSearchTerm() on ipl\Web\FormElement\SearchSuggestions|null.

Check failure on line 72 in application/controllers/ContactController.php

View workflow job for this annotation

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

Cannot call method getSearchTerm() on ipl\Web\FormElement\SearchSuggestions|null.

Check failure on line 72 in application/controllers/ContactController.php

View workflow job for this annotation

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

Cannot call method getSearchTerm() on ipl\Web\FormElement\SearchSuggestions|null.
->limit($limit);

try {
/** @var string[] $names */
$names = $query->fetchColumn();
} catch (Exception) {
continue;
}

if (empty($names)) {
continue;
}

$domain = null;
if ($backend instanceof DomainAwareInterface && $backend->getDomain()) {
$domain = '@' . $backend->getDomain();
}

foreach ($names as $name) {
yield [
'search' => $name . $domain,
'label' => $name . $domain,
'backend' => $backend->getName(),
];
}

$limit -= count($names);
}
})());

$suggestions->setGroupingCallback(function (array $data) {
return $data['backend'];
});

$suggestions->forRequest($this->getServerRequest());
$this->getDocument()->addHtml($suggestions);
}
}
42 changes: 32 additions & 10 deletions application/controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
namespace Icinga\Module\Notifications\Controllers;

use Icinga\Module\Notifications\Common\Links;
use Icinga\Module\Notifications\Model\Channel;
use Icinga\Module\Notifications\View\ContactRenderer;
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Model\Contact;
use Icinga\Module\Notifications\Web\Form\ContactForm;
use Icinga\Module\Notifications\Widget\ItemList\ObjectList;
use Icinga\Web\Notification;
use ipl\Html\Contract\Form;
use ipl\Html\TemplateString;
use ipl\Sql\Connection;
use ipl\Sql\Expression;
use ipl\Stdlib\Filter;
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\Layout\MinimalItemLayout;
use ipl\Web\Widget\ActionLink;
use ipl\Web\Widget\ButtonLink;

class ContactsController extends CompatController
Expand Down Expand Up @@ -48,8 +53,8 @@ public function indexAction()
$sortControl = $this->createSortControl(
$contacts,
[
'full_name' => t('Full Name'),
'changed_at' => t('Changed At')
'full_name' => $this->translate('Full Name'),
'changed_at' => $this->translate('Changed At')
]
);

Expand Down Expand Up @@ -79,15 +84,32 @@ public function indexAction()
$this->addControl($sortControl);
$this->addControl($limitControl);
$this->addControl($searchBar);
$this->addContent(
(new ButtonLink(t('Add Contact'), Links::contactAdd(), 'plus'))
->setBaseTarget('_next')
->addAttributes(['class' => 'add-new-component'])
);

$addButton = (new ButtonLink(
$this->translate('Add Contact'),
Links::contactAdd(),
'plus',
['class' => 'add-new-component']
))->setBaseTarget('_next');

$emptyStateMessage = null;
if (Channel::on($this->db)->columns([new Expression('1')])->limit(1)->first() === null) {
$addButton->disable($this->translate('A channel is required to add a contact'));

$emptyStateMessage = TemplateString::create(
$this->translate(
'No contacts found. To add a new contact, please {{#link}}configure a Channel{{/link}} first.'
),
['link' => (new ActionLink(null, Links::channelAdd()))->setBaseTarget('_next')]
);
}

$this->addContent($addButton);

$this->addContent(
(new ObjectList($contacts, new ContactRenderer()))
->setItemLayoutClass(MinimalItemLayout::class)
->setEmptyStateMessage($emptyStateMessage)
);

if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
Expand All @@ -102,12 +124,12 @@ public function indexAction()

public function addAction(): void
{
$this->addTitleTab(t('Add Contact'));
$this->addTitleTab($this->translate('Add Contact'));

$form = (new ContactForm($this->db))
->on(ContactForm::ON_SUCCESS, function (ContactForm $form) {
->on(Form::ON_SUBMIT, function (ContactForm $form) {
$form->addContact();
Notification::success(t('New contact has successfully been added'));
Notification::success($this->translate('New contact has successfully been added'));
$this->redirectNow(Links::contacts());
})->handleRequest($this->getServerRequest());

Expand Down
21 changes: 0 additions & 21 deletions library/Notifications/Model/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Orm\Relations;
use ipl\Sql\Connection;
use ipl\Web\Widget\Icon;

/**
Expand Down Expand Up @@ -107,24 +106,4 @@ public function getIcon(): Icon

return $icon;
}

/**
* Fetch and map all the configured channel names to a key => value array
*
* @param Connection $conn
*
* @return string[] All the channel names mapped as id => name
*/
public static function fetchChannelNames(Connection $conn): array
{
$channels = [];
$query = Channel::on($conn);
/** @var Channel $channel */
foreach ($query as $channel) {
$name = $channel->name;
$channels[$channel->id] = $name;
}

return $channels;
}
}
Loading
Loading