diff options
Diffstat (limited to 'application/views/helpers')
-rw-r--r-- | application/views/helpers/CreateTicketLinks.php | 23 | ||||
-rw-r--r-- | application/views/helpers/FormDate.php | 46 | ||||
-rw-r--r-- | application/views/helpers/FormDateTime.php | 63 | ||||
-rw-r--r-- | application/views/helpers/FormNumber.php | 77 | ||||
-rw-r--r-- | application/views/helpers/FormTime.php | 46 | ||||
-rw-r--r-- | application/views/helpers/ProtectId.php | 13 | ||||
-rw-r--r-- | application/views/helpers/Util.php | 68 |
7 files changed, 336 insertions, 0 deletions
diff --git a/application/views/helpers/CreateTicketLinks.php b/application/views/helpers/CreateTicketLinks.php new file mode 100644 index 0000000..dda55a6 --- /dev/null +++ b/application/views/helpers/CreateTicketLinks.php @@ -0,0 +1,23 @@ +<?php +/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */ + +/** + * Helper for creating ticket links from ticket hooks + */ +class Zend_View_Helper_CreateTicketLinks extends Zend_View_Helper_Abstract +{ + /** + * Create ticket links form ticket hooks + * + * @param string $text + * + * @return string + * @see \Icinga\Application\Hook\TicketHook::createLinks() + */ + public function createTicketLinks($text) + { + $tickets = $this->view->tickets; + /** @var \Icinga\Application\Hook\TicketHook|array|null $tickets */ + return ! empty($tickets) ? $tickets->createLinks($text) : $text; + } +} diff --git a/application/views/helpers/FormDate.php b/application/views/helpers/FormDate.php new file mode 100644 index 0000000..39e6d94 --- /dev/null +++ b/application/views/helpers/FormDate.php @@ -0,0 +1,46 @@ +<?php +/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */ + +/** + * Render date input controls + */ +class Zend_View_Helper_FormDate extends Zend_View_Helper_FormElement +{ + /** + * Render the date input control + * + * @param string $name + * @param int $value + * @param array $attribs + * + * @return string The rendered date input control + */ + public function formDate($name, $value = null, $attribs = null) + { + $info = $this->_getInfo($name, $value, $attribs); + + extract($info); // name, id, value, attribs, options, listsep, disable + /** @var string $id */ + /** @var bool $disable */ + + $disabled = ''; + if ($disable) { + $disabled = ' disabled="disabled"'; + } + + /** @var \Icinga\Web\View $view */ + $view = $this->view; + + $html5 = sprintf( + '<input type="date" name="%s" id="%s" value="%s"%s%s%s', + $view->escape($name), + $view->escape($id), + $view->escape($value), + $disabled, + $this->_htmlAttribs($attribs), + $this->getClosingBracket() + ); + + return $html5; + } +} diff --git a/application/views/helpers/FormDateTime.php b/application/views/helpers/FormDateTime.php new file mode 100644 index 0000000..de5eb4b --- /dev/null +++ b/application/views/helpers/FormDateTime.php @@ -0,0 +1,63 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +/** + * Render date-and-time input controls + */ +class Zend_View_Helper_FormDateTime extends Zend_View_Helper_FormElement +{ + /** + * Format date and time + * + * @param DateTime $dateTime + * @param bool $local + * + * @return string + */ + public function formatDate(DateTime $dateTime, $local) + { + $format = (bool) $local === true ? 'Y-m-d\TH:i:s' : DateTime::RFC3339; + return $dateTime->format($format); + } + + /** + * Render the date-and-time input control + * + * @param string $name The element name + * @param DateTime $value The default timestamp + * @param array $attribs Attributes for the element tag + * + * @return string The element XHTML + */ + public function formDateTime($name, $value = null, $attribs = null) + { + $info = $this->_getInfo($name, $value, $attribs); + extract($info); // name, id, value, attribs, options, listsep, disable + /** @var string $id */ + /** @var bool $disable */ + $disabled = ''; + if ($disable) { + $disabled = ' disabled="disabled"'; + } + if ($value instanceof DateTime) { + // If value was valid, it's a DateTime object + $value = $this->formatDate($value, $attribs['local']); + } + if (isset($attribs['placeholder']) && $attribs['placeholder'] instanceof DateTime) { + $attribs['placeholder'] = $this->formatDate($attribs['placeholder'], $attribs['local']); + } + $type = $attribs['local'] === true ? 'datetime-local' : 'datetime'; + unset($attribs['local']); // Unset local to not render it again in $this->_htmlAttribs($attribs) + $html5 = sprintf( + '<input type="%s" data-use-datetime-picker name="%s" id="%s" step="1" value="%s"%s%s%s', + $type, + $this->view->escape($name), + $this->view->escape($id), + $this->view->escape($value), + $disabled, + $this->_htmlAttribs($attribs), + $this->getClosingBracket() + ); + return $html5; + } +} diff --git a/application/views/helpers/FormNumber.php b/application/views/helpers/FormNumber.php new file mode 100644 index 0000000..f447180 --- /dev/null +++ b/application/views/helpers/FormNumber.php @@ -0,0 +1,77 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +/** + * Render number input controls + */ +class Zend_View_Helper_FormNumber extends Zend_View_Helper_FormElement +{ + /** + * Format a number + * + * @param $number + * + * @return string + */ + public function formatNumber($number) + { + if (empty($number)) { + return $number; + } + return $this->view->escape( + sprintf( + ctype_digit((string) $number) ? '%d' : '%F', + $number + ) + ); + } + + /** + * Render the number input control + * + * @param string $name + * @param int $value + * @param array $attribs + * + * @return string The rendered number input control + */ + public function formNumber($name, $value = null, $attribs = null) + { + $info = $this->_getInfo($name, $value, $attribs); + extract($info); // name, id, value, attribs, options, listsep, disable + /** @var string $id */ + /** @var bool $disable */ + $disabled = ''; + if ($disable) { + $disabled = ' disabled="disabled"'; + } + $min = ''; + if (isset($attribs['min'])) { + $min = sprintf(' min="%s"', $this->formatNumber($attribs['min'])); + } + unset($attribs['min']); // Unset min to not render it again in $this->_htmlAttribs($attribs) + $max = ''; + if (isset($attribs['max'])) { + $max = sprintf(' max="%s"', $this->formatNumber($attribs['max'])); + } + unset($attribs['max']); // Unset max to not render it again in $this->_htmlAttribs($attribs) + $step = ''; + if (isset($attribs['step'])) { + $step = sprintf(' step="%s"', $attribs['step'] === 'any' ? 'any' : $this->formatNumber($attribs['step'])); + } + unset($attribs['step']); // Unset step to not render it again in $this->_htmlAttribs($attribs) + $html5 = sprintf( + '<input type="number" name="%s" id="%s" value="%s"%s%s%s%s%s%s', + $this->view->escape($name), + $this->view->escape($id), + $this->view->escape($this->formatNumber($value)), + $min, + $max, + $step, + $disabled, + $this->_htmlAttribs($attribs), + $this->getClosingBracket() + ); + return $html5; + } +} diff --git a/application/views/helpers/FormTime.php b/application/views/helpers/FormTime.php new file mode 100644 index 0000000..39d1b83 --- /dev/null +++ b/application/views/helpers/FormTime.php @@ -0,0 +1,46 @@ +<?php +/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */ + +/** + * Render time input controls + */ +class Zend_View_Helper_FormTime extends Zend_View_Helper_FormElement +{ + /** + * Render the time input control + * + * @param string $name + * @param int $value + * @param array $attribs + * + * @return string The rendered time input control + */ + public function formTime($name, $value = null, $attribs = null) + { + $info = $this->_getInfo($name, $value, $attribs); + + extract($info); // name, id, value, attribs, options, listsep, disable + /** @var string $id */ + /** @var bool $disable */ + + $disabled = ''; + if ($disable) { + $disabled = ' disabled="disabled"'; + } + + /** @var \Icinga\Web\View $view */ + $view = $this->view; + + $html5 = sprintf( + '<input type="time" name="%s" id="%s" value="%s"%s%s%s', + $view->escape($name), + $view->escape($id), + $view->escape($value), + $disabled, + $this->_htmlAttribs($attribs), + $this->getClosingBracket() + ); + + return $html5; + } +} diff --git a/application/views/helpers/ProtectId.php b/application/views/helpers/ProtectId.php new file mode 100644 index 0000000..f6dc226 --- /dev/null +++ b/application/views/helpers/ProtectId.php @@ -0,0 +1,13 @@ +<?php +/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */ + +/** + * Class Zend_View_Helper_Util + */ +class Zend_View_Helper_ProtectId extends Zend_View_Helper_Abstract +{ + public function protectId($id) + { + return Zend_Controller_Front::getInstance()->getRequest()->protectId($id); + } +} diff --git a/application/views/helpers/Util.php b/application/views/helpers/Util.php new file mode 100644 index 0000000..7a3e410 --- /dev/null +++ b/application/views/helpers/Util.php @@ -0,0 +1,68 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +/** + * Class Zend_View_Helper_Util + */ +class Zend_View_Helper_Util extends Zend_View_Helper_Abstract +{ + public function util() + { + return $this; + } + + public static function showTimeSince($timestamp) + { + if (! $timestamp) { + return 'unknown'; + } + $duration = time() - $timestamp; + if ($duration > 3600 * 24 * 3) { + if (date('Y') === date('Y', $timestamp)) { + return date('d.m.', $timestamp); + } + return date('m.Y', $timestamp); + } + return self::showHourMin($duration); + } + + public static function showHourMin($sec) + { + $min = floor($sec / 60); + if ($min < 60) { + return $min . 'm ' . ($sec % 60) . 's'; + } + $hour = floor($min / 60); + if ($hour < 24) { + return date('H:i', time() - $sec); + } + return floor($hour / 24) . 'd ' . ($hour % 24) . 'h'; + } + + public static function showSeconds($sec) + { + // Todo: localization + if ($sec < 1) { + return round($sec * 1000) . 'ms'; + } + if ($sec < 60) { + return $sec . 's'; + } + return floor($sec / 60) . 'm ' . ($sec % 60) . 's'; + } + + public static function showTime($timestamp) + { + // Todo: localization + if ($timestamp < 86400) { + return 'undef'; + } + if (date('Ymd') === date('Ymd', $timestamp)) { + return date('H:i:s', $timestamp); + } + if (date('Y') === date('Y', $timestamp)) { + return date('H:i d.m.', $timestamp); + } + return date('H:i d.m.Y', $timestamp); + } +} |