From 6a49711516478b2eab968f2672d22197c73f4a4c Mon Sep 17 00:00:00 2001
From: Moritz Strohm <strohm@data-quest.de>
Date: Mon, 20 Sep 2021 08:02:15 +0000
Subject: [PATCH] resources/search/rooms: link to booking plan of the searched
 time range begin, re #95

---
 app/controllers/resources/search.php              | 15 +++++++++++++++
 .../resources/_common/_room_search_result.php     |  4 ++--
 app/views/resources/search/rooms.php              |  3 ++-
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/app/controllers/resources/search.php b/app/controllers/resources/search.php
index 2ca6a20e809..db73a13203e 100644
--- a/app/controllers/resources/search.php
+++ b/app/controllers/resources/search.php
@@ -93,6 +93,21 @@ class Resources_SearchController extends AuthenticatedController
                 }
                 PageLayout::postMessage($message);
             }
+
+            $this->booking_plan_action_params = [];
+
+            if ($this->form_submitted) {
+                //Check if the time range criteria has been set.
+                //If so, set the begin date as parameter for the
+                //booking plan action URL.
+                $criteria = $room_search_widget->getSelectedCriteria();
+                if (isset($criteria['special__time_range']) && $criteria['special__time_range']) {
+                    $begin = $criteria['special__time_range']['range']['begin'];
+                    if ($begin instanceof DateTime) {
+                        $this->booking_plan_action_params['defaultDate'] = $begin->format('Y-m-d');
+                    }
+                }
+            }
         }
     }
 }
diff --git a/app/views/resources/_common/_room_search_result.php b/app/views/resources/_common/_room_search_result.php
index b6ecc896fbf..45b700b9ec3 100644
--- a/app/views/resources/_common/_room_search_result.php
+++ b/app/views/resources/_common/_room_search_result.php
@@ -19,7 +19,7 @@
         );
         if ($room->userHasPermission($current_user, 'autor')) {
             $actions->addLink(
-                $room->getActionLink('booking_plan'),
+                $room->getActionLink('booking_plan', $booking_plan_action_params),
                 _('Wochenbelegung'),
                 Icon::create('timetable'),
                 ['target' => '_blank']
@@ -33,7 +33,7 @@
         } else {
             if ($room->booking_plan_is_public && Config::get()->RESOURCES_SHOW_PUBLIC_ROOM_PLANS) {
                 $actions->addLink(
-                    $room->getActionLink('booking_plan'),
+                    $room->getActionLink('booking_plan', $booking_plan_action_params),
                     _('Belegungsplan'),
                     Icon::create('timetable'),
                     ['data-dialog' => 'size=big']
diff --git a/app/views/resources/search/rooms.php b/app/views/resources/search/rooms.php
index b48d95d99d5..0f8307bf682 100644
--- a/app/views/resources/search/rooms.php
+++ b/app/views/resources/search/rooms.php
@@ -20,7 +20,8 @@
                     $current_user,
                     'admin'
                 ),
-                'cliboard_widget_id' => $clipboard_widget_id
+                'cliboard_widget_id' => $clipboard_widget_id,
+                'booking_plan_action_params' => $booking_plan_action_params
             ]
         ) ?>
     <? endforeach ?>
-- 
GitLab