From d9b822f45a94418be2846e6450e11c87c9cc9d90 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 9 May 2022 10:28:51 +0200 Subject: [PATCH] Bug 30717: Format dates when editing items This is a result of bug 29369 and bug 27526. Bug 29369 removed the format of the dates, and bug 27526 assumed that it was the correct behaviour. Here we don't want to let the controller know which fields is a date, or we will have to access the subfield structure to know which subfields have the "date cataloguing plugin". This patch suggests to use the altFormat options from flatpickr that will "Show the user a readable date (as per altFormat), but return something totally different to the server." It's actually an option we want to use for all our dates, that will reduce a lot the overhead in our controllers. Test plan: Edit items (additem.pl and in batch) and confirm that the subfields using the dateaccessioned plugin are displayed according to the dateformat syspref. Make sure the date is stored correctly. --- cataloguing/value_builder/dateaccessioned.pl | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cataloguing/value_builder/dateaccessioned.pl b/cataloguing/value_builder/dateaccessioned.pl index 6532d29b078..272b5aecfe2 100755 --- a/cataloguing/value_builder/dateaccessioned.pl +++ b/cataloguing/value_builder/dateaccessioned.pl @@ -27,7 +27,14 @@ my $builder = sub { my ( $params ) = @_; my $function_name = $params->{id}; - my $date = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }); + my $date = output_pref({ dt => dt_from_string, dateonly => 1 }); + + my $dateformat_pref = C4::Context->preference('dateformat'); + my $dateformat = + $dateformat_pref eq 'us' ? 'm/d/Y' + : $dateformat_pref eq 'metric' ? 'd/m/Y' + : $dateformat_pref eq 'dmydot' ? 'd.m.Y' + : 'Y-m-d'; my $res = < @@ -35,6 +42,9 @@ my $builder = sub { \$(document).ready(function(){ \$("#$function_name").flatpickr({ + altInput: true, + altFormat: "$dateformat", + altInputClass: "input_marceditor flatpickr-input", dateFormat: "Y-m-d" }); /* Set current date on page load */ @@ -53,8 +63,10 @@ function Click$function_name(event) { function set_to_today( id, force ) { /* The force parameter is used in Click but not in Focus ! */ if (! id) { alert(_("Bad id ") + id + _(" sent to set_to_today()")); return 0; } - if (\$("#" + id).val() == '' || force ) { - \$("#" + id).val("$date"); + var elt = document.querySelector("#" + id); + if ( elt.value == '' || force ) { + const fp = document.querySelector("#" + id)._flatpickr; + fp.setDate(new Date()); } } -- 2.25.1