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
|
<?php
/**
* Settings_DeputiesController - Administration of all user deputy
* related settings
*
* 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 Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @since 2.4
*/
require_once 'settings.php';
class Settings_DeputiesController extends Settings_SettingsController
{
/**
* Set up this controller.
*
* @param String $action Name of the action to be invoked
* @param Array $args Arguments to be passed to the action method
*/
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
PageLayout::setHelpKeyword('Basis.MyStudIPDeputies');
PageLayout::setTitle(_('Standardvertretung'));
Navigation::activateItem('/profile/settings/deputies');
$this->edit_about_enabled = Config::get()->DEPUTIES_EDIT_ABOUT_ENABLE;
}
/**
* Displays the deputy information of a user.
*/
public function index_action()
{
if (Request::submitted('add_deputy') && $deputy_id = Request::option('deputy_id')) {
$this->check_ticket();
if (Deputy::isDeputy($deputy_id, $this->user->user_id)) {
PageLayout::postError(sprintf(
_('%s ist bereits als Vertretung eingetragen.'),
htmlReady(get_fullname($deputy_id))
));
} else if ($deputy_id == $this->user->user_id) {
PageLayout::postError(_('Sie können sich nicht als Ihre eigene Vertretung eintragen!'));
} else if (Deputy::addDeputy($deputy_id, $this->user->user_id)) {
PageLayout::postSuccess(sprintf(
_('%s wurde als Vertretung eingetragen.'),
htmlReady(get_fullname($deputy_id))
));
} else {
PageLayout::postError(_('Fehler beim Eintragen der Vertretung!'));
}
$this->redirect('settings/deputies');
return;
}
$deputies = Deputy::findDeputies($this->user->user_id);
$deputy_user_ids = $deputies->pluck('user_id');
$exclude_users = [$this->user->user_id];
if (is_array($deputies)) {
$exclude_users = array_merge($exclude_users, array_map(function ($d) {
return $d['user_id'];
}, $deputy_user_ids));
}
$this->deputies = $deputies;
$this->search = new PermissionSearch(
'user',
_('Vor-, Nach- oder Benutzername'),
'user_id',
[
'permission' => Deputy::getValidPerms(),
'exclude_user' => $exclude_users
]
);
$actions = new ActionsWidget();
$mp = MultiPersonSearch::get('settings_add_deputy')
->setLinkText(_('Neue Standardvertretung festlegen'))
->setDefaultSelectedUser($deputy_user_ids)
->setLinkIconPath('')
->setTitle(_('Neue Standardvertretung festlegen'))
->setExecuteURL(URLHelper::getLink('dispatch.php/settings/deputies/add_member'))
->setSearchObject($this->search)
->setNavigationItem('/links/settings/deputies')
->render();
$element = LinkElement::fromHTML($mp, Icon::create('community+add'));
$actions->addElement($element);
Sidebar::Get()->addWidget($actions);
}
public function add_member_action()
{
CSRFProtection::verifyRequest();
$mp = MultiPersonSearch::load('settings_add_deputy');
$msg = [
'error' => [],
'success' => [],
];
foreach ($mp->getAddedUsers() as $_user_id) {
if (Deputy::isDeputy($_user_id, $this->user->user_id)) {
$msg['error'][] = sprintf(
_('%s ist bereits als Vertretung eingetragen.'),
htmlReady(get_fullname($_user_id))
);
} else if ($_user_id == $this->user->user_id) {
$msg['error'][] = _('Sie können sich nicht als Ihre eigene Vertretung eintragen!');
} else if (!Deputy::addDeputy($_user_id, $this->user->user_id)) {
$msg['error'][] = _('Fehler beim Eintragen der Vertretung!');
} else {
$msg['success'][] = sprintf(
_('%s wurde als Vertretung eingetragen.'),
htmlReady(get_fullname($_user_id))
);
}
}
// only show an error messagebox once.
if (!empty($msg['error'])) {
PageLayout::postError(
_('Die gewünschte Operation konnte nicht ausgeführt werden.'),
htmlReady($msg['error'])
);
}
if (!empty($msg['success'])) {
PageLayout::postSuccess(
_('Die gewünschten Personen wurden als Ihre Vertretung eingetragen!'),
htmlReady($msg['success'])
);
}
$this->redirect('settings/deputies');
}
/**
* Delete deputy
* @param Deputy $deputy
*/
public function delete_action(Deputy $deputy)
{
CSRFProtection::verifyUnsafeRequest();
if ($deputy->delete()) {
PageLayout::postSuccess(_('Die Vertretung wurde entfernt.'));
}
$this->redirect('settings/deputies');
}
/**
* Stores the deputy settings of a user.
*/
public function store_action()
{
CSRFProtection::verifyUnsafeRequest();
if ($this->edit_about_enabled) {
$deputies = Deputy::findDeputies($this->user->user_id);
$changes = Request::intArray('edit_about');
$success = true;
$changed = 0;
foreach ($deputies as $deputy) {
$state = (int)$changes[$deputy->user_id];
if ($state !== (int)$deputy->edit_about) {
$deputy->edit_about = $state;
if ($deputy->store()) {
$success = true;
$changed++;
}
}
}
if ($success && $changed > 0) {
PageLayout::postSuccess(_('Die Einstellungen wurden gespeichert.'));
} else if ($changed > 0) {
PageLayout::postError(_('Fehler beim Speichern der Einstellungen.'));
}
}
$this->redirect('settings/deputies');
}
}
|