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
|
<?php
/**
* Settings_CategoriesController - Administration of all user categories
* 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_CategoriesController extends Settings_SettingsController
{
/**
* Set up this controller. Rewrites $action on verification.
*
* @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);
Navigation::activateItem('/profile/categories');
PageLayout::setHelpKeyword('Basis.HomepageSonstiges');
PageLayout::setTitle(_('Eigene Kategorien bearbeiten'));
}
/**
* Display the categories of a user.
*
* @param mixed $verify_action Optional name of an action to be verified
* @param mixed $verify_id Optional id that belongs to the action to
* be verified
*/
public function index_action($verify_action = null, $verify_id = null)
{
$categories = Kategorie::findByUserId($this->user->user_id);
$visibilities = [];
$hidden_count = 0;
foreach ($categories as $index => $category) {
$visibilities[$category->kategorie_id] = Visibility::getStateDescription('kat_' . $category->kategorie_id, $this->user->user_id);
if ($this->restricted && $GLOBALS['perm']->have_perm('admin') && $visibilities[$category->kategorie_id] == VISIBILITY_ME) {
$hidden_count += 1;
unset($categories[$index]);
}
}
$this->categories = array_values($categories);
$this->count = count($categories);
$this->hidden_count = $hidden_count;
$this->visibilities = $visibilities;
$this->verify = $verify_action
? ['action' => $verify_action, 'id' => $verify_id]
: false;
$sidebar = Sidebar::get();
$actions = new ActionsWidget();
$actions->addLink(
_('Neue Kategorie anlegen'),
$this->url_for('settings/categories/create'),
Icon::create('add')
);
$sidebar->addWidget($actions);
}
/**
* Creates a new category
*/
public function create_action()
{
Kategorie::increasePrioritiesByUserId($this->user->user_id);
$category = new Kategorie;
$category->range_id = $this->user->user_id;
$category->name = _('neue Kategorie');
$category->content = _('Inhalt der Kategorie');
$category->priority = 0;
if ($category->store()) {
PageLayout::postSuccess(_('Neue Kategorie angelegt.'));
Visibility::addPrivacySetting($category->name, 'kat_' . $category->id, 'owncategory');
} else {
PageLayout::postSuccess(_('Anlegen der Kategorie fehlgeschlagen.'));
}
$this->redirect('settings/categories');
}
/**
* Deletes a given category.
*
* @param String $id Id of the category to be deleted
* @param bool $verified Indicates whether the delete action has been
* verfified
*/
public function delete_action($id, $verified = false)
{
$category = Kategorie::find($id);
$name = $category->name;
if ($category->range_id !== $GLOBALS['user']->user_id &&
(!Deputy::isEditActivated() || !Deputy::isDeputy($GLOBALS['user']->user_id, $category->range_id, true))) {
PageLayout::postError(_('Sie haben leider nicht die notwendige Berechtigung für diese Aktion.'));
$this->redirect('settings/categories');
return;
}
if (!$verified) {
$this->redirect($this->url_for('settings/categories/verify', 'delete', $id));
return;
}
if ($category->delete()) {
PageLayout::postSuccess(sprintf(_('Kategorie "%s" gelöscht!'), htmlReady($name)));
Visibility::removePrivacySetting('kat_' . $id);
} else {
PageLayout::postError(sprintf(_('Kategorie "%s" konnte nicht gelöscht werden!'), htmlReady($name)));
}
$this->redirect('settings/categories');
}
/**
* Stores all categories
*/
public function store_action()
{
$changed = false;
Kategorie::findEachMany(
function (Kategorie $category) use (&$changed) {
$category->name = Request::i18n("category-name-{$category->id}");
$category->content = Request::i18n(
"category-content-{$category->id}",
null,
function ($input) {
return Studip\Markup::purifyHtml($input);
}
);
if ($category->store()) {
$changed = true;
Visibility::renamePrivacySetting("kat_{$category->id}", $category->name->original());
}
},
Request::optionArray('ids')
);
if ($changed) {
PageLayout::postSuccess(_('Kategorien geändert!'));
}
$this->redirect('settings/categories');
}
/**
* Swaps the position of two categories
*
* @param String $id0 Id of the category to be swapped
* @param String $id1 Id of the other category to be swapped
*/
public function swap_action($id0, $id1)
{
$category0 = Kategorie::find($id0);
$category1 = Kategorie::find($id1);
$priorities = $category0->priority + $category1->priority;
$category0->priority = $priorities - $category0->priority;
$category1->priority = $priorities - $category1->priority;
if ($category0->store() && $category1->store()) {
PageLayout::postSuccess(_('Kategorien wurden neu geordnet'));
} else {
PageLayout::postError(_('Kategorien konnten nicht neu geordnet werden.'));
}
$this->redirect('settings/categories');
}
public function verify_action($action, $id = null)
{
if ($action === 'delete' && $id) {
PageLayout::postQuestion(
sprintf(
_('Möchten Sie wirklich die Kategorie "%s" löschen?'),
htmlReady(Kategorie::find($id)->name)
),
$this->url_for("settings/categories/delete/{$id}/1")
)->includeTicket();
}
$this->redirect('settings/categories');
}
}
|