Browse code

Integrate managed properties BE module and FE modules

Benjamin Roth authored on07/08/2024 13:28:28
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,217 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+namespace vonRotenberg\RealEstateListingBundle\Controller\FrontendModule;
6
+
7
+use vonRotenberg\RealEstateListingBundle\Model\ManagedPropertyModel;
8
+use Contao\ArrayUtil;
9
+use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
10
+use Contao\CoreBundle\DependencyInjection\Attribute\AsFrontendModule;
11
+use Contao\CoreBundle\Exception\ResponseException;
12
+use Contao\CoreBundle\Image\Studio\Figure;
13
+use Contao\CoreBundle\Image\Studio\FigureBuilder;
14
+use Contao\CoreBundle\Twig\FragmentTemplate;
15
+use Contao\File;
16
+use Contao\FilesModel;
17
+use Contao\Input;
18
+use Contao\ModuleModel;
19
+use Contao\PageModel;
20
+use Contao\StringUtil;
21
+use Contao\System;
22
+use Symfony\Component\HttpFoundation\Request;
23
+use Symfony\Component\HttpFoundation\Response;
24
+
25
+#[AsFrontendModule(category: 'vr_real_estate')]
26
+class ManagedPropertyController extends AbstractFrontendModuleController
27
+{
28
+    public const TYPE = 'managed_property';
29
+    protected function getResponse(FragmentTemplate $template, ModuleModel $model, Request $request): Response
30
+    {
31
+
32
+        $arrAssets = [];
33
+        $arrFilterOptions = [
34
+            'city' => []
35
+        ];
36
+        $arrFilterSelected = [];
37
+        $arrAssetsOptions = [];
38
+
39
+        $jumpTo = PageModel::findByPk($model->jumpTo);
40
+        $urlGenerator = System::getContainer()->get('contao.routing.content_url_generator');
41
+
42
+        // Filter
43
+        if (isset($_GET['filter']) && \is_array(Input::get('filter')))
44
+        {
45
+            foreach (Input::get('filter') as $filter=>$value)
46
+            {
47
+                if (in_array($filter, array_keys($arrFilterOptions)) && !empty($value))
48
+                {
49
+                    $rawValue = StringUtil::decodeEntities($value);
50
+                    $arrFilterSelected[$filter] = $rawValue;
51
+
52
+                    if (preg_match('/^.*<>.*$/',$rawValue))
53
+                    {
54
+                        list($start,$stop) = preg_split('/<>/',$rawValue, 2);
55
+                        $arrAssetsOptions['column'][$filter] = "$filter BETWEEN $start AND $stop";
56
+                    } else {
57
+                        $arrAssetsOptions['column'][$filter] = "$filter = ?";
58
+                        $arrAssetsOptions['value'][$filter] = $rawValue;
59
+                    }
60
+                }
61
+            }
62
+        }
63
+
64
+        if (($assets = ManagedPropertyModel::findAllPublished(0,0,$arrAssetsOptions)) === null)
65
+        {
66
+            return $template->getResponse();
67
+        }
68
+
69
+        // Figure Builder
70
+        $figureBuilder = System::getContainer()
71
+            ->get('contao.image.studio')
72
+            ->createFigureBuilder()
73
+            ->setSize($model->imgSize)
74
+            ->setLightboxGroupIdentifier('lb' . $model->id);
75
+
76
+        foreach ($assets as $asset)
77
+        {
78
+            $arrItem = array_merge($asset->row(), [
79
+                'teaserFigure' => $this->getImageFigures($asset->gallerySRC, $figureBuilder, $asset->orderSRC, 1),
80
+                'detailsUrl'   => $jumpTo !== null ? $urlGenerator->generate($jumpTo, ['parameters'=>'/items/'.$asset->id]) : null
81
+            ]);
82
+
83
+            $arrAssets[] = $arrItem;
84
+        }
85
+
86
+        // Populate filters
87
+        $filterAssets = ManagedPropertyModel::findAllPublished();
88
+        foreach ($filterAssets as $asset)
89
+        {
90
+            // Filter options
91
+            if (!empty($asset->city))
92
+            {
93
+                $tmpOptions = $arrAssetsOptions;
94
+                $tmpOptions['column'] = array_merge((isset($arrAssetsOptions['column']) ? $arrAssetsOptions['column'] : []),['city'=>'city = ?']);
95
+                $tmpOptions['value'] = array_merge((isset($arrAssetsOptions['value']) ? $arrAssetsOptions['value'] : []),['city'=>$asset->city]);
96
+                $count = ManagedPropertyModel::countAllPublished($tmpOptions);
97
+                $arrFilterOptions['city'][$asset->city] = $asset->city . ' ('.$count.')';
98
+            }
99
+        }
100
+
101
+        foreach (array_keys($arrFilterOptions) as $filterName)
102
+        {
103
+            $arrFilterOptions[$filterName] = array_unique($arrFilterOptions[$filterName]);
104
+        }
105
+
106
+        // Set template data
107
+        $template->set('filterOptions',$arrFilterOptions);
108
+        $template->set('filter',$arrFilterSelected);
109
+        $template->set('assets',$arrAssets);
110
+
111
+        // Handle ajax
112
+        if ($request->headers->get('VR-Ajax') == 'RePropertiesList')
113
+        {
114
+            throw new ResponseException($template->getResponse());
115
+        }
116
+
117
+        return $template->getResponse();
118
+    }
119
+
120
+    /**
121
+     * @param string|array  $strUuids
122
+     * @param FigureBuilder $figureBuilder
123
+     * @param string|array  $strOrder
124
+     *
125
+     * @return array|Figure
126
+     * @throws \Exception
127
+     */
128
+    public function getImageFigures($strUuids, $figureBuilder, $strOrder = null, int $intLimit = 0, int $intOffset = 0)
129
+    {
130
+        $arrImageFigures = [];
131
+
132
+        if (($imageFiles = FilesModel::findMultipleByUuids(StringUtil::deserialize($strUuids))) !== null)
133
+        {
134
+            $images = array();
135
+            while ($imageFiles->next())
136
+            {
137
+                // Continue if the files has been processed or does not exist
138
+                if (isset($images[$imageFiles->path]) || !file_exists(System::getContainer()->getParameter('kernel.project_dir') . '/' . $imageFiles->path))
139
+                {
140
+                    continue;
141
+                }
142
+
143
+                // Single files
144
+                if ($imageFiles->type == 'file')
145
+                {
146
+                    $objFile = new File($imageFiles->path);
147
+
148
+                    if (!$objFile->isImage)
149
+                    {
150
+                        continue;
151
+                    }
152
+
153
+                    // Add the image
154
+                    $images[$imageFiles->path] = $imageFiles->current();
155
+                } // Folders
156
+                else
157
+                {
158
+                    $objSubfiles = FilesModel::findByPid($imageFiles->uuid, array('order' => 'name'));
159
+
160
+                    if ($objSubfiles === null)
161
+                    {
162
+                        continue;
163
+                    }
164
+
165
+                    while ($objSubfiles->next())
166
+                    {
167
+                        // Skip subfolders
168
+                        if ($objSubfiles->type == 'folder')
169
+                        {
170
+                            continue;
171
+                        }
172
+
173
+                        $objFile = new File($objSubfiles->path);
174
+
175
+                        if (!$objFile->isImage)
176
+                        {
177
+                            continue;
178
+                        }
179
+
180
+                        // Add the image
181
+                        $images[$objSubfiles->path] = $objSubfiles->current();
182
+                    }
183
+                }
184
+            }
185
+
186
+            if ($strOrder !== null)
187
+            {
188
+                $images = ArrayUtil::sortByOrderField($images, $strOrder);
189
+            }
190
+            $images = array_values($images);
191
+
192
+            // Offset images
193
+            if ($intOffset > 0)
194
+            {
195
+                $images = \array_slice($images, ($intOffset < count($images) ? $intOffset : count($images)));
196
+            }
197
+
198
+            // Limit number of images
199
+            if ($intLimit > 0)
200
+            {
201
+                $images = \array_slice($images, 0, $intLimit);
202
+            }
203
+
204
+            // Build figures of images
205
+            foreach ($images as $image)
206
+            {
207
+                $figure = $figureBuilder
208
+                    ->fromFilesModel($image)
209
+                    ->build();
210
+
211
+                $arrImageFigures[] = $figure;
212
+            }
213
+
214
+        }
215
+        return count ($arrImageFigures) === 1 && $intLimit === 1 ? array_shift($arrImageFigures) : $arrImageFigures;
216
+    }
217
+}