-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactsController.php
More file actions
182 lines (153 loc) · 5.71 KB
/
Copy pathContactsController.php
File metadata and controls
182 lines (153 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
// SPDX-FileCopyrightText: 2023 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Notifications\Controllers;
use Icinga\Module\Notifications\Common\ConfigurationTabs;
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
{
use ConfigurationTabs;
use SearchControls;
/** @var Connection */
private $db;
/** @var Filter\Rule Filter from query string parameters */
private $filter;
public function init()
{
$this->assertPermission('notifications/config/contacts');
$this->db = Database::get();
}
public function indexAction()
{
$this->setTitle($this->translate('Contacts'));
$this->getTabs()->activate('contacts');
$contacts = Contact::on($this->db);
$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($contacts);
$sortControl = $this->createSortControl(
$contacts,
[
'full_name' => $this->translate('Full Name'),
'changed_at' => $this->translate('Changed At')
]
);
$searchBar = $this->createSearchBar(
$contacts,
[
$limitControl->getLimitParam(),
$sortControl->getSortParam()
]
);
if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
if ($searchBar->hasBeenSubmitted()) {
$filter = $this->getFilter();
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();
return;
}
} else {
$filter = $searchBar->getFilter();
}
$contacts->filter($filter);
$this->addControl($paginationControl);
$this->addControl($sortControl);
$this->addControl($limitControl);
$this->addControl($searchBar);
$addButton = (new ButtonLink(
$this->translate('Create 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'));
if ($this->Auth()->hasPermission('config/modules')) {
$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')]
);
} else {
$emptyStateMessage = $this->translate(
'No contacts found. To add a new contact, a channel is required.'
. ' Please contact your system administrator.'
);
}
}
$this->addContent($addButton);
$this->addContent(
(new ObjectList($contacts, new ContactRenderer()))
->setItemLayoutClass(MinimalItemLayout::class)
->setEmptyStateMessage($emptyStateMessage)
);
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
$this->sendMultipartUpdate();
}
}
public function addAction(): void
{
$this->addTitleTab($this->translate('Create Contact'));
$form = (new ContactForm($this->db))
->on(Form::ON_SUBMIT, function (ContactForm $form) {
$form->addContact();
Notification::success($this->translate('New contact has successfully been added'));
$this->switchToSingleColumnLayout();
})->handleRequest($this->getServerRequest());
$this->addContent($form);
}
public function completeAction(): void
{
$suggestions = new ObjectSuggestions();
$suggestions->setModel(Contact::class);
$suggestions->forRequest($this->getServerRequest());
$this->getDocument()->add($suggestions);
}
public function searchEditorAction(): void
{
$editor = $this->createSearchEditor(
Contact::on($this->db),
[
LimitControl::DEFAULT_LIMIT_PARAM,
SortControl::DEFAULT_SORT_PARAM,
]
);
$this->getDocument()->add($editor);
$this->setTitle($this->translate('Adjust Filter'));
}
/**
* Get the filter created from query string parameters
*
* @return Filter\Rule
*/
protected function getFilter(): Filter\Rule
{
if ($this->filter === null) {
$this->filter = QueryString::parse((string) $this->params);
}
return $this->filter;
}
}