From 7ba5c83bcbb05b0d32fcb558786a6cd07f796977 Mon Sep 17 00:00:00 2001
From: David Siegfried <david.siegfried@uni-vechta.de>
Date: Tue, 5 Apr 2022 14:52:25 +0000
Subject: [PATCH] fix js-error and cleanup, refs #225

---
 .../sidebar/RoomSearchWidget.class.php        | 73 ++++---------------
 resources/assets/javascripts/lib/resources.js | 46 ++++++------
 2 files changed, 35 insertions(+), 84 deletions(-)

diff --git a/lib/classes/sidebar/RoomSearchWidget.class.php b/lib/classes/sidebar/RoomSearchWidget.class.php
index b1fdc2d47cd..652edfb84ca 100644
--- a/lib/classes/sidebar/RoomSearchWidget.class.php
+++ b/lib/classes/sidebar/RoomSearchWidget.class.php
@@ -11,7 +11,6 @@ class RoomSearchWidget extends SidebarWidget
     protected $selected_criteria;
     protected $defined_properties;
 
-
     protected function setupSearchParameters()
     {
         $this->defined_properties = RoomManager::getAllRoomPropertyDefinitions(
@@ -47,32 +46,6 @@ class RoomSearchWidget extends SidebarWidget
             ];
         }
 
-        $locations = Location::findAll();
-        $location_options = [
-            [
-                'id' => '',
-                'name' => _('Alle Standorte und Gebäude'),
-                'sub_options' => []
-            ]
-        ];
-        if ($locations) {
-            foreach ($locations as $location) {
-                $buildings = Building::findByLocation($location->id);
-                $sub_options = [];
-                foreach ($buildings as $building) {
-                    $sub_options[] = [
-                        'id' => 'building_' . $building->id,
-                        'name' => $building->name
-                    ];
-                }
-                $location_options[] = [
-                    'id' => 'location_' . $location->id,
-                    'name' => $location->name,
-                    'sub_options' => $sub_options
-                ];
-            }
-        }
-
         $this->criteria = [];
 
         if ($this->defined_properties) {
@@ -126,7 +99,6 @@ class RoomSearchWidget extends SidebarWidget
             'title' => _('Standort / Gebäude'),
             'type' => 'hidden',
             'range_search' => false,
-            //'options' => $location_options,
             'switch' => false,
             'value' => '',
             'optional' => false
@@ -141,7 +113,6 @@ class RoomSearchWidget extends SidebarWidget
                     'title' => _('Standort / Gebäude'),
                     'type' => 'disabled_text',
                     'range_search' => false,
-                    //'options' => $location_options,
                     'switch' => false,
                     'value' => $selected_res->name,
                     'optional' => false
@@ -302,7 +273,7 @@ class RoomSearchWidget extends SidebarWidget
                                 );
                         }
                     }
-                } elseif (($data['type'] == 'num') and $data['range_search']) {
+                } elseif ($data['type'] === 'num' && $data['range_search']) {
                     if (Request::submitted($name . '_min')
                         || Request::submitted($name . '_max')) {
                         $this->selected_criteria[$name] = $data;
@@ -324,7 +295,6 @@ class RoomSearchWidget extends SidebarWidget
             $this->selected_criteria;
     }
 
-
     protected function restoreSearchFromSession()
     {
         if (is_array($_SESSION['room_search_criteria']['room_search'])) {
@@ -335,7 +305,6 @@ class RoomSearchWidget extends SidebarWidget
         }
     }
 
-
     protected function search()
     {
         //The properties array is a "simplified" version of the
@@ -492,7 +461,7 @@ class RoomSearchWidget extends SidebarWidget
                                 )
                             );
                         }
-                        $begin->setTime(0,0,0);
+                        $begin->setTime(0,0);
                         $end = clone $begin;
                         $end = $end->add(
                             new DateInterval('P1D')
@@ -523,14 +492,12 @@ class RoomSearchWidget extends SidebarWidget
         );
     }
 
-
     public function resetSearch()
     {
         $this->selected_criteria = [];
         $_SESSION['room_search_criteria']['room_search'] = [];
     }
 
-
     public function __construct($action_link = '')
     {
         parent::__construct();
@@ -555,25 +522,21 @@ class RoomSearchWidget extends SidebarWidget
         }
     }
 
-
     public function searchRequested()
     {
         return Request::submitted('room_search');
     }
 
-
     public function searchResetRequested()
     {
         return Request::submitted('room_search_reset');
     }
 
-
     public function getResults()
     {
         return $this->rooms;
     }
 
-
     public function setActionLink($action_link = '')
     {
         if (!$action_link) {
@@ -595,27 +558,17 @@ class RoomSearchWidget extends SidebarWidget
 
     public function render($variables = [])
     {
-        $variables['title'] = _('Suchkriterien für Räume');
-
-        $template = $GLOBALS['template_factory']->open(
-            $this->template
-        );
-
-        $template->set_layout('widgets/widget-layout');
-
-        $template->set_attributes($variables);
-
-        $template->set_attribute(
-            'criteria',
-            $this->criteria
+        $variables = array_merge($variables, [
+            'title'             => _('Suchkriterien für Räume'),
+            'criteria'          => $this->criteria,
+            'selected_criteria' => $this->selected_criteria,
+            'action_link'       => $this->action_link,
+        ]);
+
+        return $GLOBALS['template_factory']->render(
+            $this->template,
+            $variables,
+            'widgets/widget-layout'
         );
-        $template->set_attribute(
-            'action_link',
-            $this->action_link
-        );
-
-        $template->set_attribute('selected_criteria', $this->selected_criteria);
-
-        return $template->render();
     }
 }
diff --git a/resources/assets/javascripts/lib/resources.js b/resources/assets/javascripts/lib/resources.js
index 1aa861a6ebb..4189e927d87 100644
--- a/resources/assets/javascripts/lib/resources.js
+++ b/resources/assets/javascripts/lib/resources.js
@@ -212,23 +212,23 @@ class Resources
             return;
         }
 
-        var selected_option = jQuery(select_node).find(":selected")[0];
+        let selected_option = jQuery(select_node).find(":selected")[0];
         if (!selected_option) {
             return;
         }
 
-        var option_value = jQuery(selected_option).val();
+        let option_value = jQuery(selected_option).val();
         if (!option_value) {
             //The first option which is left blank intentionally
             //has been selected.
             return;
         }
-        var option_title = jQuery(selected_option).attr('data-title');
-        var option_type = jQuery(selected_option).attr('data-type');
-        var option_select_options = jQuery(selected_option).attr('data-select_options').split(';;');
-        var option_range_search = jQuery(selected_option).attr('data-range-search');
+        let option_title = jQuery(selected_option).data('title');
+        let option_type = jQuery(selected_option).data('type');
+        let option_select_options = jQuery(selected_option).data('select_options').split(';;');
+        let option_range_search = jQuery(selected_option).data('range-search');
+        let template = undefined;
 
-        var template = undefined;
         if (option_type === 'bool') {
             template = jQuery(select_node).parent().parent().find(
                 '.criteria-list .template[data-template-type="'
@@ -269,43 +269,41 @@ class Resources
             return;
         }
 
-        var criteria_list = jQuery(template).parent();
-
-        var new_criteria = jQuery(template).clone();
+        let criteria_list = jQuery(template).parent();
+        let new_criteria = jQuery(template).clone();
         jQuery(new_criteria).attr('class', 'item');
         jQuery(new_criteria).attr('data-criteria', option_value);
 
-        var new_criteria_text_field = jQuery(new_criteria).find('span')[0];
+        let new_criteria_text_field = jQuery(new_criteria).find('span')[0];
         jQuery(new_criteria_text_field).text(option_title);
 
         if (option_type === 'bool') {
-            var new_criteria_input = jQuery(new_criteria).find('input');
+            let new_criteria_input = jQuery(new_criteria).find('input');
             jQuery(new_criteria_input).attr('name', option_value);
         } else if (option_type === 'select') {
-            var new_criteria_select = jQuery(new_criteria).find('select')[0];
+            let new_criteria_select = jQuery(new_criteria).find('select')[0];
             jQuery(new_criteria_select).attr('name', option_value);
             //Build the option elements from the data-options field:
             if (!option_select_options) {
                 //Something is wrong.
                 return;
             }
-            var options_html = '';
-            for (option of option_select_options) {
-                var splitted_option = option.split('~~');
+            let options_html = '';
+            for (let option of option_select_options) {
+                let splitted_option = option.split('~~');
                 options_html += '<option value="' + splitted_option[0] + '">'
                     + splitted_option[1]
                     + '</option>';
             }
-            jQuery(new_criteria_select).html(options_html);
         } else if (option_type === 'date') {
-            var time_inputs = jQuery(new_criteria).find('input[data-time="yes"]');
-            var date_inputs = jQuery(new_criteria).find('input[type="date"]');
+            let time_inputs = jQuery(new_criteria).find('input[data-time="yes"]');
+            let date_inputs = jQuery(new_criteria).find('input[type="date"]');
 
             if (time_inputs.length < 2) {
                 //Something is wrong with the HTML.
                 return;
             }
-            var now = new Date();
+            let now = new Date();
 
             jQuery(time_inputs[0]).attr('name', option_value + '_begin_time');
             jQuery(time_inputs[1]).attr('name', option_value + '_end_time');
@@ -351,10 +349,10 @@ class Resources
 
         } else {
             if (option_type === 'num' && option_range_search) {
-                var new_criteria_inputs = jQuery(new_criteria).find('input');
+                let new_criteria_inputs = jQuery(new_criteria).find('input');
                 jQuery(new_criteria_inputs[0]).attr('name', option_value);
-                var min_input = new_criteria_inputs[1];
-                var max_input = new_criteria_inputs[2];
+                let min_input = new_criteria_inputs[1];
+                let max_input = new_criteria_inputs[2];
                 jQuery(min_input).attr('name', option_value + '_min');
                 jQuery(min_input).attr('type', 'number');
                 jQuery(max_input).attr('name', option_value + '_max');
@@ -362,7 +360,7 @@ class Resources
                 jQuery(min_input).val(Math.round(parseInt(min_input) * 1.25));
                 jQuery(max_input).val(Math.round(parseInt(max_input) * 0.75));
             } else {
-                var new_criteria_input = jQuery(new_criteria).find('input')[0];
+                let new_criteria_input = jQuery(new_criteria).find('input')[0];
                 jQuery(new_criteria_input).attr('name', option_value);
                 if (option_type === 'num') {
                     jQuery(new_criteria_input).attr('type', 'number');
-- 
GitLab