Merge pull request #5428 from christianbeeznest/ofaj-21532-3

Internal: Ensure default platform language is used for datetime picker on initial load - refs BT#21532
pull/5429/head
christianbeeznest 2 years ago committed by GitHub
commit aaefd6f666
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 56
      public/main/inc/lib/formvalidator/Element/DatePicker.php
  2. 64
      public/main/inc/lib/formvalidator/Element/DateTimePicker.php

@ -3,6 +3,7 @@
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Component\Utils\ToolIcon;
use Chamilo\CoreBundle\Framework\Container;
/**
* Form element to select a date.
@ -99,28 +100,38 @@ class DatePicker extends HTML_QuickForm_text
$localeCode = $this->getLocaleCode();
$id = $this->getAttribute('id');
$localeScript = '';
if ($localeCode !== 'en') {
$localeScript = '<script async="false" src="/build/flatpickr/l10n/' . $localeCode . '.js"></script>';
}
return $localeScript . "<script>
return "<script>
document.addEventListener('DOMContentLoaded', function () {
const fp = flatpickr('#{$id}', {
locale: '{$localeCode}',
altInput: true,
altFormat: '".get_lang('F d, Y')."',
enableTime: false,
dateFormat: 'Y-m-d',
time_24hr: true,
wrap: false
});
if ($('label[for=\"".$id."\"]').length > 0) {
$('label[for=\"".$id."\"]').hide();
function initializeFlatpickr() {
const fp = flatpickr('#{$id}', {
locale: '{$localeCode}',
altInput: true,
altFormat: '".get_lang('F d, Y')."',
enableTime: false,
dateFormat: 'Y-m-d',
time_24hr: true,
wrap: false
});
if ($('label[for=\"".$id."\"]').length > 0) {
$('label[for=\"".$id."\"]').hide();
}
document.querySelector('label[for=\"' + '{$id}' + '\"]').classList.add('datepicker-label');
}
function loadLocale() {
if ('{$localeCode}' !== 'en') {
var script = document.createElement('script');
script.src = '/build/flatpickr/l10n/{$localeCode}.js';
script.onload = initializeFlatpickr;
document.head.appendChild(script);
} else {
initializeFlatpickr();
}
}
document.querySelector('label[for=\"' + '{$id}' + '\"]').classList.add('datepicker-label');
loadLocale();
});
</script>";
}
@ -135,7 +146,12 @@ class DatePicker extends HTML_QuickForm_text
*/
private function getLocaleCode(): string
{
$locale = api_get_language_isocode();
$locale = api_get_setting('language.platform_language');
$request = Container::getRequest();
if ($request) {
$locale = $request->getLocale();
}
$userInfo = api_get_user_info();
if (is_array($userInfo) && !empty($userInfo['language']) && ANONYMOUS != $userInfo['status']) {
$locale = $userInfo['language'];

@ -67,40 +67,50 @@ class DateTimePicker extends HTML_QuickForm_text
*
* @return string
*/
private function getElementJS()
private function getElementJS(): string
{
$localeCode = $this->getLocaleCode();
$id = $this->getAttribute('id');
$localeScript = '';
if ($localeCode !== 'en') {
$localeScript = '<script async="false" src="/build/flatpickr/l10n/' . $localeCode . '.js"></script>';
}
$js = $localeScript . "<script>
$js = "<script>
document.addEventListener('DOMContentLoaded', function () {
const fp = flatpickr('#{$id}', {
locale: '{$localeCode}',
altInput: true,
altFormat: '".get_lang('F d, Y')." ".get_lang('at')." H:i',
enableTime: true,
dateFormat: 'Y-m-d H:i',
time_24hr: true,
wrap: false,
onReady: function(selectedDates, dateStr, instance) {
const validateButton = document.createElement('button');
validateButton.textContent = '".get_lang('Validate')."';
validateButton.className = 'flatpickr-validate-btn';
validateButton.type = 'button';
validateButton.onclick = function() {
instance.close();
};
instance.calendarContainer.appendChild(validateButton);
function initializeFlatpickr() {
const fp = flatpickr('#{$id}', {
locale: '{$localeCode}',
altInput: true,
altFormat: '".get_lang('F d, Y')." ".get_lang('at')." H:i',
enableTime: true,
dateFormat: 'Y-m-d H:i',
time_24hr: true,
wrap: false,
onReady: function(selectedDates, dateStr, instance) {
const validateButton = document.createElement('button');
validateButton.textContent = '".get_lang('Validate')."';
validateButton.className = 'flatpickr-validate-btn';
validateButton.type = 'button';
validateButton.onclick = function() {
instance.close();
};
instance.calendarContainer.appendChild(validateButton);
}
});
document.querySelector('label[for=\"' + '{$id}' + '\"]').classList.add('datepicker-label');
}
function loadLocaleAndInitialize() {
if ('{$localeCode}' !== 'en') {
var script = document.createElement('script');
script.src = '/build/flatpickr/l10n/' + '{$localeCode}.js';
script.onload = initializeFlatpickr;
document.head.appendChild(script);
} else {
initializeFlatpickr();
}
});
}
document.querySelector('label[for=\"' + '{$id}' + '\"]').classList.add('datepicker-label');
loadLocaleAndInitialize();
});
</script>";

Loading…
Cancel
Save