From 1eef471bc23b2c645cd01b2d93aab5fe25d2cf46 Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Thu, 26 Sep 2013 17:02:56 +0200 Subject: [PATCH 1/5] Comment overview: Basic implementation refs #4714 --- config/modules/monitoring/menu.ini | 13 ++- .../controllers/ListController.php | 32 +++++ .../views/scripts/list/comments.phtml | 109 ++++++++++++++++++ .../Backend/Ido/Query/CommentQuery.php | 67 +++++++++-- .../library/Monitoring/DataView/Comment.php | 84 ++++++++++++++ .../Monitoring/Object/AbstractObject.php | 13 ++- .../library/Monitoring/View/CommentView.php | 26 ----- 7 files changed, 298 insertions(+), 46 deletions(-) create mode 100644 modules/monitoring/application/views/scripts/list/comments.phtml create mode 100644 modules/monitoring/library/Monitoring/DataView/Comment.php delete mode 100644 modules/monitoring/library/Monitoring/View/CommentView.php diff --git a/config/modules/monitoring/menu.ini b/config/modules/monitoring/menu.ini index ff3aed3b7..04e5e7918 100755 --- a/config/modules/monitoring/menu.ini +++ b/config/modules/monitoring/menu.ini @@ -9,12 +9,13 @@ ;Changes.route = "/monitoring/list/services?sort=service_last_state_change" ;_1 = 1 ;Spacer after this section -Hosts = "/monitoring/list/hosts" -Services = "/monitoring/list/services" -Downtimes = "/monitoring/list/downtimes" -Notifications = "/monitoring/list/notifications" -Contacts = "/monitoring/list/contacts" -Contact Groups = "/monitoring/list/contactgroups" +Hosts = "/monitoring/list/hosts" +Services = "/monitoring/list/services" +Downtimes = "/monitoring/list/downtimes" +Notifications = "/monitoring/list/notifications" +Comments = "/monitoring/list/comments" +Contacts = "/monitoring/list/contacts" +Contact Groups = "/monitoring/list/contactgroups" ;Remove component as of #4583 since it's not working ;Summaries = "/monitoring/summary/group/by/hostgroup" diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index 5a4eaadb0..1cc61a2a9 100644 --- a/modules/monitoring/application/controllers/ListController.php +++ b/modules/monitoring/application/controllers/ListController.php @@ -45,6 +45,7 @@ use Icinga\Module\Monitoring\DataView\Downtime as DowntimeView; use Icinga\Module\Monitoring\DataView\Contact as ContactView; use Icinga\Module\Monitoring\DataView\Contactgroup as ContactgroupView; use Icinga\Module\Monitoring\DataView\HostAndServiceStatus as HostAndServiceStatusView; +use Icinga\Module\Monitoring\DataView\Comment as CommentView; class Monitoring_ListController extends ActionController { @@ -306,6 +307,37 @@ class Monitoring_ListController extends ActionController $this->handleFormatRequest($query); } + public function commentsAction() + { + $query = CommentView::fromRequest( + $this->_request, + array( + 'comment_objecttype_id', + 'comment_id', + 'comment_data', + 'comment_author', + 'comment_timestamp', + 'comment_type', + 'comment_is_persistent', + 'comment_expiration_timestamp', + 'host_name', + 'service_name' + ) + )->getQuery(); + + $this->view->comments = $query->paginate(); + + $this->setupSortControl( + array( + 'comment_timestamp' => 'Comment Timestamp', + 'host_service' => 'Host and Service', + 'comment_id' => 'Comment Id', + 'comment_expires' => 'Expiration Timestamp' + ) + ); + $this->handleFormatRequest($query); + } + /** * Handle the 'format' and 'view' parameter * diff --git a/modules/monitoring/application/views/scripts/list/comments.phtml b/modules/monitoring/application/views/scripts/list/comments.phtml new file mode 100644 index 000000000..e9bec93b7 --- /dev/null +++ b/modules/monitoring/application/views/scripts/list/comments.phtml @@ -0,0 +1,109 @@ +getHelper('DateFormat'); +?> +tabs->render($this); ?> + +getHelper('MonitoringState'); +?> + +
+ sortControl->render($this); ?> + paginationControl($comments, null, null, array('preserve' => $this->preserve)); ?> + + + + + + + + + + + + + + + + + + comment_objecttype_id === '1') ? 'host' : 'service'; + $objectName = ($objectType === 'host') ? $comment->host_name : $comment->service_name; + $hrefParameters = array( + $objectType => $objectName, + 'comment_id' => $comment->comment_id + ); + if ($objectType === 'service') { + $hrefParameters['host'] = $comment->host_name; + } + + $detailLink = $this->href( + 'monitoring/show/' . $objectType, + $hrefParameters + ); + + $hostLink = $this->href( + 'monitoring/show/host', + array( + 'host' => $comment->host_name + ) + ); + ?> + activeRowHref === $detailLink) ? 'class="active"' : ''; ?>> + + + + + + + + + + + + + + +
Comment IdTypeHostServiceEntry TimeAuthorCommentPersistentExpires
+ + comment_objecttype_id === '1'): ?> + {{{ICON_HOST}}} + comment_objecttype_id === '2'): ?> + {{{ICON_SERVICE}}} + + + comment_id; ?> + + comment_type; ?> + + + host_name; ?> + + + service_name): ?> + + service_name ?> + + +   + + + formatDateTime($comment->comment_timestamp); ?> + + comment_author; ?> + + comment_data; ?> + + comment_is_persistent === '1') ? 'Yes' : 'No'; ?> + + comment_expiration_timestamp) ? + $dateHelper->formatDateTime($comment->comment_expiration_timestamp) : + ' '; + ?> + + COMMAND!!!DELETE_COMMENT +
+ +
\ No newline at end of file diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentQuery.php index a21dbc1e9..4a3c9d9b7 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentQuery.php @@ -1,23 +1,61 @@ + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} namespace Icinga\Module\Monitoring\Backend\Ido\Query; +/** + * Query map for comments + */ class CommentQuery extends AbstractQuery { protected $columnMap = array( 'comments' => array( - 'comment_data' => 'cm.comment_data', - 'comment_author' => 'cm.author_name', - //'comment_timestamp' => 'UNIX_TIMESTAMP(cm.entry_time)', - 'comment_timestamp' => 'UNIX_TIMESTAMP(cm.comment_time)', - 'comment_type' => "CASE cm.entry_type WHEN 1 THEN 'comment' WHEN 2 THEN 'downtime' WHEN 3 THEN 'flapping' WHEN 4 THEN 'ack' END", + 'comment_objecttype_id' => 'co.objecttype_id', + 'comment_id' => 'cm.internal_comment_id', + 'comment_internal_id' => 'cm.internal_comment_id', + 'comment_data' => 'cm.comment_data', + 'comment_author' => 'cm.author_name COLLATE latin1_general_ci', + 'comment_timestamp' => 'UNIX_TIMESTAMP(cm.comment_time)', + 'comment_type' => "CASE cm.entry_type WHEN 1 THEN 'comment' WHEN 2 THEN 'downtime' WHEN 3 THEN 'flapping' WHEN 4 THEN 'ack' END", + 'comment_is_persistent' => 'cm.is_persistent', + 'comment_expiration_timestamp' => 'CASE cm.expires WHEN 1 THEN UNIX_TIMESTAMP(cm.expiration_time) ELSE NULL END' ), 'hosts' => array( - 'host_name' => 'ho.name1', + 'host_name' => 'ho.name1 COLLATE latin1_general_ci', + 'host' => 'ho.name1 COLLATE latin1_general_ci', + ), 'services' => array( - 'service_host_name' => 'so.name1 COLLATE latin1_general_ci', - 'service_description' => 'so.name2 COLLATE latin1_general_ci', + 'service_host_name' => 'so.name1 COLLATE latin1_general_ci', + 'service' => 'so.name2 COLLATE latin1_general_ci', + 'service_name' => 'so.name2 COLLATE latin1_general_ci', + 'service_description' => 'so.name2 COLLATE latin1_general_ci', ) ); @@ -28,6 +66,13 @@ class CommentQuery extends AbstractQuery array() ); + $this->baseQuery->join( + array( + 'co' => $this->prefix . 'objects' + ), + 'cm.object_id = co.' . $this->object_id . ' AND co.is_active = 1' + ); + $this->joinedVirtualTables = array('comments' => true); } @@ -35,16 +80,16 @@ class CommentQuery extends AbstractQuery { $this->baseQuery->join( array('ho' => $this->prefix . 'objects'), - 'cm.object_id = ho.' . $this->object_id . ' AND ho.is_active = 1 AND ho.objecttype_id = 1', + 'co.name1 = ho.name1 AND ho.is_active = 1 AND ho.objecttype_id = 1', array() ); } protected function joinServices() { - $this->baseQuery->join( + $this->baseQuery->joinLeft( array('so' => $this->prefix . 'objects'), - 'cm.object_id = so.' . $this->object_id . ' AND so.is_active = 1 AND so.objecttype_id = 2', + 'co.name1 = so.name1 AND co.name2 = so.name2 AND so.is_active = 1 AND so.objecttype_id = 2', array() ); } diff --git a/modules/monitoring/library/Monitoring/DataView/Comment.php b/modules/monitoring/library/Monitoring/DataView/Comment.php new file mode 100644 index 000000000..3ef879b4a --- /dev/null +++ b/modules/monitoring/library/Monitoring/DataView/Comment.php @@ -0,0 +1,84 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Icinga\Module\Monitoring\DataView; + +/** + * View representation for comments + */ +class Comment extends DataView +{ + /** + * Retrieve columns provided by this view + * + * @return array + */ + public function getColumns() + { + return array( + 'comment_objecttype_id', + 'comment_id', + 'comment_data', + 'comment_author', + 'comment_timestamp', + 'comment_type', + 'comment_is_persistent', + 'comment_expiration_timestamp', + 'host_name', + 'service_name' + ); + } + + /** + * Retrieve default sorting rules for particular columns. These involve sort order and potential additional to sort + * + * @return array + */ + public function getSortRules() + { + return array( + 'comment_timestamp' => array( + 'order' => self::SORT_DESC + ), + 'host_service' => array( + 'columns' => array( + 'host_name', + 'service_name' + ), + 'order' => self::SORT_ASC + ), + 'comment_id' => array( + 'order' => self::SORT_ASC + ), + 'comment_expires' => array( + 'order' => self::SORT_DESC + ) + ); + } +} diff --git a/modules/monitoring/library/Monitoring/Object/AbstractObject.php b/modules/monitoring/library/Monitoring/Object/AbstractObject.php index 408a99029..fcc512884 100644 --- a/modules/monitoring/library/Monitoring/Object/AbstractObject.php +++ b/modules/monitoring/library/Monitoring/Object/AbstractObject.php @@ -29,8 +29,15 @@ abstract class AbstractObject public function __construct(Backend $backend, $name1, $name2 = null) { $this->backend = $backend; - $this->name1 = $name1; - $this->name2 = $name2; + $this->name1 = $name1; + $this->name2 = $name2; + + if ($name1 && $name2) { + $this->type = 2; + } elseif ($name1 && !$name2) { + $this->type = 1; + } + $this->properties = (array) $this->fetchObject(); } @@ -125,7 +132,7 @@ abstract class AbstractObject 'comment_author', 'comment_data', 'comment_type', - )) + ))->where('comment_objecttype_id', $this->type) )->fetchAll(); return $this; } diff --git a/modules/monitoring/library/Monitoring/View/CommentView.php b/modules/monitoring/library/Monitoring/View/CommentView.php deleted file mode 100644 index 3a87c6a76..000000000 --- a/modules/monitoring/library/Monitoring/View/CommentView.php +++ /dev/null @@ -1,26 +0,0 @@ - array( - 'default_dir' => self::SORT_DESC - ) - ); -} From d109f370cd8e3e0b64b71ee62a1260be00f946c4 Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Fri, 27 Sep 2013 11:50:16 +0200 Subject: [PATCH 2/5] ellipsisText: Implement ellipsis component for text content Use by comment overview. refs #4714 --- .../views/scripts/list/comments.phtml | 4 +- public/js/icinga/components/ellipsisText.js | 99 +++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 public/js/icinga/components/ellipsisText.js diff --git a/modules/monitoring/application/views/scripts/list/comments.phtml b/modules/monitoring/application/views/scripts/list/comments.phtml index e9bec93b7..196d516c9 100644 --- a/modules/monitoring/application/views/scripts/list/comments.phtml +++ b/modules/monitoring/application/views/scripts/list/comments.phtml @@ -86,7 +86,9 @@ $viewHelper = $this->getHelper('MonitoringState'); comment_author; ?> + comment_data; ?> + comment_is_persistent === '1') ? 'Yes' : 'No'; ?> @@ -99,7 +101,7 @@ $viewHelper = $this->getHelper('MonitoringState'); ?> - COMMAND!!!DELETE_COMMENT + {{{REMOVE_ICON}}} diff --git a/public/js/icinga/components/ellipsisText.js b/public/js/icinga/components/ellipsisText.js new file mode 100644 index 000000000..781eed784 --- /dev/null +++ b/public/js/icinga/components/ellipsisText.js @@ -0,0 +1,99 @@ +/*global Icinga:false, Modernizr: false, document: false, History: false, define:false require:false base_url:false console:false */ +// {{{ICINGA_LICENSE_HEADER}}} +/** + * This file is part of Icinga 2 Web. + * + * Icinga 2 Web - Head for multiple monitoring backends. + * Copyright (C) 2013 Icinga Development Team + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * @copyright 2013 Icinga Development Team + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +/** + * Icinga app/ellipsisText component + * + * This component adds ellipsis with expand functionality + * to content. + * + * Example: + * + *
+ * 
+ *   
+ *     A very long example text
+ *    
+ * 
+ * 
+ */ +define(['jquery'], + function($, logger, componentLoader, URI) { + "use strict"; + + /** + * Test if a css3 ellipsis is avtive + * + * @param {Element} element + * @returns {boolean} + */ + var activeEllipsis = function(element) { + return (element.offsetWidth < element.scrollWidth); + }; + + /** + * Add classes to element to create a css3 ellipsis + * + * Parent elements width is used to calculate containing width + * and set target element width to a fixed one. + * + * @param {Element} target + * @constructor + */ + var EllipsisText = function(target) { + var parentWidth = $(target).parent().width(); + + $(target).width(parentWidth) + .css('overflow', 'hidden') + .css('text-overflow', 'ellipsis') + .css('display', 'block') + .css('white-space', 'nowrap'); + + if (activeEllipsis(target)) { + $(target).wrap('') + .css('cursor', 'pointer'); + + $(target).parent() + .attr('data-icinga-ellipsistext', 'true') + .attr('data-content', $(target).html()) + .popover({ + trigger : 'manual', + html : true, + placement : 'auto' + }) + .click(function(e) { + e.stopImmediatePropagation(); + $('a[data-icinga-ellipsistext=\'true\']').popover('hide'); + $(e.currentTarget).popover('toggle'); + }); + } + }; + + return EllipsisText; + } +); \ No newline at end of file From d7f6dcb3cec9708435dedd6abf9d8d56f2f40ee5 Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Fri, 27 Sep 2013 13:54:54 +0200 Subject: [PATCH 3/5] Comment Overview: Implement delete comment inline command [wip] refs #4714 --- .../application/views/helpers/CommandForm.php | 35 +++++++++++++++++++ .../views/scripts/list/comments.phtml | 11 +++++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 modules/monitoring/application/views/helpers/CommandForm.php diff --git a/modules/monitoring/application/views/helpers/CommandForm.php b/modules/monitoring/application/views/helpers/CommandForm.php new file mode 100644 index 000000000..7ac6a95b9 --- /dev/null +++ b/modules/monitoring/application/views/helpers/CommandForm.php @@ -0,0 +1,35 @@ +setRequest(Icinga::app()->getFrontController()->getRequest()); + $form->setView($this->view); + $form->setAction($this->view->href('monitoring/command/' . $commandName)); + $form->setSubmitLabel($submitLabel); + return $form->render(); + } +} + +// @codingStandardsIgnoreStop \ No newline at end of file diff --git a/modules/monitoring/application/views/scripts/list/comments.phtml b/modules/monitoring/application/views/scripts/list/comments.phtml index 196d516c9..600738c71 100644 --- a/modules/monitoring/application/views/scripts/list/comments.phtml +++ b/modules/monitoring/application/views/scripts/list/comments.phtml @@ -1,5 +1,6 @@ getHelper('DateFormat'); + $commandHelper = $this->getHelper('CommandForm'); ?> tabs->render($this); ?> @@ -101,7 +102,15 @@ $viewHelper = $this->getHelper('MonitoringState'); ?> - {{{REMOVE_ICON}}} + simpleForm( + 'removecomment', + 'Remove', + array( + 'commentid' => $comment->comment_id + ) + ); + ?> From 0626bb19cbafbf86c09046d2a0b204c6ca12c349 Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Tue, 8 Oct 2013 14:19:14 +0200 Subject: [PATCH 4/5] Implement a simple configurable command resolves #4769 --- .../controllers/CommandController.php | 240 ++++++++++++------ .../application/forms/Command/CommandForm.php | 16 +- .../Command/SingleArgumentCommandForm.php | 149 +++++++++++ .../application/views/helpers/CommandForm.php | 7 +- .../Command/SingleArgumentCommand.php | 157 ++++++++++++ 5 files changed, 482 insertions(+), 87 deletions(-) create mode 100644 modules/monitoring/application/forms/Command/SingleArgumentCommandForm.php create mode 100644 modules/monitoring/library/Monitoring/Command/SingleArgumentCommand.php diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index a86525298..2366116ce 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -27,24 +27,25 @@ */ // {{{ICINGA_LICENSE_HEADER}}} -use \Icinga\Application\Icinga; -use \Icinga\Application\Config; -use \Icinga\Application\Logger; -use \Icinga\Web\Form; -use \Icinga\Web\Controller\ActionController; -use \Icinga\Protocol\Commandpipe\CommandPipe; -use \Icinga\Exception\ConfigurationError; -use \Icinga\Exception\MissingParameterException; -use \Icinga\Module\Monitoring\Backend; -use \Icinga\Module\Monitoring\Form\Command\AcknowledgeForm; -use \Icinga\Module\Monitoring\Form\Command\CommentForm; -use \Icinga\Module\Monitoring\Form\Command\CommandForm; -use \Icinga\Module\Monitoring\Form\Command\CommandWithIdentifierForm; -use \Icinga\Module\Monitoring\Form\Command\CustomNotificationForm; -use \Icinga\Module\Monitoring\Form\Command\DelayNotificationForm; -use \Icinga\Module\Monitoring\Form\Command\RescheduleNextCheckForm; -use \Icinga\Module\Monitoring\Form\Command\ScheduleDowntimeForm; -use \Icinga\Module\Monitoring\Form\Command\SubmitPassiveCheckResultForm; +use Icinga\Application\Icinga; +use Icinga\Application\Config; +use Icinga\Application\Logger; +use Icinga\Module\Monitoring\Form\Command\SingleArgumentCommandForm; +use Icinga\Web\Form; +use Icinga\Web\Controller\ActionController; +use Icinga\Protocol\Commandpipe\CommandPipe; +use Icinga\Exception\ConfigurationError; +use Icinga\Exception\MissingParameterException; +use Icinga\Module\Monitoring\Backend; +use Icinga\Module\Monitoring\Form\Command\AcknowledgeForm; +use Icinga\Module\Monitoring\Form\Command\CommentForm; +use Icinga\Module\Monitoring\Form\Command\CommandForm; +use Icinga\Module\Monitoring\Form\Command\CommandWithIdentifierForm; +use Icinga\Module\Monitoring\Form\Command\CustomNotificationForm; +use Icinga\Module\Monitoring\Form\Command\DelayNotificationForm; +use Icinga\Module\Monitoring\Form\Command\RescheduleNextCheckForm; +use Icinga\Module\Monitoring\Form\Command\ScheduleDowntimeForm; +use Icinga\Module\Monitoring\Form\Command\SubmitPassiveCheckResultForm; /** * Class Monitoring_CommandController @@ -169,8 +170,26 @@ class Monitoring_CommandController extends ActionController $fields[] = "service_description"; $fields[] = "service_state"; } - $query = Backend::getInstance($this->_getParam('backend'))->select()->from("status", $fields); - return $query->applyFilters($filter)->fetchAll(); + + // Implemented manuall search because api is not ready. + // @TODO Implement this using the database api #4663 (mh) + + $query = Backend::createBackend($this->_getParam('backend'))->select()->from("status", $fields); + $data = $query->fetchAll(); + $out = array(); + + foreach ($data as $o) { + $test = (array)$o; + if ($test['host_name'] === $hostname) { + if (!$servicename) { + $out[] = (object) $o; + } elseif ($servicename && strtolower($test['service_description']) === strtolower($servicename)) { + $out[] = (object) $o; + } + } + } + + return $out; } catch (\Exception $e) { Logger::error( "CommandController: SQL Query '%s' failed (message %s) ", @@ -180,6 +199,29 @@ class Monitoring_CommandController extends ActionController } } + /** + * Convert other params into valid command structure + * + * @param array $supported Array of supported parameter names + * @param array $params Parameters from request + * + * @return array Return + */ + private function selectOtherTargets(array $supported, array $params) + { + $others = array_diff_key($supported, array('host' => true, 'service' => true)); + $otherParams = array_intersect_key($params, $others); + $out = array(); + + foreach ($otherParams as $name => $value) { + $data = new stdClass(); + $data->{$name} = $value; + $out[] = $data; + } + + return $out; + } + /** * Displays a list of all commands * @@ -208,25 +250,24 @@ class Monitoring_CommandController extends ActionController */ private function setSupportedParameters(array $supported) { - $given = array_intersect_key(array_flip($supported), $this->getRequest()->getParams()); + $objects = array(); + + $supported = array_flip($supported); + + $given = array_intersect_key($supported, $this->getRequest()->getParams()); + if (empty($given)) { throw new \Exception('Missing parameter, supported: '.implode(', ', $supported)); } - if (isset($given["host"])) { - $this->view->objects = $this->selectCommandTargets(!in_array("service", $supported)); - if (empty($this->view->objects)) { + + if (isset($given['host'])) { + $objects = $this->selectCommandTargets(!in_array("service", $supported)); + if (empty($objects)) { throw new \Exception("No objects found for your command"); } - } elseif (in_array("downtimeid", $supported)) { - $this->view->objects = array(); - $downtimes = $this->getParam("downtimeid"); - if (!is_array($downtimes)) { - $downtimes = array($downtimes); - } - foreach ($downtimes as $downtimeId) { - $this->view->objects[] = (object) array("downtime_id" => $downtimeId); - } } + + $this->view->objects = $objects; } // ------------------------------------------------------------------------ @@ -239,14 +280,16 @@ class Monitoring_CommandController extends ActionController public function disableactivechecksAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); + $form->setCommand('DISABLE_HOST_CHECK', 'DISABLE_SVC_CHECK'); + $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable Active Checks')); $form->addNote(t('Disable active checks for this object.')); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->disableActiveChecks($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -256,14 +299,16 @@ class Monitoring_CommandController extends ActionController public function enableactivechecksAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); + $form->setCommand('ENABLE_HOST_CHECK', 'ENABLE_SVC_CHECK'); + $form->setRequest($this->getRequest()); - $form->setSubmitLabel(t('Enable active checks')); + $form->setSubmitLabel(t('Enable Active Checks')); $form->addNote(t('Enable active checks for this object.')); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->enableActiveChecks($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -312,14 +357,15 @@ class Monitoring_CommandController extends ActionController public function stopobsessingAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Stop obsessing')); $form->addNote(t('Stop obsessing over this object.')); + $form->setCommand('STOP_OBSESSING_OVER_HOST', 'STOP_OBSESSING_OVER_SVC'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->stopObsessing($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -329,14 +375,15 @@ class Monitoring_CommandController extends ActionController public function startobsessingAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Start obsessing')); $form->addNote(t('Start obsessing over this object.')); + $form->setCommand('START_OBSESSING_OVER_HOST', 'START_OBSESSING_OVER_SVC'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->startObsessing($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -346,14 +393,15 @@ class Monitoring_CommandController extends ActionController public function stopacceptingpassivechecksAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Stop Accepting Passive Checks')); $form->addNote(t('Passive checks for this object will be omitted.')); + $form->setCommand('STOP_ACCEPTING_PASSIVE_HOST_CHECKS', 'STOP_ACCEPTING_PASSIVE_SVC_CHECKS'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->disablePassiveChecks($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -363,14 +411,15 @@ class Monitoring_CommandController extends ActionController public function startacceptingpassivechecksAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Start Accepting Passive Checks')); $form->addNote(t('Passive checks for this object will be accepted.')); + $form->setCommand('START_ACCEPTING_PASSIVE_HOST_CHECKS', 'START_ACCEPTING_PASSIVE_SVC_CHECKS'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->enableActiveChecks($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -380,14 +429,15 @@ class Monitoring_CommandController extends ActionController public function disablenotificationsAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable Notifications')); $form->addNote(t('Notifications for this object will be disabled.')); + $form->setCommand('DISABLE_HOST_NOTIFICATIONS', 'DISABLE_SVC_NOTIFICATIONS'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->disableNotifications($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -396,14 +446,16 @@ class Monitoring_CommandController extends ActionController */ public function enablenotificationsAction() { - $form = new CommandForm(); + $this->setSupportedParameters(array('host', 'service')); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable Notifications')); $form->addNote(t('Notifications for this object will be enabled.')); + $form->setCommand('ENABLE_HOST_NOTIFICATIONS', 'ENABLE_SVC_NOTIFICATIONS'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->enableNotifications($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -462,14 +514,15 @@ class Monitoring_CommandController extends ActionController public function removedowntimeswithchildrenAction() { $this->setSupportedParameters(array('host')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Remove Downtime(s)')); $form->addNote(t('Remove downtime(s) from this host and its services.')); + $form->setCommand('DEL_DOWNTIME_BY_HOST_NAME', 'DEL_DOWNTIME_BY_HOST_NAME'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->removeDowntime($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -479,15 +532,18 @@ class Monitoring_CommandController extends ActionController public function disablenotificationswithchildrenAction() { $this->setSupportedParameters(array('host')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable Notifications')); $form->addNote(t('Notifications for this host and its services will be disabled.')); + $form->setCommand('DISABLE_ALL_NOTIFICATIONS_BEYOND_HOST'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->disableNotifications($this->view->objects); - $this->target->disableNotificationsForServices($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); + $form->setCommand('DISABLE_HOST_NOTIFICATIONS', 'DISABLE_SVC_NOTIFICATIONS'); + $this->target->sendCommand($form->createCommand(), $this->view->objects); + } } @@ -497,15 +553,17 @@ class Monitoring_CommandController extends ActionController public function enablenotificationswithchildrenAction() { $this->setSupportedParameters(array('host')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable Notifications')); $form->addNote(t('Notifications for this host and its services will be enabled.')); + $form->setCommand('ENABLE_ALL_NOTIFICATIONS_BEYOND_HOST'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->enableNotifications($this->view->objects); - $this->target->enableNotificationsForServices($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); + $form->setCommand('ENABLE_HOST_NOTIFICATIONS', 'ENABLE_SVC_NOTIFICATIONS'); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -533,15 +591,16 @@ class Monitoring_CommandController extends ActionController public function disableactivecheckswithchildrenAction() { $this->setSupportedParameters(array('host')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable Active Checks')); $form->addNote(t('Disable active checks for this host and its services.')); + $form->setCommand('DISABLE_HOST_CHECK'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->disableActiveChecks($this->view->objects); - $this->target->disableActiveChecksWithChildren($this->view->objects); + // @TODO(mh): Missing child command + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -551,15 +610,16 @@ class Monitoring_CommandController extends ActionController public function enableactivecheckswithchildrenAction() { $this->setSupportedParameters(array('host')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable Active Checks')); $form->addNote(t('Enable active checks for this host and its services.')); + $form->setCommand('ENABLE_HOST_CHECK'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->enableActiveChecks($this->view->objects); - $this->target->enableActiveChecksWithChildren($this->view->objects); + // @TODO(mh): Missing child command + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -569,14 +629,15 @@ class Monitoring_CommandController extends ActionController public function disableeventhandlerAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable Event Handler')); $form->addNote(t('Disable event handler for this object.')); + $form->setCommand('DISABLE_HOST_EVENT_HANDLER', 'DISABLE_SVC_EVENT_HANDLER'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->disableEventHandler($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -586,14 +647,15 @@ class Monitoring_CommandController extends ActionController public function enableeventhandlerAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable Event Handler')); $form->addNote(t('Enable event handler for this object.')); + $form->setCommand('ENABLE_HOST_EVENT_HANDLER', 'ENABLE_SVC_EVENT_HANDLER'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->enableEventHandler($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -603,14 +665,15 @@ class Monitoring_CommandController extends ActionController public function disableflapdetectionAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable Flapping Detection')); $form->addNote(t('Disable flapping detection for this object.')); + $form->setCommand('DISABLE_HOST_FLAP_DETECTION', 'DISABLE_SVC_FLAP_DETECTION'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->disableFlappingDetection($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -620,14 +683,15 @@ class Monitoring_CommandController extends ActionController public function enableflapdetectionAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable Flapping Detection')); $form->addNote(t('Enable flapping detection for this object.')); + $form->setCommand('ENABLE_HOST_FLAP_DETECTION', 'ENABLE_SVC_FLAP_DETECTION'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->enableFlappingDetection($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -638,7 +702,7 @@ class Monitoring_CommandController extends ActionController { $this->setSupportedParameters(array('host', 'service')); $form = new CommentForm(); - $form->setRequest($this->getRequest()); + $form->setRequest($this->_request); $this->setForm($form); @@ -647,20 +711,41 @@ class Monitoring_CommandController extends ActionController } } + /** + * Remove a single comment + */ + public function removecommentAction() + { + $this->setSupportedParameters(array('commentid', 'host', 'service')); + $form = new SingleArgumentCommandForm(); + $form->setRequest($this->_request); + $form->setCommand('DEL_HOST_COMMENT', 'DEL_SERVICE_COMMENT'); + $form->setParameterName('commentid'); + $form->setSubmitLabel(t('Remove comment')); + $form->setObjectIgnoreFlag(true); + $this->setForm($form); + + if ($form->IsSubmittedAndValid() === true) { + $this->target->sendCommand($form->createCommand(), $this->view->objects); + } + } + /** * Handle command resetattributes */ public function resetattributesAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Reset Attributes')); $form->addNote(t('Reset modified attributes to its default.')); + $form->setCommand('CHANGE_HOST_MODATTR', 'CHANGE_SVC_MODATTR'); + $form->setParameterValue(0); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->resetAttributes($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } @@ -687,14 +772,15 @@ class Monitoring_CommandController extends ActionController public function removeacknowledgementAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new CommandForm(); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Remove Problem Acknowledgement')); $form->addNote(t('Remove problem acknowledgement for this object.')); + $form->setCommand('REMOVE_HOST_ACKNOWLEDGEMENT', 'REMOVE_SVC_ACKNOWLEDGEMENT'); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->removeAcknowledge($this->view->objects); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } diff --git a/modules/monitoring/application/forms/Command/CommandForm.php b/modules/monitoring/application/forms/Command/CommandForm.php index 10161532d..2f42fc2fd 100644 --- a/modules/monitoring/application/forms/Command/CommandForm.php +++ b/modules/monitoring/application/forms/Command/CommandForm.php @@ -28,14 +28,15 @@ namespace Icinga\Module\Monitoring\Form\Command; -use \Zend_Config; -use \Icinga\Web\Form; -use \Zend_Form_Element_Hidden; +use Zend_Config; +use Zend_Form_Element_Hidden; +use Icinga\Module\Monitoring\Command\AcknowledgeCommand; +use Icinga\Web\Form; /** * Simple confirmation command */ -class CommandForm extends Form +abstract class CommandForm extends Form { /** * Create an instance name containing hidden field @@ -126,4 +127,11 @@ class CommandForm extends Form ) ); } + + /** + * Create command object for CommandPipe protocol + * + * @return AcknowledgeCommand + */ + abstract public function createCommand(); } diff --git a/modules/monitoring/application/forms/Command/SingleArgumentCommandForm.php b/modules/monitoring/application/forms/Command/SingleArgumentCommandForm.php new file mode 100644 index 000000000..6f3aa6afa --- /dev/null +++ b/modules/monitoring/application/forms/Command/SingleArgumentCommandForm.php @@ -0,0 +1,149 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Icinga\Module\Monitoring\Form\Command; + +use \Zend_Form_Element_Hidden; +use Icinga\Module\Monitoring\Command\AcknowledgeCommand; +use Icinga\Module\Monitoring\Command\SingleArgumentCommand; + +/** + * Sending commands to core which only have one value + */ +class SingleArgumentCommandForm extends CommandForm +{ + /** + * Name of host command + * + * @var string + */ + private $hostCommand; + + /** + * Name of service command + * + * @var string + */ + private $serviceCommand; + + /** + * Name of the parameter used as value + * + * @var string + */ + private $parameterName; + + /** + * Value of used parameter + * + * @var mixed + */ + private $parameterValue; + + /** + * Flag to ignore object name + * + * @var bool + */ + private $ignoreObject = false; + + /** + * Set command names + * + * @param string $hostCommand Name of host command + * @param string $serviceCommand Name of service command + */ + public function setCommand($hostCommand, $serviceCommand = null) + { + $this->hostCommand = $hostCommand; + if ($serviceCommand !== null) { + $this->serviceCommand = $serviceCommand; + } + } + + /** + * Use an explicit value to send with command + * + * @param mixed $parameterValue + */ + public function setParameterValue($parameterValue) + { + $this->parameterValue = $parameterValue; + } + + /** + * Use a form field to take the value from + * + * @param string $parameterName + */ + public function setParameterName($parameterName) + { + $this->parameterName = $parameterName; + } + + public function setObjectIgnoreFlag($flag = true) + { + $this->ignoreObject = (bool) $flag; + } + + /** + * + */ + protected function create() + { + if ($this->parameterName) { + $field = new Zend_Form_Element_Hidden($this->parameterName); + $value = $this->getRequest()->getParam($field->getName()); + $field->setValue($value); + $this->addElement($field); + } + parent::create(); + } + + + /** + * Create command object for CommandPipe protocol + * + * @return SingleArgumentCommand + */ + public function createCommand() + { + $command = new SingleArgumentCommand(); + $command->setCommand($this->hostCommand, $this->serviceCommand); + + if ($this->parameterValue !== null) { + $command->setValue($this->parameterValue); + } else { + $command->setValue($this->getValue($this->parameterName)); + } + + $command->setObjectIgnoreFlag($this->ignoreObject); + + return $command; + } +} diff --git a/modules/monitoring/application/views/helpers/CommandForm.php b/modules/monitoring/application/views/helpers/CommandForm.php index 7ac6a95b9..d420c9bf2 100644 --- a/modules/monitoring/application/views/helpers/CommandForm.php +++ b/modules/monitoring/application/views/helpers/CommandForm.php @@ -23,12 +23,7 @@ class Zend_View_Helper_CommandForm extends Zend_View_Helper_Abstract */ public function simpleForm($commandName, $submitLabel, array $arguments = array()) { - $form = new CommandForm(); - $form->setRequest(Icinga::app()->getFrontController()->getRequest()); - $form->setView($this->view); - $form->setAction($this->view->href('monitoring/command/' . $commandName)); - $form->setSubmitLabel($submitLabel); - return $form->render(); + return '{{{COMMAND_FORM}}}'; } } diff --git a/modules/monitoring/library/Monitoring/Command/SingleArgumentCommand.php b/modules/monitoring/library/Monitoring/Command/SingleArgumentCommand.php new file mode 100644 index 000000000..b6f6ae7bb --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/SingleArgumentCommand.php @@ -0,0 +1,157 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Icinga\Module\Monitoring\Command; + +use Icinga\Protocol\Commandpipe\Command; + +/** + * Configurable simple command + */ +class SingleArgumentCommand extends Command +{ + /** + * Value used for this command + * + * @var mixed + */ + private $value; + + /** + * Name of host command + * + * @var string + */ + private $hostCommand; + + /** + * Name of service command + * + * @var string + */ + private $serviceCommand; + + /** + * Ignore host in command string + * + * @var bool + */ + private $ignoreObject = false; + + /** + * Setter for this value + * + * @param mixed $value + */ + public function setValue($value) + { + $this->value = $value; + } + + /** + * Setter for command names + * + * @param string $hostCommand + * @param string $serviceCommand + */ + public function setCommand($hostCommand, $serviceCommand) + { + $this->hostCommand = $hostCommand; + $this->serviceCommand = $serviceCommand; + } + + /** + * Ignore object values upon command creation + * + * @param bool $flag + */ + public function setObjectIgnoreFlag($flag = true) + { + $this->ignoreObject = (bool) $flag; + } + + /** + * Return this command's arguments in the order expected by the actual command definition + * + * @return array + */ + public function getArguments() + { + if ($this->value !== null) { + return array($this->value); + } else { + return array(); + } + } + + /** + * Build the argument string based on objects and arguments + * + * @param array $objectNames + * + * @return string String to append to command + */ + private function getArgumentString(array $objectNames) + { + $data = array(); + if ($this->ignoreObject === true) { + $data = $this->getArguments(); + } else { + $data = array_merge($objectNames, $this->getArguments()); + } + + return implode(';', $data); + } + + /** + * Return the command as a string with the given host being inserted + * + * @param string $hostname The name of the host to insert + * + * @return string The string representation of the command + */ + public function getHostCommand($hostname) + { + return strtoupper($this->hostCommand). ';' . $this->getArgumentString(array($hostname)); + } + + /** + * Return the command as a string with the given host and service being inserted + * + * @param string $hostname The name of the host to insert + * @param string $servicename The name of the service to insert + * + * @return string The string representation of the command + */ + public function getServiceCommand($hostname, $servicename) + { + return strtoupper($this->serviceCommand) + . ';' + . $this->getArgumentString(array($hostname, $servicename)); + } +} From 53d2e34e23c69161319d9f140ce9180616526afa Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Tue, 8 Oct 2013 16:29:28 +0200 Subject: [PATCH 5/5] Finish comment view refs #4714 --- .../controllers/CommandController.php | 12 ++- .../application/views/helpers/CommandForm.php | 21 ++++- .../views/scripts/list/comments.phtml | 24 +++-- .../icinga/components/ajaxPostSubmitForm.js | 92 +++++++++++++++++++ public/js/icinga/components/container.js | 4 +- public/js/icinga/components/mainDetailGrid.js | 5 + 6 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 public/js/icinga/components/ajaxPostSubmitForm.js diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index 2366116ce..e5a4bf09d 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -101,7 +101,17 @@ class Monitoring_CommandController extends ActionController if ($this->form->isSubmittedAndValid()) { $this->_helper->viewRenderer->setNoRender(true); $this->_helper->layout()->disableLayout(); + + $requested = strtolower($this->_request->getHeader('x-requested-with')); + $ajaxRequest = $requested === 'xmlhttprequest' ? true : false; + + if ($this->_request->getHeader('referer') && $ajaxRequest === false) { + $this->redirect($this->_request->getHeader('referer')); + } + + return; } + $this->view->form = $this->form; } parent::postDispatch(); @@ -719,7 +729,7 @@ class Monitoring_CommandController extends ActionController $this->setSupportedParameters(array('commentid', 'host', 'service')); $form = new SingleArgumentCommandForm(); $form->setRequest($this->_request); - $form->setCommand('DEL_HOST_COMMENT', 'DEL_SERVICE_COMMENT'); + $form->setCommand('DEL_HOST_COMMENT', 'DEL_SVC_COMMENT'); $form->setParameterName('commentid'); $form->setSubmitLabel(t('Remove comment')); $form->setObjectIgnoreFlag(true); diff --git a/modules/monitoring/application/views/helpers/CommandForm.php b/modules/monitoring/application/views/helpers/CommandForm.php index d420c9bf2..d2605cbb3 100644 --- a/modules/monitoring/application/views/helpers/CommandForm.php +++ b/modules/monitoring/application/views/helpers/CommandForm.php @@ -4,8 +4,8 @@ // {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}} -use Icinga\Module\Monitoring\Form\Command\CommandForm; use Icinga\Application\Icinga; +use Icinga\Web\Form; /** * Helper to build inline html command forms @@ -15,7 +15,7 @@ class Zend_View_Helper_CommandForm extends Zend_View_Helper_Abstract /** * Creates a simple form without additional input fields * - * @param string $commandName Name of command + * @param string $commandName Name of command (icinga 2 web name) * @param string $submitLabel Label of submit button * @param array $arguments Add parameter as hidden fields * @@ -23,7 +23,22 @@ class Zend_View_Helper_CommandForm extends Zend_View_Helper_Abstract */ public function simpleForm($commandName, $submitLabel, array $arguments = array()) { - return '{{{COMMAND_FORM}}}'; + $form = new Form(); + + $form->setIgnoreChangeDiscarding(true); + $form->setAttrib('data-icinga-component', 'app/ajaxPostSubmitForm'); + + $form->setRequest(Zend_Controller_Front::getInstance()->getRequest()); + $form->setSubmitLabel($submitLabel !== null ? $submitLabel : 'Submit'); + $form->setAction($this->view->href('monitoring/command/' . $commandName)); + + foreach ($arguments as $elementName => $elementValue) { + $hiddenField = new Zend_Form_Element_Hidden($elementName); + $hiddenField->setValue($elementValue); + $form->addElement($hiddenField); + } + + return $form->render(); } } diff --git a/modules/monitoring/application/views/scripts/list/comments.phtml b/modules/monitoring/application/views/scripts/list/comments.phtml index 600738c71..65e76de09 100644 --- a/modules/monitoring/application/views/scripts/list/comments.phtml +++ b/modules/monitoring/application/views/scripts/list/comments.phtml @@ -8,6 +8,8 @@ $viewHelper = $this->getHelper('MonitoringState'); ?> +

Comments

+
sortControl->render($this); ?> paginationControl($comments, null, null, array('preserve' => $this->preserve)); ?> @@ -98,17 +100,24 @@ $viewHelper = $this->getHelper('MonitoringState'); comment_expiration_timestamp) ? $dateHelper->formatDateTime($comment->comment_expiration_timestamp) : - ' '; + 'Never'; ?> - simpleForm( + $comment->comment_id, + 'host' => $comment->host_name + ); + + if ($objectType === 'service') { + $data['service'] = $comment->service_name; + } + + echo $commandHelper->simpleForm( 'removecomment', - 'Remove', - array( - 'commentid' => $comment->comment_id - ) + 'Remove Comment', + $data ); ?> @@ -117,4 +126,5 @@ $viewHelper = $this->getHelper('MonitoringState'); + paginationControl($comments, null, null, array('preserve' => $this->preserve)); ?>
\ No newline at end of file diff --git a/public/js/icinga/components/ajaxPostSubmitForm.js b/public/js/icinga/components/ajaxPostSubmitForm.js new file mode 100644 index 000000000..1547c816a --- /dev/null +++ b/public/js/icinga/components/ajaxPostSubmitForm.js @@ -0,0 +1,92 @@ +// {{{ICINGA_LICENSE_HEADER}}} +/** + * This file is part of Icinga 2 Web. + * + * Icinga 2 Web - Head for multiple monitoring backends. + * Copyright (C) 2013 Icinga Development Team + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * @copyright 2013 Icinga Development Team + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} +/*global Icinga:false define:false require:false base_url:false console:false */ + +/** + * Icinga app/ajaxPostSubmitForm component. + * + * This component converts generic post forms into ajax + * submit forms. + */ +define(['components/app/container', 'jquery'], function(Container, $) { + "use strict"; + + /** + * Returns owner container + * + * @param {Element} targetElement + * @returns {Container} + */ + var getOwnerContainer = function(targetElement) { + var me = new Container(targetElement); + return new Container(me.findNearestContainer(targetElement)); + }; + + /** + * Handler for ajax post submit + * + * @param {Event} e + */ + var submitHandler = function(e) { + var form = $(this); + var url = form.attr('action'); + var submit = form.find('input[type="submit"]'); + var data = form.serialize(); + + e.preventDefault(); + + // Submit name is missing for valid submission + if (data) { + data += '&'; + } + data += submit.attr('name') + '=1'; + + $.ajax({ + url: url, + type: 'POST', + data: data, + beforeSend: function() { + submit.prop('disabled', true); + } + }).done(function() { + var container = getOwnerContainer(form); + container.replaceDomFromUrl(container.getContainerHref()); + }).error(function() { + submit.removeProp('disabled'); + }); + }; + + /** + * The component bootstrap + * + * @param {Element} targetElement + */ + return function(targetForm) { + var form = $(targetForm); + form.submit(submitHandler); + }; +}); \ No newline at end of file diff --git a/public/js/icinga/components/container.js b/public/js/icinga/components/container.js index 84aca00c9..c50173a76 100644 --- a/public/js/icinga/components/container.js +++ b/public/js/icinga/components/container.js @@ -91,7 +91,7 @@ define(['jquery', 'logging', 'icinga/componentLoader', 'URIjs/URI', 'URIjs/URITe * @returns {HTMLElement|null} The nearest container found or null if target is no container * and no container is above target */ - var findNearestContainer = function(target) { + this.findNearestContainer = function(target) { target = $(target); if (target.attr('data-icinga-component') === 'app/container' || target.attr('id') === 'icingamain' || target.attr('id') === 'icingadetail') { @@ -106,7 +106,7 @@ define(['jquery', 'logging', 'icinga/componentLoader', 'URIjs/URI', 'URIjs/URITe * @param {HTMLElement, jQuery, String} target A jQuery resultset, dom element or matcher string */ this.construct = function(target) { - this.containerDom = $(findNearestContainer(target)); + this.containerDom = $(this.findNearestContainer(target)); this.containerType = CONTAINER_TYPES.GENERIC; if (this.containerDom.attr('id') === CONTAINER_TYPES.MAIN) { diff --git a/public/js/icinga/components/mainDetailGrid.js b/public/js/icinga/components/mainDetailGrid.js index 04da06271..2ada235bb 100644 --- a/public/js/icinga/components/mainDetailGrid.js +++ b/public/js/icinga/components/mainDetailGrid.js @@ -130,6 +130,11 @@ function(Container, $, logger, URI) { if (true || Container.isExternalLink(a.attr('href'))) { return true; } + } else if (targetEl.nodeName.toLowerCase() === 'input') { + var type = $(targetEl).attr('type'); + if (type === 'submit') { + return true; + } } Container.getDetailContainer().replaceDomFromUrl($('a', this).attr('href'));