From 8ca6cc32b2c789a3149861159ad258f2cb9491e3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:39:39 +0200 Subject: Adding upstream version 2.11.4. Signed-off-by: Daniel Baumann --- application/views/helpers/CreateTicketLinks.php | 23 ++++++++ application/views/helpers/FormDate.php | 46 +++++++++++++++ application/views/helpers/FormDateTime.php | 63 ++++++++++++++++++++ application/views/helpers/FormNumber.php | 77 +++++++++++++++++++++++++ application/views/helpers/FormTime.php | 46 +++++++++++++++ application/views/helpers/ProtectId.php | 13 +++++ application/views/helpers/Util.php | 68 ++++++++++++++++++++++ 7 files changed, 336 insertions(+) create mode 100644 application/views/helpers/CreateTicketLinks.php create mode 100644 application/views/helpers/FormDate.php create mode 100644 application/views/helpers/FormDateTime.php create mode 100644 application/views/helpers/FormNumber.php create mode 100644 application/views/helpers/FormTime.php create mode 100644 application/views/helpers/ProtectId.php create mode 100644 application/views/helpers/Util.php (limited to 'application/views/helpers') diff --git a/application/views/helpers/CreateTicketLinks.php b/application/views/helpers/CreateTicketLinks.php new file mode 100644 index 0000000..4f8a272 --- /dev/null +++ b/application/views/helpers/CreateTicketLinks.php @@ -0,0 +1,23 @@ +view->tickets; + /** @var \Icinga\Application\Hook\TicketHook $tickets */ + return isset($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 @@ +_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( + '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 @@ +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( + '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 @@ +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( + '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 @@ +_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( + '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 @@ +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 @@ + 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); + } +} -- cgit v1.2.3