diff --git a/application/controllers/EditController.php b/application/controllers/EditController.php index bf25d15..12f7a28 100644 --- a/application/controllers/EditController.php +++ b/application/controllers/EditController.php @@ -74,8 +74,6 @@ public function indexAction() $view = $c->loadByName($name); } - $view->setFormat($c::FORMAT_YAML); - $this->view->form = $form = new EditForm(); $form->setViewConfig($c); $form->setViews($view); diff --git a/application/forms/EditForm.php b/application/forms/EditForm.php index 5be2cea..3c12f76 100644 --- a/application/forms/EditForm.php +++ b/application/forms/EditForm.php @@ -99,7 +99,7 @@ public function getRedirectUrl() */ public function onRequest() { - $values = array(); + $values = []; $values['name'] = $this->view->getName(); $values['config'] = $this->view->getText(); @@ -124,56 +124,56 @@ public function createElements(array $formData) $this->addElement( 'text', 'name', - array( - 'label' => $this->translate('File name'), + [ + 'label' => $this->translate('File name'), 'required' => true - ) + ] ); $this->addElement( 'textarea', 'config', - array( - 'label' => $this->translate('YAML Config'), - 'class' => 'code-editor codemirror', - 'decorators' => array( - array('Label', array('tag'=>'div', 'separator' => '')), - array('HtmlTag', array('tag' => 'div')), + [ + 'label' => $this->translate('YAML Config'), + 'class' => 'code-editor codemirror', + 'decorators' => [ + ['Label', ['tag' => 'div', 'separator' => '']], + ['HtmlTag', ['tag' => 'div']], 'ViewHelper' - ), + ], 'data-codemirror-mode' => 'yaml' - ) + ] ); $this->addElement( 'submit', 'btn_submit_save_session', - array( + [ 'ignore' => true, 'label' => $this->translate('Save for the current Session'), - 'decorators' => array('ViewHelper') - ) + 'decorators' => ['ViewHelper'] + ] ); $this->addElement( 'submit', 'btn_submit_save_file', - array( + [ 'ignore' => true, 'label' => $this->translate('Save to config file'), - 'decorators' => array('ViewHelper') - ) + 'decorators' => ['ViewHelper'] + ] ); if ($this->view->hasBeenLoadedFromSession()) { $this->addElement( 'submit', 'btn_submit_cancel', - array( + [ 'ignore' => true, 'label' => $this->translate('Cancel editing'), 'class' => 'btn-cancel', - 'decorators' => array('ViewHelper') - ) + 'decorators' => ['ViewHelper'] + ] ); } @@ -181,13 +181,13 @@ public function createElements(array $formData) $this->addElement( 'submit', 'btn_submit_delete', - array( + [ 'ignore' => true, 'label' => $this->translate('Delete config'), 'class' => 'btn-remove', 'onclick' => 'return confirm("' . $this->translate('Confirm deletion') . '")', - 'decorators' => array('ViewHelper') - ) + 'decorators' => ['ViewHelper'] + ] ); } } diff --git a/application/views/helpers/Badges.php b/application/views/helpers/Badges.php index 2bbe5e4..8292769 100644 --- a/application/views/helpers/Badges.php +++ b/application/views/helpers/Badges.php @@ -2,21 +2,13 @@ /* Icinga Web 2 Top Level View | (c) 2017 Icinga Development Team | GPLv2+ */ use Icinga\Module\Toplevelview\Tree\TLVStatus; +use Icinga\Module\Toplevelview\Util\Str; class Zend_View_Helper_Badges extends Zend_View_Helper_Abstract { /** @var \Icinga\Web\View */ public $view; - protected function prettyTitle($identifier) - { - $s = ''; - foreach (preg_split('/[\.\-_\s]+/', $identifier) as $p) { - $s .= ' ' . ucfirst($p); - } - return trim($s); - } - /** * badges renders a TLVNode's TLVStatus into HTML. * A badge represents the number of status of a given node @@ -35,7 +27,7 @@ public function badges(TLVStatus $status, $problemsOnly = true, $showTotal = fal } if ($value !== null && $value > 0) { $values = true; - $title = $value . ' ' . $this->prettyTitle($key); + $title = $value . ' ' . Str::prettyTitle($key); $class = 'tlv-status-tile ' . str_replace('_', ' ', $key); $htm .= sprintf( '
%s
', diff --git a/application/views/helpers/Breadcrumb.php b/application/views/helpers/Breadcrumb.php index 60fccbc..645f86e 100644 --- a/application/views/helpers/Breadcrumb.php +++ b/application/views/helpers/Breadcrumb.php @@ -23,10 +23,10 @@ public function breadcrumb($breadcrumb, $config_name) $htm .= '
  • ' . $this->view->qlink( Str::limit($crumb->getTitle()), 'toplevelview/show/tree', - array( - 'name' => $config_name, - 'id' => $crumb->getFullId() - ) + [ + 'name' => $config_name, + 'id' => $crumb->getFullId() + ] ) . '
  • '; } $htm .= ''; diff --git a/application/views/helpers/Tiles.php b/application/views/helpers/Tiles.php index 73b80a8..575677c 100644 --- a/application/views/helpers/Tiles.php +++ b/application/views/helpers/Tiles.php @@ -22,7 +22,7 @@ public function tiles(TLVTreeNode $node, $levels = 2, $classes = array()) } else { $statusClass = 'tlv-status-tile'; } - $statusClasses = array($statusClass, $status->getOverall()); + $statusClasses = [$statusClass, $status->getOverall()]; $htm .= sprintf( '
    ' . "\n", @@ -34,13 +34,13 @@ public function tiles(TLVTreeNode $node, $levels = 2, $classes = array()) $htm .= $this->view->qlink( $title . $badges, 'toplevelview/show/tree', - array( + [ 'name' => $node->getRoot()->getViewName(), 'id' => $node->getFullId() - ), - array( + ], + [ 'class' => 'tlv-tile-title' - ), + ], false ); diff --git a/application/views/helpers/Tree.php b/application/views/helpers/Tree.php index 85ee47b..4cc8720 100644 --- a/application/views/helpers/Tree.php +++ b/application/views/helpers/Tree.php @@ -41,19 +41,19 @@ public function tree(TLVTreeNode $node, $classes = array(), $level = 0) $icon = 'services'; $url = Url::fromPath( 'icingadb/servicegroup', - array( + [ 'name' => $node->get('servicegroup'), 'sort' => 'service.state.severity desc' - ) + ] ); } elseif ($type === 'hostgroup') { $icon = 'cubes'; $url = Url::fromPath( 'icingadb/services', - array( + [ 'hostgroup.name' => $node->get('hostgroup'), 'sort' => 'service.state.severity desc' - ) + ] ); if (($h = $status->getMeta('hosts_unhandled')) > 0) { @@ -68,10 +68,10 @@ public function tree(TLVTreeNode $node, $classes = array(), $level = 0) $htmExtra .= ' ' . $this->view->qlink( $hostTitle, 'icingadb/hosts', - array( + [ 'hostgroup.name' => $node->get('hostgroup'), 'sort' => 'service.state.severity desc' - ), + ], null, false ); @@ -79,10 +79,10 @@ public function tree(TLVTreeNode $node, $classes = array(), $level = 0) $icon = null; $url = Url::fromPath( 'toplevelview/show/tree', - array( + [ 'name' => $node->getRoot()->getViewName(), 'id' => $node->getFullId() - ) + ] ); } diff --git a/application/views/scripts/index/index.phtml b/application/views/scripts/index/index.phtml index d47ae90..5da3470 100644 --- a/application/views/scripts/index/index.phtml +++ b/application/views/scripts/index/index.phtml @@ -7,11 +7,11 @@ if (! $this->compact): ?> qlink( $this->translate('Add'), 'toplevelview/edit/add', - array(), - array( + [], + [ 'class' => 'action-link', 'icon' => 'plus', - ) + ] ) ?>
    @@ -21,7 +21,7 @@ if (! $this->compact): ?>
    $view): - $url = $this->url('toplevelview/show', array('name' => $name)); + $url = $this->url('toplevelview/show', ['name' => $name]); ?>
    getMeta('name') ?>
    diff --git a/application/views/scripts/show/actions.phtml b/application/views/scripts/show/actions.phtml index a94a438..b3e231a 100644 --- a/application/views/scripts/show/actions.phtml +++ b/application/views/scripts/show/actions.phtml @@ -7,49 +7,49 @@ echo $this->qlink( $this->translate('Source'), 'toplevelview/show/source', - array('name' => $view->getName()), - array( + ['name' => $view->getName()], + [ 'class' => 'action-link', 'icon' => 'doc-text', 'data-base-target' => '_next' - ) + ] ); echo $this->qlink( $this->translate('Fullscreen'), 'toplevelview/show', - array( + [ 'name' => $view->getName(), 'view' => 'compact', 'showFullscreen' => true - ), - array( - 'class' => 'action-link', - 'icon' => 'resize-full', + ], + [ + 'class' => 'action-link', + 'icon' => 'resize-full', 'target' => '_blank' - ) + ] ); if ($this->hasPermission('toplevelview/edit')) { echo $this->qlink( $this->translate('Edit'), 'toplevelview/edit', - array('name' => $view->getName()), - array( + ['name' => $view->getName()], + [ 'class' => 'action-link', 'icon' => 'edit', 'data-base-target' => '_next' - ) + ] ); echo $this->qlink( $this->translate('Clone'), 'toplevelview/edit/clone', - array('name' => $view->getName()), - array( + ['name' => $view->getName()], + [ 'class' => 'action-link', 'icon' => 'rewind', 'data-base-target' => '_next' - ) + ] ); } } diff --git a/application/views/scripts/show/index.phtml b/application/views/scripts/show/index.phtml index 811aeca..b2d6970 100644 --- a/application/views/scripts/show/index.phtml +++ b/application/views/scripts/show/index.phtml @@ -1,8 +1,6 @@ getTree(); if (! $this->compact): diff --git a/application/views/scripts/show/tree.phtml b/application/views/scripts/show/tree.phtml index ed503a3..7c622d0 100644 --- a/application/views/scripts/show/tree.phtml +++ b/application/views/scripts/show/tree.phtml @@ -1,8 +1,6 @@ getTree(); diff --git a/application/views/scripts/text/index.phtml b/application/views/scripts/text/index.phtml index 3aaf6d0..7396bf6 100644 --- a/application/views/scripts/text/index.phtml +++ b/application/views/scripts/text/index.phtml @@ -1,5 +1,4 @@ compact): ?>
    diff --git a/library/Toplevelview/Model/View.php b/library/Toplevelview/Model/View.php index bb3d90b..2a65516 100644 --- a/library/Toplevelview/Model/View.php +++ b/library/Toplevelview/Model/View.php @@ -200,17 +200,6 @@ public function getTextChecksum(): string return $this->textChecksum; } - /** - * setFormat sets the format for this View - * @param string $format Format for this view (e.g. 'yml') - * @return $this - */ - public function setFormat($format) - { - $this->format = $format; - return $this; - } - /** * getFormat returns the View's format */ diff --git a/library/Toplevelview/Tree/TLVStatus.php b/library/Toplevelview/Tree/TLVStatus.php index 4ef43e5..a39f033 100644 --- a/library/Toplevelview/Tree/TLVStatus.php +++ b/library/Toplevelview/Tree/TLVStatus.php @@ -8,7 +8,10 @@ */ class TLVStatus { - protected $properties = array( + /** + * Properties track each tree nodes Icinga states + */ + protected $properties = [ 'critical_unhandled' => null, 'critical_handled' => null, 'warning_unhandled' => null, @@ -20,9 +23,12 @@ class TLVStatus 'ok' => null, 'missing' => null, 'total' => null, - ); + ]; - protected static $statusPriority = array( + /** + * statusPriority decribes the priority from worst to best + */ + protected static $statusPriority = [ 'critical_unhandled', 'warning_unhandled', 'unknown_unhandled', @@ -32,10 +38,16 @@ class TLVStatus 'ok', 'downtime_handled', 'missing', - ); + ]; - protected $meta = array(); + /** + * meta tracks get overall count of hosts and services if this status object + */ + protected $meta = []; + /** + * merge merges another TLVStatus object's properties into this object + */ public function merge(TLVStatus $status) { $properties = $status->getProperties(); @@ -49,22 +61,42 @@ public function merge(TLVStatus $status) return $this; } + /** + * get returns the given key's value from the properties + * + * @param string $key key of the property + */ public function get($key) { return $this->properties[$key]; } + /** + * set sets the given key/value in the properties + * + * @param string $key key of the property + * @param int $value value to set to property to + */ public function set($key, $value) { $this->properties[$key] = (int) $value; return $this; } + /** + * getProperties returns all properties + */ public function getProperties() { return $this->properties; } + /** + * add adds the given value (integer) to the given property + * + * @param string $key key of the property + * @param int $value value to add to the property + */ public function add($key, $value = 1) { if ($this->properties[$key] === null) { @@ -74,6 +106,9 @@ public function add($key, $value = 1) return $this; } + /** + * zero sets all properties to zero (0) + */ public function zero() { foreach (array_keys($this->properties) as $key) { @@ -82,7 +117,13 @@ public function zero() return $this; } - public function getOverall() + /** + * getOverall returns the worst state of this TLVStatus, + * given the statusPriority. + * + * @return string + */ + public function getOverall(): string { foreach (static::$statusPriority as $key) { if ($this->properties[$key] !== null && $this->properties[$key] > 0) { @@ -92,11 +133,18 @@ public function getOverall() return 'missing'; } + /** + * cssFriendly transforms the given key to be CSS friendly, + * meaning using spaces between the state and the handled indicator + */ protected function cssFriendly($key): string { return str_replace('_', ' ', $key); } + /** + * getMeta returns the given key's value from the metadata + */ public function getMeta($key) { if (array_key_exists($key, $this->meta)) { @@ -106,11 +154,9 @@ public function getMeta($key) } } - public function getAllMeta() - { - return $this->meta; - } - + /** + * setMeta sets the given key/value in the metadata + */ public function setMeta($key, $value) { $this->meta[$key] = $value; diff --git a/library/Toplevelview/Tree/TLVTree.php b/library/Toplevelview/Tree/TLVTree.php index 012e9be..fc6ebd6 100644 --- a/library/Toplevelview/Tree/TLVTree.php +++ b/library/Toplevelview/Tree/TLVTree.php @@ -69,7 +69,7 @@ public function getById($id) return $currentNode; } - public function getViewName(): string + public function getViewName(): ?string { return $this->viewName; } @@ -80,7 +80,7 @@ public function setViewName(string $name) return $this; } - public function getViewChecksum(): string + public function getViewChecksum(): ?string { return $this->viewChecksum; } diff --git a/library/Toplevelview/Tree/TLVTreeNode.php b/library/Toplevelview/Tree/TLVTreeNode.php index 395bfee..bd2ec28 100644 --- a/library/Toplevelview/Tree/TLVTreeNode.php +++ b/library/Toplevelview/Tree/TLVTreeNode.php @@ -70,12 +70,12 @@ class TLVTreeNode extends TreeNode * * @var array */ - protected static $typeMap = array( + protected static $typeMap = [ 'host' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVHostNode', 'service' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVServiceNode', 'hostgroup' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVHostGroupNode', 'servicegroup' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVServiceGroupNode', - ); + ]; /** * Mapping keys to a type @@ -84,12 +84,12 @@ class TLVTreeNode extends TreeNode * * @var array */ - protected static $typeKeyMap = array( - 'service' => array('host', 'service'), + protected static $typeKeyMap = [ + 'service' => ['host', 'service'], 'host' => 'host', 'hostgroup' => 'hostgroup', 'servicegroup' => 'servicegroup', - ); + ]; /** * @param $array @@ -116,7 +116,7 @@ public static function fromArray($array, TLVTreeNode $parent = null, TLVTree $ro if (! array_key_exists('type', $array)) { foreach (self::$typeKeyMap as $type => $keys) { if (! is_array($keys)) { - $keys = array($keys); + $keys = [$keys]; } $matched = false; foreach ($keys as $k) { diff --git a/library/Toplevelview/Util/Str.php b/library/Toplevelview/Util/Str.php index ce93041..0560bb2 100644 --- a/library/Toplevelview/Util/Str.php +++ b/library/Toplevelview/Util/Str.php @@ -32,4 +32,20 @@ public static function limit($str, $len = 25, $end = '...'): string // and add the given end to it. return mb_strimwidth($str, 0, $len, '', 'UTF-8') . $end; } + + /** + * Transforms the title badge title "warning_unhandled" to "Warning Unhandled" + * + * @param string $identifier + * + * @return string + */ + public static function prettyTitle($identifier): string + { + $s = ''; + foreach (explode('_', $identifier) as $p) { + $s .= ' ' . ucfirst($p); + } + return trim($s); + } } diff --git a/test/php/library/Toplevelview/Tree/TLVStatusTest.php b/test/php/library/Toplevelview/Tree/TLVStatusTest.php index 0f976ca..6cc0ba2 100644 --- a/test/php/library/Toplevelview/Tree/TLVStatusTest.php +++ b/test/php/library/Toplevelview/Tree/TLVStatusTest.php @@ -27,11 +27,21 @@ public function testGetOverall() $this->assertSame(0, $t->get('missing')); } + public function testGetOverallWithMissing() + { + $t = new TLVStatus(); + $this->assertSame('missing', $t->getOverall()); + } + public function testGetterSetter() { $t = new TLVStatus(); $t->set('missing', 123); $this->assertSame(123, $t->get('missing')); + + $t->setMeta('hosts_total', 321); + $this->assertSame(321, $t->getMeta('hosts_total')); + $this->assertSame(null, $t->getMeta('services_total')); } public function testMerge() diff --git a/test/php/library/Toplevelview/Util/StrTest.php b/test/php/library/Toplevelview/Util/StrTest.php index cb3fe6b..6106115 100644 --- a/test/php/library/Toplevelview/Util/StrTest.php +++ b/test/php/library/Toplevelview/Util/StrTest.php @@ -41,4 +41,14 @@ public function testLimitWithLongerStringAndSpecificEnd() Str::limit('Кто это читает, тот дурак', 1, ' (🦔🦔🦔)') ); } + + public function testPrettyPrint() + { + $this->assertSame('Critical Unhandled', Str::prettyTitle('critical_unhandled')); + $this->assertSame('Warning Handled', Str::prettyTitle('warning_handled')); + $this->assertSame('Foo Bar', Str::prettyTitle('foo_bar')); + $this->assertSame('XXX YYY', Str::prettyTitle('XXX_YYY')); + $this->assertSame('', Str::prettyTitle('')); + $this->assertSame('Ok', Str::prettyTitle('ok')); + } }