aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/admin/configuration.php
blob: 1e6b3c352fe76e5f13180d64d7ae695fd4f4bf33 (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
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
<?php

/**
 * configuration.php - controller class for the configuration
 *
 * @author  Jan-Hendrik Willms <tleilax+stuip@gmail.com>
 * @author  Nico Müller <nico.mueller@uni-oldenburg.de>
 * @author  Michael Riehemann <michael.riehemann@uni-oldenburg.de>
 * @license GPL2 or any later version
 * @package admin
 * @since   2.0
 */
class Admin_ConfigurationController extends AuthenticatedController
{
    /**
     * Common before filter for all actions.
     *
     * @param String $action Called actions
     * @param Array $args Passed arguments
     */
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        // user must have root permission
        $GLOBALS['perm']->check('root');

        // set navigation
        Navigation::activateItem('/admin/config/configuration');

        $this->range_type = 'global';
        foreach (['range', 'user', 'course', 'institute'] as $range_type) {
            if (mb_strpos($action, $range_type) !== false) {
                $this->range_type = $range_type;
            }
        }

        $this->setupSidebar($this->range_type);
    }

    /**
     * Maintenance view for the configuration parameters
     *
     * @param mixed $section Open section
     */
    public function configuration_action($open_section = null)
    {
        PageLayout::setTitle(_('Verwaltung von Systemkonfigurationen'));

        // Display only one section?
        $section = Request::option('section');
        if ($section == '-1') {
            $section = null;
        }

        // Search for specific entries?
        $needle = trim(Request::get('needle')) ?: null;
        if ($needle) {
            $this->subtitle = _('Suchbegriff:') . ' "' . htmlReady($needle) . '"';
        }

        // set variables for view
        $this->only_section = $section;
        $this->open_section = $open_section ?: $section;
        $this->needle = $needle;
        $this->sections = ConfigurationModel::getConfig($section, $needle);

        $this->title = _('Verwaltung von Systemkonfigurationen');
        $this->linkchunk = 'admin/configuration/edit_configuration';
        $this->has_sections = true;

        if ($needle && empty($this->sections)) {
            PageLayout::postError(sprintf(_('Es wurden keine Ergebnisse zu dem Suchbegriff "%s" gefunden.'), htmlReady($needle)));
            $this->redirect('admin/configuration/configuration');
        }
    }

    /**
     * Editview: Edit the configuration parameters: value, comment, section
     */
    public function edit_configuration_action()
    {
        $field = Request::get('field');
        $value = Request::get('value');

        if (Request::isPost()) {
            CSRFProtection::verifyUnsafeRequest();

            if ($this->validateInput($field, $value)) {
                $section = Request::get('section_new') ?: Request::get('section');
                $comment = Request::get('comment');

                Config::get()->store($field, compact(words('value section comment')));

                PageLayout::postSuccess(sprintf(
                    _('Der Konfigurationseintrag "%s" wurde erfolgreich übernommen!'),
                    htmlReady($field)
                ));

                $this->relocate('admin/configuration/configuration/' . $section);
                return;
            }
        }

        // set variables for view
        $this->config = ConfigurationModel::getConfigInfo($field);
        $this->allconfigs = ConfigurationModel::getConfig();

        PageLayout::setTitle(sprintf(_('Konfigurationsparameter: %s editieren'), $this->config['field']));
    }

    /**
     * Rangeview: Show all user-parameter for a Range or show the system range-parameter
     */
    public function range_configuration_action()
    {
        PageLayout::setTitle(_('Verwalten von Range-Konfigurationen'));

        $range_id = Request::option('id');
        if ($range_id) {
            $range = RangeFactory::find($range_id);

            $this->configs = ConfigurationModel::searchConfiguration($range);
            $this->title = sprintf(
                _('Vorhandene Konfigurationsparameter für "%s"'),
                $range->getFullname()
            );
            $this->linkchunk = 'admin/configuration/edit_range_config/' . $range_id;
        } else {
            $this->configs = ConfigurationModel::searchConfiguration(null);
            $this->title = _('Globale Konfigurationsparameter für alle Ranges');
            $this->linkchunk = 'admin/configuration/edit_configuration/';
        }
        $this->has_sections = false;
    }

    /**
     * Editview: Change range-parameter for one range (value)
     *
     * @param String $range_id
     */
    public function edit_range_config_action($range_id)
    {
        $field = Request::get('field');
        $range = RangeFactory::find($range_id);

        PageLayout::setTitle(_('Bearbeiten von Konfigurationsparametern für die Range: ') . $range->getFullname());

        if (Request::isPost()) {
            CSRFProtection::verifyUnsafeRequest();

            $value = Request::get('value');
            if ($this->validateInput($field, $value)) {
                $range->getConfiguration()->store($field, $value);

                PageLayout::postSuccess(sprintf(
                    _('Der Konfigurationseintrag: %s wurde erfolgreich geändert!'),
                    htmlReady($field)
                ));

                $this->relocate('admin/configuration/range_configuration?id=' . $range_id);
                return;
            }
        }

        $this->config = ConfigurationModel::showConfiguration($range, $field);
        $this->range = $range;
        $this->field = $field;
    }

    /**
     * Userview: Show all user-parameter for a user or show the system user-parameter
     */
    public function user_configuration_action()
    {
        PageLayout::setTitle(_('Verwalten von Personenkonfigurationen'));

        $user_id = Request::option('id');
        $user = new User($user_id);

        if (!$user->isNew()) {
            $this->configs = ConfigurationModel::searchConfiguration($user);
            $this->title = sprintf(
                _('Vorhandene Konfigurationsparameter für "%s"'),
                $user->getFullname()
            );
            $this->linkchunk = 'admin/configuration/edit_user_config/' . $user_id;
        } else {
            $this->configs = ConfigurationModel::searchConfiguration($user);
            $this->title = _('Globale Konfigurationsparameter für alle Personen');
            $this->linkchunk = 'admin/configuration/edit_configuration';
        }
        $this->has_sections = false;
        $this->render_action('range_configuration');
    }

    /**
     * Editview: Change user-parameter for one user (value)
     *
     * @param String $user_id
     */
    public function edit_user_config_action(User $user)
    {
        PageLayout::setTitle(_('Bearbeiten von Konfigurationsparametern für die Person: ') . $user->getFullname());

        $field = Request::get('field');

        if (Request::isPost()) {
            CSRFProtection::verifyUnsafeRequest();

            $value = Request::get('value');
            if ($this->validateInput($field, $value)) {
                $user->getConfiguration()->store($field, $value);

                PageLayout::postSuccess(sprintf(
                    _('Der Konfigurationseintrag: %s wurde erfolgreich geändert!'),
                    htmlReady($field)
                ));

                $this->relocate('admin/configuration/user_configuration?id=' . $user->id);
                return;
            }
        }

        $this->config = ConfigurationModel::showConfiguration($user, $field);
        $this->range = $user;
        $this->field = $field;

        $this->render_action('edit_range_config');
    }

    /**
     * Show all parameters for a course or show the system course parameters
     */
    public function course_configuration_action()
    {
        PageLayout::setTitle(_('Verwalten von Veranstaltungskonfigurationen'));

        $course_id = Request::option('id');
        $course = new Course($course_id);
        if (!$course->isNew()) {
            $this->configs = ConfigurationModel::searchConfiguration($course);
            $this->title = sprintf(
                _('Vorhandene Konfigurationsparameter für "%s"'),
                $course->getFullname()
            );
            $this->linkchunk = 'admin/configuration/edit_course_config/' . $course_id;
        } else {
            $this->configs = ConfigurationModel::searchConfiguration($course);
            $this->title = _('Globale Konfigurationsparameter für alle Veranstaltungen');
            $this->linkchunk = 'admin/configuration/edit_configuration';
        }
        $this->has_sections = false;
        $this->render_action('range_configuration');
    }

    /**
     * Change course parameter for one course (value)
     *
     * @param String $course_id
     */
    public function edit_course_config_action(Course $course)
    {
        PageLayout::setTitle(_('Bearbeiten von Konfigurationsparametern für die Veranstaltung: ') . $course->getFullname());

        $field = Request::get('field');

        if (Request::isPost()) {
            CSRFProtection::verifyUnsafeRequest();

            $value = Request::get('value');
            if ($this->validateInput($field, $value)) {
                $course->getConfiguration()->store($field, $value);

                PageLayout::postSuccess(sprintf(
                    _('Der Konfigurationseintrag: %s wurde erfolgreich geändert!'),
                    htmlReady($field)
                ));

                $this->relocate('admin/configuration/course_configuration?id=' . $course->id);
                return;
            }
        }

        $this->config = ConfigurationModel::showConfiguration($course, $field);
        $this->range = $course;
        $this->field = $field;

        $this->render_action('edit_range_config');
    }

    /**
     * Show all parameters for an institute or show the system institute parameters
     */
    public function institute_configuration_action()
    {
        PageLayout::setTitle(_('Verwalten von Einrichtungskonfigurationen'));

        $institute_id = Request::option('id');
        $institute = new Institute($institute_id);
        if (!$institute->isNew()) {
            $this->configs = ConfigurationModel::searchConfiguration($institute);
            $this->title = sprintf(
                _('Vorhandene Konfigurationsparameter für "%s"'),
                $institute->getFullname()
            );
            $this->linkchunk = 'admin/configuration/edit_institute_config/' . $institute_id;
        } else {
            $this->configs = ConfigurationModel::searchConfiguration($institute);
            $this->title = _('Globale Konfigurationsparameter für alle Einrichtungen');
            $this->linkchunk = 'admin/configuration/edit_configuration';
        }
        $this->has_sections = false;
        $this->render_action('range_configuration');
    }

    /**
     * Change institute parameter for one institute (value)
     *
     * @param String $institute
     */
    public function edit_institute_config_action(Institute $institute)
    {
        PageLayout::setTitle(_('Bearbeiten von Konfigurationsparametern für die Einrichtung: ') . $institute->getFullname());

        $field = Request::get('field');

        if (Request::isPost()) {
            CSRFProtection::verifyUnsafeRequest();

            $value = Request::get('value');
            if ($this->validateInput($field, $value)) {
                $institute->getConfiguration()->store($field, $value);

                PageLayout::postSuccess(sprintf(_('Der Konfigurationseintrag: %s wurde erfolgreich geändert!'), htmlReady($field)));

                $this->relocate('admin/configuration/institute_configuration?id=' . $institute->id);
                return;
            }
        }

        $this->config = ConfigurationModel::showConfiguration($institute, $field);
        $this->range = $institute;
        $this->field = $field;

        $this->render_action('edit_range_config');
    }

    /**
     * Validates given input
     *
     * @param String $field Config field to validate
     * @param String $value Value that has been input
     * @return boolean indicating whether the value is valid
     */
    protected function validateInput($field, &$value)
    {
        $config = Config::get()->getMetadata($field);

        // Step 1: Prepare input
        if ($config['type'] === 'array') {
            $value = json_decode($value, true);
        } elseif ($config['type'] === 'i18n') {
            $value = Request::i18n('value');
        }

        // Step 2: Validate
        if ($config['type'] === 'integer' && !is_numeric($value)) {
            $error = _('Bitte geben Sie bei Parametern vom Typ "integer" nur Zahlen ein!');
        } elseif ($config['type'] === 'array' && !is_array($value)) {
            $error = _('Bitte geben Sie bei Parametern vom Typ "array" ein Array oder Objekt in korrekter JSON Notation ein!');
        } else {
            return true;
        }

        PageLayout::postError($error);

        return false;
    }

    /**
     * Sets up the sidebar
     *
     * @param bool $range_type Determine the sidebar search type
     */
    protected function setupSidebar($range_type)
    {
        // Basic info and layout
        $sidebar = Sidebar::Get();

        // Views
        $views = $sidebar->addWidget(new ViewsWidget());
        $views->addLink(
            _('Globale Konfiguration'),
            $this->url_for('admin/configuration/configuration')
        )->setActive($range_type === 'global');
        $views->addLink(
            _('Range-Konfiguration'),
            $this->url_for('admin/configuration/range_configuration')
        )->setActive($range_type === 'range');
        $views->addLink(
            _('Personenkonfiguration'),
            $this->url_for('admin/configuration/user_configuration')
        )->setActive($range_type === 'user');
        $views->addLink(
            _('Veranstaltungskonfiguration'),
            $this->url_for('admin/configuration/course_configuration')
        )->setActive($range_type === 'course');
        $views->addLink(
            _('Einrichtungskonfiguration'),
            $this->url_for('admin/configuration/institute_configuration')
        )->setActive($range_type === 'institute');

        // Add section selector when not in user mode
        if ($range_type === 'global') {
            $options = [];
            foreach (ConfigurationModel::getConfig() as $key => $value) {
                $options[$key] = $key ?: '- ' . _('Ohne Kategorie') . ' -';
            }
            $widget = new SelectWidget(
                _('Anzeigefilter'),
                $this->url_for('admin/configuration/configuration'),
                'section',
                'get'
            );
            $widget->addElement(new SelectElement(-1, _('alle anzeigen')));
            $widget->setOptions($options);
            $sidebar->addWidget($widget);
        }

        // Add specific searches (specific user when in user mode, keyword
        // otherwise)
        if ($range_type === 'range') {
            $search = new SearchWidget($this->url_for('admin/configuration/range_configuration'));
            $search->addNeedle(
                _('Range suchen'), 'id', true,
                new RangeSearch(),
                'function () { $(this).closest("form").submit(); }',
                Request::option('id')
            );
        } elseif ($range_type === 'user') {
            $search = new SearchWidget($this->url_for('admin/configuration/user_configuration'));
            $search->addNeedle(
                _('Person suchen'), 'id', true,
                new StandardSearch('user_id'),
                'function () { $(this).closest("form").submit(); }',
                Request::option('id')
            );
        } else if ($range_type === 'course') {
            $search = new SearchWidget($this->url_for('admin/configuration/course_configuration'));
            $search->addNeedle(
                _('Veranstaltung suchen'), 'id', true,
                new StandardSearch('Seminar_id'),
                'function () { $(this).closest("form").submit(); }',
                Request::option('id')
            );
        } else if ($range_type === 'institute') {
            $search = new SearchWidget($this->url_for('admin/configuration/institute_configuration'));
            $search->addNeedle(
                _('Einrichtungen suchen'), 'id', true,
                new StandardSearch('Institut_id'),
                'function () { $(this).closest("form").submit(); }',
                Request::option('id')
            );
        } else {
            $search = new SearchWidget($this->url_for('admin/configuration/configuration'));
            $search->addNeedle(_('Suchbegriff'), 'needle', true);
        }
        $sidebar->addWidget($search);
    }
}