Skip to content

Commit

Permalink
configure am/pm time-format as user preference (#2789)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed Nov 14, 2021
1 parent dce0578 commit 8b0962e
Show file tree
Hide file tree
Showing 32 changed files with 216 additions and 260 deletions.
34 changes: 10 additions & 24 deletions .env.dist
@@ -1,30 +1,16 @@
### DATABASE CONFIGURATION
# Replace "user", "password" and "database" with your database connection.
# Configure the server version, MariaDB requires the "mariadb-" prefix, eg:
# for MySQL "serverVersion=5.7" and for MariaDB "serverVersion=mariadb-10.5.8"
# Configure your database connection and set the correct server version:
# for MySQL "serverVersion=5.7" and for MariaDB "serverVersion=mariadb-10.5.8"
DATABASE_URL=mysql://user:password@127.0.0.1:3306/database?charset=utf8&serverVersion=5.7

### EMAIL CONFIGURATION
# Emails will be sent "from":
# Email will be sent with this address as sender
MAILER_FROM=kimai@example.com

# Email connection (disabled by default with MAILER_URL=null://null)
# SMTP: smtp://localhost:25?encryption=&auth_mode=
# Google: gmail://username:password@default
# Amazon: ses://ACCESS_KEY:SECRET_KEY@default?region=eu-west-1
# Mailchimp: mandrill://KEY@default
# Mailgun: mailgun://KEY:DOMAIN@default
# Postmark: postmark://ID@default
# Sendgrid: sendgrid://KEY@default
# Disable emails: null://null
# Email connection (disabled by default) more info at https://www.kimai.org/documentation/emails.html
MAILER_URL=null://null

### APPLICATION CONFIGURATION
# do not change, unless you are developing for Kimai
APP_ENV=prod
# should be changed to a unique character sequence
APP_SECRET=change_this_to_something_unique

# Running in a "special" environment, eg. behind reverse proxies?
# Check those:
# TRUSTED_PROXIES=127.0.0.1,127.0.0.2
# TRUSTED_HOSTS=localhost,example.com
# unlikely, that you need to change this one
CORS_ALLOW_ORIGIN=^https?://localhost(:[0-9]+)?$
# Running behind reverse proxies? Use those:
# TRUSTED_PROXIES=127.0.0.1,127.0.0.2
# TRUSTED_HOSTS=localhost,example.com
7 changes: 7 additions & 0 deletions UPGRADING.md
Expand Up @@ -8,6 +8,13 @@ you can upgrade your Kimai installation to the latest stable release.
Check below if there are more version specific steps required, which need to be executed after the normal update process.
Perform EACH version specific task between your version and the new one, otherwise you risk data inconsistency or a broken installation.


## [1.16](https://github.com/kevinpapst/kimai2/releases/tag/1.16)

**DEVELOPER**

- Removed `formDateTime` field from API model `I18nConfig`

## [1.15](https://github.com/kevinpapst/kimai2/releases/tag/1.15)

**Many database changes: don't forget to [run the updater](https://www.kimai.org/documentation/updates.html).**
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -165,7 +165,8 @@
"bin/console lint:xliff translations",
"bin/console doctrine:schema:validate --skip-sync -vvv --no-interaction"
],
"kimai:tests": "vendor/bin/phpunit tests/",
"tests": "vendor/bin/phpunit tests/",
"kimai:tests": "@tests",
"kimai:tests-unit": "vendor/bin/phpunit --exclude-group integration tests/",
"kimai:tests-integration": "vendor/bin/phpunit --group integration tests/",
"kimai:phpstan": "@phpstan",
Expand Down
18 changes: 0 additions & 18 deletions config/packages/kimai.yaml
Expand Up @@ -214,99 +214,81 @@ kimai:
# --------------------------------------------------------------------------------
languages:
cs:
date_time_type: 'dd.MM.yyyy HH:mm'
date_type: 'dd.MM.yyyy'
date: 'd.m.Y'
date_time: 'd.m H:i'
da:
date_time_type: 'dd.MM.yyyy HH:mm'
date_type: 'dd.MM.yyyy'
date: 'd.m.Y'
date_time: 'd.m. H:i'
de:
date_time_type: 'dd.MM.yyyy HH:mm'
date_type: 'dd.MM.yyyy'
date: 'd.m.Y'
date_time: 'd.m. H:i'
de_AT:
date_time_type: 'dd.MM.yyyy HH:mm'
date_type: 'dd.MM.yyyy'
date: 'd.m.Y'
date_time: 'd.m. H:i'
de_CH:
date_time_type: 'dd.MM.yyyy HH:mm'
date_type: 'dd.MM.yyyy'
date: 'd.m.Y'
date_time: 'd.m. H:i'
el:
date_time_type: 'dd.MM.yyyy HH:mm'
date_type: 'dd.MM.yyyy'
date: 'd.m.Y'
date_time: 'd.m. H:i'
en:
date_time_type: 'yyyy-MM-dd HH:mm'
date_type: 'yyyy-MM-dd'
date: 'Y-m-d'
date_time: 'm-d H:i'
duration: '%%h:%%m h'
es:
date_time_type: 'dd.MM.yyyy HH:mm'
date_type: 'dd.MM.yyyy'
date: 'd.m.Y'
date_time: 'd.m. H:i'
fi:
date_time_type: 'dd.MM.yyyy HH:mm'
date_type: 'dd.MM.yyyy'
date: 'd.m.Y'
date_time: 'd.m. H:i'
fr:
date_time_type: 'dd/MM/yyyy HH:mm'
date_type: 'dd/MM/yyyy'
date: 'd/m/Y'
date_time: 'd/m H:i'
duration: '%%h h %%m'
he:
date_time_type: 'dd/MM/yyyy HH:mm'
date_type: 'dd/MM/yyyy'
date: 'd/m/Y'
date_time: 'd/m H:i'
duration: '%%h:%%m'
hu:
date_time_type: 'yyyy.MM.dd. HH:mm'
date_type: 'yyyy.MM.dd.'
date: 'Y.m.d.'
date_time: 'm.d. H:i'
it:
date_time_type: 'dd.MM.yyyy HH:mm'
date_type: 'dd.MM.yyyy'
date: 'd.m.Y'
date_time: 'd.m. H:i'
nl:
date_time_type: 'dd.MM.yyyy HH:mm'
date_type: 'dd.MM.yyyy'
date: 'd.m.Y'
date_time: 'd.m. H:i'
duration: '%%hu%%m'
pt_BR:
date_time_type: 'dd-MM-yyyy HH:mm'
date_type: 'dd-MM-yyyy'
date: 'd-m-Y'
date_time: 'd-m H:i'
ru:
date_time_type: 'dd.MM.yyyy HH:mm'
date_type: 'dd.MM.yyyy'
date: 'd.m.Y'
date_time: 'd.m. H:i'
sk:
date_time_type: 'dd. MM. yyyy HH:mm'
date_type: 'dd. MM. yyyy'
date: 'd. m. Y'
date_time: 'd. m. H:i'
sv:
duration: '%%h:%%m tim'
date_time: 'd/m H:i'
pl:
date_time_type: 'dd. MM. yyyy HH:mm'
date_type: 'dd. MM. yyyy'
date: 'd. m. Y'
date_time: 'd. m. H:i'
Expand Down
3 changes: 1 addition & 2 deletions src/API/ConfigurationController.php
Expand Up @@ -64,13 +64,12 @@ public function i18nAction(LanguageFormattings $formats): Response

$model = new I18nConfig();
$model
->setFormDateTime($formats->getDateTimeTypeFormat($locale))
->setFormDate($formats->getDateTypeFormat($locale))
->setDateTime($formats->getDateTimeFormat($locale))
->setDate($formats->getDateFormat($locale))
->setDuration($formats->getDurationFormat($locale))
->setTime($formats->getTimeFormat($locale))
->setIs24hours($formats->isTwentyFourHours($locale))
->setIs24hours($user->is24Hour())
->setNow($this->getDateTimeFactory()->createDateTime())
;

Expand Down
18 changes: 0 additions & 18 deletions src/API/Model/I18nConfig.php
Expand Up @@ -18,17 +18,6 @@
*/
final class I18nConfig
{
/**
* Format used for 'begin' and 'end'
*
* @var string
*
* @Serializer\Expose()
* @Serializer\Groups({"Default"})
* @Serializer\Type(name="string")
* @phpstan-ignore-next-line
*/
private $formDateTime = '';
/**
* Format used for toolbar queries
*
Expand Down Expand Up @@ -114,13 +103,6 @@ public function setNow(\DateTime $now): I18nConfig
return $this;
}

public function setFormDateTime(string $formDateTime): I18nConfig
{
$this->formDateTime = $formDateTime;

return $this;
}

public function setFormDate(string $formDate): I18nConfig
{
$this->formDate = $formDate;
Expand Down
33 changes: 0 additions & 33 deletions src/Configuration/LanguageFormattings.php
Expand Up @@ -60,28 +60,6 @@ public function getDatePickerFormat(string $locale): string
return $this->momentFormatter->convert($this->getDateTypeFormat($locale));
}

/**
* Returns the format which is used by the form component to handle datetime values.
*
* @param string $locale
* @return string
*/
public function getDateTimeTypeFormat(string $locale): string
{
return $this->getConfig('date_time_type', $locale);
}

/**
* Returns the format which is used by the Javascript component to handle datetime values.
*
* @param string $locale
* @return string
*/
public function getDateTimePickerFormat(string $locale): string
{
return $this->momentFormatter->convert($this->getDateTimeTypeFormat($locale));
}

/**
* Returns the locale specific date format, which should be used in combination with the twig filter "|date".
*
Expand Down Expand Up @@ -126,17 +104,6 @@ public function getDurationFormat(string $locale): string
return $this->getConfig('duration', $locale);
}

/**
* Returns whether this locale uses the 24 hour format.
*
* @param string $locale
* @return bool
*/
public function isTwentyFourHours(string $locale): bool
{
return (bool) $this->getConfig('24_hours', $locale);
}

/**
* @param string $key
* @param string $locale
Expand Down
10 changes: 8 additions & 2 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -329,13 +329,19 @@ protected function getLanguagesNode()
->useAttributeAsKey('name', false) // see https://github.com/symfony/symfony/issues/18988
->arrayPrototype()
->children()
->scalarNode('date_time_type')->defaultValue('yyyy-MM-dd HH:mm')->end() // for DateTimeType
->scalarNode('date_time_type') // for DateTimeType
->defaultValue('yyyy-MM-dd HH:mm')
->setDeprecated('date_time_type is deprecated since 1.16 and was replaced by the 24 user configuration')
->end()
->scalarNode('date_type')->defaultValue('yyyy-MM-dd')->end() // for DateType
->scalarNode('date')->defaultValue('Y-m-d')->end() // for display via twig
->scalarNode('date_time')->defaultValue('m-d H:i')->end() // for display via twig
->scalarNode('duration')->defaultValue('%%h:%%m h')->end() // for display via twig
->scalarNode('time')->defaultValue('H:i')->end() // for display via twig
->booleanNode('24_hours')->defaultTrue()->end() // for DateTimeType JS component
->booleanNode('24_hours') // for DateTimeType JS component
->defaultTrue()
->setDeprecated('24_hours is deprecated since 1.16 and a user configuration now')
->end()
->end()
->end()
;
Expand Down
14 changes: 14 additions & 0 deletions src/Entity/User.php
Expand Up @@ -430,6 +430,20 @@ public function getPreference(string $name): ?UserPreference
return null;
}

public function getTimeFormat(): string
{
if ($this->is24Hour()) {
return 'H:i';
}

return 'h:i A';
}

public function is24Hour(): bool
{
return (bool) $this->getPreferenceValue(UserPreference::HOUR_24, true);
}

public function getLocale(): string
{
return $this->getPreferenceValue(UserPreference::LOCALE, User::DEFAULT_LANGUAGE);
Expand Down
1 change: 1 addition & 0 deletions src/Entity/UserPreference.php
Expand Up @@ -30,6 +30,7 @@ class UserPreference
public const INTERNAL_RATE = 'internal_rate';
public const SKIN = 'skin';
public const LOCALE = 'language';
public const HOUR_24 = 'hours_24';
public const TIMEZONE = 'timezone';
public const FIRST_WEEKDAY = 'first_weekday';

Expand Down
7 changes: 7 additions & 0 deletions src/EventSubscriber/UserPreferenceSubscriber.php
Expand Up @@ -112,6 +112,13 @@ public function getDefaultPreferences(User $user)
->setSection('locale')
->setType(FirstWeekDayType::class),

(new UserPreference())
->setName(UserPreference::HOUR_24)
->setValue(true)
->setOrder(305)
->setSection('locale')
->setType(CheckboxType::class),

(new UserPreference())
->setName(UserPreference::SKIN)
->setValue($this->configuration->getUserDefaultTheme())
Expand Down
21 changes: 16 additions & 5 deletions src/Form/Type/DateTimePickerType.php
Expand Up @@ -10,11 +10,15 @@
namespace App\Form\Type;

use App\API\BaseApiController;
use App\Entity\User;
use App\Utils\DateFormatConverter;
use App\Utils\LocaleSettings;
use App\Utils\MomentFormatConverter;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -34,9 +38,6 @@ public function __construct(LocaleSettings $localeSettings)
*/
public function configureOptions(OptionsResolver $resolver)
{
$dateTimePicker = $this->localeSettings->getDateTimePickerFormat();
$dateTimeFormat = $this->localeSettings->getDateTimeTypeFormat();

$resolver->setDefaults([
'documentation' => [
'type' => 'string',
Expand All @@ -46,8 +47,18 @@ public function configureOptions(OptionsResolver $resolver)
'label' => 'label.begin',
'widget' => 'single_text',
'html5' => false,
'format' => $dateTimeFormat,
'format_picker' => $dateTimePicker,
'format' => function (Options $options) {
/** @var User $user */
$user = $options['user'];
$converter = new DateFormatConverter();

return $this->localeSettings->getDateTypeFormat() . ' ' . $converter->convert($user->getTimeFormat()); // PHP
},
'format_picker' => function (Options $options) {
$converter = new MomentFormatConverter();

return $converter->convert($options['format']); // JS
},
'with_seconds' => false,
'time_increment' => 1,
]);
Expand Down

0 comments on commit 8b0962e

Please sign in to comment.