From 7f1bdc86e4daf7ca48f47dc4daaa4a0f4b2350c5 Mon Sep 17 00:00:00 2001
From: David Siegfried <david.siegfried@uni-vechta.de>
Date: Fri, 11 Mar 2022 10:11:44 +0000
Subject: [PATCH] Delete clipboard items when room is deleted, closes #361

---
 app/routes/Clipboard.php                      | 14 ++++++--------
 lib/models/resources/Room.class.php           | 12 +++++++++---
 resources/assets/javascripts/lib/clipboard.js | 10 ++++++----
 3 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/app/routes/Clipboard.php b/app/routes/Clipboard.php
index 3b983e67a14..2f630dc78cc 100644
--- a/app/routes/Clipboard.php
+++ b/app/routes/Clipboard.php
@@ -60,7 +60,7 @@ class Clipboard extends \RESTAPI\RouteMap
         if ($clipboard->user_id != $GLOBALS['user']->id) {
             //Thou shalt not delete clipboards
             //which don't belong to you!
-            throw new AccessDeniedException();
+            throw new \AccessDeniedException();
         }
 
         $name = $this->data['name'];
@@ -70,8 +70,6 @@ class Clipboard extends \RESTAPI\RouteMap
 
         $clipboard->name = $name;
 
-        $success = false;
-
         if ($clipboard->isDirty()) {
             $success = $clipboard->store();
         } else {
@@ -107,10 +105,10 @@ class Clipboard extends \RESTAPI\RouteMap
             $this->notFound(_('Ungültige Merkzettel-ID!'));
         }
 
-        if ($clipboard->user_id != $GLOBALS['user']->id) {
+        if ($clipboard->user_id !== $GLOBALS['user']->id) {
             //Thou shalt not delete items of clipboards
             //which don't belong to you!
-            throw new AccessDeniedException();
+            throw new \AccessDeniedException();
         }
 
         if (!$clipboard->delete()) {
@@ -136,7 +134,7 @@ class Clipboard extends \RESTAPI\RouteMap
         if ($clipboard->user_id != $GLOBALS['user']->id) {
             //Thou shalt not add items to clipboards
             //which don't belong to you!
-            throw new AccessDeniedException();
+            throw new \AccessDeniedException();
         }
 
         $range_id = \Request::get('range_id');
@@ -162,7 +160,7 @@ class Clipboard extends \RESTAPI\RouteMap
                 $result['widget_id'] = $widget_id;
             }
             return $result;
-        } catch (Exception $e) {
+        } catch (\Exception $e) {
             $this->halt(500, $e->getMessage());
         }
     }
@@ -183,7 +181,7 @@ class Clipboard extends \RESTAPI\RouteMap
         if ($clipboard->user_id != $GLOBALS['user']->id) {
             //Thou shalt not delete items of clipboards
             //which don't belong to you!
-            throw new AccessDeniedException();
+            throw new \AccessDeniedException();
         }
 
         if ($clipboard->removeItem($range_id)) {
diff --git a/lib/models/resources/Room.class.php b/lib/models/resources/Room.class.php
index 39e4d02be75..e5960dbe63b 100644
--- a/lib/models/resources/Room.class.php
+++ b/lib/models/resources/Room.class.php
@@ -52,6 +52,7 @@ class Room extends Resource
 
         $config['additional_fields']['building']['get']   = 'findBuilding';
         $config['registered_callbacks']['before_store'][] = 'cbValidate';
+        $config['registered_callbacks']['after_delete'][] = 'cbDeleteClipboardItems';
 
         parent::configure($config);
     }
@@ -155,7 +156,7 @@ class Room extends Resource
      * @param int $limit A limit for the result set.
      * @param Room[] $searchable_rooms An (optional) array of rooms
      *     which will limit the search to the rooms in the array.
-     * @param Array $properties An array providing request properties
+     * @param array $properties An array providing request properties
      *     and their values in case the request doesn't have (the desired)
      *     properties set.
      *
@@ -324,7 +325,7 @@ class Room extends Resource
 
 
     /**
-     * Checks wheter rooms with public booking plans exist.
+     * Checks whether rooms with public booking plans exist.
      *
      * @return bool True, if at least one room has a public booking plan,
      *     false otherwise.
@@ -506,6 +507,11 @@ class Room extends Resource
         return true;
     }
 
+    public function cbDeleteClipboardItems()
+    {
+        ClipboardItem::deleteBySQL('range_id = ?', [$this->id]);
+    }
+
 
     public function getRequiredPropertyNames()
     {
@@ -554,7 +560,7 @@ class Room extends Resource
      *
      * @param Resource $resource The resource which shall be added as child.
      *
-     * @return True, if the resource could be added as child, false otherwise.
+     * @return bool True, if the resource could be added as child, false otherwise.
      * @throws InvalidResourceException If the specified resource belongs to
      *     the resource classes Room, Building or Location.
      *
diff --git a/resources/assets/javascripts/lib/clipboard.js b/resources/assets/javascripts/lib/clipboard.js
index 86a2c62eea6..a06d422273a 100644
--- a/resources/assets/javascripts/lib/clipboard.js
+++ b/resources/assets/javascripts/lib/clipboard.js
@@ -1,3 +1,5 @@
+import {$gettext} from './gettext';
+
 const Clipboard = {
 
     current_delete_icon: null,
@@ -19,7 +21,7 @@ const Clipboard = {
             var current_clipboard_id = jQuery(clipboard).attr('data-id');
 
             if (current_clipboard_id) {
-                if (current_clipboard_id == selected_clipboard_id) {
+                if (current_clipboard_id === selected_clipboard_id) {
                     jQuery(clipboard).removeClass('invisible');
                     if (jQuery(clipboard).find(".empty-clipboard-message").hasClass("invisible")) {
                         jQuery("#clipboard-group-container").find('.widget-links').removeClass('invisible');
@@ -213,7 +215,7 @@ const Clipboard = {
             //don't belong on the displayed page. That's all.
 
             allowed_classes = allowed_classes.replace(' ', '').split(',');
-            if (allowed_classes.indexOf(range_type) == -1) {
+            if (allowed_classes.indexOf(range_type) === -1) {
                 //The dropped item does not belong to the right class.
                 //Set the "not allowed" CSS class
                 //for the "not allowed" animation.
@@ -432,7 +434,7 @@ const Clipboard = {
     confirmRemoveClick: function(event) {
         STUDIP.Clipboard.current_delete_icon = event.target;
         STUDIP.Dialog.confirm(
-            'Sind Sie sicher?',
+            $gettext('Sind Sie sicher?'),
             STUDIP.Clipboard.handleRemoveClick
         );
     },
@@ -475,7 +477,7 @@ const Clipboard = {
     confirmRemoveItemClick: function(event) {
         STUDIP.Clipboard.current_delete_icon = event.target;
         STUDIP.Dialog.confirm(
-            'Sind Sie sicher?',
+            $gettext('Sind Sie sicher?'),
             STUDIP.Clipboard.removeItem
         );
     },
-- 
GitLab