diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index 91ad5940..4d4eb25b 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -38,6 +38,7 @@ before switching to a new version. * FEATURE: Inspect feature got refactored, also for Services (#264, #689, #1396, #1397) * FEATURE: The "Modify" hook is now available for Services (#689), regardless of whether they have been directly assigned, inherited or applied +* FEATURE: Config preview links imports, hosts and commands to related objects (#1521) * FIX: Don't suggest Command templates where Commands are required (#1414) * FIX: Do not allow to delete Commands being used by other objects (#1443) * FIX: Show 'Inspect' tab only for Endpoints with an ApiUser (#1293) diff --git a/library/Director/Web/ObjectPreview.php b/library/Director/Web/ObjectPreview.php index a281648e..d7032dde 100644 --- a/library/Director/Web/ObjectPreview.php +++ b/library/Director/Web/ObjectPreview.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Director\Web; +use dipl\Html\Text; use Icinga\Module\Director\Exception\NestingError; use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Web\Request; @@ -99,7 +100,75 @@ class ObjectPreview $classes[] = 'logfile'; } - $content->add(Html::tag('pre', ['class' => $classes], $file->getContent())); + $plain = Html::wantHtml($file->getContent())->render(); + $plain = preg_replace_callback( + '/^(\s+import\s+\"\;)(.+)(\"\;)/m', + [$this, 'linkImport'], + $plain + ); + $plain = preg_replace_callback( + '/^(\s+(?:check_|event_)?command\s+=\s+\"\;)(.+)(\"\;)/m', + [$this, 'linkCommand'], + $plain + ); + $plain = preg_replace_callback( + '/^(\s+host_name\s+=\s+\"\;)(.+)(\"\;)/m', + [$this, 'linkHost'], + $plain + ); + $text = Text::create($plain)->setEscaped(); + + $content->add(Html::tag('pre', ['class' => $classes], $text)); } } + + /** + * @api internal + * @param $match + * @return string + */ + public function linkImport($match) + { + $blacklist = [ + 'plugin-notification-command', + 'plugin-check-command', + ]; + if (in_array($match[2], $blacklist)) { + return $match[1] . $match[2] . $match[3]; + } + + return $match[1] . Link::create( + $match[2], + sprintf('director/' . $this->object->getShortTableName()), + ['name' => $match[2]] + )->render() . $match[3]; + } + + /** + * @api internal + * @param $match + * @return string + */ + public function linkCommand($match) + { + return $match[1] . Link::create( + $match[2], + sprintf('director/command'), + ['name' => $match[2]] + )->render() . $match[3]; + } + + /** + * @api internal + * @param $match + * @return string + */ + public function linkHost($match) + { + return $match[1] . Link::create( + $match[2], + sprintf('director/host'), + ['name' => $match[2]] + )->render() . $match[3]; + } }