Browse code

Save BE reservation list filter in session

Benjamin Roth authored on11/09/2024 10:03:22
Showing2 changed files
... ...
@@ -20,19 +20,19 @@
20 20
                 </div>
21 21
                 <div class="tl_filter tl_subpanel">
22 22
                     <strong>Filter:</strong>
23
-                    <select name="tl_day" id="tl_day" class="tl_select tl_chosen">
23
+                    <select name="tl_day" id="tl_day" class="tl_select tl_chosen{% if filter.day.selected is defined %} active{% endif %}">
24 24
                         <option value=""{{ filter.day.selected is not defined or not filter.day.selected ? ' selected' : '' }}>Tag</option>
25 25
                         {% for value, label in filter.day.options %}
26 26
                             <option value="{{ value }}"{{ filter.day.selected is defined and filter.day.selected == value ? ' selected' : '' }}>{{ label }}</option>
27 27
                         {% endfor %}
28 28
                     </select>
29
-                    {#<select name="tl_month" id="tl_month" class="tl_select tl_chosen">
29
+                    {#<select name="tl_month" id="tl_month" class="tl_select tl_chosen{% if filter.month.selected is defined %} active{% endif %}">
30 30
                         <option value=""{{ filter.month.selected is not defined or not filter.month.selected ? ' selected' : '' }}>Monat</option>
31 31
                         {% for value, label in filter.month.options %}
32 32
                             <option value="{{ value }}"{{ filter.month.selected is defined and filter.month.selected == value ? ' selected' : '' }}>{{ label }}</option>
33 33
                         {% endfor %}
34 34
                     </select>#}
35
-                    <select name="tl_standort" id="tl_standort" class="tl_select tl_chosen">
35
+                    <select name="tl_standort" id="tl_standort" class="tl_select tl_chosen{% if filter.standort.selected is defined %} active{% endif %}">
36 36
                         <option value=""{{ filter.standort.selected is not defined or not filter.standort.selected ? ' selected' : '' }}>Standort</option>
37 37
                         {% for value, label in filter.standort.options %}
38 38
                             <option value="{{ value }}"{{ filter.standort.selected is defined and filter.standort.selected == value ? ' selected' : '' }}>{{ label }}</option>
... ...
@@ -23,6 +23,7 @@ use Contao\System;
23 23
 use Doctrine\DBAL\Connection;
24 24
 use Symfony\Component\HttpFoundation\RequestStack;
25 25
 use Symfony\Component\HttpFoundation\Response;
26
+use Symfony\Component\HttpFoundation\Session\SessionInterface;
26 27
 use Symfony\Component\Routing\Annotation\Route;
27 28
 use Symfony\Contracts\Translation\TranslatorInterface;
28 29
 use Twig\Environment as TwigEnvironment;
... ...
@@ -71,50 +72,70 @@ class WeinanlieferungBookingsController extends AbstractController
71 72
         System::loadLanguageFile('default');
72 73
 
73 74
         // Filter
74
-        if (Input::post('FORM_SUBMIT') === 'tl_filters' && Input::post('filter_reset') === null)
75
+        /** @var SessionInterface $objSession */
76
+        $objSession = \System::getContainer()->get('session');
77
+
78
+        $session = $objSession->get('filter');
79
+
80
+        if (Input::post('FORM_SUBMIT') === 'tl_filters')
75 81
         {
76
-            $queryBuilder = $this->db->createQueryBuilder()
77
-                ->select('id')
78
-                ->from(WeinanlieferungSlotsModel::getTable());
82
+            $arrFilterFields = ['tl_day', 'tl_standort', 'tl_status'];
79 83
 
80
-            if (!empty(Input::post('tl_day')) && is_numeric(Input::post('tl_day')))
84
+            foreach ($arrFilterFields as $v)
81 85
             {
82
-                $Day = new Date(Input::post('tl_day'));
83
-                $arrData['filter']['day']['selected'] = Input::post('tl_day');
84
-                $queryBuilder->andWhere('date BETWEEN :day_start AND :day_end')
85
-                    ->setParameter('day_start', $Day->dayBegin)
86
-                    ->setParameter('day_end', $Day->dayEnd);
87
-            } else {
88
-                $Today = new Date();
89
-                $queryBuilder->andWhere("time >= :today")
90
-                    ->setParameter('today',$Today->dayBegin);
86
+                if ($v == Input::post($v) || Input::post('filter_reset') == '1')
87
+                {
88
+                    unset($session['tl_vr_wa_reservation'][$v]);
89
+                } // Apply the filter
90
+                else
91
+                {
92
+                    $session['tl_vr_wa_reservation'][$v] = Input::post($v);
93
+                }
91 94
             }
95
+            $objSession->set('filter', $session);
96
+        }
92 97
 
93
-            /*if (!empty(Input::post('tl_month')) && is_numeric(Input::post('tl_month')))
94
-            {
95
-                $Month = new Date(Input::post('tl_month'));
96
-                $arrData['filter']['month']['selected'] = Input::post('tl_month');
97
-                $queryBuilder->andWhere('date BETWEEN :month_start AND :month_end')
98
-                    ->setParameter('month_start', $Month->monthBegin)
99
-                    ->setParameter('month_end', $Month->monthEnd);
100
-            } else {
101
-                $Today = new Date();
102
-                $queryBuilder->andWhere("time >= :today")
103
-                    ->setParameter('today',$Today->dayBegin);
104
-            }*/
98
+        $queryBuilder = $this->db->createQueryBuilder()
99
+            ->select('id')
100
+            ->from(WeinanlieferungSlotsModel::getTable());
105 101
 
106
-            if (!empty(Input::post('tl_standort')) && is_numeric(Input::post('tl_standort')))
107
-            {
108
-                $Month = new Date(Input::post('tl_standort'));
109
-                $arrData['filter']['standort']['selected'] = Input::post('tl_standort');
110
-                $queryBuilder->andWhere('pid = :pid')
111
-                    ->setParameter('pid', (int) Input::post('tl_standort'));
112
-            }
102
+        if (!empty($session['tl_vr_wa_reservation']['tl_day']) && is_numeric($session['tl_vr_wa_reservation']['tl_day']))
103
+        {
104
+            $Day = new Date($session['tl_vr_wa_reservation']['tl_day']);
105
+            $arrData['filter']['day']['selected'] = $session['tl_vr_wa_reservation']['tl_day'];
106
+            $queryBuilder->andWhere('date BETWEEN :day_start AND :day_end')
107
+                ->setParameter('day_start', $Day->dayBegin)
108
+                ->setParameter('day_end', $Day->dayEnd);
109
+        } else {
110
+            $Today = new Date();
111
+            $queryBuilder->andWhere("time >= :today")
112
+                ->setParameter('today',$Today->dayBegin);
113
+        }
113 114
 
114
-            $arrSlots = $queryBuilder->fetchFirstColumn();
115
+        /*if (!empty(Input::post('tl_month')) && is_numeric(Input::post('tl_month')))
116
+        {
117
+            $Month = new Date(Input::post('tl_month'));
118
+            $arrData['filter']['month']['selected'] = Input::post('tl_month');
119
+            $queryBuilder->andWhere('date BETWEEN :month_start AND :month_end')
120
+                ->setParameter('month_start', $Month->monthBegin)
121
+                ->setParameter('month_end', $Month->monthEnd);
122
+        } else {
123
+            $Today = new Date();
124
+            $queryBuilder->andWhere("time >= :today")
125
+                ->setParameter('today',$Today->dayBegin);
126
+        }*/
115 127
 
128
+        if (!empty($session['tl_vr_wa_reservation']['tl_standort']) && is_numeric($session['tl_vr_wa_reservation']['tl_standort']))
129
+        {
130
+            $Month = new Date($session['tl_vr_wa_reservation']['tl_standort']);
131
+            $arrData['filter']['standort']['selected'] = $session['tl_vr_wa_reservation']['tl_standort'];
132
+            $queryBuilder->andWhere('pid = :pid')
133
+                ->setParameter('pid', (int) $session['tl_vr_wa_reservation']['tl_standort']);
116 134
         }
117 135
 
136
+        $arrSlots = $queryBuilder->fetchFirstColumn();
137
+
138
+
118 139
         $arrDayOptions = [];
119 140
         $DayRequest = $this->db->executeQuery("SELECT s.date, DATE_FORMAT(FROM_UNIXTIME(s.date),'%d.%m.%Y') as 'day_label' FROM tl_vr_wa_reservation r INNER JOIN tl_vr_wa_slot s ON s.id = r.pid GROUP BY day_label ORDER BY s.date ASC");
120 141