Newer
Older
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
<?php
/**
* admin.php - contains Resources_AdminController
*
* 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 2018
* @category Stud.IP
* @since TODO
*/
/**
* Resources_AdminController contains actions
* for the global resource administration.
*/
class Resources_AdminController extends AuthenticatedController
{
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
$this->sidebar = Sidebar::get();
$this->current_user = User::findCurrent();
if (!ResourceManager::userHasGlobalPermission($this->current_user, 'admin')
&& !$GLOBALS['perm']->have_perm('root')) {
throw new AccessDeniedException();
}
}
public function permissions_action($resource_id = null)
{
if (Navigation::hasItem('/resources/admin/permissions')) {
Navigation::activateItem('/resources/admin/permissions');
}
//The relayed controller needs a resource object or the flag
//that global permissions shall be edited.
//Furthermore it needs the attribute resource_id.
if ($resource_id == 'global') {
$this->edit_global_permissions = true;
} else {
$this->resource = Resource::find($resource_id);
if (!$this->resource) {
PageLayout::postError(
_('Die angegebene Ressource wurde nicht gefunden!')
);
return;
}
if (!$this->resource->userHasPermission($this->current_user, 'admin')
&& !$GLOBALS['perm']->have_perm('root')) {
throw new AccessDeniedException();
}
}
$this->resource_id = $resource_id;
$response = $this->relay('resources/resource/permissions/' . $resource_id);
//We must replace all paths directing to the original controller
//with the path to this controller so that the click on the "save"
//button in the relayed controller links to this controller.
$this->other_controllers_html = str_replace(
'dispatch.php/resources/resource/permissions',
'dispatch.php/resources/admin/permissions',
$response->body
);
}
public function global_locks_action()
{
if (Navigation::hasItem('/resources/admin/global_locks')) {
Navigation::activateItem('/resources/admin/global_locks');
}
PageLayout::setTitle(
_('Globale Sperren verwalten')
);
$actions = new ActionsWidget();
$actions->addLink(
_('Sperrung hinzufügen'),
$this->url_for('resources/global_locks/add'),
Icon::create('add'),
[
'data-dialog' => 'size=auto'
]
);
$this->sidebar->addWidget($actions);
$now = time();
$this->current_lock = GlobalResourceLock::findOneBySql(
'begin <= :now AND end >= :now',
[
'now' => $now
]
);
$this->future_locks = GlobalResourceLock::findBySql(
'(begin > :now) OR (end > :now)
ORDER BY begin ASC, end ASC',
[
'now' => $now
]
);
}
public function user_permissions_action()
{
if (Navigation::hasItem('/resources/admin/user_permissions')) {
Navigation::activateItem('/resources/admin/user_permissions');
}
PageLayout::setTitle(
_('Berechtigungs-Übersicht')
);
$user_search = new SearchWidget(
$this->url_for('resources/admin/user_permissions')
);
$user_search->addNeedle(
_('Suche nach einer Person'),
'user_id',
_('Name oder Nutzername der Person'),
new StandardSearch('user_id'),
'function(){jQuery(this).closest("form").submit();}'
);
$this->sidebar->addWidget($user_search);
$user_id = Request::get('user_id');
$this->user = User::find($user_id);
if ($this->user) {
PageLayout::setTitle(
sprintf(
_('Berechtigungen für %s'),
$this->user->getFullName()
) . sprintf(
' (%1$s) (%2$s)',
$this->user->username,
$this->user->perms
)
);
if (GlobalResourceLock::currentlyLocked()) {
$this->current_global_lock = true;
}
//get the permissions of that user:
$this->global_permission = ResourcePermission::findOneBySql(
"user_id = :user_id AND resource_id = 'global'",
[
'user_id' => $this->user->id
]
);
$this->permissions = ResourcePermission::findBySql(
'INNER JOIN resources
ON resource_permissions.resource_id = resources.id
WHERE
user_id = :user_id
ORDER BY resources.name ASC, resource_permissions.mkdate ASC',
[
'user_id' => $this->user->id
]
);
$this->temporary_permissions = ResourceTemporaryPermission::findBySql(
'INNER JOIN resources
ON resource_temporary_permissions.resource_id = resources.id
WHERE
user_id = :user_id
ORDER BY resources.name ASC, resource_temporary_permissions.mkdate ASC',
[
'user_id' => $this->user->id
]
);
//Get last activity:
$this->now = new DateTime();
$this->last_activity = ResourceManager::getUserInactivityInterval(
$this->user,
$this->now
);
if ($this->last_activity instanceof DateInterval) {
//Calculate the date of the last inactivity:
$last_activity_date = $this->now->sub($this->last_activity);
$this->last_activity_date = $last_activity_date->format('d.m.Y H:i');
}
Moritz Strohm
committed
} else {
//No user selected. Show a list of all users that have
//at least one permission in the room management system.
if ($user_id) {
//User-ID specified, but no user could be found.
PageLayout::postError(
_('Die angegebene Person wurde nicht gefunden!')
);
}
$this->users = User::findBySql(
'`user_id` IN (
SELECT `user_id`
FROM `resource_permissions`
UNION
SELECT `user_id`
FROM `resource_temporary_permissions`
)
ORDER BY `nachname` ASC, `vorname` ASC'
);
if (!$this->users) {
//No user found.
PageLayout::postInfo(
_('Es gibt keine Personen mit Berechtigungen in der Raumverwaltung!')
Moritz Strohm
committed
);
}
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
}
}
public function booking_log_action($user_id = null, $resource_id = null)
{
$this->show_all_records = Request::get('show_all_records');
$this->user = User::find($user_id);
if (!$this->user) {
PageLayout::postError(
_('Die angegebene Person wurde nicht gefunden!')
);
return;
}
$this->resource = null;
if ($resource_id) {
$this->resource = Resource::find($resource_id);
if (!$this->resource) {
PageLayout::postError(
_('Die angegebene Ressource wurde nicht gefunden!')
);
return;
}
$this->resource = $this->resource->getDerivedClassInstance();
}
//Get bookings:
$this->bookings = null;
if ($this->resource) {
$this->bookings = ResourceBooking::findBySql(
'range_id = :user_id AND resource_id = :resource_id '
. (!$this->show_all_records
? 'AND begin > (UNIX_TIMESTAMP() - 86400) '
: ''
) . 'ORDER BY begin ASC, end ASC',
[
'user_id' => $this->user->id,
'resource_id' => $this->resource->id
]
);
} else {
$this->bookings = ResourceBooking::findBySql(
'range_id = :user_id '
. (!$this->show_all_records
? 'AND begin > (UNIX_TIMESTAMP() - 86400) '
: ''
) . 'ORDER BY begin ASC, end ASC',
[
'user_id' => $this->user->id
]
);
}
}
public function categories_action()
{
if (!ResourceManager::userHasGlobalPermission($this->current_user, 'admin')) {
throw new AccessDeniedException();
}
PageLayout::setTitle(
_('Kategorien verwalten')
);
if (Navigation::hasItem('/resources/admin/categories')) {
Navigation::activateItem('/resources/admin/categories');
}
$actions = new ActionsWidget();
$actions->addLink(
_('Neue Kategorie'),
URLHelper::getURL(
'dispatch.php/resources/category/add'
),
Icon::create('add'),
['data-dialog' => 'size=auto;reload-on-close']
);
$this->sidebar->addWidget($actions);
$this->categories = ResourceCategory::findAll();
if (!$this->categories) {
PageLayout::postInfo(
_('Es wurden keine Kategorien gefunden!')
);
}
}
public function properties_action()
{
if (Navigation::hasItem('/resources/admin/properties')) {
Navigation::activateItem('/resources/admin/properties');
}
PageLayout::setTitle(
_('Eigenschaften verwalten')
);
$actions = new ActionsWidget();
$actions->addLink(
_('Eigenschaft hinzufügen'),
$this->url_for('resources/property/add'),
Icon::create('add'),
[
'data-dialog' => 'size=auto'
]
);
$this->sidebar->addWidget($actions);
//Get all properties:
$this->properties = ResourcePropertyDefinition::findBySql(
'TRUE
GROUP BY property_id
ORDER BY name ASC, type ASC, mkdate ASC'
);
//Get the categories where the properties are used.
$this->categories = [];
if (is_array($this->properties)) {
$db = DBManager::get();
$stmt = $db->prepare(
"SELECT DISTINCT rc.name AS name
FROM resource_category_properties rcp
INNER JOIN resource_categories rc
ON rcp.category_id = rc.id
WHERE
rcp.property_id = :property_id
ORDER BY name ASC"
);
foreach ($this->properties as $property) {
$stmt->execute(
[
'property_id' => $property->id
]
);
$this->categories[$property->id] = $stmt->fetchAll(
PDO::FETCH_COLUMN,
0
);
}
}
}
public function property_groups_action()
{
if (Navigation::hasItem('/resources/admin/property_groups')) {
Navigation::activateItem('/resources/admin/property_groups');
}
PageLayout::setTitle(_('Eigenschaftsgruppen verwalten'));
if (Request::submitted('save')) {
CSRFProtection::verifyUnsafeRequest();
//Fields from the first table:
$this->selected_groups = Request::getArray('selected_groups');
$this->selected_group_properties = Request::getArray('selected_group_properties');
//Fields from the second table:
$this->new_group_name = Request::get('new_group_name');
$this->selected_properties = Request::getArray('selected_properties');
$this->property_move = Request::getArray('property_move');
$this->group_position = Request::getArray('group_position');
$this->edited_group_names = Request::getArray('edited_group_names');
$this->property_position = Request::getArray('property_position');
$property_object_cache = [];
$group_object_cache = [];
//Process fields from the first table:
if ($this->selected_group_properties) {
foreach ($this->selected_group_properties as $group_properties) {
foreach ($group_properties as $property_id) {
$property = ResourcePropertyDefinition::find($property_id);
if (!$property) {
//Invalid / non-existant property.
continue;
}
$property_object_cache[$property->id] = $property;
$property->property_group_id = '';
$property->property_group_pos = '0';
}
}
}
if ($this->selected_groups) {
ResourcePropertyGroup::deleteBySql(
'id IN ( :group_ids )',
[
'group_ids' => $this->selected_groups
]
);
}
//Process fields from the second table:
if ($this->new_group_name) {
$group = new ResourcePropertyGroup();
$group->name = $this->new_group_name;
$group->position = '0';
if ($group->store()) {
PageLayout::postSuccess(
sprintf(
_('Die neue Eigenschaftsgruppe mit dem Namen %s wurde angelegt!'),
htmlReady($group->name)
)
);
$this->new_group_name = '';
} else {
PageLayout::postError(
sprintf(
_('Fehler beim Anlegen der Eigenschaftsgruppe %s!'),
htmlReady($group->name)
)
);
}
if ($this->selected_properties) {
foreach ($this->selected_properties as $property_id) {
$property = ResourcePropertyDefinition::find($property_id);
if (!$property) {
//Invalid / non-existing property.
continue;
}
$property_object_cache[$property->id] = $property;
$property->property_group_id = $group->id;
if ($property->property_group_pos == '') {
$property->property_group_pos = '0';
}
}
}
}
if ($this->property_move) {
//At least one property is selected for moving into another
//property group.
foreach ($this->property_move as $property_id => $group_id) {
$property = $property_object_cache[$property_id];
if (!$property) {
$property = ResourcePropertyDefinition::find($property_id);
}
if (!$property) {
continue;
}
$property_object_cache[$property->id] = $property;
if ($group_id) {
$group = $group_object_cache[$group_id];
if (!$group) {
$group = ResourcePropertyGroup::find($group_id);
}
if (!$group) {
continue;
}
$group_object_cache[$group->id] = $group;
$property->property_group_id = $group->id;
if ($property->property_group_pos == '') {
$property->property_group_pos = '0';
}
}
}
}
if ($this->group_position) {
foreach ($this->group_position as $group_id => $position) {
$group = $group_object_cache[$group_id];
if (!$group) {
$group = ResourcePropertyGroup::find($group_id);
}
if (!$group) {
//Invalid / non-existing group.
continue;
}
$group_object_cache[$group_id] = $group;
$group->position = $position;
}
}
if ($this->edited_group_names) {
foreach ($this->edited_group_names as $group_id => $new_name) {
$group = $group_object_cache[$group_id];
if (!$group) {
$group = ResourcePropertyGroup::find($group_id);
}
if (!$group) {
//Invalid / non-existing group.
continue;
}
$group_object_cache[$group_id] = $group;
if ($group->name != $new_name) {
$group->name = $new_name;
}
}
}
if ($this->property_position) {
foreach ($this->property_position as $property_id => $position) {
$property = $property_object_cache[$property_id];
if (!$property) {
$property = ResourcePropertyDefinition::find($property_id);
}
if (!$property) {
continue;
}
$property_object_cache[$property->id] = $property;
//Make sure the position is a number:
$position = intval($position);
$property->property_group_pos = $position;
}
}
foreach ($group_object_cache as $group) {
if ($group->isDirty()) {
$group->store();
}
}
foreach ($property_object_cache as $property) {
if ($property->isDirty()) {
$property->store();
}
}
}
$this->property_groups = ResourcePropertyGroup::findBySql(
'TRUE ORDER BY position, name ASC'
);
$this->ungrouped_properties = ResourcePropertyDefinition::findBySql(
"property_group_id IS NULL OR property_group_id = ''
ORDER BY name ASC, type ASC"
);
}
protected function deleteSeparableRoomsById($separable_room_ids = [])
{
if (!is_array($separable_room_ids)) {
return;
}
if (count($separable_room_ids) == 1) {
//Only one separable room to delete.
$separable_room = SeparableRoom::find($separable_room_ids[0]);
if ($separable_room) {
if ($separable_room->delete()) {
PageLayout::postSuccess(
sprintf(
_('Der teilbare Raum %s wurde gelöscht!'),
htmlReady($separable_room->name)
)
);
} else {
PageLayout::postError(
sprintf(
_('Fehler beim Löschen des teilbaren Raumes %s!'),
htmlReady($separable_room->name)
)
);
}
} else {
PageLayout::postError(
_('Der gewählte teilbare Raum wurde nicht gefunden!')
);
}
} else {
//More than one separable room to delete.
$errors = [];
$rooms_not_found = 0;
foreach ($separable_room_ids as $separable_room_id) {
$separable_room = SeparableRoom::find($separable_room_id);
if ($separable_room) {
if (!$separable_room->delete()) {
$errors[] = sprintf(
_('Fehler beim Löschen des teilbaren Raumes %s!'),
htmlReady($separable_room->name)
);
}
} else {
$rooms_not_found++;
}
}
if ($rooms_not_found > 0) {
//Add an error message on top of the other error messages.
array_unshift(
$errors,
sprintf(
_('%d teilbare Räume wurden nicht gefunden!'),
$rooms_not_found
)
);
}
if ($errors) {
PageLayout::postError(
ngettext(
'Der folgende Fehler trat beim Löschen mehrerer teilbarer Räume auf:',
'Die folgenden Fehler traten beim Löschen mehrerer teilbarer Räume auf:',
count($errors)
),
$errors
);
}
}
}
protected function deleteSeparableRoomPartsById($room_part_ids = [])
{
if (!is_array($room_part_ids)) {
return;
}
if (count($room_part_ids) == 1) {
$separable_room_part = SeparableRoomPart::find(
explode('_', $room_part_ids[0])
);
if ($separable_room_part) {
$room = $separable_room_part->room;
$separable_room = $separable_room_part->separable_room;
if ($separable_room_part->delete()) {
//Check if the separable room as any parts left:
if ($separable_room) {
if (count($separable_room->parts) == 0) {
$separable_room->delete();
}
}
PageLayout::postSuccess(
sprintf(
_('Der Raum %1$s wurde aus dem teilbaren Raum %2$s gelöscht!'),
($room ? htmlReady($room->name) : _('unbekannt')),
($separable_room ? htmlReady($separable_room->name) : _('unbekannt'))
)
);
} else {
PageLayout::postError(
sprintf(
_('Fehler beim Löschen des Raumes %1$s aus dem teilbaren Raum %2$s!'),
($room ? htmlReady($room->name) : _('unbekannt')),
($separable_room ? htmlReady($separable_room->name) : _('unbekannt'))
)
);
}
} else {
PageLayout::postError(
_('Der gewählte Raumteil wurde nicht gefunden!')
);
}
} else {
$errors = [];
$parts_not_found = 0;
foreach ($room_part_ids as $room_part_id) {
$separable_room_part = SeparableRoomPart::find(
explode('_', $room_part_id)
);
if ($separable_room_part) {
$room = $separable_room_part->room;
$separable_room = $separable_room_part->separable_room;
if ($separable_room_part->delete()) {
//Check if the separable room as any parts left.
if ($separable_room) {
if (count($separable_room->parts) == 0) {
//There are no parts left in the separable room
//so that it can be deleted, too.
$separable_room->delete();
}
}
PageLayout::postSuccess(
sprintf(
_('Der Raum %1$s wurde aus dem teilbaren Raum %2$s gelöscht!'),
($room ? htmlReady($room->name) : _('unbekannt')),
($separable_room ? htmlReady($separable_room->name) : _('unbekannt'))
)
);
} else {
PageLayout::postError(
sprintf(
_('Fehler beim Löschen des Raumes %1$s aus dem teilbaren Raum %2$s!'),
($room ? htmlReady($room->name) : _('unbekannt')),
($separable_room ? htmlReady($separable_room->name) : _('unbekannt'))
)
);
}
} else {
$parts_not_found++;
}
}
if ($parts_not_found > 0) {
//Add an error message on top of the other error messages.
array_unshift(
$errors,
sprintf(
_('%d Raumteile wurden nicht gefunden!'),
$parts_not_found
)
);
}
if ($errors) {
PageLayout::postError(
ngettext(
'Der folgende Fehler trat beim Löschen mehrerer Raumteile auf:',
'Die folgenden Fehler traten beim Löschen mehrerer Raumeteile auf:',
count($errors)
),
$errors
);
}
}
}
public function separable_rooms_action()
{
if (Navigation::hasItem('/resources/admin/separable_rooms')) {
Navigation::activateItem('/resources/admin/separable_rooms');
}
PageLayout::setTitle(
_('Teilbare Räume verwalten')
);
if (Request::isPost()) {
CSRFProtection::verifyUnsafeRequest();
}
$db = DBManager::get();
$this->buildings = [];
$this->building = null;
$this->building_id = Request::get('building_id');
if ($this->building_id) {
$this->building = Building::find($this->building_id);
} else {
$this->buildings = Building::findAll();
}
if (Request::submitted('create_separable_room')) {
$selected_single_room_ids = Request::getArray('selected_single_rooms');
$this->separable_room_name = Request::get('separable_room_name');
$resources = Resource::findMany($selected_single_room_ids);
//Check if all IDs represent rooms:
$all_rooms = [];
foreach ($resources as $resource) {
$resource = $resource->getDerivedClassInstance();
if ($resource instanceof Room) {
$all_rooms[] = $resource;
}
}
if (count($all_rooms) != count($resources)) {
PageLayout::postError(
sprintf(
_('Teilbare Räume dürfen nur aus Raum-Objekten bestehen! %d ausgewählte Objekte sind keine Räume!'),
count($resources) - count($all_rooms)
)
);
return;
}
//Check if the rooms are already part of other separable rooms:
$separable_room_part_ids_stmt = $db->prepare(
'SELECT room_id FROM separable_room_parts
INNER JOIN separable_rooms
ON separable_room_parts.separable_room_id = separable_rooms.id
WHERE separable_rooms.building_id = :building_id'
);
$separable_room_part_ids_stmt->execute(
[
'building_id' => $this->building_id,
]
);
$separable_room_part_ids = $separable_room_part_ids_stmt->fetchAll(
PDO::FETCH_COLUMN,
0
);
$rooms = [];
if ($separable_room_part_ids) {
//There are other separable rooms.
foreach ($all_rooms as $room) {
if (!in_array($room->id, $separable_room_part_ids)) {
//The room is not part of another separable room.
//We can add it to the final list of rooms
//that will be included in the new separable room.
$rooms[] = $room;
}
}
} else {
//No separable rooms exist: All rooms can be added to
//a new separable room.
$rooms = $all_rooms;
}
//Now we create a separable room:
try {
$separable_room = SeparableRoom::createFromRooms(
$this->building_id,
$rooms,
$this->separable_room_name
);
//Reset the separable room name:
$this->separable_room_name = '';
} catch (SeparableRoomException $e) {
//Show a warning for the exception when not all
//rooms could be added to the separable room:
PageLayout::postWarning(
_('Der teilbare Raum konnte nicht korrekt gespeichert werden!'),
[$e->getMessage()]
);
return;
} catch (Exception $e) {
PageLayout::postError(
$e->getMessage()
);
return;
}
}
if (Request::submitted('add_room_part')) {
$selected_single_room_ids = Request::getArray('selected_single_rooms');
$resources = Resource::findMany($selected_single_room_ids);
//Check if all IDs represent rooms:
$all_rooms = [];
foreach ($resources as $resource) {
$resource = $resource->getDerivedClassInstance();
if ($resource instanceof Room) {
$all_rooms[] = $resource;
}
}
if (count($all_rooms) != count($resources)) {
PageLayout::postError(
sprintf(
_('Teilbare Räume dürfen nur aus Raum-Objekten bestehen! %d ausgewählte Objekte sind keine Räume!'),
count($resources) - count($all_rooms)
)
);
return;
}
//Check if the rooms are already part of other separable rooms:
$separable_room_part_ids_stmt = $db->prepare(
'SELECT room_id FROM separable_room_parts
INNER JOIN separable_rooms
ON separable_room_parts.separable_room_id = separable_rooms.id
WHERE separable_rooms.building_id = :building_id'
);
$separable_room_part_ids_stmt->execute(
[
'building_id' => $this->building_id,
]
);
$separable_room_part_ids = $separable_room_part_ids_stmt->fetchAll(
PDO::FETCH_COLUMN,
0
);
$rooms = [];
if ($separable_room_part_ids) {
//There are other separable rooms.
foreach ($all_rooms as $room) {
if (!in_array($room->id, $separable_room_part_ids)) {
//The room is not part of another separable room.
//We can add it to the final list of rooms
//that will be included in the new separable room.
$rooms[] = $room;
}
}
}
//Get the selected separable room:
$separable_room_id = Request::get('separable_room_id');
if ($separable_room_id) {
$separable_room = SeparableRoom::find($separable_room_id);
if ($separable_room) {
foreach ($rooms as $room) {
$separable_room_part = SeparableRoomPart::findOneBySql(
'separable_room_id = :separable_room_id
AND room_id = :room_id',
[
'separable_room_id' => $separable_room_id,
'room_id' => $room->id
]
);
if ($separable_room_part) {
PageLayout::postInfo(
sprintf(
_('Der Raum %1$s ist bereits Teil des teilbaren Raumes %2$s!'),
htmlReady($room->name),
htmlReady($separable_room->name)
)
);
} else {
$separable_room_part = new SeparableRoomPart();
$separable_room_part->separable_room_id = $separable_room_id;
$separable_room_part->room_id = $room->id;
if (!$separable_room_part->store()) {
PageLayout::postError(
sprintf(
_('Fehler beim Zuordnen des Raumteiles %1$s zum teilbaren Raum %2$s!'),
htmlReady($room->name),
htmlReady($separable_room->name)
)
);
}
}
}
} else {
PageLayout::postError(
_('Der gewählte teilbare Raum wurde nicht gefunden!')
);
}
} else {
PageLayout::postError(
_('Es wurde kein teilbarer Raum ausgewählt!')
);
}
}
if (Request::submitted('delete_separable_room')) {
$delete_separable_room_array = Request::getArray('delete_separable_room');
$separable_room_id = array_keys($delete_separable_room_array)[0];
$this->deleteSeparableRoomsById([$separable_room_id]);
}
if (Request::submitted('bulk_delete_separable_rooms')) {
$separable_room_ids = Request::getArray('selected_separable_rooms');
$this->deleteSeparableRoomsById($separable_room_ids);
}
if (Request::submitted('delete_room_part')) {
$delete_room_part_array = Request::getArray('delete_room_part');
$room_part_id = array_keys($delete_room_part_array)[0];
$this->deleteSeparableRoomPartsById([$room_part_id]);
}
if (Request::submitted('bulk_delete_room_parts')) {
$room_part_ids = Request::getArray('selected_room_parts');
$this->deleteSeparableRoomPartsById($room_part_ids);
}
//The following code is responsible for displaying rooms
//and separable rooms:
if ($this->building_id) {
//Load all rooms for that building:
$this->rooms = Room::findByBuilding($this->building_id);