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
|
<?php
/**
* extern.php - administration controller for system-wide external pages
*
* 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 Peter Thienel <thienel@data-quest.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @package extern
* @since 5.5
*/
class Admin_ExternController extends AuthenticatedController
{
protected $range;
protected $template_path;
/**
* @see PluginController::before_filter()
*/
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
$this->checkPerm();
$this->init();
$this->setSidebar();
}
/**
* Initialize the controller.
*/
protected function init()
{
$this->range = 'studip';
$nav = Navigation::getItem('admin/locations/external');
if ($nav) {
$nav->setActive(true);
}
$this->getSystemWideConfigTypes();
$this->config_types['PersBrowse'] = [
'name' => _('Personen-Browser'),
'description' => _('Personal der Einrichtungen'),
'icon' => 'person',
'template' => 'admin/extern/extern_config/persbrowse'
];
$this->fetchPlugins(true);
PageLayout::setTitle(_('Externe Seiten (Systemweit)'));
}
/**
* Action to show index page.
*/
public function index_action()
{
$configs = [];
$count_not_migrated = 0;
ExternPageConfig::findEachBySQL(
function ($c) use (&$configs, &$count_not_migrated) {
$configs[$c->type][] = $c;
if (isset($c->conf['not_fixed_after_migration'])) {
$count_not_migrated++;
}
},
"range_id = ?", [$this->range]
);
$this->configs = $configs;
if ($count_not_migrated > 0) {
PageLayout::postInfo(
_('Die mit einem Ausrufezeichen versehenen Konfigurationen müssen eventuell überarbeitet werden.'),
[
_('Der Aufbau der Templates hat sich in der aktuellen Stud.IP-Version geändert.'),
_('Nach dem Speichern der gekennzeichneten Templates wird die Markierung entfernt.')
]
);
}
$actions_widget = Sidebar::get()->getWidget('actions');
$actions_widget->addLink(
_('Konfiguration hochladen'),
$this->uploadURL(),
Icon::create('upload'),
['data-dialog' => 'size=auto']
);
$this->render_template('institute/extern/index', $this->layout);
}
/**
* Shows dialog to select a new page configuration
*/
public function new_action()
{
PageLayout::setTitle(_('Neue externe Seite anlegen'));
$this->render_template('institute/extern/new', $this->layout);
}
/**
* Create a new configuration or edit an existing one.
*
* @param string $type_id The type of the configuration.
* @param string $config_id The id of the configuration.
* @throws AccessDeniedException
* @throws InvalidSecurityTokenException
* @throws MethodNotAllowedException
*/
public function edit_action(string $type_id, string $config_id = '')
{
$this->load($type_id, $config_id);
$actions_widget = Sidebar::get()->getWidget('actions');
$actions_widget->addLink(
_('Vorschau anzeigen'),
$this->url_for('extern/index', $this->config->id),
Icon::create('log'),
['target' => '_blank']
);
$actions_widget->addLink(
_('Informationen anzeigen'),
$this->infoURL($this->config->id),
Icon::create('infopage'),
['data-dialog' => 'size=auto']
);
$actions_widget->addLink(
_('Konfiguration herunterladen'),
$this->downloadURL($this->config->id),
Icon::create('download')
);
$actions_widget->addLink(
_('Konfiguration hochladen'),
$this->uploadURL($this->config->id),
Icon::create('upload'),
['data-dialog' => 'size=auto']
);
$this->render_template($this->config_types[$type_id]['template'], $this->layout);
}
/**
* Action to store an external page with configuration.
*
* @param string $type_id The type of the external page.
* @param string $config_id The id of the configuration.
* @throws AccessDeniedException
* @throws InvalidSecurityTokenException
* @throws MethodNotAllowedException
*/
public function store_action(string $type_id, string $config_id = '')
{
$this->load($type_id, $config_id);
if (Request::isPost()) {
CSRFProtection::verifyUnsafeRequest();
$this->page->conf = Request::extract(($this->page->getConfigFields()));
$this->page->name = Request::get('name');
$this->page->description = Request::get('description');
$this->page->template = Request::get('template');
if ($this->page->store()) {
if ($this->page->page_config->isNew()) {
PageLayout::postSuccess(sprintf(
_('Eine neue externe Seite "%$1s" vom Typ %$2s wurde angelegt.'),
htmlReady($this->page->name),
htmlReady($this->page->type)
));
} else {
PageLayout::postSuccess(
sprintf(_('Die Konfiguration der externen Seite "%s" wurde gespeichert.'),
htmlReady($this->page->name)
));
}
} else {
PageLayout::postInfo(_('Es wurden keine Änderungen vorgenommen'));
}
if (Request::submitted('store_cancel')) {
$this->redirect($this->indexURL(['open_type' => $type_id]));
return;
}
}
$this->redirect($this->editURL($type_id, $this->page->id));
}
/**
* Loads the configuration to edit or store.
*
* @param string $type_id The type of the external page.
* @param string $config_id The id of the external page.
* @throws AccessDeniedException
*/
protected function load(string $type_id, string $config_id = '')
{
if (!$config_id) {
if (!in_array($type_id, array_keys($this->config_types))) {
throw new AccessDeniedException();
}
$this->config = new ExternPageConfig();
$this->config->type = $type_id;
$this->config->range_id = $this->range;
} else {
$this->config = ExternPageConfig::find($config_id);
if ($this->config->range_id === 'studip') {
$GLOBALS['perm']->check('root');
$have_perm = true;
} else {
$have_perm = $GLOBALS['perm']->have_studip_perm('admin', $this->config->range_id);
}
if (!$this->config || !$have_perm) {
throw new AccessDeniedException();
}
}
$this->page = ExternPage::get($this->config);
}
/**
* Deletes a configuration.
*
* @param string $config_id The id of the configuration to delete.
* @throws AccessDeniedException
* @throws InvalidSecurityTokenException
* @throws MethodNotAllowedException
*/
public function delete_action(string $config_id)
{
$config_type = '';
if (Request::isPost()) {
CSRFProtection::verifyUnsafeRequest();
$config = ExternPageConfig::find($config_id);
if ($config->range_id === 'studip') {
$GLOBALS['perm']->check('root');
$have_perm = true;
} else {
$have_perm = $GLOBALS['perm']->have_studip_perm('admin', $config->range_id);
}
if (!$config || !$have_perm) {
throw new AccessDeniedException();
}
if ($config) {
$config_name = $config->name;
$config_type = $config->type;
if ($config->delete()) {
PageLayout::postSuccess(
sprintf(_('Die Konfiguration "%s" wurde gelöscht.'), htmlReady($config_name))
);
}
}
}
$this->redirect($this->indexURL(['open_type' => $config_type]));
}
/**
* Show dialog with information about the given configuration.
*
* @param $config_id
*/
public function info_action(string $config_id)
{
$this->page = ExternPage::get(ExternPageConfig::find($config_id));
if ($this->page->author) {
$this->author = '<a href="'
. URLHelper::getLink('dispatch.php/profile', ['username' => $this->page->author->username])
. '">'
. htmlReady($this->page->author)
. '</a>';
} else {
$this->author = _('unbekannt');
}
if ($this->page->editor) {
$this->editor = '<a href="'
. URLHelper::getLink('dispatch.php/profile', ['username' => $this->page->editor->username])
. '">'
. htmlReady($this->page->editor)
. '</a>';
} else {
$this->editor = _('unbekannt');
}
PageLayout::setHelpKeyword('Basis.ExterneSeiten' . $this->page->type);
$this->datafields = $this->page->getDataFields();
$this->render_template('institute/extern/info', $this->layout);
}
/**
* Action to download given configuration.
*
* @param $config_id
*/
public function download_action(string $config_id)
{
$config = ExternPageConfig::find($config_id);
$perm = $GLOBALS['perm']->have_studip_perm('admin', $config->range_id)
|| ($GLOBALS['perm']->have_perm('root') && $config->range_id === 'studip');
if (!$config || !$perm) {
throw new AccessDeniedException();
}
$this->response->add_header(
'Content-Disposition',
'attachment; ' . encode_header_parameter('filename', $config->name . '.cfg')
);
$this->render_json($config->toArray('type name description conf template'));
}
/**
* Action to create a new configuration from uploaded file.
*/
public function upload_action()
{
$this->render_template('institute/extern/upload', $this->layout);
}
/**
* Action to import a configuration as json file.
*
* @return void
* @throws InvalidSecurityTokenException
* @throws MethodNotAllowedException
*/
public function import_action()
{
if (Request::submitted('import')) {
$this->config_name = Request::get('config_name', '');
CSRFProtection::verifyUnsafeRequest();
$file = $_FILES['config_file'];
if (!$file) {
PageLayout::postError(_('Es wurde keine Datei ausgewählt!'));
$this->redirect($this->uploadURL());
return;
}
$config_data = json_decode(file_get_contents($file['tmp_name']));
if (!$config_data) {
PageLayout::postError(_('Die Datei hat ein ungültiges Format!'));
$this->redirect($this->uploadURL());
return;
}
$config = new ExternPageConfig();
$fields = [
'name',
'description',
'conf',
'template',
'type',
];
foreach ($fields as $field) {
if (isset($config_data->$field)) {
$config->$field = $config_data->$field;
} else {
PageLayout::postError(_('Die Datei kann nicht importiert werden!'));
$this->redirect($this->uploadURL());
return;
}
}
if (!in_array($config->type, array_keys($this->config_types))) {
PageLayout::postError(_('Der Typ der Konfiguration ist ungültig!'));
$this->redirect($this->uploadURL());
return;
}
if (trim($this->config_name)) {
$config->name = $this->config_name;
}
$config->range_id = $this->range;
$config->author_id = $config->editor_id = $GLOBALS['user']->id;
$config->store();
PageLayout::postSuccess(
sprintf(_('Die Konfiguration "%s" wurde erfolgreich importiert.'),
htmlReady($config->name)
));
}
$this->relocate($this->indexURL(['open_type' => $config->type]));
}
/**
* Action to get study areas by search term.
*
* @param string $search_term The search term.
* @return void
*/
public function search_studyareas_action(string $search_term = '')
{
$search_term = Request::get('term', $search_term);
$paths = [];
$study_areas = StudipStudyArea::search($search_term);
foreach ((array) $study_areas as $study_area) {
if ($study_area->isHidden()) {
continue;
}
$path = $study_area->getPath(' > ');
$paths[$path] = [
'id' => $study_area->id,
'text' => $path
];
}
ksort($paths, SORT_LOCALE_STRING);
$this->render_json([
'results' => array_values($paths)
]);
}
/**
* Checks for sufficient rights.
*
* @throws AccessDeniedException
*/
protected function checkPerm()
{
if (!$GLOBALS['perm']->have_perm('root')) {
throw new AccessDeniedException();
}
}
/**
* Creates the sidebar menu.
*/
protected function setSidebar()
{
if ($this->range) {
$actions_widget = new ActionsWidget();
$actions_widget->addLink(
_('Neue externe Seite anlegen'),
$this->newURL(),
Icon::create('settings'),
['data-dialog' => 'size=870x500']
);
Sidebar::Get()->addWidget($actions_widget);
}
}
/**
* Retrofits navigation with plugins.
*
* @param bool $is_system True to fetch system-wide external page plugins.
* @return void
*/
protected function fetchPlugins(bool $is_system): void
{
$plugins = PluginEngine::getPlugins('ExternPagePlugin');
foreach ($plugins as $plugin) {
if (
$is_system === $plugin->isSystemPage()
|| !$is_system === $plugin->isInstitutePage()
) {
$nav = $plugin->getConfigurationFormNavigation();
$this->config_types[$nav->name] = [
'name' => $nav->title,
'description' => $nav->description,
'icon' => $nav->image,
'template' => $plugin->getConfigurationFormTemplate(),
];
}
}
}
/**
* Sets data for system-wide external pages.
*
* @return void
*/
protected function getSystemWideConfigTypes(): void
{
$this->config_types = [
'PersonDetails' => [
'name' => _('Personen-Details'),
'description' => _('Details zu einer Person'),
'icon' => 'person2',
'template' => 'institute/extern/extern_config/persondetails'
],
'Courses' => [
'name' => _('Veranstaltungen'),
'description' => _('Liste der Veranstaltungen'),
'icon' => 'seminar',
'template' => 'institute/extern/extern_config/courses'
],
'CourseDetails' => [
'name' => _('Veranst.-Details'),
'description' => _('Details zu einer Veranstaltung'),
'icon' => 'seminar',
'template' => 'institute/extern/extern_config/coursedetails'
],
'Timetable' => [
'name' => _('Termine'),
'description' => _('Zeiten/Termine/Themen der Veranstaltungen'),
'icon' => 'date-cycle',
'template' => 'institute/extern/extern_config/timetable'
]
];
}
}
|