From 4463f16f04972f4eae8eb7fb9a62cf8de015dfd3 Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Thu, 7 May 2015 15:11:54 +0200 Subject: [PATCH] Add abbillity to remove multiple comments by id refs #8624 --- .../controllers/CommentsController.php | 4 +- .../Object/DeleteCommentsCommandForm.php | 47 +++++++++++-------- .../Object/DeleteDowntimesCommandForm.php | 8 +--- .../views/scripts/list/comments.phtml | 8 +++- .../views/scripts/list/downtimes.phtml | 7 ++- .../Controller/MonitoredObjectController.php | 8 ++-- 6 files changed, 48 insertions(+), 34 deletions(-) diff --git a/modules/monitoring/application/controllers/CommentsController.php b/modules/monitoring/application/controllers/CommentsController.php index 106dd4650..9521af7c9 100644 --- a/modules/monitoring/application/controllers/CommentsController.php +++ b/modules/monitoring/application/controllers/CommentsController.php @@ -90,8 +90,8 @@ class Monitoring_CommentsController extends Controller $this->translate('Confirm removal of %d comments.'), count($this->comments) )); - $delCommentForm->setObjects($this->comments) - ->setRedirectUrl(Url::fromPath('monitoring/list/downtimes')) + $delCommentForm->setComments($this->comments) + ->setRedirectUrl(Url::fromPath('monitoring/list/comments')) ->handleRequest(); $this->view->delCommentForm = $delCommentForm; } diff --git a/modules/monitoring/application/forms/Command/Object/DeleteCommentsCommandForm.php b/modules/monitoring/application/forms/Command/Object/DeleteCommentsCommandForm.php index 119020411..57f6aae5a 100644 --- a/modules/monitoring/application/forms/Command/Object/DeleteCommentsCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/DeleteCommentsCommandForm.php @@ -4,13 +4,21 @@ namespace Icinga\Module\Monitoring\Forms\Command\Object; use Icinga\Module\Monitoring\Command\Object\DeleteCommentCommand; +use \Icinga\Module\Monitoring\Forms\Command\CommandForm; use Icinga\Web\Notification; /** * Form for deleting host or service comments */ -class DeleteCommentsCommandForm extends ObjectsCommandForm +class DeleteCommentsCommandForm extends CommandForm { + /** + * The comments deleted on success + * + * @var array + */ + protected $comments; + /** * (non-PHPDoc) * @see \Zend_Form::init() For the method documentation. @@ -27,20 +35,10 @@ class DeleteCommentsCommandForm extends ObjectsCommandForm public function createElements(array $formData = array()) { $this->addElements(array( - array( - 'hidden', - 'comment_id', - array( - 'required' => true, - 'decorators' => array('ViewHelper') - ) - ), array( 'hidden', 'redirect', - array( - 'decorators' => array('ViewHelper') - ) + array('decorators' => array('ViewHelper')) ) )); return $this; @@ -74,13 +72,11 @@ class DeleteCommentsCommandForm extends ObjectsCommandForm */ public function onSuccess() { - foreach ($this->objects as $object) { - /** @var \Icinga\Module\Monitoring\Object\MonitoredObject $object */ - $delComment = new DeleteCommentCommand(); - $delComment - ->setObject($object) - ->setCommentId($this->getElement('comment_id')->getValue()); - $this->getTransport($this->request)->send($delComment); + foreach ($this->comments as $comment) { + $cmd = new DeleteCommentCommand(); + $cmd->setCommentId($comment->id) + ->setIsService(isset($comment->service_description)); + $this->getTransport($this->request)->send($cmd); } $redirect = $this->getElement('redirect')->getValue(); if (! empty($redirect)) { @@ -89,4 +85,17 @@ class DeleteCommentsCommandForm extends ObjectsCommandForm Notification::success($this->translate('Deleting comment..')); return true; } + + /** + * Set the comments to be deleted upon success + * + * @param array $comments + * + * @return this fluent interface + */ + public function setComments(array $comments) + { + $this->comments = $comments; + return $this; + } } diff --git a/modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php b/modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php index f11ac1128..566ad572c 100644 --- a/modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php @@ -64,11 +64,7 @@ class DeleteDowntimesCommandForm extends CommandForm foreach ($this->downtimes as $downtime) { $delDowntime = new DeleteDowntimeCommand(); $delDowntime->setDowntimeId($downtime->id); - $delDowntime->setDowntimeType( - isset($downtime->service_description) ? - DeleteDowntimeCommand::DOWNTIME_TYPE_SERVICE : - DeleteDowntimeCommand::DOWNTIME_TYPE_HOST - ); + $delDowntime->setIsService(isset($downtime->service_description)); $this->getTransport($this->request)->send($delDowntime); } $redirect = $this->getElement('redirect')->getValue(); @@ -86,7 +82,7 @@ class DeleteDowntimesCommandForm extends CommandForm * * @return $this */ - public function setDowntimes($downtimes) + public function setDowntimes(array $downtimes) { $this->downtimes = $downtimes; return $this; diff --git a/modules/monitoring/application/views/scripts/list/comments.phtml b/modules/monitoring/application/views/scripts/list/comments.phtml index 141c688e7..690095360 100644 --- a/modules/monitoring/application/views/scripts/list/comments.phtml +++ b/modules/monitoring/application/views/scripts/list/comments.phtml @@ -83,7 +83,13 @@ if (count($comments) === 0) { populate(array('comment_id' => $comment->id, 'redirect' => $this->url)); + $delCommentForm->populate( + array( + 'comment_id' => $comment->id, + 'comment_is_service' => isset($comment->service_description), + 'redirect' => $this->url + ) + ); $delCommentForm->setAction($this->url('monitoring/comment/remove', array('comment_id' => $comment->id))); echo $delCommentForm; ?> diff --git a/modules/monitoring/application/views/scripts/list/downtimes.phtml b/modules/monitoring/application/views/scripts/list/downtimes.phtml index 859326b2e..1268a4050 100644 --- a/modules/monitoring/application/views/scripts/list/downtimes.phtml +++ b/modules/monitoring/application/views/scripts/list/downtimes.phtml @@ -128,7 +128,12 @@ if (count($downtimes) === 0) { populate(array('downtime_id' => $downtime->id, 'redirect' => $this->url)); + $delDowntimeForm->populate( + array( + 'downtime_id' => $downtime->id, + 'redirect' => $this->url + ) + ); $delDowntimeForm->setAction($this->url('monitoring/downtime/remove', array('downtime_id' => $downtime->id))); echo $delDowntimeForm; ?> diff --git a/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php b/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php index 3629a4182..ce565df86 100644 --- a/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php +++ b/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php @@ -5,7 +5,7 @@ namespace Icinga\Module\Monitoring\Web\Controller; use Icinga\Module\Monitoring\Controller; use Icinga\Module\Monitoring\Forms\Command\Object\CheckNowCommandForm; -use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentsCommandForm; +use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm; use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimeCommandForm; use Icinga\Module\Monitoring\Forms\Command\Object\ObjectsCommandForm; use Icinga\Module\Monitoring\Forms\Command\Object\RemoveAcknowledgementCommandForm; @@ -81,10 +81,8 @@ abstract class MonitoredObjectController extends Controller ->handleRequest(); $this->view->toggleFeaturesForm = $toggleFeaturesForm; if (! empty($this->object->comments) && $auth->hasPermission('monitoring/command/comment/delete')) { - $delCommentForm = new DeleteCommentsCommandForm(); - $delCommentForm - ->setObjects($this->object) - ->handleRequest(); + $delCommentForm = new DeleteCommentCommandForm(); + $delCommentForm->handleRequest(); $this->view->delCommentForm = $delCommentForm; } if (! empty($this->object->downtimes) && $auth->hasPermission('monitoring/command/downtime/delete')) {