From 94fd98424e59cff38ab8cf8ddafe6cdf195d0516 Mon Sep 17 00:00:00 2001
From: Moritz Strohm <strohm@data-quest.de>
Date: Wed, 14 Dec 2022 14:54:08 +0000
Subject: [PATCH] StEP 1352, closes #1352

Closes #1352

Merge request studip/studip!1110
---
 app/controllers/resources/room_group.php      | 59 ++++++++++---------
 app/controllers/room_management/overview.php  | 21 +++++++
 .../resources/room_group/permissions.php      |  2 +-
 .../room_management/overview/buildings.php    | 52 +++++++++-------
 4 files changed, 83 insertions(+), 51 deletions(-)

diff --git a/app/controllers/resources/room_group.php b/app/controllers/resources/room_group.php
index 2b383b446e4..8191ad018c3 100644
--- a/app/controllers/resources/room_group.php
+++ b/app/controllers/resources/room_group.php
@@ -66,11 +66,11 @@ class Resources_RoomGroupController extends AuthenticatedController
                 if (!is_array($user_permissions[$permission->user_id])) {
                     $user_permissions[$permission->user_id] = [];
                 }
-        
+
                 if (!is_array($user_permissions[$permission->user_id][$permission->perms])) {
                     $user_permissions[$permission->user_id][$permission->perms] = [];
                 }
-        
+
                 $user_permissions[$permission->user_id][$permission->perms][] = $permission;
             }
         }
@@ -121,35 +121,38 @@ class Resources_RoomGroupController extends AuthenticatedController
             }
         }
 
-        if (!$selected_clipboard_id) {
-            //Check if a clipboard is selected:
-            $selected_clipboard_id = $_SESSION['selected_clipboard_id'];
-        } else {
-            $_SESSION['selected_clipboard_id'] = $selected_clipboard_id;
-        }
-
-        $clipboard = Clipboard::find($selected_clipboard_id);
-        if (!$clipboard) {
-            PageLayout::postError(
-                _('Die gewählte Raumgruppe wurde nicht gefunden!')
-            );
-            return;
-        }
-
-        if ($clipboard->user_id != $GLOBALS['user']->id) {
-            throw new AccessDeniedException();
-        }
-
-        PageLayout::setTitle(
-            $clipboard->name . ': ' . _('Berechtigungen setzen')
-        );
+        $clipboard = null;
+        $this->room_ids = Request::optionArray('room_ids');
 
-        $this->room_ids = $clipboard->getAllRangeIds('Room');
         if (!$this->room_ids) {
-            PageLayout::postInfo(
-                _('Die Raumgruppe enthält keine Räume!')
+            if (!$selected_clipboard_id) {
+                //Check if a clipboard is selected:
+                $selected_clipboard_id = $_SESSION['selected_clipboard_id'];
+            } else {
+                $_SESSION['selected_clipboard_id'] = $selected_clipboard_id;
+            }
+            $clipboard = Clipboard::find($selected_clipboard_id);
+            if (!$clipboard) {
+                PageLayout::postError(
+                    _('Die gewählte Raumgruppe wurde nicht gefunden!')
+                );
+                return;
+            }
+
+            if ($clipboard->user_id != $GLOBALS['user']->id) {
+                throw new AccessDeniedException();
+            }
+            PageLayout::setTitle(
+                $clipboard->name . ': ' . _('Berechtigungen setzen')
             );
-            return;
+
+            $this->room_ids = $clipboard->getAllRangeIds('Room');
+            if (!$this->room_ids) {
+                PageLayout::postInfo(
+                    _('Die Raumgruppe enthält keine Räume!')
+                );
+                return;
+            }
         }
 
         $this->rooms = Room::findMany($this->room_ids);
diff --git a/app/controllers/room_management/overview.php b/app/controllers/room_management/overview.php
index 099f9154d07..e0d54799c0e 100644
--- a/app/controllers/room_management/overview.php
+++ b/app/controllers/room_management/overview.php
@@ -300,6 +300,27 @@ class RoomManagement_OverviewController extends AuthenticatedController
                     )
                 );
             }
+        } elseif (Request::submitted('room_permissions')) {
+            $this->building_ids = Request::optionArray('building_ids');
+
+            $room_ids = [];
+
+            Building::findEachMany(
+                function (Building $building) use (&$room_ids) {
+                    foreach ($building->rooms as $room) {
+                        $room_ids[] = $room->id;
+                    }
+                },
+                $this->building_ids
+            );
+
+            if ($room_ids) {
+                //Redirect to the permissions dialog:
+                $this->redirect(
+                    'resources/room_group/permissions',
+                    ['room_ids' => $room_ids]
+                );
+            }
         }
     }
 
diff --git a/app/views/resources/room_group/permissions.php b/app/views/resources/room_group/permissions.php
index aee23c3a8f0..17090ceab1d 100644
--- a/app/views/resources/room_group/permissions.php
+++ b/app/views/resources/room_group/permissions.php
@@ -32,7 +32,7 @@
                 'custom_save_button_text' => _('Zuweisen'),
                 'custom_form_action_link' => URLHelper::getLink('dispatch.php/resources/room_group/permissions/' . $clipboard->id),
                 'custom_hidden_fields' => [
-                    'resource_ids[]' => $room_ids
+                    'room_ids[]' => $room_ids
                 ],
                 'permissions' => $common_permissions,
                 'table_caption' => _('Gemeinsame Rechte'),
diff --git a/app/views/room_management/overview/buildings.php b/app/views/room_management/overview/buildings.php
index 74439867c98..472fd571c79 100644
--- a/app/views/room_management/overview/buildings.php
+++ b/app/views/room_management/overview/buildings.php
@@ -1,5 +1,5 @@
 <? if ($buildings): ?>
-    <form class="default" method="post"
+    <form class="default building-list" method="post"
           action="<?= URLHelper::getLink('dispatch.php/room_management/overview/buildings') ?>">
         <table class="default building-list">
             <colgroup>
@@ -14,7 +14,7 @@
                     <th>
                         <input type="checkbox" class="proxy"
                                data-proxyfor="input[name='building_ids[]']"
-                               data-activates="table.building-list button.bulk-action">
+                               data-activates="form.building-list button.bulk-action">
                     </th>
                     <th><?= _('Name') ?></th>
                     <th><?= _('Nummer') ?></th>
@@ -22,26 +22,7 @@
                     <th class="actions"><?= _('Aktionen') ?></th>
                 </tr>
             </thead>
-            <tfoot>
-                <tr>
-                    <td colspan="5">
-                        <?
-                        $button_attrs = [
-                            'class' => 'bulk-action',
-                            'data-activates-condition' => 'table.building-list :checkbox:checked'
-                        ];
-                        if (!$building_ids) {
-                            $button_attrs['disabled'] = 'disabled';
-                        }
-                        ?>
-                        <?= \Studip\Button::create(
-                            _('Raumgruppen für Gebäude erstellen'),
-                            'create_clipboards',
-                            $button_attrs
-                        ) ?>
-                    </td>
-                </tr>
-            </tfoot>
+
             <tbody>
                 <? foreach ($buildings as $building): ?>
                     <?= $this->render_partial(
@@ -90,5 +71,32 @@
                 <? endforeach ?>
             </tbody>
         </table>
+        <footer>
+            <?
+            $button_attrs = [
+                'class' => 'bulk-action',
+                'data-activates-condition' => 'table.building-list :checkbox:checked'
+            ];
+            if (!$building_ids) {
+                $button_attrs['disabled'] = 'disabled';
+            }
+            ?>
+            <?= \Studip\Button::create(
+                _('Raumgruppen für Gebäude erstellen'),
+                'create_clipboards',
+                array_merge($button_attrs, ['aria-title' => _('Raumgruppen für Gebäude erstellen')])
+            ) ?>
+            <?= \Studip\Button::create(
+                _('Berechtigungen für alle Räume setzen'),
+                'room_permissions',
+                array_merge(
+                    $button_attrs,
+                    [
+                        'data-dialog' => 'size=big',
+                        'aria-title' => _('Berechtigungen für alle Räume setzen')
+                    ]
+                )
+            ) ?>
+        </footer>
     </form>
 <? endif ?>
-- 
GitLab