aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/resources/building.php
blob: e77fed8a4ff892d3c7036ae06a429f24a1d5ae4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php

/**
 * building.php - contains Resources_BuildingController
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @copyright   2017
 * @category    Stud.IP
 * @since       4.1
 */


/**
 * Resources_BuildingController contains actions for Building resources.
 */
class Resources_BuildingController extends AuthenticatedController
{
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);
        if (!Request::isDialog()) {
            if (Navigation::hasItem('/resources/structure')) {
                Navigation::activateItem('/resources/structure');
            }
        }
    }

    public function index_action($building_id)
    {
        $this->building = Building::find($building_id);
        $building_details = [];

        if($this->building->number) {
            $building_details[_('Gebäudenummer')] = $this->building->number;
        }
        if($this->building->address) {
            $building_details[_('Adresse')] = $this->building->address;
        }

        $this->building_details = $building_details;
        $this->geo_coordinates_object = $this->building->getPropertyObject(
            'geo_coordinates'
        );

        PageLayout::setTitle(
            $this->building->getFullName() . ' - ' . _('Informationen')
        );
        if (Navigation::hasItem('/room_management/overview/index')) {
            Navigation::activateItem('/room_management/overview/index');
        }

        $user = User::findCurrent();

        $current_user_is_resource_admin = $this->building->userHasPermission(
            $user,
            'admin'
        );

        $sidebar           = Sidebar::get();
        $actions           = new ActionsWidget();
        $actions_available = false;
        if ($current_user_is_resource_admin) {
            $actions_available = true;
            $actions->addLink(
                _('Gebäude bearbeiten'),
                $this->building->getActionURL('edit'),
                Icon::create('edit'),
                [
                    'data-dialog' => '1'
                ]
            );
            $actions->addLink(
                _('Rechte bearbeiten'),
                $this->building->getActionURL('permissions'),
                Icon::create('roles'),
                [
                    'data-dialog' => '1'
                ]
            );
        }
        if ($this->geo_coordinates_object instanceof ResourceProperty) {
            $actions_available = true;
            $actions->addLink(
                _('Lageplan anzeigen'),
                ResourceManager::getMapUrlForResourcePosition($this->geo_coordinates_object),
                Icon::create('globe'),
                ['target' => '_blank']
            );
        }
        if ($actions_available) {
            $sidebar->addWidget($actions);
        }

        $tree_widget = new ResourceTreeWidget(Location::findAll(), null, null);
        $tree_widget->setCurrentResource($this->building);

        $sidebar->addWidget($tree_widget);
    }

    public function select_category_action()
    {
        PageLayout::setTitle(_('Gebäude hinzufügen'));
        if (!ResourceManager::userHasGlobalPermission(User::findCurrent(), 'admin')) {
            throw new AccessDeniedException();
        }

        $this->next_action = Request::get('next_action');
        $this->categories  = ResourceCategory::findByClass_name('Building');

        if (!$this->categories) {
            PageLayout::postError(
                _('Es sind keine Gebäudekategorien eingerichtet!')
            );
        }
    }

    protected function addEditHandler($mode = 'edit', $building_id = null)
    {
        $this->mode      = $mode;
        $this->show_form = false;
        if ($mode == 'edit') {
            $this->building = Building::find($building_id);
            if (!$this->building) {
                PageLayout::postError(
                    _('Das angegebene Gebäude wurde nicht gefunden!')
                );
                return;
            }
        }

        //Get all properties of the building:
        $this->property_data              = [];
        $this->grouped_defined_properties = [];
        if ($mode == 'add') {
            $this->category_id = Request::get('category_id');
            if (!$this->category_id) {
                PageLayout::postInfo(
                    _('Bitte wählen Sie eine Gebäudekategorie aus!')
                );
                return;
            }
            $this->category = ResourceCategory::find($this->category_id);
            if (!$this->category) {
                PageLayout::postError(
                    _('Die gewählte Gebäudekategorie wurde nicht gefunden!')
                );
                return;
            }

            $this->grouped_defined_properties = $this->category->getGroupedPropertyDefinitions(
                ['geo_coordinates', 'number', 'address']
            );
        } elseif ($mode == 'edit') {
            $this->grouped_defined_properties =
                $this->building->category->getGroupedPropertyDefinitions(
                    $this->building->getRequiredPropertyNames()
                );
            PageLayout::setTitle(sprintf(_('%s: Bearbeiten'), $this->building->getFullName()));
        }

        if (($mode == 'add') || ($mode == 'edit')) {
            $this->possible_parents = [];
            $locations              = Location::findAll();
            foreach ($locations as $location) {
                $this->possible_parents[] = $location;

                //Get all ResourceLabel resources from the location
                //and add them as possible parents:
                $labels = $location->findChildrenByClassName('ResourceLabel', 3);

                foreach ($labels as $label) {
                    $this->possible_parents[] = $label;
                }
            }
        }

        foreach ($this->grouped_defined_properties as $properties) {
            foreach ($properties as $property) {
                if ($mode == 'add') {
                    $this->property_data[$property->id] = '';
                } elseif ($mode == 'edit') {
                    $this->property_data[$property->id] =
                        $this->building->getProperty($property->name);
                }
            }
        }

        $this->show_form = true;
        if (Request::submitted('confirmed')) {
            CSRFProtection::verifyUnsafeRequest();
            //Process submitted form:
            $this->name          = Request::get('name');
            $this->description   = Request::get('description');
            $this->number        = Request::get('number');
            $this->address       = Request::get('address');
            $this->parent_id     = Request::get('parent_id');
            $this->latitude      = Request::float('geo_coordinates_latitude');
            $this->longitude     = Request::float('geo_coordinates_longitude');
            $this->altitude      = Request::float('geo_coordinates_altitude');
            $this->osm_link      = Request::get('geo_coordinates_osm_link');
            $this->sort_position = Request::get('sort_position');

            $this->property_data = Request::getArray('properties');

            //validation:
            if (!$this->name) {
                PageLayout::postError(
                    _('Der Name des Gebäudes ist leer!')
                );
                return;
            }
            if (!$this->parent_id) {
                PageLayout::postError(
                    _('Es wurde keine Hierarchieebene ausgewählt!')
                );
                return;
            }
            if (!Resource::exists($this->parent_id)) {
                PageLayout::postError(
                    _('Die gewählte Hierarchieebene wurde nicht gefunden!')
                );
                return;
            }

            //data conversion:

            $position_string = '';
            if ($this->latitude >= 0.0) {
                $position_string .= '+';
            }
            $position_string .= number_format($this->latitude, 7);

            if ($this->longitude >= 0.0) {
                $position_string .= '+';
            }
            $position_string .= number_format($this->longitude, 7);

            if ($this->altitude >= 0.0) {
                $position_string .= '+';
            }
            $position_string .= number_format($this->altitude, 7) . 'CRSWGS_84/';

            //store data:

            if ($mode == 'add') {
                $this->building              = new Building();
                $this->building->category_id = $this->category_id;
            }

            $this->building->name        = $this->name;
            $this->building->description = $this->description;
            $this->building->parent_id   = $this->parent_id;

            if ($this->building->isDirty()) {
                $successfully_stored = $this->building->store();
            } else {
                $successfully_stored = true;
            }

            if ($GLOBALS['perm']->have_perm('root')) {
                $this->building->sort_position = $this->sort_position;
            }

            //Store properties:

            //Special treatment for the geo_coordinates property:

            $failed_properties = 0;
            try {
                $this->building->geo_coordinates = $position_string;
            } catch (Exception $e) {
                $failed_properties++;
            }

            //Special treatment for the number property:
            try {
                $this->building->number = $this->number;
            } catch (Exception $e) {
                $failed_properties++;
            }

            //Special treatment for the address property:
            try {
                $this->building->address = $this->address;
            } catch (Exception $e) {
                $failed_properties++;
            }

            //Store all non-special properties:

            $user              = User::findCurrent();
            $failed_properties += count(
                $this->building->setPropertiesById(
                    $this->property_data,
                    $user
                )
            );

            //Display result messages:

            if ($successfully_stored && !$failed_properties) {
                $this->show_form = false;
                PageLayout::postSuccess(
                    _('Das Gebäude wurde gespeichert!')
                );
            } elseif ($successfully_stored) {
                $this->show_form = false;
                PageLayout::postWarning(
                    sprintf(
                        ngettext(
                            'Das Gebäude wurde gespeichert, aber eine Eigenschaft konnte nicht gespeichert werden!',
                            'Das Gebäude wurde gespeichert, aber %d Eigenschaften konnten nicht gespeichert werden!',
                            $failed_properties
                        ),
                        $failed_properties
                    )
                );
            } else {
                PageLayout::postError(
                    _('Fehler beim Speichern des Gebäudes!')
                );
            }
        } else {
            if($this->building) {
                //Show form with current data:
                $this->name        = $this->building->name;
                $this->description = $this->building->description;
                $this->number      = $this->building->number;
                $this->address     = $this->building->address;
                $this->parent_id   = $this->building->parent_id;
                $geo_coordinates = $this->building->getPropertyObject('geo_coordinates');
                if ($mode == 'edit' && $geo_coordinates) {
                    $position_data   = ResourceManager::getPositionArray(
                        $geo_coordinates
                    );
                    $this->latitude  = $position_data[0];
                    $this->longitude = $position_data[1];
                    $this->altitude  = $position_data[2];
                }
                $this->sort_position = $this->building->sort_position;
            }
        }
    }


    public function add_action()
    {
        if (!ResourceManager::userHasGlobalPermission(User::findCurrent(), 'admin')) {
            throw new AccessDeniedException();
        }

        PageLayout::setTitle(_('Gebäude hinzufügen'));

        $this->category_id = Request::get('category_id');
        $this->category    = ResourceCategory::find($this->category_id);
        if (!$this->category) {
            //If no category_id is set we must redirect to the
            //select_category action:
            $this->redirect(
                URLHelper::getURL(
                    'dispatch.php/resources/building/select_category'
                )
            );
        } else {
            $category_class = $this->category->class_name;
            if (($category_class != 'Building') && !is_subclass_of($category_class, 'Building')) {
                PageLayout::postError(
                    _('Die gewählte Kategorie ist für Gebäude nicht geeignet!')
                );
                return;
            }
            $this->addEditHandler('add');
        }
    }


    public function edit_action($building_id = null)
    {
        if (!ResourceManager::userHasGlobalPermission(User::findCurrent(), 'admin')) {
            throw new AccessDeniedException();
        }

        $this->addEditHandler('edit', $building_id);
    }


    public function delete_action($building_id = null)
    {
        if (!ResourceManager::userHasGlobalPermission(User::findCurrent(), 'admin')) {
            throw new AccessDeniedException();
        }

        $this->show_form = false;

        $this->building = Building::find($building_id);
        if (!$this->building) {
            PageLayout::postError(
                _('Das angegebene Gebäude wurde nicht gefunden!')
            );
            return;
        }
        PageLayout::setTitle(sprintf(_('%s: Löschen'), $this->building->getFullName()));

        //geo_coordinates_object is needed in the index view that is loaded
        //from the view for this action.
        $this->geo_coordinates_object = $this->building->getPropertyObject(
            'geo_coordinates'
        );
        $this->required_properties    = Building::getRequiredProperties(
            ['geo_coordinates']
        );

        $this->show_form = true;
        if (Request::submitted('save')) {
            CSRFProtection::verifyUnsafeRequest();
            if ($this->building->delete()) {
                $this->show_form = false;
                PageLayout::postSuccess(_('Das Gebäude wurde gelöscht!'));
            } else {
                PageLayout::postError(_('Fehler beim Löschen des Gebaüdes!'));
            }
        }
    }
}