Skip to content

Commit

Permalink
DeleteCommentCommand: process multiple objects at once
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Jul 25, 2023
1 parent 9f123d3 commit fa64e14
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 45 deletions.
17 changes: 13 additions & 4 deletions application/forms/Command/Object/DeleteCommentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

namespace Icinga\Module\Icingadb\Forms\Command\Object;

use CallbackFilterIterator;
use Generator;
use Icinga\Module\Icingadb\Command\Object\DeleteCommentCommand;
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
use Icinga\Web\Notification;
use ipl\Orm\Model;
use ipl\Web\Common\RedirectOption;
use ipl\Web\Widget\Icon;
use Iterator;
use IteratorIterator;
use Traversable;

class DeleteCommentForm extends CommandForm
Expand Down Expand Up @@ -55,13 +60,17 @@ protected function assembleSubmitButton()

protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/comment/delete', $object->{$object->object_type})) {
continue;
$granted = (function () use ($objects): Generator {
foreach ($objects as $object) {
if ($this->isGrantedOn('icingadb/command/comment/delete', $object->{$object->object_type})) {
yield $object;
}
}
})();

if ($granted->valid()) {
$command = new DeleteCommentCommand();
$command->setCommentName($object->name);
$command->setObjects($granted);
$command->setAuthor($this->getAuth()->getUser()->getUsername());

yield $command;
Expand Down
41 changes: 1 addition & 40 deletions library/Icingadb/Command/Object/DeleteCommentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,10 @@

namespace Icinga\Module\Icingadb\Command\Object;

use Icinga\Module\Icingadb\Command\IcingaCommand;

/**
* Delete a host or service comment
*/
class DeleteCommentCommand extends IcingaCommand
class DeleteCommentCommand extends ObjectsCommand
{
use CommandAuthor;

/**
* Name of the comment
*
* @var string
*/
protected $commentName;

/**
* Get the name of the comment
*
* @return string
*/
public function getCommentName(): string
{
if ($this->commentName === null) {
throw new \LogicException(
'You are accessing an unset property. Please make sure to set it beforehand.'
);
}

return $this->commentName;
}

/**
* Set the name of the comment
*
* @param string $commentName
*
* @return $this
*/
public function setCommentName(string $commentName): self
{
$this->commentName = $commentName;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,16 @@ public function renderToggleObjectFeature(ToggleObjectFeatureCommand $command):

public function renderDeleteComment(DeleteCommentCommand $command): IcingaApiCommand
{
$comments = [];

foreach ($command->getObjects() as $object) {
$comments[] = $object->name;
}

$endpoint = 'actions/remove-comment';
$data = [
'author' => $command->getAuthor(),
'comment' => $command->getCommentName()
'comments' => $comments
];

return IcingaApiCommand::create($endpoint, $data);
Expand Down

0 comments on commit fa64e14

Please sign in to comment.