Skip to content

Commit beb8a21

Browse files
authored
Improve ContactForm (#301)
resolves #276, #74 ## Requires: - Icinga/icingaweb2#5370 - Icinga/ipl-web#284 - Icinga/ipl-web#305
2 parents 14a94e6 + 29cc836 commit beb8a21

File tree

6 files changed

+215
-74
lines changed

6 files changed

+215
-74
lines changed

application/controllers/ChannelsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function addAction(): void
118118
$form->getValue('name')
119119
)
120120
);
121-
$this->redirectNow(Links::channels());
121+
$this->switchToSingleColumnLayout();
122122
})
123123
->handleRequest($this->getServerRequest());
124124

application/controllers/ContactController.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@
44

55
namespace Icinga\Module\Notifications\Controllers;
66

7+
use Exception;
8+
use Icinga\Application\Config;
9+
use Icinga\Authentication\User\DomainAwareInterface;
10+
use Icinga\Authentication\User\UserBackend;
11+
use Icinga\Data\Selectable;
712
use Icinga\Module\Notifications\Common\Database;
813
use Icinga\Module\Notifications\Web\Form\ContactForm;
14+
use Icinga\Repository\Repository;
915
use Icinga\Web\Notification;
1016
use ipl\Web\Compat\CompatController;
17+
use ipl\Web\FormElement\SearchSuggestions;
1118

1219
class ContactController extends CompatController
1320
{
@@ -44,4 +51,60 @@ public function indexAction(): void
4451

4552
$this->addContent($form);
4653
}
54+
55+
public function suggestIcingaWebUserAction(): void
56+
{
57+
$suggestions = new SearchSuggestions((function () use (&$suggestions) {
58+
$userBackends = [];
59+
foreach (Config::app('authentication') as $backendName => $backendConfig) {
60+
$candidate = UserBackend::create($backendName, $backendConfig);
61+
if ($candidate instanceof Selectable) {
62+
$userBackends[] = $candidate;
63+
}
64+
}
65+
66+
$limit = 10;
67+
while ($limit > 0 && ! empty($userBackends)) {
68+
/** @var Repository $backend */
69+
$backend = array_shift($userBackends);
70+
$query = $backend->select()
71+
->from('user', ['user_name'])
72+
->where('user_name', $suggestions->getSearchTerm())
73+
->limit($limit);
74+
75+
try {
76+
/** @var string[] $names */
77+
$names = $query->fetchColumn();
78+
} catch (Exception) {
79+
continue;
80+
}
81+
82+
if (empty($names)) {
83+
continue;
84+
}
85+
86+
$domain = null;
87+
if ($backend instanceof DomainAwareInterface && $backend->getDomain()) {
88+
$domain = '@' . $backend->getDomain();
89+
}
90+
91+
foreach ($names as $name) {
92+
yield [
93+
'search' => $name . $domain,
94+
'label' => $name . $domain,
95+
'backend' => $backend->getName(),
96+
];
97+
}
98+
99+
$limit -= count($names);
100+
}
101+
})());
102+
103+
$suggestions->setGroupingCallback(function (array $data) {
104+
return $data['backend'];
105+
});
106+
107+
$suggestions->forRequest($this->getServerRequest());
108+
$this->getDocument()->addHtml($suggestions);
109+
}
47110
}

application/controllers/ContactsController.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@
55
namespace Icinga\Module\Notifications\Controllers;
66

77
use Icinga\Module\Notifications\Common\Links;
8+
use Icinga\Module\Notifications\Model\Channel;
89
use Icinga\Module\Notifications\View\ContactRenderer;
910
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
1011
use Icinga\Module\Notifications\Common\Database;
1112
use Icinga\Module\Notifications\Model\Contact;
1213
use Icinga\Module\Notifications\Web\Form\ContactForm;
1314
use Icinga\Module\Notifications\Widget\ItemList\ObjectList;
1415
use Icinga\Web\Notification;
16+
use ipl\Html\Contract\Form;
17+
use ipl\Html\TemplateString;
1518
use ipl\Sql\Connection;
19+
use ipl\Sql\Expression;
1620
use ipl\Stdlib\Filter;
1721
use ipl\Web\Compat\CompatController;
1822
use ipl\Web\Compat\SearchControls;
1923
use ipl\Web\Control\LimitControl;
2024
use ipl\Web\Control\SortControl;
2125
use ipl\Web\Filter\QueryString;
2226
use ipl\Web\Layout\MinimalItemLayout;
27+
use ipl\Web\Widget\ActionLink;
2328
use ipl\Web\Widget\ButtonLink;
2429

2530
class ContactsController extends CompatController
@@ -48,8 +53,8 @@ public function indexAction()
4853
$sortControl = $this->createSortControl(
4954
$contacts,
5055
[
51-
'full_name' => t('Full Name'),
52-
'changed_at' => t('Changed At')
56+
'full_name' => $this->translate('Full Name'),
57+
'changed_at' => $this->translate('Changed At')
5358
]
5459
);
5560

@@ -79,15 +84,32 @@ public function indexAction()
7984
$this->addControl($sortControl);
8085
$this->addControl($limitControl);
8186
$this->addControl($searchBar);
82-
$this->addContent(
83-
(new ButtonLink(t('Add Contact'), Links::contactAdd(), 'plus'))
84-
->setBaseTarget('_next')
85-
->addAttributes(['class' => 'add-new-component'])
86-
);
87+
88+
$addButton = (new ButtonLink(
89+
$this->translate('Add Contact'),
90+
Links::contactAdd(),
91+
'plus',
92+
['class' => 'add-new-component']
93+
))->setBaseTarget('_next');
94+
95+
$emptyStateMessage = null;
96+
if (Channel::on($this->db)->columns([new Expression('1')])->limit(1)->first() === null) {
97+
$addButton->disable($this->translate('A channel is required to add a contact'));
98+
99+
$emptyStateMessage = TemplateString::create(
100+
$this->translate(
101+
'No contacts found. To add a new contact, please {{#link}}configure a Channel{{/link}} first.'
102+
),
103+
['link' => (new ActionLink(null, Links::channelAdd()))->setBaseTarget('_next')]
104+
);
105+
}
106+
107+
$this->addContent($addButton);
87108

88109
$this->addContent(
89110
(new ObjectList($contacts, new ContactRenderer()))
90111
->setItemLayoutClass(MinimalItemLayout::class)
112+
->setEmptyStateMessage($emptyStateMessage)
91113
);
92114

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

103125
public function addAction(): void
104126
{
105-
$this->addTitleTab(t('Add Contact'));
127+
$this->addTitleTab($this->translate('Add Contact'));
106128

107129
$form = (new ContactForm($this->db))
108-
->on(ContactForm::ON_SUCCESS, function (ContactForm $form) {
130+
->on(Form::ON_SUBMIT, function (ContactForm $form) {
109131
$form->addContact();
110-
Notification::success(t('New contact has successfully been added'));
132+
Notification::success($this->translate('New contact has successfully been added'));
111133
$this->redirectNow(Links::contacts());
112134
})->handleRequest($this->getServerRequest());
113135

library/Notifications/Model/Channel.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use ipl\Orm\Model;
1212
use ipl\Orm\Query;
1313
use ipl\Orm\Relations;
14-
use ipl\Sql\Connection;
1514
use ipl\Web\Widget\Icon;
1615

1716
/**
@@ -107,24 +106,4 @@ public function getIcon(): Icon
107106

108107
return $icon;
109108
}
110-
111-
/**
112-
* Fetch and map all the configured channel names to a key => value array
113-
*
114-
* @param Connection $conn
115-
*
116-
* @return string[] All the channel names mapped as id => name
117-
*/
118-
public static function fetchChannelNames(Connection $conn): array
119-
{
120-
$channels = [];
121-
$query = Channel::on($conn);
122-
/** @var Channel $channel */
123-
foreach ($query as $channel) {
124-
$name = $channel->name;
125-
$channels[$channel->id] = $name;
126-
}
127-
128-
return $channels;
129-
}
130109
}

0 commit comments

Comments
 (0)