2017-08-14 12:40:02 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web;
|
|
|
|
|
2019-09-06 09:54:03 +02:00
|
|
|
use dipl\Html\Text;
|
2017-08-14 12:40:02 +02:00
|
|
|
use Icinga\Module\Director\Exception\NestingError;
|
|
|
|
use Icinga\Module\Director\Objects\IcingaObject;
|
|
|
|
use Icinga\Web\Request;
|
2019-09-06 09:54:03 +02:00
|
|
|
use dipl\Html\Html;
|
|
|
|
use dipl\Html\Link;
|
|
|
|
use dipl\Translation\TranslationHelper;
|
|
|
|
use dipl\Web\Widget\ControlsAndContent;
|
2017-08-14 12:40:02 +02:00
|
|
|
|
|
|
|
class ObjectPreview
|
|
|
|
{
|
|
|
|
use TranslationHelper;
|
|
|
|
|
|
|
|
/** @var IcingaObject */
|
|
|
|
protected $object;
|
|
|
|
|
|
|
|
/** @var Request */
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
public function __construct(IcingaObject $object, Request $request)
|
|
|
|
{
|
|
|
|
$this->object = $object;
|
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
|
2018-05-08 19:54:00 +02:00
|
|
|
/**
|
|
|
|
* @param ControlsAndContent $cc
|
2018-10-15 14:49:47 +02:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2018-05-08 19:54:00 +02:00
|
|
|
*/
|
2017-08-14 12:40:02 +02:00
|
|
|
public function renderTo(ControlsAndContent $cc)
|
|
|
|
{
|
|
|
|
$object = $this->object;
|
|
|
|
$url = $this->request->getUrl();
|
|
|
|
$params = $url->getParams();
|
|
|
|
$cc->addTitle(
|
|
|
|
$this->translate('Config preview: %s'),
|
|
|
|
$object->getObjectName()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($params->shift('resolved')) {
|
|
|
|
$object = $object::fromPlainObject(
|
|
|
|
$object->toPlainObject(true),
|
|
|
|
$object->getConnection()
|
|
|
|
);
|
|
|
|
|
|
|
|
$cc->actions()->add(Link::create(
|
|
|
|
$this->translate('Show normal'),
|
|
|
|
$url->without('resolved'),
|
|
|
|
null,
|
|
|
|
['class' => 'icon-resize-small state-warning']
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
if ($object->supportsImports() && $object->imports()->count() > 0) {
|
|
|
|
$cc->actions()->add(Link::create(
|
|
|
|
$this->translate('Show resolved'),
|
|
|
|
$url->with('resolved', true),
|
|
|
|
null,
|
|
|
|
['class' => 'icon-resize-full']
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} catch (NestingError $e) {
|
|
|
|
// No resolve link with nesting errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $cc->content();
|
|
|
|
if ($object->isDisabled()) {
|
2018-05-08 19:54:00 +02:00
|
|
|
$content->add(Html::tag(
|
|
|
|
'p',
|
2017-08-14 12:40:02 +02:00
|
|
|
['class' => 'error'],
|
|
|
|
$this->translate('This object will not be deployed as it has been disabled')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if ($object->isExternal()) {
|
2018-05-08 19:54:00 +02:00
|
|
|
$content->add(Html::tag('p', null, $this->translate((
|
2017-08-14 12:40:02 +02:00
|
|
|
'This is an external object. It has been imported from Icinga 2 through the'
|
|
|
|
. ' Core API and cannot be managed with the Icinga Director. It is however'
|
|
|
|
. ' perfectly valid to create objects using this or referring to this object.'
|
|
|
|
. ' You might also want to define related Fields to make work based on this'
|
|
|
|
. ' object more enjoyable.'
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
$config = $object->toSingleIcingaConfig();
|
|
|
|
|
|
|
|
foreach ($config->getFiles() as $filename => $file) {
|
|
|
|
if (! $object->isExternal()) {
|
2018-05-08 19:54:00 +02:00
|
|
|
$content->add(Html::tag('h2', null, $filename));
|
2017-08-14 12:40:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$classes = array();
|
|
|
|
if ($object->isDisabled()) {
|
|
|
|
$classes[] = 'disabled';
|
|
|
|
} elseif ($object->isExternal()) {
|
|
|
|
$classes[] = 'logfile';
|
|
|
|
}
|
|
|
|
|
2019-03-26 11:58:06 +01:00
|
|
|
$type = $object->getShortTableName();
|
|
|
|
|
2018-06-12 21:21:32 +02:00
|
|
|
$plain = Html::wantHtml($file->getContent())->render();
|
|
|
|
$plain = preg_replace_callback(
|
|
|
|
'/^(\s+import\s+\"\;)(.+)(\"\;)/m',
|
|
|
|
[$this, 'linkImport'],
|
|
|
|
$plain
|
|
|
|
);
|
2019-03-26 11:58:06 +01:00
|
|
|
|
|
|
|
if ($type !== 'command') {
|
|
|
|
$plain = preg_replace_callback(
|
|
|
|
'/^(\s+(?:check_|event_)?command\s+=\s+\"\;)(.+)(\"\;)/m',
|
|
|
|
[$this, 'linkCommand'],
|
|
|
|
$plain
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-12 21:21:32 +02:00
|
|
|
$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));
|
2017-08-14 12:40:02 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-12 21:21:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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];
|
|
|
|
}
|
2017-08-14 12:40:02 +02:00
|
|
|
}
|