Browse code

Optimize filter and introduce day filter in FE slot list

Benjamin Roth authored on21/08/2024 15:26:38
Showing2 changed files
... ...
@@ -2,7 +2,7 @@
2 2
 
3 3
     {% block filter %}
4 4
         <form hx-get="{{ pageUrl is defined ? pageUrl : '' }}" hx-push-url="true" hx-headers='{"VR-Ajax": "WaSlotsModule"}' hx-trigger="change, submit" hx-target="closest .content-wrapper" class="filter">
5
-            <div class="row">
5
+            <div class="row u-justify-center">
6 6
                 <div class="col-md-2 kapazitaet">
7 7
                     <select name="filter_kapazitaet">
8 8
                         <option value="">-- Kapazität --</option>
... ...
@@ -19,6 +19,14 @@
19 19
                         {% endfor %}
20 20
                     </select>
21 21
                 </div>
22
+                <div class="col-md-2 day">
23
+                    <select name="filter_tag">
24
+                        <option value="">-- Datum --</option>
25
+                        {% for key, option in filter.tag.options %}
26
+                            <option value="{{ key }}"{% if filter.tag.selected is defined and filter.tag.selected == key %} selected{% endif %}>{{ option }}</option>
27
+                        {% endfor %}
28
+                    </select>
29
+                </div>
22 30
                 {#<div class="col-md-2 ernteart">
23 31
                     <select name="filter_ernteart">
24 32
                         <option value="">-- Ernteart --</option>
... ...
@@ -24,6 +24,7 @@ use Contao\PageModel;
24 24
 use Contao\StringUtil;
25 25
 use Contao\System;
26 26
 use Contao\Template;
27
+use Doctrine\DBAL\Connection;
27 28
 use Symfony\Component\HttpFoundation\Request;
28 29
 use Symfony\Component\HttpFoundation\Response;
29 30
 use vonRotenberg\WeinanlieferungBundle\Model\WeinanlieferungLeseartModel;
... ...
@@ -41,9 +42,12 @@ class WeinanlieferungSlotsListModuleController extends AbstractFrontendModuleCon
41 42
 
42 43
     private $insertTagParser;
43 44
 
44
-    public function __construct(InsertTagParser $insertTagParser)
45
+    private $db;
46
+
47
+    public function __construct(InsertTagParser $insertTagParser, Connection $connection)
45 48
     {
46 49
         $this->insertTagParser = $insertTagParser;
50
+        $this->db = $connection;
47 51
     }
48 52
 
49 53
 
... ...
@@ -56,7 +60,7 @@ class WeinanlieferungSlotsListModuleController extends AbstractFrontendModuleCon
56 60
         $arrData = $template->getData();
57 61
         $arrOptions = [];
58 62
 
59
-        // Add filter values
63
+        // Add filter sql
60 64
         if (!empty($_GET['filter_kapazitaet']))
61 65
         {
62 66
             $arrData['filter']['kapazitaet']['selected'] = $_GET['filter_kapazitaet'];
... ...
@@ -69,6 +73,13 @@ class WeinanlieferungSlotsListModuleController extends AbstractFrontendModuleCon
69 73
             $arrOptions['column'][] = 'pid = ?';
70 74
             $arrOptions['value'][] = $_GET['filter_standort'];
71 75
         }
76
+        if (!empty($_GET['filter_tag']) && ($Tag = new Date($_GET['filter_tag'])))
77
+        {
78
+            $arrData['filter']['tag']['selected'] = $_GET['filter_tag'];
79
+            $arrOptions['column'][] = 'tl_vr_wa_slot.time BETWEEN ? and ?';
80
+            $arrOptions['value'][] = $Tag->dayBegin;
81
+            $arrOptions['value'][] = $Tag->dayEnd;
82
+        }
72 83
         if (!empty($_GET['filter_ernteart']))
73 84
         {
74 85
             $arrData['filter']['ernteart']['selected'] = $_GET['filter_ernteart'];
... ...
@@ -93,34 +104,11 @@ class WeinanlieferungSlotsListModuleController extends AbstractFrontendModuleCon
93 104
             }
94 105
         }
95 106
 
96
-        $arrData['filter']['kapazitaet']['options'] = range(1,30);
97
-
98
-        if (($Standorte = WeinanlieferungStandortModel::findBy(["id IN (".implode(',',$standortIds).")"],null,['order'=>'title ASC'])) !== null)
99
-        {
100
-            $arrData['filter']['standort']['options'] = array_combine($Standorte->fetchEach('id'),$Standorte->fetchEach('title'));
101
-        }
102
-
103
-        if (isset($GLOBALS['TL_DCA']['tl_vr_wa_slot']['fields']['ernteart']['options']))
104
-        {
105
-            foreach ($GLOBALS['TL_DCA']['tl_vr_wa_slot']['fields']['ernteart']['options'] as $ernteart)
106
-            {
107
-                $arrData['filter']['ernteart']['options'][$ernteart] = $GLOBALS['TL_LANG']['REF']['wa_ernteart'][$ernteart] ?? $ernteart;
108
-            }
109
-        }
110
-
111
-        if (($Sorten = WeinanlieferungRebsorteModel::findAll(['order'=>'title ASC'])) !== null)
112
-        {
113
-            $arrData['filter']['sorte']['options'] = array_combine($Sorten->fetchEach('id'),$Sorten->fetchEach('title'));
114
-        }
115
-
116
-        if (($Leseart = WeinanlieferungLeseartModel::findAll(['order'=>'title ASC'])) !== null)
117
-        {
118
-            $arrData['filter']['leseart']['options'] = array_combine($Leseart->fetchEach('id'),$Leseart->fetchEach('title'));
119
-        }
120
-
121 107
         // Get available slots
122 108
         if (($slots = WeinanlieferungSlotsModel::findMultiplePublishedByPids($standortIds,$arrOptions)) !== null)
123 109
         {
110
+            $slotIds = $slots->fetchEach('id');
111
+
124 112
             /** @var WeinanlieferungSlotsModel $slot */
125 113
             foreach ($slots as $slot)
126 114
             {
... ...
@@ -168,6 +156,43 @@ class WeinanlieferungSlotsListModuleController extends AbstractFrontendModuleCon
168 156
                     'gebucht' => (boolean) WeinanlieferungReservationModel::countBy(["uid = ?","pid = ?"],[FrontendUser::getInstance()->id,$slot->id])
169 157
                 ]);
170 158
             }
159
+
160
+            // Get filter values
161
+            $result = $this->db->executeQuery("SELECT MAX(behaelter) as 'kapazitaet' FROM tl_vr_wa_slot WHERE id IN (".implode(',',$slotIds).")");
162
+            $intMaxKapazitaet = $result->fetchOne();
163
+            $arrData['filter']['kapazitaet']['options'] = range(1,max(1,$intMaxKapazitaet));
164
+
165
+            if (($Standorte = WeinanlieferungStandortModel::findBy(["id IN (".implode(',',$standortIds).")","id IN (SELECT tl_vr_wa_slot.pid FROM tl_vr_wa_slot WHERE tl_vr_wa_slot.id IN (".implode(',',$slotIds)."))"],null,['order'=>'title ASC'])) !== null)
166
+            {
167
+                $arrData['filter']['standort']['options'] = array_combine($Standorte->fetchEach('id'),$Standorte->fetchEach('title'));
168
+            }
169
+
170
+            $slots->reset();
171
+            foreach ($slots as $slot)
172
+            {
173
+                $Tag = new Date($slot->time);
174
+                $arrData['filter']['tag']['options'][$Tag->dayBegin] = Date::parse(Date::getNumericDateFormat(),$Tag->dayBegin);
175
+            }
176
+
177
+            /*if (isset($GLOBALS['TL_DCA']['tl_vr_wa_slot']['fields']['ernteart']['options']))
178
+            {
179
+                foreach ($GLOBALS['TL_DCA']['tl_vr_wa_slot']['fields']['ernteart']['options'] as $ernteart)
180
+                {
181
+                    $arrData['filter']['ernteart']['options'][$ernteart] = $GLOBALS['TL_LANG']['REF']['wa_ernteart'][$ernteart] ?? $ernteart;
182
+                }
183
+            }
184
+
185
+            if (($Sorten = WeinanlieferungRebsorteModel::findAll(['order'=>'title ASC'])) !== null)
186
+            {
187
+                $arrData['filter']['sorte']['options'] = array_combine($Sorten->fetchEach('id'),$Sorten->fetchEach('title'));
188
+            }
189
+
190
+            if (($Leseart = WeinanlieferungLeseartModel::findAll(['order'=>'title ASC'])) !== null)
191
+            {
192
+                $arrData['filter']['leseart']['options'] = array_combine($Leseart->fetchEach('id'),$Leseart->fetchEach('title'));
193
+            }*/
194
+
195
+
171 196
         }
172 197
 
173 198
         // Add page URL