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
<?php
/**
* overview.php - contains RoomManagement_OverviewController
*
* 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-2020
* @category Stud.IP
* @since 4.5
*/
/**
* RoomManagement_OverviewController contains general overview actions
* for the new room management functionality.
*
* NOTE: It is derived from StudipController instead of AuthenticatedController
* since the public_booking_plans action needs to be visible for
* nobody users.
*/
class RoomManagement_OverviewController extends AuthenticatedController
{
public function before_filter(&$action, &$args)
{
if ($action == 'public_booking_plans') {
if (Config::get()->RESOURCES_SHOW_PUBLIC_ROOM_PLANS) {
$this->allow_nobody = true;
} else {
throw new AccessDeniedException();
}
}
parent::before_filter($action, $args);
$this->user = User::findCurrent();
$this->show_resource_actions = (
ResourceManager::userHasGlobalPermission($this->user, 'autor')
||
ResourceManager::userHasResourcePermissions($this->user, 'autor')
);
$this->show_admin_actions = (
$this->user_is_global_resource_admin
||
ResourceManager::userHasResourcePermissions($this->user)
||
$GLOBALS['perm']->have_perm('root')
);
$this->user_is_global_resource_user = ResourceManager::userHasGlobalPermission($this->user);
$this->user_is_root = $GLOBALS['perm']->have_perm('root');
$this->user_is_global_resource_admin = ResourceManager::userHasGlobalPermission(
$this->user,
'admin'
) || $this->user_is_root;
$this->show_global_admin_actions = $this->user_is_global_resource_admin
&& ResourceManager::userHasGlobalPermission(
$this->user,
'admin'
);
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
}
public function index_action()
{
if (Navigation::hasItem('/resources/overview')) {
Navigation::activateItem('/resources/overview');
}
$sufficient_permissions =
ResourceManager::userHasGlobalPermission($this->user)
||
ResourceManager::userHasResourcePermissions($this->user, 'user');
if (!$sufficient_permissions) {
throw new AccessDeniedException();
}
if (!$this->show_admin_actions) {
$this->redirect($this->url_for('/rooms'));
return;
}
PageLayout::setTitle(_('Übersicht'));
if (Navigation::hasItem('/resources/overview/index')) {
Navigation::activateItem('/resources/overview/index');
}
$sidebar = Sidebar::get();
$room_search = new RoomSearch();
$room_search->setAdditionalPropertyFormat('');
$search = new SearchWidget($this->indexURL());
$search->addNeedle(
_('Suche'),
'tree_selected_resource',
true,
$room_search,
"function(room_id) {STUDIP.Dialog.fromURL(STUDIP.URLHelper.getURL('dispatch.php/resources/room/index/' + room_id));}"
);
$sidebar->addWidget($search);
$tree_selected_resource = null;
if ($this->user_is_global_resource_admin) {
if (ResourceManager::userHasGlobalPermission($this->user, 'admin')){
$locations = Location::findAll();
if($locations) {
$sidebar->addWidget(
new ResourceTreeWidget($locations)
);
}
}
$tree_selected_resource = Request::get('tree_selected_resource');
if ($tree_selected_resource) {
//A resource has been selected: render its index page:
$resource = Resource::find($tree_selected_resource);
if ($resource) {
$resource = $resource->getDerivedClassInstance();
if ($resource) {
if($resource instanceof Room) {
$this->relocate(
$resource->getActionURL('booking_plan')
);
} else {
$this->redirect(
$resource->getActionURL('show')
);
}
}
}
}
}
$this->room_requests_activated = Config::get()->RESOURCES_ALLOW_ROOM_REQUESTS;
$this->display_current_requests = false;
if (!$tree_selected_resource && $this->room_requests_activated ) {
if (Config::get()->RESOURCES_DISPLAY_CURRENT_REQUESTS_IN_OVERVIEW) {
$this->display_current_requests = true;
$this->current_user = User::findCurrent();
//Load a list with the current room requests:
if (ResourceManager::userHasGlobalPermission($this->current_user, 'admin')) {
//Global resource admins can see all room requests.
//Get the 10 latest requests:
$room_requests = RoomRequest::findBySql(
"resource_requests.closed = '0'
ORDER BY chdate DESC
LIMIT 10",
['room_class_names' => RoomManager::getAllRoomClassNames()]
);
} else {
//Users who aren't global resource admins see only the requests
//of the rooms where they have at least 'autor' permissions.
$rooms = RoomManager::getUserRooms(
$this->current_user,
'autor'
);
$room_ids = [];
foreach ($rooms as $room) {
$room_ids[] = $room->id;
}
$room_requests = RoomRequest::findBySql(
"INNER JOIN resources
ON resource_requests.resource_id = resources.id
INNER JOIN resource_categories
ON resources.category_id = resource_categories.id
WHERE
resource_requests.resource_id IN ( :room_ids )
AND
resource_categories.class_name IN ( :room_class_names )
AND
resource_requests.closed = '0'
ORDER BY chdate DESC
LIMIT 10",
[
'room_ids' => $room_ids,
'room_class_names' => RoomManager::getAllRoomClassNames()
]
);
}
$this->room_requests = SimpleCollection::createFromArray($room_requests)
->filter(function($room_request) {
return $room_request->getEndDate()->getTimestamp() > time();
});
}
}
}
public function locations_action()
{
if (Navigation::hasItem('/resources/overview')) {
Navigation::activateItem('/resources/overview');
}
if (Navigation::hasItem('/resources/overview/locations')) {
Navigation::activateItem('/resources/overview/locations');
}
PageLayout::setTitle(
_('Übersicht über alle Standorte')
);
//Check permissions:
if (!$this->user_is_global_resource_admin) {
throw new AccessDeniedException();
}
$actions = new ActionsWidget();
$actions->addLink(
_('Neuer Standort'),
$this->url_for('resources/location/select_category'),
Icon::create('add'),
['data-dialog' => 'size=auto']
);
Sidebar::get()->addWidget($actions);
$this->locations = Location::findAll();
if (!$this->locations) {
PageLayout::postInfo(_('Es wurden keine Standorte gefunden!'));
}
}
public function buildings_action()
{
if (Navigation::hasItem('/resources/overview')) {
Navigation::activateItem('/resources/overview');
}
if (Navigation::hasItem('/resources/overview/buildings')) {
Navigation::activateItem('/resources/overview/buildings');
}
PageLayout::setTitle(_('Übersicht über alle Gebäude'));
//Check permissions:
if (!$this->user_is_global_resource_admin) {
throw new AccessDeniedException();
}
$actions = new ActionsWidget();
$actions->addLink(
_('Neues Gebäude'),
URLHelper::getURL(
'dispatch.php/resources/building/select_category'
),
Icon::create('add'),
['data-dialog' => 'size=auto']
);
Sidebar::get()->addWidget($actions);
$this->buildings = Building::findAll();
if (!$this->buildings) {
PageLayout::postInfo(
_('Es wurden keine Gebäude gefunden!')
);
}
$this->building_ids = [];
$building_names = [];
if (Request::submitted('create_clipboards')) {
$this->building_ids = Request::getArray('building_ids');
foreach ($this->building_ids as $building_id) {
$building = Building::find($building_id);
if (!$building) {
continue;
}
$rooms = $building->rooms;
if (!$rooms) {
//The building has no rooms:
//There is no need to create empty clipboards.
continue;
}
$clipboard = new Clipboard();
$clipboard->user_id = $this->user->id;
$clipboard->name = $building->getFullName();
$clipboard->allowed_item_class = 'Room';
$clipboard->store();
foreach ($rooms as $room) {
$clipboard->addItem($room->id, 'Room');
}
$building_names[] = $building->name;
}
$building_names = implode(', ', $building_names);
if ($this->building_ids) {
PageLayout::postSuccess(
sprintf(
ngettext(
'Die Raumgruppe für das Gebäude %s wurde angelegt!',
'Die Raumgruppen für die Gebäude %s wurden angelegt!',
count($this->building_ids)
),
htmlReady($building_names)
)
);
}
}
}
public function rooms_action()
{
if (ResourceManager::userHasGlobalPermission($this->user)) {
PageLayout::setTitle(_('Übersicht über alle Räume'));
} else {
PageLayout::setTitle(_('Meine Räume'));
}
if (Navigation::hasItem('/resources/overview')) {
Navigation::activateItem('/resources/overview');
}
if (Navigation::hasItem('/resources/overview/rooms')) {
Navigation::activateItem('/resources/overview/rooms');
}
//Check permissions:
$sufficient_permissions = (
$this->user_is_global_resource_user
||
ResourceManager::userHasResourcePermissions($this->user, 'user')
);
if (!$sufficient_permissions) {
throw new AccessDeniedException();
}
//build sidebar:
$sidebar = Sidebar::get();
if ($this->user_is_global_resource_admin) {
$actions = new ActionsWidget();
$actions->addLink(
_('Raum hinzufügen'),
URLHelper::getURL(
'dispatch.php/resources/room/select_category'
),
Icon::create('add'),
['data-dialog' => 'size=auto']
);
$sidebar->addWidget($actions);
}
$clipboard = new RoomClipboardWidget();
$sidebar->addWidget($clipboard);
$search = new SearchWidget($this->roomsURL());
$search->setTitle(_('Raumsuche'));
$search->addNeedle(_('Gebäude'), 'building_name', true);
if ($this->user_is_global_resource_user) {
$search->addNeedle(_('Raum'), 'room_name', true);
}
$sidebar->addWidget($search);
if ($this->user_is_global_resource_user) {
if(Request::get('room_name') || Request::get('building_name')) {
$this->rooms = Room::findByNameOrBuilding(Request::get('room_name'), Request::get('building_name'));
} else {
$this->rooms = Room::findAll();
}
} else {
//Get only the locations for which
//the user has at least user permissions:
$rooms_parameter = [
'user_id' => $this->user->id,
'now' => time()
];
$rooms_sql = "";
if (Request::get('building_name')) {
$rooms_sql .= " INNER JOIN resources pr ON resources.parent_id = pr.id";
}
$rooms_sql .= " INNER JOIN resource_categories rc
ON resources.category_id = rc.id
WHERE rc.class_name IN ( :room_class_names )";
$rooms_parameter['room_class_names'] = RoomManager::getAllRoomClassNames();
if (Request::get('room_name')) {
$rooms_sql .= " AND resources.name LIKE CONCAT('%', :room_name, '%')";
$rooms_parameter['room_name'] = Request::get('room_name');
}
if (Request::get('building_name')) {
$rooms_sql .= " AND pr.name LIKE CONCAT('%', :building_name, '%')";
$rooms_parameter['building_name'] = Request::get('building_name');
}
$rooms_sql .= " AND resources.id IN (
SELECT resource_id
FROM resource_permissions
WHERE
user_id = :user_id
UNION
SELECT resource_id
FROM resource_temporary_permissions
WHERE
user_id = :user_id
AND
begin <= :now
AND
end >= :now
)
ORDER BY sort_position DESC, name ASC, mkdate ASC";
$this->rooms = Room::findBySql($rooms_sql, $rooms_parameter);
}
if (!$this->rooms) {
PageLayout::postInfo(_('Es wurden keine Räume gefunden!'));
}
}
/**
* Get a list of all rooms with have the property
* booking_plan_is_public set.
*/
public function public_booking_plans_action()
{
if (Navigation::hasItem('/resources/overview/public_booking_plans')) {
Navigation::activateItem('/resources/overview/public_booking_plans');
}
if (Navigation::hasItem('/public_booking_plans')) {
Navigation::activateItem('/public_booking_plans');
}
PageLayout::setTitle(_('Öffentlich zugängliche Raumpläne'));
$rooms = Room::findByPublicBookingPlans();
if (!$rooms) {
PageLayout::postInfo(
_('Es sind keine öffentlich zugänglichen Raumpläne vorhanden!')
);
}
$this->grouped_rooms = RoomManager::groupRooms($rooms);
}
/**
* Displays the whole structure of the room management system.
*/
public function structure_action()
{
if (Navigation::hasItem('/resources/structure')) {
Navigation::activateItem('/resources/structure');
}
PageLayout::setTitle(_('Strukturansicht'));
if (!RoomManager::userHasRooms($this->user, 'user', true) && !$this->user_is_global_resource_user) {
throw new AccessDeniedException();
}
$this->locations = Location::findAll();
if (!$this->locations) {
PageLayout::postInfo(
_('Es sind keine Standorte in Stud.IP vorhanden!')
);
}
}