Skip to content

Commit

Permalink
EZP-30263: User date format don't apply to date fields in content ite…
Browse files Browse the repository at this point in the history
…m update and preview (#26)

* wip

* refatoring

* fixes cs

* EZP-30263: User date format don't apply to date fields in content item update and preview

* adds support for timestamp to twig filters

* cs fix

* adds option to overide timezone

* fixws allowed datetime formats
  • Loading branch information
pawbuj authored and Łukasz Serwatka committed Mar 28, 2019
1 parent b08d5c5 commit 23659e2
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 38 deletions.
16 changes: 8 additions & 8 deletions src/bundle/Resources/config/ezplatform_default_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ parameters:

# User Preferences
ezsettings.default.user_preferences.short_datetime_format:
date_format: 'dd/MM/Y'
date_format: 'dd/MM/yyyy'
time_format: 'HH:mm'
ezsettings.default.user_preferences.full_datetime_format:
date_format: 'LLLL dd, Y'
date_format: 'LLLL dd, yyyy'
time_format: 'HH:mm'
ezsettings.default.user_preferences.allowed_short_date_formats:
'mm/dd/yyyy': 'MM/dd/Y'
'mm/dd/yyyy': 'MM/dd/yyyy'
'mm/dd/yy': 'MM/dd/yy'
'dd/mm/yyyy': 'dd/MM/Y'
'dd/mm/yyyy': 'dd/MM/yyyy'
'dd/mm/yy': 'dd/MM/yy'
'yy/mm/dd': 'yy/MM/dd'
'mm-dd-yyyy': 'MM-dd-Y'
'mm-dd-yyyy': 'MM-dd-yyyy'
'mm-dd-yy': 'MM-dd-yy'
'dd-mm-yyyy': 'dd-MM-Y'
'dd-mm-yyyy': 'dd-MM-yyyy'
'dd-mm-yy': 'dd-MM-yy'
ezsettings.default.user_preferences.allowed_short_time_formats:
'hh:mm AM/PM': 'hh:mm a'
'hh:mm': 'HH:mm'
ezsettings.default.user_preferences.allowed_full_date_formats:
'M dd, yyyy': 'LLLL dd, Y'
'dd M yyyy': 'dd LLLL Y'
'M dd, yyyy': 'LLLL dd, yyyy'
'dd M yyyy': 'dd LLLL yyyy'
ezsettings.default.user_preferences.allowed_full_time_formats:
'hh:mm AM/PM': 'hh:mm a'
'hh:mm': 'HH:mm'
Expand Down
4 changes: 4 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ services:
EzSystems\EzPlatformUser\Templating\Twig\DateTimeExtension:
arguments:
$shortDateTimeFormatter: '@ezplatform.user.settings.short_datetime_format.formatter'
$shortDateFormatter: '@ezplatform.user.settings.short_date_format.formatter'
$shortTimeFormatter: '@ezplatform.user.settings.short_time_format.formatter'
$fullDateTimeFormatter: '@ezplatform.user.settings.full_datetime_format.formatter'
$fullDateFormatter: '@ezplatform.user.settings.full_date_format.formatter'
$fullTimeFormatter: '@ezplatform.user.settings.full_time_format.formatter'
tags:
- { name: twig.extension }
28 changes: 28 additions & 0 deletions src/bundle/Resources/config/services/user_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,31 @@ services:
tags:
- { name: ezplatform.admin_ui.user_setting.value, identifier: short_datetime_format, priority: 40 }
- { name: ezplatform.admin_ui.user_setting.form_mapper, identifier: short_datetime_format }

EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FullDateFormatterFactory: ~

ezplatform.user.settings.full_date_format.formatter:
lazy: true
factory: 'EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FullDateFormatterFactory:getFormatter'
class: 'EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\Formatter'

EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FullTimeFormatterFactory: ~

ezplatform.user.settings.full_time_format.formatter:
lazy: true
factory: 'EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FullTimeFormatterFactory:getFormatter'
class: 'EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\Formatter'

EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\ShortDateFormatterFactory: ~

ezplatform.user.settings.short_date_format.formatter:
lazy: true
factory: 'EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\ShortDateFormatterFactory:getFormatter'
class: 'EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\Formatter'

EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\ShortTimeFormatterFactory: ~

ezplatform.user.settings.short_time_format.formatter:
lazy: true
factory: 'EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\ShortDateFormatterFactory:getFormatter'
class: 'EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\Formatter'
72 changes: 45 additions & 27 deletions src/lib/Templating/Twig/DateTimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,59 @@
namespace EzSystems\EzPlatformUser\Templating\Twig;

use DateTimeImmutable;
use DateTimeInterface;
use EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer;
use EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface;
use EzSystems\EzPlatformUser\UserSetting\UserSettingService;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class DateTimeExtension extends AbstractExtension
{
/** @var \EzSystems\EzPlatformUser\UserSetting\UserSettingService */
private $userSettingService;

/** @var \EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer */
private $dateTimeFormatSerializer;

/** @var \IntlDateFormatter */
/** @var \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface */
private $shortDateTimeFormatter;

/** @var \IntlDateFormatter */
/** @var \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface */
private $shortDateFormatter;

/** @var \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface */
private $shortTimeFormatter;

/** @var \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface */
private $fullDateTimeFormatter;

/** @var \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface */
private $fullDateFormatter;

/** @var \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface */
private $fullTimeFormatter;

/**
* @param \EzSystems\EzPlatformUser\UserSetting\UserSettingService $userSettingService
* @param \EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer $dateTimeFormatSerializer
* @param \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface $shortDateTimeFormatter
* @param \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface $shortDateFormatter
* @param \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface $shortTimeFormatter
* @param \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface $fullDateTimeFormatter
* @param \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface $fullDateFormatter
* @param \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface $fullTimeFormatter
*/
public function __construct(
UserSettingService $userSettingService,
DateTimeFormatSerializer $dateTimeFormatSerializer,
FormatterInterface $shortDateTimeFormatter,
FormatterInterface $fullDateTimeFormatter
FormatterInterface $shortDateFormatter,
FormatterInterface $shortTimeFormatter,
FormatterInterface $fullDateTimeFormatter,
FormatterInterface $fullDateFormatter,
FormatterInterface $fullTimeFormatter
) {
$this->userSettingService = $userSettingService;
$this->dateTimeFormatSerializer = $dateTimeFormatSerializer;
$this->shortDateTimeFormatter = $shortDateTimeFormatter;
$this->shortDateFormatter = $shortDateFormatter;
$this->shortTimeFormatter = $shortTimeFormatter;
$this->fullDateTimeFormatter = $fullDateTimeFormatter;
$this->fullDateFormatter = $fullDateFormatter;
$this->fullTimeFormatter = $fullTimeFormatter;
}

/**
Expand All @@ -54,36 +70,38 @@ public function __construct(
public function getFilters(): array
{
return [
new TwigFilter('ez_short_datetime', [$this, 'toShortFormat']),
new TwigFilter('ez_full_datetime', [$this, 'toFullFormat']),
new TwigFilter('ez_short_datetime', function ($date, $timezone = null) { return $this->format($this->shortDateTimeFormatter, $date, $timezone); }),
new TwigFilter('ez_short_date', function ($date, $timezone = null) { return $this->format($this->shortDateFormatter, $date, $timezone); }),
new TwigFilter('ez_short_time', function ($date, $timezone = null) { return $this->format($this->shortTimeFormatter, $date, $timezone); }),
new TwigFilter('ez_full_datetime', function ($date, $timezone = null) { return $this->format($this->fullDateTimeFormatter, $date, $timezone); }),
new TwigFilter('ez_full_date', function ($date, $timezone = null) { return $this->format($this->fullDateFormatter, $date, $timezone); }),
new TwigFilter('ez_full_time', function ($date, $timezone = null) { return $this->format($this->fullTimeFormatter, $date, $timezone); }),
];
}

/**
* @param DateTimeInterface|null $date
* @param \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface $formatter
* @param mixed|null $date
* @param string|null $timezone
*
* @return string
*
* @throws \Exception
*/
public function toShortFormat(?DateTimeInterface $date = null): string
public function format(FormatterInterface $formatter, $date = null, string $timezone = null): string
{
if ($date === null) {
$date = new DateTimeImmutable();
}

return $this->shortDateTimeFormatter->format($date);
}
if (is_int($date)) {
$date = new DateTimeImmutable('@' . $date);
}

/**
* @param DateTimeInterface|null $date
*
* @return string
*/
public function toFullFormat(?DateTimeInterface $date = null): string
{
if ($date === null) {
$date = new DateTimeImmutable();
if (!$date instanceof \DateTimeInterface) {
throw new \RuntimeException('The date argument passed to format function must be int or DateTimeInterface');
}

return $this->fullDateTimeFormatter->format($date);
return $formatter->format($date, $timezone);
}
}
15 changes: 13 additions & 2 deletions src/lib/UserSetting/DateTimeFormat/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,19 @@ public function __construct(string $locale, string $timezone, string $format)
/**
* {@inheritdoc}
*/
public function format(DateTimeInterface $datetime): string
public function format(DateTimeInterface $datetime, string $timezone = null): string
{
return $this->formatter->format($datetime);
if ($timezone) {
$currentTimezone = $this->formatter->getTimeZone();
$this->formatter->setTimeZone($timezone);
}

$result = $this->formatter->format($datetime);

if ($timezone) {
$this->formatter->setTimeZone($currentTimezone);
}

return $result;
}
}
3 changes: 2 additions & 1 deletion src/lib/UserSetting/DateTimeFormat/FormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ interface FormatterInterface
{
/**
* @param \DateTimeInterface $datetime
* @param string|null $timezone
*
* @return string
*/
public function format(DateTimeInterface $datetime): string;
public function format(DateTimeInterface $datetime, string $timezone = null): string;
}
40 changes: 40 additions & 0 deletions src/lib/UserSetting/DateTimeFormat/FullDateFormatterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformUser\UserSetting\DateTimeFormat;

use EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer;
use EzSystems\EzPlatformUser\UserSetting\UserSettingService;

class FullDateFormatterFactory extends AbstractDateTimeFormatterFactory implements DateTimeFormatterFactoryInterface
{
/** @var \EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer */
private $dateTimeFormatSerializer;

/**
* @param \EzSystems\EzPlatformUser\UserSetting\UserSettingService $userSettingService
* @param \EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer $dateTimeFormatSerializer
*/
public function __construct(
UserSettingService $userSettingService,
DateTimeFormatSerializer $dateTimeFormatSerializer
) {
parent::__construct($userSettingService);
$this->dateTimeFormatSerializer = $dateTimeFormatSerializer;
}

/**
* {@inheritdoc}
*/
protected function getFormat(): string
{
return (string)$this->dateTimeFormatSerializer->deserialize(
$this->userSettingService->getUserSetting('full_datetime_format')->value
)->getDateFormat();
}
}
40 changes: 40 additions & 0 deletions src/lib/UserSetting/DateTimeFormat/FullTimeFormatterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformUser\UserSetting\DateTimeFormat;

use EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer;
use EzSystems\EzPlatformUser\UserSetting\UserSettingService;

class FullTimeFormatterFactory extends AbstractDateTimeFormatterFactory implements DateTimeFormatterFactoryInterface
{
/** @var \EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer */
private $dateTimeFormatSerializer;

/**
* @param \EzSystems\EzPlatformUser\UserSetting\UserSettingService $userSettingService
* @param \EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer $dateTimeFormatSerializer
*/
public function __construct(
UserSettingService $userSettingService,
DateTimeFormatSerializer $dateTimeFormatSerializer
) {
parent::__construct($userSettingService);
$this->dateTimeFormatSerializer = $dateTimeFormatSerializer;
}

/**
* {@inheritdoc}
*/
protected function getFormat(): string
{
return (string)$this->dateTimeFormatSerializer->deserialize(
$this->userSettingService->getUserSetting('full_datetime_format')->value
)->getTimeFormat();
}
}
40 changes: 40 additions & 0 deletions src/lib/UserSetting/DateTimeFormat/ShortDateFormatterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformUser\UserSetting\DateTimeFormat;

use EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer;
use EzSystems\EzPlatformUser\UserSetting\UserSettingService;

class ShortDateFormatterFactory extends AbstractDateTimeFormatterFactory implements DateTimeFormatterFactoryInterface
{
/** @var \EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer */
private $dateTimeFormatSerializer;

/**
* @param \EzSystems\EzPlatformUser\UserSetting\UserSettingService $userSettingService
* @param \EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer $dateTimeFormatSerializer
*/
public function __construct(
UserSettingService $userSettingService,
DateTimeFormatSerializer $dateTimeFormatSerializer
) {
parent::__construct($userSettingService);
$this->dateTimeFormatSerializer = $dateTimeFormatSerializer;
}

/**
* {@inheritdoc}
*/
protected function getFormat(): string
{
return (string)$this->dateTimeFormatSerializer->deserialize(
$this->userSettingService->getUserSetting('short_datetime_format')->value
)->getDateFormat();
}
}
Loading

0 comments on commit 23659e2

Please sign in to comment.