From 2ebb832cd22c396c8d7a767e21c65469451b99f6 Mon Sep 17 00:00:00 2001
From: Elmar Ludwig <elmar.ludwig@uni-osnabrueck.de>
Date: Tue, 15 Aug 2023 11:40:17 +0000
Subject: [PATCH] simplify search settings code (and drop redundant order
 value), fixes #2967

Closes #2967

Merge request studip/studip!2013
---
 app/controllers/globalsearch.php              | 53 ++++---------------
 app/views/global_search/settings.php          | 26 ++++-----
 .../globalsearch/GlobalSearchModule.php       |  2 +-
 3 files changed, 21 insertions(+), 60 deletions(-)

diff --git a/app/controllers/globalsearch.php b/app/controllers/globalsearch.php
index 1a5915cd877..4a198a200cc 100644
--- a/app/controllers/globalsearch.php
+++ b/app/controllers/globalsearch.php
@@ -131,34 +131,24 @@ class GlobalSearchController extends AuthenticatedController
         Navigation::activateItem('/admin/config/globalsearch');
 
         $this->config = Config::get()->GLOBALSEARCH_MODULES;
-
         $this->modules = [];
 
-        // Scan for available modules.
-        foreach (scandir($GLOBALS['STUDIP_BASE_PATH'] . '/lib/classes/globalsearch') as $filename) {
-            $path = pathinfo($filename);
-            if ($path['extension'] === 'php') {
-                class_exists($path['filename']);
+        foreach ($this->config as $className => $config) {
+            if (class_exists($className)) {
+                $this->modules[$className] = new $className();
             }
         }
 
         // Search declared classes for GlobalSearchModules
-        $endindex = 100;
         foreach (get_declared_classes() as $className) {
-            if (is_a($className, 'GlobalSearchModule', true)
-                    && $className !== 'GlobalSearchModule') {
-                $class = new $className();
+            if (is_subclass_of($className, 'GlobalSearchModule')) {
 
                 // Add new classes at module array end and not activated.
-                if (in_array($className, array_keys($this->config))) {
-                    $this->modules[$this->config[$className]['order']] = $class;
-                } else {
-                    $this->modules[$endindex++] = $class;
+                if (!isset($this->modules[$className])) {
+                    $this->modules[$className] = new $className();
                 }
             }
         }
-
-        ksort($this->modules);
     }
 
     /**
@@ -170,40 +160,19 @@ class GlobalSearchController extends AuthenticatedController
 
         $config = [];
 
-        $order = 1;
         foreach (Request::getArray('modules') as $module) {
             $config[$module['class']] = [
-                'order'    => $order,
                 'active'   => (bool)$module['active'],
                 'fulltext' => is_a($module['class'], 'GlobalSearchFulltext', true) && $module['fulltext']
             ];
-            $order++;
         }
 
-        $success = true;
-        if (Request::int('async_queries', 0) != Config::get()->GLOBALSEARCH_ASYNC_QUERIES) {
-            $success = Config::get()->store('GLOBALSEARCH_ASYNC_QUERIES',
-                ['value' => Request::int('async_queries', 0)]);
-            Config::get()->GLOBALSEARCH_ASYNC_QUERIES = Request::int('async_queries', 0);
-        }
+        Config::get()->store('GLOBALSEARCH_ASYNC_QUERIES', Request::int('async_queries', 0));
+        Config::get()->store('GLOBALSEARCH_MAX_RESULT_OF_TYPE', Request::int('entries_per_type', 3));
+        Config::get()->store('GLOBALSEARCH_MODULES', $config);
 
-        if (Request::int('entries_per_type', 3) != Config::get()->GLOBALSEARCH_MAX_RESULT_OF_TYPE) {
-            $success = Config::get()->store('GLOBALSEARCH_MAX_RESULT_OF_TYPE',
-                ['value' => Request::int('entries_per_type', 3)]);
-            Config::get()->GLOBALSEARCH_MAX_RESULT_OF_TYPE = Request::int('entries_per_type', 3);
-        }
+        PageLayout::postSuccess(_('Die Einstellungen wurden gespeichert.'));
 
-        if ($config != Config::get()->GLOBALSEARCH_MODULES) {
-            $success = Config::get()->store('GLOBALSEARCH_MODULES', ['value' => $config]);
-        }
-
-        if ($success) {
-            PageLayout::postSuccess(_('Die Einstellungen wurden gespeichert.'));
-        } else {
-            PageLayout::postError(_('Die Einstellungen konnten nicht gespeichert werden.'));
-        }
-
-        $this->relocate('globalsearch/settings');
+        $this->redirect('globalsearch/settings');
     }
-
 }
diff --git a/app/views/global_search/settings.php b/app/views/global_search/settings.php
index a0bb852a997..65aaa37e971 100644
--- a/app/views/global_search/settings.php
+++ b/app/views/global_search/settings.php
@@ -45,33 +45,25 @@
                 </tr>
             </thead>
             <tbody>
-                <?php foreach ($modules as $module) : ?>
+                <?php foreach ($modules as $className => $module) : ?>
                     <tr>
                         <td class="drag-handle"></td>
                         <td>
-                            <?php $resourcesInactive = (get_class($module) == 'GlobalSearchResources' ||
-                                get_class($module) == 'GlobalSearchRoomBookings') && !Config::get()->RESOURCES_ENABLE ?>
-                            <label for="active[<?= htmlReady(get_class($module)) ?>]"
-                                <?php if ($resourcesInactive) : ?>
-                                    class="inactive-settings-category"
-                                    data-tooltip="<?= htmlReady(sprintf(_('%s sind inaktiv, da die Ressourcenverwaltung derzeit deaktiviert ist.'), $module->getName())) ?>"
-                                <?php endif ?>
-                            >
+                            <label for="active[<?= htmlReady($className) ?>]">
                                 <?= htmlReady($module->getName()) ?>
                             </label>
-                            <input type="hidden" name="modules[<?= htmlReady(get_class($module)) ?>][class]"
-                                   value="<?= htmlReady(get_class($module)) ?>">
+                            <input type="hidden" name="modules[<?= htmlReady($className) ?>][class]"
+                                   value="<?= htmlReady($className) ?>">
                         </td>
                         <td>
-                            <input type="checkbox" id="active[<?= htmlReady(get_class($module)) ?>]"
-                                   name="modules[<?= htmlReady(get_class($module)) ?>][active]" value="1"
-                                <?= !empty($config[get_class($module)]['active']) && !$resourcesInactive ? ' checked' : '' ?>
-                                <?= $resourcesInactive ? ' disabled' : '' ?>>
+                            <input type="checkbox" id="active[<?= htmlReady($className) ?>]"
+                                   name="modules[<?= htmlReady($className) ?>][active]" value="1"
+                                <?= !empty($config[$className]['active']) ? ' checked' : '' ?>>
                         </td>
                         <td>
                             <?php if (is_a($module, 'GlobalSearchFulltext')) : ?>
-                                <input type="checkbox" name="modules[<?= htmlReady(get_class($module)) ?>][fulltext]"
-                                       value="1"<?= $config[get_class($module)]['fulltext'] ? ' checked' : ''?>>
+                                <input type="checkbox" name="modules[<?= htmlReady($className) ?>][fulltext]"
+                                       value="1"<?= $config[$className]['fulltext'] ? ' checked' : ''?>>
                             <?php endif ?>
                         </td>
                     </tr>
diff --git a/lib/classes/globalsearch/GlobalSearchModule.php b/lib/classes/globalsearch/GlobalSearchModule.php
index 7e9f251d2d7..82551c62695 100644
--- a/lib/classes/globalsearch/GlobalSearchModule.php
+++ b/lib/classes/globalsearch/GlobalSearchModule.php
@@ -218,7 +218,7 @@ abstract class GlobalSearchModule
                     return false;
                 }
 
-                if (in_array($module, ['GlobalSearchResources', 'GlobalSearchRoomAssignments'])
+                if (in_array($module, ['GlobalSearchResources', 'GlobalSearchRoomBookings'])
                     && !Config::get()->RESOURCES_ENABLE
                 ) {
                     return false;
-- 
GitLab