Skip to content

Commit

Permalink
feat,fix: contact group configuration & references, fix model naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ncosta-ic committed May 17, 2024
1 parent 0d44e63 commit 2ec3a27
Show file tree
Hide file tree
Showing 14 changed files with 644 additions and 10 deletions.
28 changes: 28 additions & 0 deletions application/controllers/ContactGroupController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/* Icinga Notifications Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Notifications\Controllers;

use Icinga\Module\Notifications\Common\Database;
use ipl\Sql\Connection;
use ipl\Web\Compat\CompatController;

class ContactGroupController extends CompatController
{
/** @var Connection $db */
private $db;

Check failure on line 14 in application/controllers/ContactGroupController.php

View workflow job for this annotation

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

Property Icinga\Module\Notifications\Controllers\ContactGroupController::$db is never read, only written.

Check failure on line 14 in application/controllers/ContactGroupController.php

View workflow job for this annotation

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

Property Icinga\Module\Notifications\Controllers\ContactGroupController::$db is never read, only written.

Check failure on line 14 in application/controllers/ContactGroupController.php

View workflow job for this annotation

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

Property Icinga\Module\Notifications\Controllers\ContactGroupController::$db is never read, only written.

Check failure on line 14 in application/controllers/ContactGroupController.php

View workflow job for this annotation

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

Property Icinga\Module\Notifications\Controllers\ContactGroupController::$db is never read, only written.

Check failure on line 14 in application/controllers/ContactGroupController.php

View workflow job for this annotation

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

Property Icinga\Module\Notifications\Controllers\ContactGroupController::$db is never read, only written.

Check failure on line 14 in application/controllers/ContactGroupController.php

View workflow job for this annotation

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

Property Icinga\Module\Notifications\Controllers\ContactGroupController::$db is never read, only written.

Check failure on line 14 in application/controllers/ContactGroupController.php

View workflow job for this annotation

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

Property Icinga\Module\Notifications\Controllers\ContactGroupController::$db is never read, only written.

public function init()
{
$this->assertPermission('notifications/config/contact-groups');

$this->db = Database::get();
}

public function editAction(): void
{
// TODO: add edit logic
$groupId = $this->params->getRequired('id');
}
}
180 changes: 180 additions & 0 deletions application/controllers/ContactGroupsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php

/* Icinga Notifications Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Notifications\Controllers;

use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Common\Links;
use Icinga\Module\Notifications\Forms\ContactGroupForm;
use Icinga\Module\Notifications\Model\ContactGroup;
use Icinga\Module\Notifications\Widget\ItemList\ContactGroupList;
use Icinga\Module\Notifications\Widget\MemberSuggestions;
use Icinga\Web\Notification;
use ipl\Html\Form;
use ipl\Html\Text;
use ipl\Html\ValidHtml;
use ipl\Sql\Connection;
use ipl\Web\Common\BaseItemList;
use ipl\Web\Compat\CompatController;
use ipl\Web\Compat\SearchControls;
use ipl\Web\Widget\ButtonLink;

class ContactGroupsController extends CompatController
{
use SearchControls;

/** @var Connection */
private $db;

public function init()
{
$this->assertPermission('notifications/config/contact-groups');
$this->db = Database::get();
}

public function getTabs()
{
if ($this->getRequest()->getActionName() === 'index') {
return parent::getTabs()
->add('schedules', [
'label' => $this->translate('Schedules'),
'url' => Links::schedules(),
'baseTarget' => '_main'
])->add('event-rules', [
'label' => $this->translate('Event Rules'),
'url' => Links::eventRules(),
'baseTarget' => '_main'
])->add('contacts', [
'label' => $this->translate('Contacts'),
'url' => Links::contacts(),
'baseTarget' => '_main'
])->add('contact-groups', [
'label' => $this->translate('Contact Groups'),
'url' => Links::contactGroups(),
'baseTarget' => '_main'
]);
}

return parent::getTabs();
}

public function indexAction(): void
{
$groups = ContactGroup::on($this->db);

$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($groups);
$sortControl = $this->createSortControl(
$groups,
[
'name' => t('Group Name'),
]
);

$searchBar = $this->createSearchBar(
$groups,
[
$limitControl->getLimitParam(),
$sortControl->getSortParam()
]
);

if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
if ($searchBar->hasBeenSubmitted()) {
// TODO(nc): Add proper filter
// $filter = $this->getFilter();
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();
return;
}
} else {
$filter = $searchBar->getFilter();
}

$groups->filter($filter);

Check failure on line 96 in application/controllers/ContactGroupsController.php

View workflow job for this annotation

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

Variable $filter might not be defined.

Check failure on line 96 in application/controllers/ContactGroupsController.php

View workflow job for this annotation

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

Variable $filter might not be defined.

Check failure on line 96 in application/controllers/ContactGroupsController.php

View workflow job for this annotation

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

Variable $filter might not be defined.

Check failure on line 96 in application/controllers/ContactGroupsController.php

View workflow job for this annotation

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

Variable $filter might not be defined.

Check failure on line 96 in application/controllers/ContactGroupsController.php

View workflow job for this annotation

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

Variable $filter might not be defined.

Check failure on line 96 in application/controllers/ContactGroupsController.php

View workflow job for this annotation

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

Variable $filter might not be defined.

Check failure on line 96 in application/controllers/ContactGroupsController.php

View workflow job for this annotation

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

Variable $filter might not be defined.

$this->addControl($paginationControl);
$this->addControl($sortControl);
$this->addControl($limitControl);
$this->addControl($searchBar);
$this->addContent(
(new ButtonLink(
Text::create(t('Add Contact Group')),
Links::contactGroupsAdd(),
'plus'
))->openInModal()
->addAttributes(['class' => 'add-new-component'])
);

$this->addContent(new ContactGroupList($groups));

if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
$this->sendMultipartUpdate();
}

$this->setAutorefreshInterval(30);

$this->setTitle($this->translate('Contact Groups'));
$this->getTabs()->activate('contact-groups');
}

protected function addAction(): void
{
$form = (new ContactGroupForm($this->db))
->setAction($this->getRequest()->getUrl()->getAbsoluteUrl())
->on(Form::ON_SENT, function (ContactGroupForm $form) {
if ($form->hasBeenCancelled()) {
$this->switchToSingleColumnLayout();
} elseif (! $form->hasBeenSubmitted()) {
foreach ($form->getPartUpdates() as $update) {
if (! is_array($update)) {
$update = [$update];
}

$this->addPart(...$update);
}
}
})
->on(Form::ON_SUCCESS, function (ContactGroupForm $form) {
$groupIdentifier = $form->addGroup();
if ($groupIdentifier) {
Notification::success(t('New group has been successfully added'));
$this->closeModalAndRefreshRemainingViews($this->getRequest()->getUrl()->getAbsoluteUrl());
} else {
Notification::error(t('New group could not be added'));
// close the modal and refresh col1
$this->closeModalAndRefreshRelatedView($this->getRequest()->getUrl()->getAbsoluteUrl());
}
})
->handleRequest($this->getServerRequest());

$this->addContent($form);
$this->setTitle($this->translate('Add Contact Group'));
}

protected function suggestMemberAction(): void
{
$members = new MemberSuggestions();
$members->forRequest($this->getServerRequest());

$this->getDocument()->addHtml($members);
}

/**
* Add attribute 'class' => 'full-width' if the content is an instance of BaseItemList
*
* @param ValidHtml $content
*
* @return ContactGroupsController
*/
protected function addContent(ValidHtml $content): ContactGroupsController
{
if ($content instanceof BaseItemList) {
$this->content->getAttributes()->add('class', 'full-width');
}

return parent::addContent($content);
}
}
Loading

0 comments on commit 2ec3a27

Please sign in to comment.