2014-03-17 17:14:16 +01:00
|
|
|
<?php
|
2015-02-03 15:49:34 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | http://www.gnu.org/licenses/gpl-2.0.txt */
|
2014-03-17 17:14:16 +01:00
|
|
|
|
|
|
|
namespace Icinga\Web\View;
|
|
|
|
|
|
|
|
use Icinga\Web\Url;
|
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
|
|
|
|
$view = $this;
|
|
|
|
|
|
|
|
$this->addHelperFunction('href', function ($path = null, $params = null) use ($view) {
|
|
|
|
return $view->url($path, $params);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->addHelperFunction('url', function ($path = null, $params = null) {
|
|
|
|
if ($path === null) {
|
|
|
|
$url = Url::fromRequest();
|
|
|
|
} elseif ($path instanceof Url) {
|
|
|
|
$url = $path;
|
|
|
|
} else {
|
|
|
|
$url = Url::fromPath($path);
|
|
|
|
}
|
|
|
|
if ($params !== null) {
|
2014-09-05 17:56:55 +02:00
|
|
|
$url->overwriteParams($params);
|
2014-03-17 17:14:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$this->addHelperFunction('qlink', function ($title, $url, $params = null, $properties = array()) use ($view) {
|
|
|
|
return sprintf(
|
|
|
|
'<a href="%s"%s>%s</a>',
|
|
|
|
$view->url($url, $params),
|
|
|
|
$view->propertiesToString($properties),
|
|
|
|
$view->escape($title)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->addHelperFunction('img', function ($url, array $properties = array()) use ($view) {
|
|
|
|
if (! array_key_exists('alt', $properties)) {
|
|
|
|
$properties['alt'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
'<img src="%s"%s />',
|
|
|
|
$view->url($url),
|
|
|
|
$view->propertiesToString($properties)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->addHelperFunction('icon', function ($img, $title = null, array $properties = array()) use ($view) {
|
2014-11-13 15:17:36 +01:00
|
|
|
$isClass = strpos($img, '.') === false;
|
|
|
|
$class = null;
|
|
|
|
|
|
|
|
if ($isClass) {
|
|
|
|
$class = 'icon-' . $img;
|
|
|
|
} else {
|
|
|
|
$class = 'icon';
|
|
|
|
}
|
2014-03-17 17:14:16 +01:00
|
|
|
if ($title !== null) {
|
2014-11-13 15:17:36 +01:00
|
|
|
$properties['alt'] = $title;
|
|
|
|
$properties['title'] = $title;
|
2014-03-17 17:14:16 +01:00
|
|
|
}
|
2015-02-03 15:49:34 +01:00
|
|
|
|
2014-11-13 15:17:36 +01:00
|
|
|
if ($class !== null) {
|
|
|
|
if (isset($props['class'])) {
|
|
|
|
$properties['class'] .= ' ' . $class;
|
|
|
|
} else {
|
|
|
|
$properties['class'] = $class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($isClass) {
|
|
|
|
return sprintf('<i %s ></i>', $view->propertiesToString($properties));
|
2014-03-17 17:14:16 +01:00
|
|
|
|
2014-11-13 15:17:36 +01:00
|
|
|
} else {
|
|
|
|
return $view->img('img/icons/' . $img, $properties);
|
|
|
|
}
|
2014-03-17 17:14:16 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->addHelperFunction('propertiesToString', function ($properties) use ($view) {
|
|
|
|
if (empty($properties)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$attributes = array();
|
|
|
|
|
|
|
|
foreach ($properties as $key => $val) {
|
|
|
|
if ($key === 'style' && is_array($val)) {
|
|
|
|
if (empty($val)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$parts = array();
|
|
|
|
foreach ($val as $k => $v) {
|
|
|
|
$parts[] = "$k: $v";
|
|
|
|
}
|
|
|
|
$val = implode('; ', $parts);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$attributes[] = $view->attributeToString($key, $val);
|
|
|
|
}
|
|
|
|
return ' ' . implode(' ', $attributes);
|
|
|
|
});
|
|
|
|
|
2014-09-10 16:56:26 +02:00
|
|
|
$this->addHelperFunction('attributeToString', function ($key, $value) use ($view) {
|
2014-03-17 17:14:16 +01:00
|
|
|
// TODO: Doublecheck this!
|
|
|
|
if (! preg_match('~^[a-zA-Z0-9-]+$~', $key)) {
|
2014-08-26 11:15:19 +02:00
|
|
|
throw new ProgrammingError(
|
2014-03-17 17:14:16 +01:00
|
|
|
'Trying to set an invalid HTML attribute name: %s',
|
|
|
|
$key
|
2014-08-26 11:15:19 +02:00
|
|
|
);
|
2014-03-17 17:14:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
'%s="%s"',
|
|
|
|
$key,
|
2014-09-10 16:54:57 +02:00
|
|
|
$view->escape($value)
|
2014-03-17 17:14:16 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|