diff --git a/modules/monitoring/application/forms/Command/Object/AddCommentCommandForm.php b/modules/monitoring/application/forms/Command/Object/AddCommentCommandForm.php index a958dfee6..72133a0c5 100644 --- a/modules/monitoring/application/forms/Command/Object/AddCommentCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/AddCommentCommandForm.php @@ -3,6 +3,8 @@ namespace Icinga\Module\Monitoring\Forms\Command\Object; +use DateInterval; +use DateTime; use Icinga\Application\Config; use Icinga\Module\Monitoring\Command\Object\AddCommentCommand; use Icinga\Web\Notification; @@ -63,6 +65,52 @@ class AddCommentCommandForm extends ObjectsCommandForm ) ); } + + if (version_compare($this->getBackend()->getProgramVersion(), '2.13.0', '>=')) { + $config = Config::module('monitoring'); + $commentExpire = (bool) $config->get('settings', 'comment_expire', false); + + $this->addElement( + 'checkbox', + 'expire', + [ + 'label' => $this->translate('Use Expire Time'), + 'value' => $commentExpire, + 'description' => $this->translate('If the comment should expire, check this option.'), + 'autosubmit' => true + ] + ); + + if (isset($formData['expire']) ? $formData['expire'] : $commentExpire) { + $expireTime = new DateTime(); + $expireTime->add(new DateInterval($config->get('settings', 'comment_expire_time', 'PT1H'))); + + $this->addElement( + 'dateTimePicker', + 'expire_time', + [ + 'label' => $this->translate('Expire Time'), + 'value' => $expireTime, + 'description' => $this->translate( + 'Enter the expire date and time for this comment here. Icinga will delete the' + . ' comment after this time expired.' + ) + ] + ); + + $this->addDisplayGroup( + ['expire', 'expire_time'], + 'expire-expire_time', + [ + 'decorators' => [ + 'FormElements', + ['HtmlTag', ['tag' => 'div']] + ] + ] + ); + } + } + return $this; } @@ -81,6 +129,13 @@ class AddCommentCommandForm extends ObjectsCommandForm if (($persistent = $this->getElement('persistent')) !== null) { $comment->setPersistent($persistent->isChecked()); } + + $expire = $this->getElement('expire'); + + if ($expire !== null && $expire->isChecked()) { + $comment->setExpireTime($this->getElement('expire_time')->getValue()->getTimestamp()); + } + $this->getTransport($this->request)->send($comment); } Notification::success($this->translatePlural( diff --git a/modules/monitoring/application/views/scripts/show/components/comments.phtml b/modules/monitoring/application/views/scripts/show/components/comments.phtml index 4edc47106..a5fddef8f 100644 --- a/modules/monitoring/application/views/scripts/show/components/comments.phtml +++ b/modules/monitoring/application/views/scripts/show/components/comments.phtml @@ -50,6 +50,13 @@ if (empty($object->comments) && ! $addLink) { translate('commented') ?> timeAgo($comment->timestamp) ?> + expiration): ?> + + translate('Expires %s'), + $this->timeUntil($comment->expiration) + ) ?> + persistent ? $this->icon('attach', 'This comment is persistent.') : '' ?> persistent; } + + /** + * Set the time when the acknowledgement should expire + * + * @param int $expireTime + * + * @return $this + */ + public function setExpireTime($expireTime) + { + $this->expireTime = (int) $expireTime; + + return $this; + } + + /** + * Get the time when the acknowledgement should expire + * + * @return int|null + */ + public function getExpireTime() + { + return $this->expireTime; + } } diff --git a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php index 9557db3c4..3eb9a84c4 100644 --- a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php +++ b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php @@ -99,6 +99,11 @@ class IcingaApiCommandRenderer implements IcingaCommandRendererInterface 'author' => $command->getAuthor(), 'comment' => $command->getComment() ); + + if ($command->getExpireTime() !== null) { + $data['expiry'] = $command->getExpireTime(); + } + $this->applyFilter($data, $command->getObject()); return IcingaApiCommand::create($endpoint, $data); }