From a66a1a47299eb0c11961e10dcd10efbd1b5413d1 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 29 May 2014 13:03:10 +0200 Subject: [PATCH] Refactor the timeSincePrefix and timeUntilPrefix functions Instead of having two separate functions to get the prefix and the formatted time interval the new functions return both the prefix and the time interval. refs #5981 --- library/Icinga/Util/Format.php | 52 +++++++------------ library/Icinga/Web/View/helpers/format.php | 8 +-- .../application/clicommands/ListCommand.php | 3 +- .../views/scripts/list/hosts.phtml | 2 +- .../views/scripts/list/services.phtml | 2 +- .../views/scripts/process/info.phtml | 2 +- .../scripts/show/components/header.phtml | 4 +- 7 files changed, 30 insertions(+), 43 deletions(-) diff --git a/library/Icinga/Util/Format.php b/library/Icinga/Util/Format.php index 2b4bf9fa3..33c7ebd83 100644 --- a/library/Icinga/Util/Format.php +++ b/library/Icinga/Util/Format.php @@ -101,32 +101,20 @@ class Format return self::showHourMin($duration); } - protected static function showHourMin($sec, $returnPrefix = false) + protected static function showHourMin($sec, $includePrefix = false) { $min = floor($sec / 60); if ($min < 60) { - if ($returnPrefix) { - return 'For'; - } else { - return $min . 'm ' . ($sec % 60) . 's'; - } + return ($includePrefix ? t('for') . ' ' : '') . $min . 'm ' . ($sec % 60) . 's'; } $hour = floor($min / 60); if ($hour < 24) { - if ($returnPrefix) { - return 'Since'; - } else { - return date('H:i', time() - $sec); - } - } - if ($returnPrefix) { - return 'For'; - } else { - return floor($hour / 24) . 'd ' . ($hour % 24) . 'h'; + return ($includePrefix ? t('since') . ' ' : '') . date('H:i', time() - $sec); } + return ($includePrefix ? t('for') . ' ' : '') . floor($hour / 24) . 'd ' . ($hour % 24) . 'h'; } - protected static function smartTimeDiff($diff, $timestamp, $returnPrefix = false) + protected static function smartTimeDiff($diff, $timestamp, $includePrefix = false) { if ($timestamp === null || $timestamp === false) { return '-'; @@ -140,19 +128,11 @@ class Format } if (abs($diff) > 3600 * 24 * 3) { if (date('Y') === date('Y', $timestamp)) { - if ($returnPrefix) { - return 'Since'; - } else { - return date('d.m.', $timestamp); - } - } - if ($returnPrefix) { - return 'Since'; - } else { - return date('m.Y', $timestamp); + return ($includePrefix ? t('since') . ' ' : '') . date('d.m.', $timestamp); } + return ($includePrefix ? t('since') . ' ' : '') . date('m.Y', $timestamp); } - return $prefix . self::showHourMin(abs($diff), $returnPrefix); + return $prefix . self::showHourMin(abs($diff), $includePrefix); } public static function timeSince($timestamp) @@ -160,9 +140,13 @@ class Format return self::smartTimeDiff(time() - $timestamp, $timestamp); } - public static function timeSincePrefix($timestamp) + public static function prefixedTimeSince($timestamp, $ucfirst = false) { - return self::smartTimeDiff(time() - $timestamp, $timestamp, true); + $result = self::smartTimeDiff(time() - $timestamp, $timestamp, true); + if ($ucfirst) { + $result = ucfirst($result); + } + return $result; } public static function timeUntil($timestamp) @@ -170,9 +154,13 @@ class Format return self::smartTimeDiff($timestamp - time(), $timestamp); } - public static function timeUntilPrefix($timestamp) + public static function prefixedTimeUntil($timestamp, $ucfirst) { - return self::smartTimeDiff($timestamp - time(), $timestamp, true); + $result = self::smartTimeDiff($timestamp - time(), $timestamp, true); + if ($ucfirst) { + $result = ucfirst($result); + } + return $result; } protected static function formatForUnits($value, & $units, $base) diff --git a/library/Icinga/Web/View/helpers/format.php b/library/Icinga/Web/View/helpers/format.php index 01ec92ea5..60861e7ee 100644 --- a/library/Icinga/Web/View/helpers/format.php +++ b/library/Icinga/Web/View/helpers/format.php @@ -15,9 +15,9 @@ $this->addHelperFunction('timeSince', function ($timestamp) { . ''; }); -$this->addHelperFunction('timeSincePrefix', function ($timestamp) { +$this->addHelperFunction('prefixedTimeSince', function ($timestamp, $ucfirst = false) { return '' - . $this->translate(Format::timeSincePrefix($timestamp)) + . Format::prefixedTimeSince($timestamp, $ucfirst) . ' '; }); @@ -28,9 +28,9 @@ $this->addHelperFunction('timeUntil', function ($timestamp) { . ''; }); -$this->addHelperFunction('timeUntilPrefix', function ($timestamp) { +$this->addHelperFunction('prefixedTimeUntil', function ($timestamp, $ucfirst = false) { if (! $timestamp) return ''; return '' - . $this->translate(Format::timeUntilPrefix($timestamp)) + . Format::prefixedTimeUntil($timestamp, $ucfirst) . ''; }); diff --git a/modules/monitoring/application/clicommands/ListCommand.php b/modules/monitoring/application/clicommands/ListCommand.php index 15224cb1a..71bf1083f 100644 --- a/modules/monitoring/application/clicommands/ListCommand.php +++ b/modules/monitoring/application/clicommands/ListCommand.php @@ -295,8 +295,7 @@ class ListCommand extends Command $leaf, $screen->underline($row->service_description), $screen->colorize($utils->objectStateFlags('service', $row) . $perf, 'lightblue'), - strtolower(Format::timeSincePrefix($row->service_last_state_change)), - Format::timeSince($row->service_last_state_change) + Format::prefixedTimeSince($row->service_last_state_change, true) ); if ($this->isVerbose) { $out .= $emptyLine . preg_replace( diff --git a/modules/monitoring/application/views/scripts/list/hosts.phtml b/modules/monitoring/application/views/scripts/list/hosts.phtml index 2f2ddf4d3..d12b11a73 100644 --- a/modules/monitoring/application/views/scripts/list/hosts.phtml +++ b/modules/monitoring/application/views/scripts/list/hosts.phtml @@ -78,7 +78,7 @@ if ($hosts->count() === 0) { monitoringState($host, 'host')); ?>
- timeSincePrefix($host->host_last_state_change) ?> timeSince($host->host_last_state_change); ?> + prefixedTimeSince($host->host_last_state_change, true) ?> host_state > 0): ?>
host_state_type === '1') ? $this->translate('Hard') : $this->translate('Soft') ?> host_current_check_attempt; ?>/host_max_check_attempts; ?> diff --git a/modules/monitoring/application/views/scripts/list/services.phtml b/modules/monitoring/application/views/scripts/list/services.phtml index a7d393ab6..11795da5b 100644 --- a/modules/monitoring/application/views/scripts/list/services.phtml +++ b/modules/monitoring/application/views/scripts/list/services.phtml @@ -41,7 +41,7 @@ foreach ($services as $service): translate(strtoupper($helper->monitoringState($service, 'service'))) ?>
- compact): ?>timeSincePrefix($service->service_last_state_change) ?> timeSince($service->service_last_state_change); ?> + compact): ?>prefixedTimeSince($service->service_last_state_change); ?>timeSince($service->service_last_state_change); ?> service_state > 0 && (int) $service->service_state_type === 0): ?>
Soft service_attempt ?> diff --git a/modules/monitoring/application/views/scripts/process/info.phtml b/modules/monitoring/application/views/scripts/process/info.phtml index 57406664c..350966c49 100644 --- a/modules/monitoring/application/views/scripts/process/info.phtml +++ b/modules/monitoring/application/views/scripts/process/info.phtml @@ -10,7 +10,7 @@ $cf = $this->getHelper('CommandForm');

Backend backendName; ?> -is_currently_running === '1' ? sprintf('has been running with PID %d %s ', $ps->process_id, strtolower($this->timeSincePrefix($ps->program_start_time))) . $this->timeSince($ps->program_start_time) : 'is not running'; ?>. +is_currently_running === '1' ? sprintf('has been running with PID %d ', $ps->process_id) . $this->prefixedTimeSince($ps->program_start_time) : 'is not running'; ?>. diff --git a/modules/monitoring/application/views/scripts/show/components/header.phtml b/modules/monitoring/application/views/scripts/show/components/header.phtml index 51a8e4f05..6f4d3e058 100644 --- a/modules/monitoring/application/views/scripts/show/components/header.phtml +++ b/modules/monitoring/application/views/scripts/show/components/header.phtml @@ -6,7 +6,7 @@
> translate($this->util()->getHostStateName($object->host_state)) ?>
- timeSincePrefix($object->host_last_state_change) ?> timeSince($object->host_last_state_change) ?> + prefixedTimeSince($object->host_last_state_change, true) ?>
escape($object->host_name) ?>host_address && $object->host_address !== $object->host_name): ?> @@ -18,7 +18,7 @@
translate($this->util()->getServiceStateName($object->service_state)) ?>
- timeSincePrefix($object->service_last_state_change) ?> timeSince($object->service_last_state_change) ?> + prefixedTimeSince($object->service_last_state_change, true) ?>
translate('Service') ?>: escape($object->service_description) ?>