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
|
<?php
/**
* formerly admin_institut.php - Grunddaten fuer ein Institut
*
* @author Arne Schröder <schroeder@data-quest.de>
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @author Cornelis Kater <ckater@gwdg.de>
* @author Stefan Suchi <suchi@gmx.de>
* @license GPL2 or any version
* @since Stud.IP 3.3
*/
class Institute_BasicdataController extends AuthenticatedController
{
/**
* common tasks for all actions
*/
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
// Ensure only admins gain access to this page
if (!$GLOBALS['perm']->have_perm("admin")) {
throw new AccessDeniedException();
}
}
/**
* show institute basicdata page
*
* @param mixed $i_id Optional institute id
* @throws AccessDeniedException
*/
public function index_action($i_id = false)
{
PageLayout::setTitle(_('Verwaltung der Grunddaten'));
//get ID from an open Institut
$i_view = $i_id ?: Request::option('i_view', Context::getId());
if (!$i_view) {
Navigation::activateItem('/admin/institute/details');
require_once 'lib/admin_search.inc.php';
// This search just died a little inside, so it should be safe to
// continue here but we nevertheless return just to be sure
return;
} elseif ($i_view === 'new') {
closeObject();
Navigation::activateItem('/admin/institute/create');
} else {
Navigation::activateItem('/admin/institute/details');
}
// allow only inst-admin and root to view / edit
if ($i_view && !$GLOBALS['perm']->have_studip_perm('admin', $i_view) && $i_view !== 'new') {
throw new AccessDeniedException();
}
//Change header_line if open object
$header_line = null;
if (Context::get())
{
$header_line = Context::getHeaderLine();
}
if ($header_line) {
PageLayout::setTitle($header_line . ' - ' . PageLayout::getTitle());
}
if (Request::get('i_trykill')) {
$message = _('Sind Sie sicher, dass Sie diese Einrichtung löschen wollen?');
$post['i_kill'] = 1;
$post['studipticket'] = get_ticket();
$this->question = (string) QuestionBox::create(
$message,
$this->url_for('institute/basicdata/delete/' . $i_view, $post),
$this->url_for('institute/basicdata/index/' . $i_view)
);
}
$lockrule = LockRules::getObjectRule($i_view);
if ($lockrule && $lockrule->description && LockRules::CheckLockRulePermission($i_view, $lockrule['permission'])) {
PageLayout::postInfo(formatLinks($lockrule->description));
}
// Load institute data
$institute = new Institute($i_view === 'new' ? null : $i_view);
//add the free administrable datafields
$datafields = [];
$localEntries = DataFieldEntry::getDataFieldEntries($institute->id, 'inst');
if ($localEntries) {
$invalidEntries = $this->flash['invalid_entries'] ?: [];
foreach ($localEntries as $entry) {
if (!$entry->isVisible()) {
continue;
}
$color = '#000000';
if (in_array($entry->getId(), $invalidEntries)) {
$color = '#ff0000';
}
$datafields[] = [
'color' => $color,
'value' => ($GLOBALS['perm']->have_perm($entry->isEditable())
&& !LockRules::Check($institute['Institut_id'], $entry->getId()))
? $entry->getHTML('datafields')
: $entry->getDisplayValue(),
];
}
}
// Read faculties if neccessary
if (count($institute->sub_institutes) === 0) {
if ($GLOBALS['perm']->have_perm('root')) {
$this->faculties = Institute::findBySQL('Institut_id = fakultaets_id ORDER BY Name ASC');
} else {
$temp = User::find($GLOBALS['user']->id)
->institute_memberships->findBy('inst_perms', 'admin')
->pluck('institute');
$institutes = SimpleORMapCollection::createFromArray($temp);
$faculties = $institutes->filter(function ($institute) {
return $institute->is_fak;
});
$this->faculties = $faculties;
}
}
$reason_txt = '';
// Indicates whether the current user is allowed to delete the institute
$this->may_delete = $i_view !== 'new'
&& !(count($institute->home_courses) || count($institute->sub_institutes))
&& ($GLOBALS['perm']->have_perm('root')
|| ($GLOBALS['perm']->is_fak_admin() && Config::get()->INST_FAK_ADMIN_PERMS == 'all'));
if (!$this->may_delete) {
//Set infotext for disabled delete-button
$reason_txt = _('Löschen nicht möglich');
if (count($institute->home_courses) > 0) {
$reason_txt .= ' ';
$reason_txt .= sprintf(ngettext('Es ist eine Veranstaltung zugeordnet.',
'Es sind %u Veranstaltungen zugeordnet.',
count($institute->home_courses)),
count($institute->home_courses));
}
if (count($institute->sub_institutes) > 0) {
$reason_txt .= ' ';
$reason_txt .= sprintf(ngettext('Es ist eine Einrichtung zugeordnet.',
'Es sind %u Einrichtungen zugeordnet.',
count($institute->sub_institutes)),
count($institute->sub_institutes));
}
}
// Indicates whether the current user is allowed to change the faculty
$this->may_edit_faculty = $GLOBALS['perm']->is_fak_admin()
&& !LockRules::Check($institute['Institut_id'], 'fakultaets_id')
&& ($GLOBALS['perm']->have_studip_perm('admin', $institute['fakultaets_id']) || $i_view === 'new');
// Prepare template
$this->institute = $institute;
$this->i_view = $i_view;
$this->datafields = $datafields;
$this->reason_txt = $reason_txt;
}
/**
* Stores the changed or created institute data
*
* @param String $i_id Institute id or 'new' to create
* @throws MethodNotAllowedException
*/
public function store_action($i_id)
{
// We won't accept anything but a POST request
if (!Request::isPost()) {
throw new MethodNotAllowedException();
}
$create_institute = $i_id === 'new';
$institute = new Institute($create_institute ? null : $i_id);
$institute->name = Request::i18n('Name', $institute->name)->trim();
$institute->fakultaets_id = Request::option('Fakultaet', $institute->fakultaets_id);
$institute->strasse = Request::get('strasse', $institute->strasse);
// Beware: Despite the name, this contains both zip code AND city name
$institute->plz = Request::get('plz', $institute->plz);
$institute->url = Request::i18n('url', $institute->url)->trim();
$institute->telefon = Request::get('telefon', $institute->telefon);
$institute->email = Request::get('email', $institute->email);
$institute->fax = Request::get('fax', $institute->fax);
$institute->type = Request::int('type', $institute->type);
$institute->lit_plugin_name = Request::get('lit_plugin_name', $institute->lit_plugin_name);
$institute->lock_rule = Request::option('lock_rule', $institute->lock_rule);
// Do we have all necessary data?
if (!mb_strlen($institute->name)) {
PageLayout::postError(_('Bitte geben Sie eine Bezeichnung für die Einrichtung ein!'));
$this->redirect('institute/basicdata/index/' . $i_id);
return;
}
if ($create_institute) {
$institute->id = $institute->getNewId();
// Is the user allowed to create new faculties
if (!$institute->fakultaets_id && !$GLOBALS['perm']->have_perm('root')) {
PageLayout::postError(_('Sie haben nicht die Berechtigung, neue Fakultäten zu erstellen'));
$this->redirect('institute/basicdata/index/new');
return;
}
// Is the user allowed to create new institutes
if (!$GLOBALS['perm']->have_perm('root') && !($GLOBALS['perm']->is_fak_admin() && Config::get()->INST_FAK_ADMIN_PERMS !== 'none')) {
PageLayout::postError(_('Sie haben nicht die Berechtigung, um neue Einrichtungen zu erstellen!'));
$this->redirect('institute/basicdata/index/new');
return;
}
// Does an institute with the given name already exist in the given faculty?
if ($institute->fakultaets_id && Institute::findOneBySQL('Name = ? AND fakultaets_id = ?', [$institute->name, $institute->fakultaets_id]) !== null) {
PageLayout::postError(sprintf(
_('Die Einrichtung "%s" existiert bereits innerhalb der angegebenen Fakultät!'),
htmlReady($institute->name)
));
$this->redirect('institute/basicdata/index/new');
return;
}
// Does a faculty with the given name already exist
if (!$institute->fakultaets_id && Institute::findOneBySQL('Name = ? AND fakultaets_id = Institut_id', [$institute->name]) !== null) {
PageLayout::postError(sprintf(_('Die Fakultät "%s" existiert bereits!'), htmlReady($institute->name)));
$this->redirect('institute/basicdata/index/new');
return;
}
// Declare faculty status if neccessary
if (!$institute->fakultaets_id) {
$institute->fakultaets_id = $institute->getId();
}
} else {
// Is the user allowed to change the institute/faculty?
if (!$GLOBALS['perm']->have_studip_perm('admin', $institute->id)) {
PageLayout::postError(_('Sie haben nicht die Berechtigung diese Einrichtung zu verändern!'));
$this->redirect('institute/basicdata/index/' . $institute->id);
return;
}
// Save datafields
$datafields = Request::getArray('datafields');
$invalidEntries = [];
$datafields_stored = 0;
foreach (DataFieldEntry::getDataFieldEntries($institute->id, 'inst') as $entry) {
if (isset($datafields[$entry->getId()])) {
$valueBefore = $entry->getValue();
$entry->setValueFromSubmit($datafields[$entry->getId()]);
if ($valueBefore != $entry->getValue()) {
if ($entry->isValid()) {
$datafields_stored += 1;
$entry->store();
} else {
$invalidEntries[] = $entry->getId();
}
}
}
}
// If any errors occured while updating the datafields, report them
if (count($invalidEntries) > 0) {
$this->flash['invalid_entries'] = $invalidEntries;
PageLayout::postError(_('ungültige Eingaben (s.u.) wurden nicht gespeichert'));
}
}
// Try to store the institute, report any errors
if ($institute->isDirty() && $institute->store() === false) {
if ($institute->isNew()) {
PageLayout::postError(_('Die Einrichtung konnte nicht angelegt werden.'));
} else {
PageLayout::postError(_('Die Änderungen konnten nicht gespeichert werden.'));
}
$this->redirect('institute/basicdata/index/' . $i_id);
return;
}
if ($create_institute) {
// Log creation of institute
StudipLog::log('INST_CREATE', $institute->id, null, null, ''); // logging
PageLayout::postSuccess(sprintf(
_('Die Einrichtung "%s" wurde erfolgreich angelegt.'),
htmlReady($institute->name))
);
object_set_visit($institute->id, 0);
} else {
PageLayout::postSuccess(sprintf(
_('Die Änderung der Einrichtung "%s" wurde erfolgreich gespeichert.'),
htmlReady($institute->name))
);
}
$this->redirect('institute/basicdata/index/' . $institute->id, ['cid' => $institute->id]);
}
/**
* Deletes an institute
* @param String $i_id Institute id
*/
public function delete_action($i_id)
{
CSRFProtection::verifyUnsafeRequest();
// Missing parameter
if (!Request::get('i_kill')) {
$this->redirect('institute/basicdata/index/' . $i_id);
return;
}
// Invalid ticket
if (!check_ticket(Request::option('studipticket'))) {
PageLayout::postError(_('Ihr Ticket ist abgelaufen. Versuchen Sie die letzte Aktion erneut.'));
$this->redirect('institute/basicdata/index/' . $i_id);
return;
}
// User may not delete this institue
if (!$GLOBALS['perm']->have_perm('root') && !($GLOBALS['perm']->is_fak_admin() && Config::get()->INST_FAK_ADMIN_PERMS === 'all')) {
PageLayout::postError(_('Sie haben nicht die Berechtigung Fakultäten zu löschen!'));
$this->redirect('institute/basicdata/index/' . $i_id);
return;
}
$institute = Institute::find($i_id);
if ($institute === null) {
throw new Exception('Invalid institute id');
}
// Institut in use?
if (count($institute->home_courses)) {
PageLayout::postError(
_('Diese Einrichtung kann nicht gelöscht werden, da noch Veranstaltungen an dieser Einrichtung existieren!')
);
$this->redirect('institute/basicdata/index/' . $i_id);
return;
}
// Institute has sub institutes?
if (count($institute->sub_institutes)) {
PageLayout::postError(
_('Diese Einrichtung kann nicht gelöscht werden, da sie den Status Fakultät hat und noch andere Einrichtungen zugeordnet sind!')
);
$this->redirect('institute/basicdata/index/' . $i_id);
return;
}
// Is the user allowed to delete faculties?
if ($institute->is_fak && !$GLOBALS['perm']->have_perm('root')) {
PageLayout::postError(_('Sie haben nicht die Berechtigung Fakultäten zu löschen!'));
$this->redirect('institute/basicdata/index/' . $i_id);
return;
}
// Save users, name and number of courses
$user_ids = $institute->members->pluck('user_id');
$i_name = $institute->name;
$i_courses = count($institute->courses);
// Delete that institute
if (!$institute->delete()) {
PageLayout::postError(_('Die Einrichtung konnte nicht gelöscht werden.'));
} else {
$details = [];
// logging - put institute's name in info - it's no longer derivable from id afterwards
StudipLog::log('INST_DEL', $i_id, NULL, $i_name);
// set a suitable default institute for each user
foreach ($user_ids as $user_id) {
StudipLog::log('INST_USER_DEL', $i_id, $user_id);
InstituteMember::ensureDefaultInstituteForUser($user_id);
}
if (count($user_ids)) {
$details[] = sprintf(_('%u Mitarbeiter gelöscht.'), count($user_ids));
}
// Report number of formerly associated courses
if ($i_courses) {
$details[] = sprintf(_('%u Beteiligungen an Veranstaltungen gelöscht'), $i_courses);
}
// delete news-links
StudipNews::DeleteNewsRanges($i_id);
//delete entry in news_rss_range
StudipNews::UnsetRssId($i_id);
//updating range_tree
$query = "UPDATE range_tree SET name = ?, studip_object = '', studip_object_id = '' WHERE studip_object_id = ?";
$statement = DBManager::get()->prepare($query);
$statement->execute([
_('(in Stud.IP gelöscht)'),
$i_id,
]);
if (($db_ar = $statement->rowCount()) > 0) {
$details[] = sprintf(_('%u Bereiche im Einrichtungsbaum angepasst.'), $db_ar);
}
// Statusgruppen entfernen
if ($db_ar = Statusgruppen::deleteBySQL('range_id = ?', [$i_id]) > 0) {
$details[] = sprintf(_('%s Funktionen/Gruppen gelöscht.'), $db_ar);
}
//kill the datafields
DataFieldEntry::removeAll($i_id);
//kill all wiki-pages
$removed_wiki_pages = 0;
WikiPage::findEachBySQL(
function (WikiPage $page) use (&$removed_wiki_pages) {
if ($page->delete()) {
$removed_wiki_pages += 1;
}
},
'range_id = ?',
[$i_id]
);
if ($removed_wiki_pages > 0) {
$details[] = sprintf(_('%u Wikiseiten gelöscht.'));
}
// delete all configuration files for the "extern modules"
if (Config::get()->EXTERN_ENABLE) {
$counts = ExternPageConfig::deleteBySQL('range_id = ?', [$i_id]);
if ($counts) {
$details[] = sprintf(_('%u Konfigurationsdateien für externe Seiten gelöscht.'), $counts);
}
}
// Alle Pluginzuordnungen entfernen
PluginManager::getInstance()->deactivateAllPluginsForRange('inst', $i_id);
//Delete all documents of the institute:
//We must use findOneBySql since findTopFolder cannot determine if
//the folder exists. The institute has already been deleted and
//therefore the method findRangeTypeById (called in findTopFolder)
//cannot determine the range, causing findTopFolder to return null.
$top_folder = Folder::findOneBySql(
"range_id = :range_id AND parent_id = ''",
[
'range_id' => $i_id
]
);
$file_result = false;
if ($top_folder) {
//The top folder for the deleted institute exists:
//We can safely delete it (and its content):
$file_result = $top_folder->delete();
}
if ($file_result) {
$details[] = _('Alle Dokumente gelöscht.');
}
//kill the object_user_vists for this institut
object_kill_visits(null, $i_id);
PageLayout::postSuccess(
sprintf(_('Die Einrichtung "%s" wurde gelöscht!'), htmlReady($i_name)),
$details
);
}
$this->redirect('institute/basicdata/index?cid=');
}
}
|