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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
|
<?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);
$this->locks = GlobalResourceLock::findBySql('1 ORDER BY begin, end');
}
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 PermissionSearch('user'),
'function(){jQuery(this).closest("form").submit();}'
);
$this->sidebar->addWidget($user_search);
$this->last_activity_date = null;
$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
)
);
//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');
}
} 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!')
);
}
}
}
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(
':user_id IN (range_id, booking_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(
':user_id IN (range_id, booking_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'));
$this->new_group_name = '';
$this->property_move = [];
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] ?? null;
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] ?? null;
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')
);
$this->separable_room_name = '';
$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')) {
CSRFProtection::verifyUnsafeRequest();
$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')) {
CSRFProtection::verifyUnsafeRequest();
$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')) {
CSRFProtection::verifyUnsafeRequest();
$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')) {
CSRFProtection::verifyUnsafeRequest();
$separable_room_ids = Request::getArray('selected_separable_rooms');
$this->deleteSeparableRoomsById($separable_room_ids);
}
if (Request::submitted('delete_room_part')) {
CSRFProtection::verifyUnsafeRequest();
$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')) {
CSRFProtection::verifyUnsafeRequest();
$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);
$this->separable_rooms = SeparableRoom::findBySql(
'building_id = :building_id',
[
'building_id' => $this->building_id
]
);
$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
);
if ($separable_room_part_ids) {
$rooms = $this->building->rooms;
$this->single_rooms = [];
foreach ($rooms as $room) {
if (!in_array($room->id, $separable_room_part_ids)) {
$this->single_rooms[] = $room;
}
}
} else {
$this->single_rooms = $this->building->rooms;
}
}
}
public function configuration_action()
{
if (Navigation::hasItem('/resources/admin/configuration')) {
Navigation::activateItem('/resources/admin/configuration');
}
PageLayout::setTitle(
_('Konfigurationsoptionen')
);
$this->config = Config::get();
$this->resources_booking_plan_start_hour =
$this->config->RESOURCES_BOOKING_PLAN_START_HOUR;
$this->resources_booking_plan_end_hour =
$this->config->RESOURCES_BOOKING_PLAN_END_HOUR;
$this->bookingtypes = [
0 => _('Buchung'),
1 => _('Reservierung'),
2 => _('Sperrbuchung'),
3 => _('geplante Buchung')
];
if (Request::submitted('save')) {
//Get colors:
$colours = Request::getArray('colours');
//Validate:
CSRFProtection::verifyUnsafeRequest();
$this->resources_booking_plan_start_hour = Request::get('resources_booking_plan_start_hour');
$this->resources_booking_plan_end_hour = Request::get('resources_booking_plan_end_hour');
//Adjust format of booking plan start and end hours:
//Add another zero in front of the string, if the hour contains
//of just one digit.
if (preg_match('/^[0-9]\:/', $this->resources_booking_plan_start_hour)) {
$this->resources_booking_plan_start_hour = '0'
. $this->resources_booking_plan_start_hour;
}
if (preg_match('/^[0-9]\:/', $this->resources_booking_plan_end_hour)) {
$this->resources_booking_plan_end_hour = '0'
. $this->resources_booking_plan_end_hour;
}
//Check format of booking plan start and end hours:
$hour_regex = '/^(([01][0-9])|(2[0-3]))\:[0-5][0-9]$/';
if (!preg_match($hour_regex, $this->resources_booking_plan_start_hour)) {
PageLayout::postError(
_('Die Startuhrzeit für den Belegungsplan ist im falschen Format!')
);
return;
}
if (!preg_match($hour_regex, $this->resources_booking_plan_end_hour)) {
PageLayout::postError(
_('Die Enduhrzeit für den Belegungsplan ist im falschen Format!')
);
return;
}
//Check the time value:
$begin = strtotime($this->resources_booking_plan_start_hour);
$end = strtotime($this->resources_booking_plan_end_hour);
if ($begin >= $end) {
PageLayout::postError(
_('Die Startuhrzeit für den Belegungsplan darf nicht hinter der Enduhrzeit liegen!')
);
return;
}
//Store colors:
foreach ($colours as $colour_id => $value) {
//Validate value:
if (!preg_match('/#([0-9A-Fa-f]{2}){3}/', $value)) {
PageLayout::postError(
sprintf(
_('Der Farbwert für %s ist ungültig!'),
htmlReady($colour_id)
)
);
return;
}
//We don't want to create new colors here so we only
//modify colors that exist.
$colour = ColourValue::find($colour_id);
if ($colour) {
//Strip the first character from $value since we don't need
//the '#'-character in the database. Furthermore we add 'ff'
//to the end of the string to make the color intransparent.
$colour->value = mb_strtolower(substr($value, 1)) . 'ff';
if ($colour->isDirty()) {
if (!$colour->store()) {
PageLayout::postError(
sprintf(
_('Fehler beim Speichern des Farbwertes für %s!'),
htmlReady($colour_id)
)
);
return;
}
}
}
}
//Store config values:
$this->config->store(
'RESOURCES_ENABLE',
(bool)Request::get('resources_enable')
);
$this->config->store(
'RESOURCES_ALLOW_ROOM_PROPERTY_REQUESTS',
(bool)Request::get('resources_allow_room_property_requests')
);
$this->config->store(
'RESOURCES_ALLOW_ROOM_REQUESTS',
(bool)Request::get('resources_allow_room_requests')
);
$this->config->store(
'RESOURCES_DIRECT_ROOM_REQUESTS_ONLY',
(bool)Request::get('resources_direct_room_requests_only')
);
$this->config->store(
'RESOURCES_ALLOW_SINGLE_ASSIGN_PERCENTAGE',
Request::int('resources_allow_single_assign_percentage')
);
$this->config->store(
'RESOURCES_ALLOW_SINGLE_DATE_GROUPING',
Request::int('resources_allow_single_date_grouping')
);
$this->config->store(
'RESOURCES_MAP_SERVICE_URL',
Request::get('resources_map_service_url')
);
$this->config->store(
'RESOURCES_MAX_PREPARATION_TIME',
Request::get('resources_max_preparation_time')
);
$this->config->store(
'RESOURCES_MIN_BOOKING_TIME',
Request::get('resources_min_booking_time')
);
$this->config->store(
'RESOURCES_DISPLAY_CURRENT_REQUESTS_IN_OVERVIEW',
Request::get('resources_display_current_requests_in_overview')
);
$this->config->store(
'RESOURCES_BOOKING_PLAN_START_HOUR',
$this->resources_booking_plan_start_hour
);
$this->config->store(
'RESOURCES_BOOKING_PLAN_END_HOUR',
$this->resources_booking_plan_end_hour
);
$this->config->store(
'RESOURCES_ADDITIONAL_TEXT_ROOM_EXPORT',
Studip\Markup::purifyHtml(Request::get('additional_text'))
);
$this->config->store(
'RESOURCES_EXPORT_BOOKINGTYPES_DEFAULT',
Request::intArray('export_booking_types')
);
PageLayout::postSuccess(
_('Die Konfigurationsoptionen wurden gespeichert!')
);
}
$this->colours = ColourValue::findBySql(
"colour_id LIKE 'Resources%'
ORDER BY colour_id ASC"
);
$this->export_bookingtypes_default = $this->config->RESOURCES_EXPORT_BOOKINGTYPES_DEFAULT;
}
/**
* This action is called from the resource permission overview page.
* It is designed to be only called via HTTP POST.
*/
public function delete_permissions_action()
{
CSRFProtection::verifyUnsafeRequest();
$type = Request::get('permission_type');
if (!$type) {
return;
}
if ($type === 'permanent' || $type === 'temporary') {
$user_id = Request::option('user_id');
$resource_ids = Request::optionArray('resource_ids');
$deleted = 0;
if ($type === 'permanent') {
$deleted = ResourcePermission::deleteBySQL(
'`user_id` = :user_id AND `resource_id` IN ( :resource_ids )',
[
'user_id' => $user_id,
'resource_ids' => $resource_ids
]
);
} elseif ($type === 'temporary') {
$deleted = ResourceTemporaryPermission::deleteBySQL(
'`user_id` = :user_id AND `resource_id` IN ( :resource_ids )',
[
'user_id' => $user_id,
'resource_ids' => $resource_ids
]
);
}
if ($deleted > 0) {
PageLayout::postSuccess(sprintf(
ngettext(
'%u Berechtigung wurde gelöscht.',
'%u Berechtigungen wurden gelöscht.',
$deleted
),
$deleted
));
}
$this->redirect('resources/admin/user_permissions', ['user_id' => $user_id]);
} elseif ($type === 'all_from_users') {
$user_ids = Request::optionArray('user_ids');
$deleted = ResourcePermission::deleteBySql(
'`user_id` IN ( :user_ids )',
['user_ids' => $user_ids]
);
$deleted += ResourceTemporaryPermission::deleteBySql(
'`user_id` IN ( :user_ids )',
['user_ids' => $user_ids]
);
if ($deleted > 0) {
PageLayout::postSuccess(sprintf(
ngettext(
'Die Berechtigungen von einer Person wurden gelöscht.',
'Die Berechtigungen von %u Personen wurden gelöscht.',
count($user_ids)
),
count($user_ids)
));
}
$this->redirect('resources/admin/user_permissions');
}
}
}
|