diff --git a/administrator/components/com_actionlogs/tmpl/actionlogs/default.php b/administrator/components/com_actionlogs/tmpl/actionlogs/default.php index 39076a3affeba..eb59c209ca142 100644 --- a/administrator/components/com_actionlogs/tmpl/actionlogs/default.php +++ b/administrator/components/com_actionlogs/tmpl/actionlogs/default.php @@ -9,6 +9,7 @@ defined('_JEXEC') or die; +use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\Layout\LayoutHelper; @@ -26,6 +27,7 @@ $wa->useScript('keepalive') ->useScript('com_actionlogs.admin-actionlogs'); +$now = Factory::getDate(); ?>
@@ -86,10 +88,7 @@ escape(Text::_($extension)); ?> - log_date); ?> -
- log_date, Text::_('DATE_FORMAT_LC6')); ?> -
+ log_date, Text::_('DATE_FORMAT_LC1'), 'below', $now); ?> escape($item->name); ?> diff --git a/administrator/components/com_privacy/tmpl/consents/default.php b/administrator/components/com_privacy/tmpl/consents/default.php index 558f80d10196f..88ddc653fd0a8 100644 --- a/administrator/components/com_privacy/tmpl/consents/default.php +++ b/administrator/components/com_privacy/tmpl/consents/default.php @@ -104,10 +104,7 @@ body; ?> - created), null, $now); ?> -
- created, Text::_('DATE_FORMAT_LC6')); ?> -
+ created, Text::_('DATE_FORMAT_LC1'), 'below', $now); ?> id; ?> diff --git a/administrator/components/com_privacy/tmpl/requests/default.php b/administrator/components/com_privacy/tmpl/requests/default.php index 9b00b18d487d1..ebfacf305cae7 100644 --- a/administrator/components/com_privacy/tmpl/requests/default.php +++ b/administrator/components/com_privacy/tmpl/requests/default.php @@ -101,10 +101,7 @@ request_type); ?> - -
- requested_at, Text::_('DATE_FORMAT_LC6')); ?> -
+ id; ?> diff --git a/administrator/components/com_users/forms/user.xml b/administrator/components/com_users/forms/user.xml index 6bbca9083accc..779742b67124c 100644 --- a/administrator/components/com_users/forms/user.xml +++ b/administrator/components/com_users/forms/user.xml @@ -197,6 +197,17 @@ + + + + +
diff --git a/administrator/language/en-GB/com_users.ini b/administrator/language/en-GB/com_users.ini index 8ab8a3e429368..5fcd45b81cd31 100644 --- a/administrator/language/en-GB/com_users.ini +++ b/administrator/language/en-GB/com_users.ini @@ -348,6 +348,7 @@ COM_USERS_USER_FIELD_RESETCOUNT_LABEL="Password Reset Count" COM_USERS_USER_FIELD_SENDEMAIL_LABEL="Receive System Emails" COM_USERS_USER_FIELD_TIMEZONE_LABEL="Time Zone" COM_USERS_USER_FIELD_TWOFACTOR_LABEL="Authentication Method" +COM_USERS_USER_FIELD_USE_RELATIVE_DATES_LABEL="Show Relative Dates in Lists" COM_USERS_USER_FIELD_USERNAME_LABEL="Login Name (Username)" COM_USERS_USER_FORM_EDIT="Edit User" COM_USERS_USER_FORM_NEW="New User" diff --git a/administrator/modules/mod_latestactions/tmpl/default.php b/administrator/modules/mod_latestactions/tmpl/default.php index 532eb228b2d29..0f0f774191bcb 100644 --- a/administrator/modules/mod_latestactions/tmpl/default.php +++ b/administrator/modules/mod_latestactions/tmpl/default.php @@ -9,9 +9,11 @@ defined('_JEXEC') or die; +use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; +$now = Factory::getDate(); ?> @@ -29,7 +31,7 @@ message; ?> diff --git a/libraries/src/HTML/Helpers/Date.php b/libraries/src/HTML/Helpers/Date.php index d709d2e60bf81..b943fde6ac8cb 100644 --- a/libraries/src/HTML/Helpers/Date.php +++ b/libraries/src/HTML/Helpers/Date.php @@ -11,8 +11,10 @@ \defined('JPATH_PLATFORM') or die; use Joomla\CMS\Date\Date as DateHelper; +use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; +use Joomla\CMS\User\User; /** * Extended Utility class for handling date display. @@ -90,4 +92,62 @@ public static function relative($date, $unit = null, $time = null, $format = nul // Over a month, return the absolute time return HTMLHelper::_('date', $date, $format); } + + /** + * Converts a given date into relative or absolute format, depending on user preference, and displays it along with the full date. + * + * @param string $date The date to convert + * @param string|null $format An optional format for the HTMLHelper::date output. Used if the date display should be absolute, + * either because of user settings or because it is too far in the past for relative display. + * @param string $showAbsoluteDate One of: + * - 'tooltip': Display the full date in a tooltip. + * - 'below': Display the full date below the relative date. + * - 'hide': Don't display the full date. + * + * @param string|null $time An optional time to compare to, defaults to now. + * @param bool $forceRelative Whether to force relative date display, regardless of user preference. + * @param string|null $unit The optional unit of measurement to return if the value of the diff is greater than one. + * Only applies to relative display. + * + * @return string The relative or absolute date, plus the full date if applicable. + * + * @since __DEPLOY_VERSION__ + */ + public static function relativeFormatted( + $date, $format = null, $showAbsoluteDate = 'tooltip', $time = null, $forceRelative = false, $unit = null) + { + $user = Factory::getApplication()->getIdentity(); + + if ($user === null) + { + $useRelative = true; + } + else + { + $useRelative = $forceRelative || $user->getParam('use_relative_dates', true); + } + + // Format for the full / absolute date display. + $formatAbsolute = Text::_('DATE_FORMAT_LC6'); + + if (!$useRelative && $format === $formatAbsolute) + { + return HTMLHelper::_('date', $date, $format); + } + + $dateMain = $useRelative ? HTMLHelper::_('date.relative', $date, $unit, $time, $format) : HTMLHelper::_('date', $date, $format); + $dateAbsolute = HTMLHelper::_('date', $date, $formatAbsolute); + + if ($showAbsoluteDate === 'tooltip') + { + return '' . $dateMain . ''; + } + + if ($showAbsoluteDate === 'below') + { + return $dateMain . ''; + } + + return $dateMain; + } } diff --git a/tests/Unit/Libraries/Cms/Html/JHtmlDateTest.php b/tests/Unit/Libraries/Cms/Html/JHtmlDateTest.php index 7325a9cfa064e..209e4d92bf567 100644 --- a/tests/Unit/Libraries/Cms/Html/JHtmlDateTest.php +++ b/tests/Unit/Libraries/Cms/Html/JHtmlDateTest.php @@ -9,9 +9,15 @@ namespace Joomla\Tests\Unit\Libraries\Cms\Html; -use JHtmlDate; +use DateTimeZone; +use Joomla\CMS\Application\CMSApplication; use Joomla\CMS\Date\Date; +use Joomla\CMS\Factory; +use Joomla\CMS\HTML\Helpers\Date as HTMLDate; +use Joomla\CMS\Language\Text; +use Joomla\CMS\User\User; use Joomla\Tests\Unit\UnitTestCase; +use PHPUnit\Framework\MockObject\MockObject; /** * Test class for JHtmlDate. @@ -22,6 +28,32 @@ */ class JHtmlDateTest extends UnitTestCase { + /** + * Mock for the user object. + * + * @var User|MockObject + * + * @since __DEPLOY_VERSION__ + */ + protected $user; + + /** + * Set up test case. Creates mock objects. + * + * @return void + * @since __DEPLOY_VERSION__ + */ + protected function setUp() : void + { + parent::setUp(); + $this->user = $this->createMock(User::class); + $this->user->method('getTimezone')->willReturn(new DateTimeZone('UTC')); + $application = $this->createMock(CMSApplication::class); + $application->method('getIdentity')->willReturn($this->user); + + Factory::$application = $application; + } + /** * Test data for the testRelative method * @@ -91,6 +123,194 @@ public function dataTestRelative(): array */ public function testRelative($result, $date, $unit = null, $time = null) { - $this->assertEquals($result, JHtmlDate::relative($date, $unit, $time)); + $this->assertEquals($result, HtmlDate::relative($date, $unit, $time)); + } + + /** + * Test data for the testRelativeFormatted method + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function dataTestRelativeFormatted(): array + { + return [ + // Element order: result, date, unit, time, format, forceRelative, showAbsoluteDate, useRelativeDate. + /* + * User: relative, force: false + */ + // Relative with full date in tooltip - 10 days ago + [ + 'JLIB_HTML_DATE_RELATIVE_DAYS', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + null, + false, + 'tooltip', + true + ], + // Relative with full date below - 10 days ago + [ + 'JLIB_HTML_DATE_RELATIVE_DAYS', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + null, + false, + 'below', + true + ], + // Relative without full date - 10 days ago + [ + 'JLIB_HTML_DATE_RELATIVE_DAYS', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + null, + false, + 'hide', + true + ], + /* + * User: absolute, force: false + */ + // Absolute with full date in tooltip - Monday, 05 April 2021 + [ + 'Monday, 05 April 2021', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + null, + false, + 'tooltip', + false + ], + // Absolute with full date below - Monday, 05 April 2021 + [ + 'Monday, 05 April 2021', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + null, + false, + 'below', + false + ], + // Absolute without full date - Monday, 05 April 2021 + [ + 'Monday, 05 April 2021', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + null, + false, + 'hide', + false + ], + /* + * User: absolute, force: true -> relative date + */ + // Relative with full date in tooltip - 10 days ago + [ + 'JLIB_HTML_DATE_RELATIVE_DAYS', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + null, + true, + 'tooltip', + false + ], + // Relative with full date below - 10 days ago + [ + 'JLIB_HTML_DATE_RELATIVE_DAYS', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + null, + true, + 'below', + false + ], + // Relative without full date - 10 days ago + [ + 'JLIB_HTML_DATE_RELATIVE_DAYS', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + null, + true, + 'hide', + false + ], + /* + * Absolute with the same format -> don't display the same date twice. + */ + // Absolute - 2021-04-05 12:00:00 + [ + '2021-04-05 12:00:00', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + Text::_('DATE_FORMAT_LC6'), + false, + 'tooltip', + false + ], + // Absolute - 2021-04-05 12:00:00 + [ + '2021-04-05 12:00:00', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + Text::_('DATE_FORMAT_LC6'), + false, + 'below', + false + ], + // Absolute - 2021-04-05 12:00:00 + [ + '2021-04-05 12:00:00', + new Date('2021-04-05 12:00:00'), + 'day', + new Date('2021-04-15 12:00:00'), + Text::_('DATE_FORMAT_LC6'), + false, + 'hide', + false + ], + ]; + } + + /** + * Tests the JHtmlDate::relativeFormatted method. + * + * @param string $result The expected test result + * @param string $date The date to convert + * @param string|null $unit The optional unit of measurement to return if the value of the diff is greater than one. + * Only applies to relative display. + * @param string|null $time An optional time to compare to, defaults to now. + * @param string|null $format An optional format for the HTMLHelper::date output. Used if the date display should be absolute, + * either because of user settings or because it is too far in the past for relative display. + * @param bool $forceRelative Whether to force relative date display, regardless of user preference. + * @param string $showAbsoluteDate One of: + * - 'tooltip': Display the full date in a tooltip. + * - 'below': Display the full date below the relative date. + * - 'hide': Don't display the full date. + * @param bool $useRelativeDate User Parameter "use relative date". + * + * @return void + * + * @since __DEPLOY_VERSION__ + * @dataProvider dataTestRelativeFormatted + */ + public function testRelativeFormatted( + $result, $date, $unit, $time, $format, $forceRelative, $showAbsoluteDate, $useRelativeDate + ) + { + $this->user->method('getParam')->with('use_relative_dates', true)->willReturn($useRelativeDate); + $this->assertEquals($result, HTMLDate::relativeFormatted($date, $format, $showAbsoluteDate, $time, $forceRelative, $unit)); } }
title; ?>
- log_date); ?> + log_date, Text::_('DATE_FORMAT_LC5'), 'tooltip', $now); ?>