|
5 | 5 |
|
6 | 6 | namespace Icinga\Module\Notifications\Controllers; |
7 | 7 |
|
| 8 | +use ipl\Web\Compat\SearchControls; |
8 | 9 | use Icinga\Module\Notifications\Common\Auth; |
9 | 10 | use Icinga\Module\Notifications\Common\Database; |
10 | 11 | use Icinga\Module\Notifications\Common\Links; |
11 | 12 | use Icinga\Module\Notifications\Model\Contact; |
12 | 13 | use Icinga\Module\Notifications\Model\Incident; |
| 14 | +use Icinga\Module\Notifications\Model\NotificationHistory; |
| 15 | +use Icinga\Module\Notifications\View\NotificationHistoryRenderer; |
| 16 | +use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions; |
13 | 17 | use Icinga\Module\Notifications\Widget\Detail\IncidentDetail; |
14 | 18 | use Icinga\Module\Notifications\Widget\Detail\IncidentQuickActions; |
15 | 19 | use Icinga\Module\Notifications\Widget\Detail\ObjectHeader; |
| 20 | +use Icinga\Module\Notifications\Widget\ItemList\ObjectList; |
16 | 21 | use ipl\Html\Attributes; |
17 | 22 | use ipl\Html\Contract\Form; |
18 | 23 | use ipl\Stdlib\Filter; |
19 | 24 | use ipl\Web\Compat\CompatController; |
| 25 | +use ipl\Web\Control\LimitControl; |
| 26 | +use ipl\Web\Control\SortControl; |
| 27 | +use ipl\Web\Filter\QueryString; |
| 28 | +use ipl\Web\Url; |
20 | 29 |
|
21 | 30 | class IncidentController extends CompatController |
22 | 31 | { |
23 | 32 | use Auth; |
| 33 | + use SearchControls; |
| 34 | + |
| 35 | + public function init(): void |
| 36 | + { |
| 37 | + $this->setTitleTab($this->getRequest()->getActionName()); |
| 38 | + } |
24 | 39 |
|
25 | 40 | public function indexAction(): void |
26 | 41 | { |
27 | | - $this->addTitleTab(t('Incident')); |
| 42 | + $this->setTitle(t('Incident')); |
28 | 43 |
|
29 | 44 | $id = $this->params->getRequired('id'); |
30 | 45 |
|
@@ -63,4 +78,110 @@ public function indexAction(): void |
63 | 78 |
|
64 | 79 | $this->addContent(new IncidentDetail($incident)); |
65 | 80 | } |
| 81 | + |
| 82 | + public function notificationHistoryAction(): void |
| 83 | + { |
| 84 | + $this->setTitle(t('Notification History')); |
| 85 | + |
| 86 | + $notificationHistory = NotificationHistory::on(Database::get()) |
| 87 | + ->with(['contact', 'channel']) |
| 88 | + ->filter(Filter::equal('notification_history.incident_id', $this->params->shiftRequired('id'))); |
| 89 | + $this->applyRestrictions($notificationHistory); |
| 90 | + |
| 91 | + $limitControl = $this->createLimitControl(); |
| 92 | + $sortControl = $this->createSortControl( |
| 93 | + $notificationHistory, |
| 94 | + [ |
| 95 | + 'notification_history.triggered_at desc' => t('Triggered At'), |
| 96 | + 'notification_history.state, notification_history.triggered_at desc' => t('State') |
| 97 | + ] |
| 98 | + ); |
| 99 | + |
| 100 | + $paginationControl = $this->createPaginationControl($notificationHistory); |
| 101 | + $preserveParams = [ |
| 102 | + $limitControl->getLimitParam(), |
| 103 | + $sortControl->getSortParam(), |
| 104 | + 'id' |
| 105 | + ]; |
| 106 | + |
| 107 | + $searchBar = $this->createSearchBar($notificationHistory, $preserveParams); |
| 108 | + |
| 109 | + if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) { |
| 110 | + if ($searchBar->hasBeenSubmitted()) { |
| 111 | + $filter = QueryString::parse((string) $this->params); |
| 112 | + } else { |
| 113 | + $this->addControl($searchBar); |
| 114 | + $this->sendMultipartUpdate(); |
| 115 | + return; |
| 116 | + } |
| 117 | + } else { |
| 118 | + $filter = $searchBar->getFilter(); |
| 119 | + } |
| 120 | + |
| 121 | + $notificationHistory->filter($filter); |
| 122 | + |
| 123 | + $this->addControl($paginationControl); |
| 124 | + $this->addControl($sortControl); |
| 125 | + $this->addControl($limitControl); |
| 126 | + $this->addControl($searchBar); |
| 127 | + |
| 128 | + $incidentList = (new ObjectList($notificationHistory, new NotificationHistoryRenderer())); |
| 129 | + |
| 130 | + $this->addContent($incidentList); |
| 131 | + |
| 132 | + if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) { |
| 133 | + $this->sendMultipartUpdate(); |
| 134 | + } |
| 135 | + |
| 136 | + $this->setAutorefreshInterval(10); |
| 137 | + } |
| 138 | + |
| 139 | + public function completeAction(): void |
| 140 | + { |
| 141 | + $suggestions = (new ObjectSuggestions()) |
| 142 | + ->setModel(NotificationHistory::class) |
| 143 | + ->setBaseFilter( |
| 144 | + Filter::equal('notification_history.incident_id', $this->params->shiftRequired('id')) |
| 145 | + ) |
| 146 | + ->forRequest($this->getServerRequest()); |
| 147 | + |
| 148 | + $this->getDocument()->addHtml($suggestions); |
| 149 | + } |
| 150 | + |
| 151 | + public function searchEditorAction(): void |
| 152 | + { |
| 153 | + $preserveParams = [ |
| 154 | + LimitControl::DEFAULT_LIMIT_PARAM, |
| 155 | + SortControl::DEFAULT_SORT_PARAM, |
| 156 | + 'id' |
| 157 | + ]; |
| 158 | + |
| 159 | + $editor = $this->createSearchEditor( |
| 160 | + NotificationHistory::on(Database::get()), |
| 161 | + Url::fromPath('notifications/incident/notification-history', ['id' => $this->params->getRequired('id')]), |
| 162 | + $preserveParams |
| 163 | + ); |
| 164 | + $editor->setSuggestionUrl( |
| 165 | + Url::fromPath('notifications/incident/complete') |
| 166 | + ->setParams(Url::fromRequest()->onlyWith($preserveParams)->getParams()) |
| 167 | + ); |
| 168 | + |
| 169 | + $this->getDocument()->addHtml($editor); |
| 170 | + $this->setTitle($this->translate('Adjust Filter')); |
| 171 | + } |
| 172 | + |
| 173 | + protected function setTitleTab(string $name): void |
| 174 | + { |
| 175 | + $id = $this->params->getRequired('id'); |
| 176 | + $this->getTabs() |
| 177 | + ->add('index', [ |
| 178 | + 'label' => $this->translate('Incident'), |
| 179 | + 'url' => Links::incident($id) |
| 180 | + ]) |
| 181 | + ->add('notification-history', [ |
| 182 | + 'label' => $this->translate('Notification History'), |
| 183 | + 'url' => Url::fromPath('notifications/incident/notification-history', ['id' => $id]) |
| 184 | + ]) |
| 185 | + ->activate($name); |
| 186 | + } |
66 | 187 | } |
0 commit comments