From a0dd66e71949fc78f6ea9d88a0d4cd241185f373 Mon Sep 17 00:00:00 2001
From: Moritz Strohm <strohm@data-quest.de>
Date: Mon, 27 May 2024 15:37:49 +0000
Subject: [PATCH] set defaultDate on changed calendar dates in sidebar widgets,
 fixes #3924

Closes #3924

Merge request studip/studip!3046
---
 app/controllers/calendar/calendar.php         |  6 +++++-
 .../assets/javascripts/lib/fullcalendar.js    | 19 +++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/app/controllers/calendar/calendar.php b/app/controllers/calendar/calendar.php
index fa33e2e5fad..0cb96ad9178 100644
--- a/app/controllers/calendar/calendar.php
+++ b/app/controllers/calendar/calendar.php
@@ -111,6 +111,8 @@ class Calendar_CalendarController extends AuthenticatedController
     {
         PageLayout::setTitle(_('Kalender'));
 
+        $default_date = \Studip\Calendar\Helper::getDefaultCalendarDate();
+
         if (Request::isPost()) {
             //In case the checkbox of the options widget is clicked, the resulting
             //POST request must be catched here and result in a redirect.
@@ -203,6 +205,7 @@ class Calendar_CalendarController extends AuthenticatedController
             if ($calendar_owner && $calendar_owner->id === User::findCurrent()->id) {
                 //The user is viewing their own calendar.
                 $options = new OptionsWidget();
+                $options->addLayoutCSSClass('calendar-action');
                 $options->addCheckbox(
                     _('Abgelehnte Termine anzeigen'),
                     Request::bool('show_declined'),
@@ -237,6 +240,7 @@ class Calendar_CalendarController extends AuthenticatedController
                         $this->url_for('calendar/calendar/index', ['view' => 'group']),
                         'group_id'
                     );
+                    $group_select->addLayoutCSSClass('calendar-action');
                     $options = [
                         '' => _('(bitte wählen)')
                     ];
@@ -263,6 +267,7 @@ class Calendar_CalendarController extends AuthenticatedController
                     $this->url_for('calendar/calendar'),
                     'user_id'
                 );
+                $calendar_select->addLayoutCSSClass('calendar-action');
                 $select_options = [
                     '' => _('(bitte wählen)'),
                     User::findCurrent()->id => _('Eigener Kalender')
@@ -335,7 +340,6 @@ class Calendar_CalendarController extends AuthenticatedController
         $slot_durations = $this->getUserCalendarSlotSettings();
 
         //Create the fullcalendar object:
-        $default_date = \Studip\Calendar\Helper::getDefaultCalendarDate();
 
         $data_url_params = [];
         if (Request::bool('show_declined')) {
diff --git a/resources/assets/javascripts/lib/fullcalendar.js b/resources/assets/javascripts/lib/fullcalendar.js
index b001ab4c4cb..3b8fa11050d 100644
--- a/resources/assets/javascripts/lib/fullcalendar.js
+++ b/resources/assets/javascripts/lib/fullcalendar.js
@@ -737,6 +737,25 @@ class Fullcalendar
                 url.searchParams.set('defaultDate', changed_date)
                 this.href = url.toString();
             });
+            jQuery('.sidebar-widget.calendar-action').each(function() {
+                //Each sidebar widget is different. The placement of the defaultDate URL parameter
+                //has to reflect that.
+                jQuery(this).find('button[formaction]').each(function() {
+                    //Modify the formaction attribute:
+                    let url = new URL(jQuery(this).attr('formaction'));
+                    url.searchParams.set('defaultDate', changed_date);
+                    jQuery(this).attr('formaction', url.toString());
+                });
+                jQuery(this).find('form[action]').each(function() {
+                    //Add a hidden input with the defaultDate:
+                    let hidden_input = jQuery(this).find('input[name="defaultDate"]')[0];
+                    if (!hidden_input) {
+                        hidden_input = jQuery('<input type="hidden" name="defaultDate">');
+                        jQuery(this).append(hidden_input);
+                    }
+                    jQuery(hidden_input).val(changed_date);
+                });
+            });
 
             // Now change the URL of the window.
             const url = new URL(window.location.href);
-- 
GitLab