From d57219a86c2bae0777ba34b6cea8f2f3a35e372f Mon Sep 17 00:00:00 2001
From: Lucas Gass <lucas@bywatersolutions.com>
Date: Mon, 23 Sep 2024 16:23:13 +0000
Subject: [PATCH] Bug 37398: Add set to today function to calendar.inc

To test:
1. Try editing some items with a empty value in the flatpickr.
2. Make sure the empty date fields get a date set to today when you click on the empty field.
3. Try a datetime field, making sure the datetime is correctly set.
---
 koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc    | 9 +++++++++
 .../intranet-tmpl/prog/en/includes/html_helpers.inc      | 4 ++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc
index 9f1cdb6ceb2..384ade62cc8 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc
@@ -122,6 +122,15 @@
                 }
             }
         },
+        onOpen: function( selectedDates, dateText, instance) {
+            var thisInput = instance.input;
+            let set_to_today = $(thisInput).data('flatpickr-set-to-today');
+            if ( set_to_today ) {
+                if ( thisInput.value === '' ) {
+                    instance.setDate( new Date() );
+                }
+            }
+        },
         onClose: function( selectedDates, dateText, instance) {
             validate_date( dateText, instance );
             var thisInput = instance.input;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc
index 48c1c682f33..ead708ade3a 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc
@@ -167,9 +167,9 @@
                             <textarea id="[%- mv.id | html -%]" name="[% kohafield | html %]" class="input_marceditor [% kohafield | html %]" rows="5" cols="64" >[% mv.value | html %]</textarea>
                         [% END %]
                     [% ELSIF ( mv.type == 'date_field' ) %]
-                         <input type="text" id="[% mv.id | html %]" name="[% kohafield | html %]" class="input_marceditor [% kohafield | html %] flatpickr" value="[% mv.value | html %]" class="flatpickr" />
+                         <input type="text" id="[% mv.id | html %]" name="[% kohafield | html %]" class="input_marceditor [% kohafield | html %] flatpickr" value="[% mv.value | html %]" class="flatpickr" data-flatpickr-set-to-today="true" />
                     [% ELSIF ( mv.type == 'datetime_field' ) %]
-                         <input type="text" id="[% mv.id | html %]" name="[% kohafield | html %]" class="input_marceditor [% kohafield | html %] flatpickr" value="[% mv.value | html %]" data-flatpickr-enable-time="true" />
+                         <input type="text" id="[% mv.id | html %]" name="[% kohafield | html %]" class="input_marceditor [% kohafield | html %] flatpickr" value="[% mv.value | html %]" data-flatpickr-enable-time="true" data-flatpickr-set-to-today="true" />
                     [% END %]
 
                     [% IF subfield.kohafield == 'items.more_subfields_xml' %]
-- 
2.39.2