From ff198de85cf50c71e9b278fbf9d9c83d1ef35191 Mon Sep 17 00:00:00 2001
From: Elmar Ludwig <elmar.ludwig@uni-osnabrueck.de>
Date: Thu, 31 Aug 2023 08:02:33 +0000
Subject: [PATCH] add data-formaction attribute for submit-upon-select, fixes
 #3115

Closes #3115

Merge request studip/studip!2097
---
 lib/classes/sidebar/OptionsWidget.php         | 28 +++++++++++--------
 .../assets/javascripts/bootstrap/forms.js     |  3 ++
 2 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/lib/classes/sidebar/OptionsWidget.php b/lib/classes/sidebar/OptionsWidget.php
index a3660b5cc8c..304a5ac26ca 100644
--- a/lib/classes/sidebar/OptionsWidget.php
+++ b/lib/classes/sidebar/OptionsWidget.php
@@ -77,20 +77,26 @@ class OptionsWidget extends ListWidget
      */
     public function addSelect($label, $url, $name, $options, $selected_option = false, $attributes = [])
     {
-        $widget = new SelectWidget($label, $url, $name);
-        $widget->layout = false;
+        $option_content = '';
 
-        foreach ($options as $value => $option_label) {
-            $widget->addElement(new SelectElement($value, $option_label, $value === $selected_option));
+        foreach ($options as $value => $option) {
+            $selected = $value === $selected_option ? 'selected' : '';
+            $option_content .= sprintf(
+                '<option value="%s" %s>%s</option>', 
+                htmlReady($value),
+                $selected, 
+                htmlReady($option)
+            );
         }
 
-        if (isset($widget->attributes) && is_array($widget->attributes)) {
-            $widget->attributes = array_merge($widget->attributes, $attributes);
-        } else {
-            $widget->attributes = $attributes;
-        }
-
-        $content = $widget->render();
+        $content = sprintf(
+            '<select data-formaction="%s" class="sidebar-selectlist submit-upon-select" name="%s" aria-label="%s" %s>%s</select>',
+            htmlReady($url),
+            htmlReady($name),
+            htmlReady($label),
+            arrayToHtmlAttributes($attributes),
+            $option_content
+        );
 
         $this->addElement(new WidgetElement($content));
     }
diff --git a/resources/assets/javascripts/bootstrap/forms.js b/resources/assets/javascripts/bootstrap/forms.js
index cffc17d9d63..16e403191dd 100644
--- a/resources/assets/javascripts/bootstrap/forms.js
+++ b/resources/assets/javascripts/bootstrap/forms.js
@@ -107,6 +107,9 @@ $(document)
         // Submit only if value has changed and either enter was pressed or
         // select was opened by click
         if (!is_default && shouldSubmit) {
+            if ($(this).data('formaction')) {
+                $(this.form).attr('action', $(this).data('formaction'));
+            }
             $(this.form).submit();
             $('option', this).prop('defaultSelected', false).filter(':selected').prop('defaultSelected', true);
             return false;
-- 
GitLab