From 6bb7cf96b1d4dc08a603a01b4a32afbc994d1fe2 Mon Sep 17 00:00:00 2001
From: Murtaza Sultani <sultani@data-quest.de>
Date: Mon, 5 May 2025 16:47:40 +0200
Subject: [PATCH] Store tile layout state in user config

---
 controllers/categories.php        | 16 ++++++++++------
 migrations/03_add_log_actions.php | 19 -------------------
 migrations/04_add_user_config.php | 31 +++++++++++++++++++++++++++++++
 views/categories/index.php        |  3 ++-
 4 files changed, 43 insertions(+), 26 deletions(-)
 create mode 100644 migrations/04_add_user_config.php

diff --git a/controllers/categories.php b/controllers/categories.php
index a566392..912445c 100644
--- a/controllers/categories.php
+++ b/controllers/categories.php
@@ -328,25 +328,29 @@ class CategoriesController extends BookableController {
      */
     public function buildSidebar(): void
     {
+        $user_id = User::findCurrent()->user_id;
+
         $actions = new ActionsWidget();
 
         $actions->setTitle('Ansichten');
 
-        if (Request::option('view')) {
-            $_SESSION['Bookable']['categories_view'] = Request::option('view');
+        if (Request::option('layout')) {
+            $layout = Request::option('layout') === 'card' ? 1 : 0;
+            UserConfig::get($user_id)->store('BOOKABLE_TILE_LAYOUT', $layout);
         }
 
-        if (Request::option('view') === 'card'
-                || (!empty($_SESSION['Bookable']['categories_view']) && $_SESSION['Bookable']['categories_view'] === 'card')) {
+        $this->tile_layout = UserConfig::get($user_id)->BOOKABLE_TILE_LAYOUT;
+
+        if ($this->tile_layout) {
             $actions->addLink(
                 _('Listenansicht'),
-                URLHelper::getURL('', ['view' => 'list', 'search_keyword' => Request::get('search_keyword')]),
+                URLHelper::getURL('', ['layout' => 'list', 'search_keyword' => Request::get('search_keyword')]),
                 Icon::create('view-list')
             );
         } else {
             $actions->addLink(
                 _('Kachelansicht'),
-                URLHelper::getURL('', ['view' => 'card', 'search_keyword' => Request::get('search_keyword')]),
+                URLHelper::getURL('', ['layout' => 'card', 'search_keyword' => Request::get('search_keyword')]),
                 Icon::create('block-gallery')
             );
         }
diff --git a/migrations/03_add_log_actions.php b/migrations/03_add_log_actions.php
index 71658b5..e69de29 100644
--- a/migrations/03_add_log_actions.php
+++ b/migrations/03_add_log_actions.php
@@ -1,19 +0,0 @@
-<?php
-
-final class AddLogActions extends Migration
-{
-    public function up()
-    {
-        StudipLog::registerActionPlugin(
-            'BOOKABLE_BOOKING_STATUS',
-            'Der Buchungsstand Änderung',
-            '%user ändert/setzt Buchung(ID: %affected) im %sem(%coaffected). %info',
-            Bookable::class
-        );
-    }
-
-    public function down()
-    {
-        StudipLog::unregisterAction('BOOKABLE_BOOKING_STATUS');
-    }
-}
diff --git a/migrations/04_add_user_config.php b/migrations/04_add_user_config.php
new file mode 100644
index 0000000..853e18a
--- /dev/null
+++ b/migrations/04_add_user_config.php
@@ -0,0 +1,31 @@
+<?php
+
+final class AddUserConfig extends Migration
+{
+    public function up()
+    {
+        DBManager::get()->execute(
+            "INSERT IGNORE INTO `config` VALUES (:field, :value, :type, :range, :section, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), :description)",
+            [
+                'field' => 'BOOKABLE_TILE_LAYOUT',
+                'value' => 1,
+                'type' => 'boolean',
+                'range' => 'user',
+                'section' => 'Bookable',
+                'description' => 'Konfiguration der Ansicht der Kategorien'
+            ]
+        );
+    }
+
+    public function down()
+    {
+        $sql = "DELETE `config`, `config_values` FROM `config`
+                    LEFT JOIN `config_values` USING (`field`)
+                    WHERE `field` = :field";
+
+        DBManager::get()->execute(
+            $sql,
+            ['field' => 'BOOKABLE_TILE_LAYOUT']
+        );
+    }
+}
diff --git a/views/categories/index.php b/views/categories/index.php
index f943e5d..25c60c9 100644
--- a/views/categories/index.php
+++ b/views/categories/index.php
@@ -4,11 +4,12 @@
  * @var Bookable\Models\BookableCategory[] $categories
  * @var Bookable $plugin
  * @var bool $is_admin
+ * @var bool $tile_layout
  */
 ?>
 
 <div class="bookable ">
-    <? if (Request::get('view') === 'card' || (!empty($_SESSION['Bookable']['categories_view']) && $_SESSION['Bookable']['categories_view'] === 'card')) : ?>
+    <? if ($tile_layout) : ?>
         <ul class="categories-card-container use-utility-classes">
             <? foreach ($categories as $category): ?>
                 <li>
-- 
GitLab