Merge pull request #3858 from Icinga/feature/downtime-schedule-times

Require time output for downtime schedule and end/expire times
This commit is contained in:
Johannes Meyer 2019-07-18 08:47:55 +02:00 committed by GitHub
commit 40529e76ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 24 deletions

View File

@ -149,12 +149,13 @@ class DateFormatter
* *
* @param int|float $time * @param int|float $time
* @param bool $timeOnly * @param bool $timeOnly
* @param bool $requireTime
* *
* @return string * @return string
*/ */
public static function timeAgo($time, $timeOnly = false) public static function timeAgo($time, $timeOnly = false, $requireTime = false)
{ {
list($type, $ago, $invert) = static::diff($time); list($type, $ago, $invert) = static::diff($time, $requireTime);
if ($timeOnly) { if ($timeOnly) {
return $ago; return $ago;
} }
@ -185,12 +186,13 @@ class DateFormatter
* *
* @param int|float $time * @param int|float $time
* @param bool $timeOnly * @param bool $timeOnly
* @param bool $requireTime
* *
* @return string * @return string
*/ */
public static function timeSince($time, $timeOnly = false) public static function timeSince($time, $timeOnly = false, $requireTime = false)
{ {
list($type, $since, $invert) = static::diff($time); list($type, $since, $invert) = static::diff($time, $requireTime);
if ($timeOnly) { if ($timeOnly) {
return $since; return $since;
} }

View File

@ -38,25 +38,25 @@ $this->addHelperFunction('formatTime', function ($time) {
return DateFormatter::formatTime($time); return DateFormatter::formatTime($time);
}); });
$this->addHelperFunction('timeAgo', function ($time, $timeOnly = false) { $this->addHelperFunction('timeAgo', function ($time, $timeOnly = false, $requireTime = false) {
if (! $time) { if (! $time) {
return ''; return '';
} }
return sprintf( return sprintf(
'<span class="relative-time time-ago" title="%s">%s</span>', '<span class="relative-time time-ago" title="%s">%s</span>',
DateFormatter::formatDateTime($time), DateFormatter::formatDateTime($time),
DateFormatter::timeAgo($time, $timeOnly) DateFormatter::timeAgo($time, $timeOnly, $requireTime)
); );
}); });
$this->addHelperFunction('timeSince', function ($time, $timeOnly = false) { $this->addHelperFunction('timeSince', function ($time, $timeOnly = false, $requireTime = false) {
if (! $time) { if (! $time) {
return ''; return '';
} }
return sprintf( return sprintf(
'<span class="relative-time time-since" title="%s">%s</span>', '<span class="relative-time time-since" title="%s">%s</span>',
DateFormatter::formatDateTime($time), DateFormatter::formatDateTime($time),
DateFormatter::timeSince($time, $timeOnly) DateFormatter::timeSince($time, $timeOnly, $requireTime)
); );
}); });

View File

@ -47,7 +47,7 @@
)) ?> )) ?>
</span> </span>
<?php if (! $downtime->is_in_effect && $downtime->start >= time()): ?> <?php if (! $downtime->is_in_effect && $downtime->start >= time()): ?>
<span><?= sprintf($this->translate('expires %s'), $this->timeUntil($downtime->end, false, true)) ?></span> <span><?= sprintf($this->translate('expires %s'), $this->timeUntil($downtime->is_flexible ? $downtime->scheduled_end : $downtime->end, false, true)) ?></span>
<?php endif ?> <?php endif ?>
<span class="comment-icons"> <span class="comment-icons">
<?php if ($downtime->is_flexible): ?> <?php if ($downtime->is_flexible): ?>

View File

@ -52,29 +52,21 @@ if (empty($object->comments) && ! $addLink) {
if ((bool) $downtime->is_in_effect) { if ((bool) $downtime->is_in_effect) {
$state = sprintf( $state = sprintf(
$this->translate('expires %s', 'Last format parameter represents the downtime expire time'), $this->translate('expires %s', 'Last format parameter represents the downtime expire time'),
$this->timeUntil($downtime->end) $this->timeUntil($downtime->end, false, true)
); );
} else { } else {
if ($downtime->start <= time()) { if ($downtime->start <= time()) {
$state = sprintf( $state = sprintf(
$this->translate('ends %s', 'Last format parameter represents the end time'), $this->translate('ends %s', 'Last format parameter represents the end time'),
$this->timeUntil($downtime->is_flexible ? $downtime->scheduled_end : $downtime->end) $this->timeUntil($downtime->is_flexible ? $downtime->scheduled_end : $downtime->end, false, true)
); );
} else { } else {
if ((bool) $downtime->is_fixed) { $state = sprintf(
$state = sprintf( $this->translate('scheduled %s', 'Last format parameter represents the time scheduled'),
$this->translate('scheduled %s', 'Last format parameter represents the time scheduled'), $this->timeUntil($downtime->start, false, true)
$this->timeUntil($downtime->start, false, true) ) . ' ' . sprintf(
);
} else {
$state = sprintf(
$this->translate('scheduled flexible %s', 'Last format parameter represents the time scheduled'),
$this->timeUntil($downtime->start, false, true)
);
}
$state .= ' ' . sprintf(
$this->translate('expires %s', 'Last format parameter represents the downtime expire time'), $this->translate('expires %s', 'Last format parameter represents the downtime expire time'),
$this->timeUntil($downtime->end, false, true) $this->timeUntil($downtime->is_flexible ? $downtime->scheduled_end : $downtime->end, false, true)
); );
} }
} }