aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/admin/lockrules.php
blob: 56879febfb5c879c5a55c3b3ef6bd1c81106606e (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
<?php
# Lifter010: TODO
/**
 * lockrules.php - lock rules admin controller
 *
 * 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      André Noack <noack@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @package     admin
 */

class Admin_LockrulesController extends AuthenticatedController
{
    /**
     * common tasks for all actions
     */
    public function before_filter (&$action, &$args)
    {
        parent::before_filter($action, $args);

        $GLOBALS['perm']->check(Config::get()->LOCK_RULE_ADMIN_PERM ? Config::get()->LOCK_RULE_ADMIN_PERM : 'admin');

        PageLayout::setTitle(_('Verwaltung der Sperrebenen'));
        Navigation::activateItem('/admin/locations/lock_rules');

        URLHelper::bindLinkParam('lock_rule_type', $this->lock_rule_type);

        if (!$this->lock_rule_type || !$GLOBALS['perm']->have_perm('root')) {
            $this->lock_rule_type = 'sem';
        }
        if ($this->lock_rule_type === 'sem') {
            if($GLOBALS['perm']->have_perm('root')) {
                $this->lock_rule_permissions =  ['tutor','dozent','admin','root'];
            } else {
                $this->lock_rule_permissions =  ['tutor','dozent'];
            }
        } elseif ($this->lock_rule_type === 'inst') {
            $this->lock_rule_permissions = ['admin','root'];
        } elseif ($this->lock_rule_type === 'user') {
            $this->lock_rule_permissions = ['tutor','dozent','admin','root'];
        }
        $this->sidebar = Sidebar::Get();
        $this->rule_type_names = [
            'sem'   => _('Veranstaltung'),
            'inst'  => _('Einrichtung'),
            'user'  => _('Person')
        ];
    }

    /**
     * Display the list of lock rules
     */
    public function index_action()
    {
        $actions = new ActionsWidget();
        $actions->addLink(
            _('Neue Sperrebene anlegen'),
            $this->url_for('admin/lockrules/new'),
            Icon::create('add')
        );
        $this->sidebar->addWidget($actions);
        if ($GLOBALS['perm']->have_perm('root')) {
            $list = new SelectWidget(
                _('Bereichsauswahl'),
                $this->url_for('admin/lockrules'),
                'lock_rule_type'
            );
            $types = [
                'sem'   => _('Veranstaltung'),
                'inst'  => _('Einrichtung'),
                'user'  => _('Nutzer')
            ];
            foreach ($types as $type => $desc) {
                $list->addElement(
                    new SelectElement(
                        $type,
                        $desc,
                        Request::get('lock_rule_type') === $type
                    ),
                    'lock_rule_type-' . $type
                );
            }
            $this->sidebar->addWidget($list);
        }

        if ($this->lock_rule_type === 'sem') {
           $this->lock_rules = LockRules::getAdministrableSeminarRules($GLOBALS['user']->id);
        } else {
            $this->lock_rules = LockRule::findAllByType($this->lock_rule_type);
        }
    }

    /**
     * edit one lock rule
     */
    public function edit_action($lock_rule_id)
    {
        $this->lock_rule = LockRule::find($lock_rule_id);
        $this->lock_config = LockRules::getLockRuleConfig($this->lock_rule_type);

        if (Request::submitted('ok')) {
            $ok = $this->handle_form_data();
            if ($ok === false) {
               PageLayout::postError(_('Die Änderungen der Sperrebene konnten nicht gespeichert werden.'), $this->msg['error']);
           } else if ($ok) {
                PageLayout::postSuccess(_('Die Änderungen wurden gespeichert.'));
           }
        }

        $info = new ListWidget();
        $info->setTitle(_('Informationen'));
        $info->addElement(
            new WidgetElement(sprintf(
                _('Diese Sperrebene wird von %s Objekten benutzt.'),
                $this->lock_rule->getUsage())
            )
        );
        $this->sidebar->addWidget($info);
        $actions = new ActionsWidget();
        $actions->addLink(
            _('Diese Ebene löschen'),
            $this->url_for('admin/lockrules/delete/' . $this->lock_rule->getid()),
            Icon::create('trash')
        );
        $actions->addLink(
            _('Bearbeiten abbrechen'),
            $this->url_for('admin/lockrules'),
            Icon::create('decline')
        );
        $this->sidebar->addWidget($actions);

    }

    public function new_action()
    {
        $this->lock_rule = new LockRule();
        $this->lock_config = LockRules::getLockRuleConfig($this->lock_rule_type);

        if (Request::submitted('ok')) {
           $this->lock_rule->user_id = $GLOBALS['user']->id;
           $this->lock_rule->object_type = $this->lock_rule_type;
           if (!$this->handle_form_data()) {
               PageLayout::postError(_('Die neue Sperrebene konnte nicht gespeichert werden.'), $this->msg['error']);
           } else {
               PageLayout::postSuccess(_('Die neue Sperrebene wurde gespeichert'));
               $this->redirect($this->url_for('admin/lockrules/edit/' . $this->lock_rule->getid()));
           }
        }
        $actions = new ActionsWidget();
        $actions->addLink(
            _('Bearbeiten abbrechen'),
            $this->url_for('admin/lockrules'),
            Icon::create('decline')
        );
        $this->sidebar->addWidget($actions);
    }

    public function delete_action($lock_rule_id)
    {
        $this->lock_rule = LockRule::find($lock_rule_id);
        if (!(!$this->lock_rule->isNew() && ($GLOBALS['perm']->have_perm('root') || $this->lock_rule->user_id === $GLOBALS['user']->id))) {
            throw new Trails_Exception(403);
        }
        CSRFProtection::verifyUnsafeRequest();
        if ($this->lock_rule->delete()) {
            PageLayout::postSuccess(_('Die Sperrebene wurde gelöscht.'));
        }
        $this->redirect($this->url_for('admin/lockrules'));
    }

    public function handle_form_data()
    {
        CSRFProtection::verifyUnsafeRequest();
        $this->lock_rule->name = Request::get('lockdata_name');
        $this->lock_rule->description = Request::get('lockdata_description');
        $this->lock_rule->permission = Request::option('lockdata_permission');
        $this->lock_rule->attributes = Request::intArray('lockdata_attributes');
        if (!$this->lock_rule->name) {
            $this->msg['error'][] = _('Bitte geben Sie einen Namen für die Sperrebene an!');
            return false;
        }
        return $this->lock_rule->store();
    }
}