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
|
<?php
/**
* content_terms_of_use.php
*
* Controller for adding, editing and deleting content terms of use entries.
*
* 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 Moritz Strohm <strohm@data-quest.de>
* @copyright data-quest Suchi & Berg GmbH
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @package admin
* @since 4.0
*/
class Admin_ContentTermsOfUseController extends AuthenticatedController
{
public function before_filter(&$action, &$args)
{
if ($action === 'add') {
$action = 'edit';
}
parent::before_filter($action, $args);
// Do the permission check here since all actions are only accessible
// for root users:
if (!$GLOBALS['perm']->have_perm('root')) {
throw new AccessDeniedException();
}
PageLayout::setHelpKeyword('Dateien.Nutzungsbedingungen');
Navigation::activateItem('/admin/locations/content_terms_of_use');
}
/**
* This action displays a list of all content terms of use entries.
*/
public function index_action()
{
PageLayout::setTitle(_('Inhalts-Nutzungsbedingungen'));
//build sidebar
$actions = new ActionsWidget();
$actions->addLink(
_('Eintrag hinzufügen'),
$this->url_for('admin/content_terms_of_use/add'),
Icon::create('add')
)->asDialog('size=auto');
Sidebar::get()->addWidget($actions);
//load all ContentTermsOfUse entries:
$this->entries = ContentTermsOfUse::findBySql('1 ORDER BY position ASC, id ASC');
}
/**
* This action lets a root user edit a new content terms of use entry.
*/
public function edit_action()
{
$id = Request::get('entry_id') ?: null; // Convert possible empty string to null
$this->entry = new ContentTermsOfUse($id);
PageLayout::setTitle(
$this->entry->isNew() ? _('Eintrag hinzufügen') : _('Eintrag bearbeiten')
);
if ($id !== null && $this->entry->isNew()) {
PageLayout::postError(sprintf(
_('Eintrag für Nutzungsbedingungen mit ID %s wurde nicht in der Datenbank gefunden!'),
htmlReady($id)
));
$this->redirect('admin/content_terms_of_use/index');
return;
}
}
public function store_action()
{
if (!Request::isPost()) {
throw new MethodNotAllowedException();
}
$id = Request::get('id') ?: null; // Convert possible empty string to null
$entry = new ContentTermsOfUse($id);
$entry->id = Request::get('entry_id');
$entry->name = Request::i18n('name');
$entry->download_condition = Request::int('download_condition');
$entry->icon = Request::get('icon');
$entry->position = Request::int('position');
$entry->description = Request::i18n('description');
$entry->student_description = Request::i18n('student_description');
$entry->is_default = Request::int('is_default') ?: 0;
if (($errors = $entry->validate()) || $entry->store() === false) {
if (count($errors) === 1) {
PageLayout::postError(reset($errors));
} else {
PageLayout::postError(
_('Fehler beim Speichern des Eintrags für Nutzungsbedingungen'),
$errors
);
}
$this->entry = $entry;
$this->render_action('edit');
return;
}
PageLayout::postSuccess(_('Eintrag für Nutzungsbedingungen wurde gespeichert!'));
$this->redirect('admin/content_terms_of_use/index');
}
/**
* This action lets a root user delete a new content terms of use entry.
*/
public function delete_action()
{
PageLayout::setTitle(_('Eintrag löschen'));
$this->entry_id = Request::get('entry_id');
//load entry by looking at the entry_id:
$entry = ContentTermsOfUse::find($this->entry_id);
if (!$entry) {
//entry not found: return to index page
PageLayout::postError(sprintf(
_('Eintrag für Nutzungsbedingungen mit ID %s wurde nicht in der Datenbank gefunden!'),
htmlReady($this->entry_id)
));
$this->redirect('admin/content_terms_of_use/index');
return;
}
$this->dependent_files_count = FileRef::countBySql(
'content_terms_of_use_id = ?',
[$this->entry_id]
);
//delete was confirmed
if (Request::isPost()) {
if ($this->dependent_files_count > 0) {
//files depend on the entry! We must give them
//a new terms of use entry before we can delete
//the entry!
$this->other_entry_id = Request::get('other_entry_id');
if (!$this->other_entry_id) {
//Files depend on the old entry, but no new entry
//was selected. That's an error!
PageLayout::postError(sprintf(
_('Fehler beim Löschen von Eintrag mit ID %s: Es wurde für betroffene Dateien kein neuer Nutzungsbedingungen-Eintrag ausgewählt!'),
htmlReady($this->entry_id)
));
$this->redirect('admin/content_terms_of_use/index');
return;
}
//Change the content_terms_of_use_id for all file_refs
//that have the ID of the entry which is going to be removed.
$query = "UPDATE file_refs
SET content_terms_of_use_id = :new_id
WHERE content_terms_of_use_id = :removed_id";
$statement = DBManager::get()->prepare($query);
$statement->bindValue(':new_id', $this->other_entry_id);
$statement->bindValue(':removed_id', $this->entry_id);
$result = $statement->execute();
if (!$result) {
PageLayout::postError(sprintf(
_('Fehler beim Zuordnen von Dateien zu Eintrag mit ID %s! Eintrag mit ID %s wurde nicht gelöscht!'),
htmlReady($this->other_entry_id),
htmlReady($this->entry_id)
));
$this->redirect('admin/content_terms_of_use/index');
return;
}
}
//delete the entry:
if ($entry->delete()) {
if ($this->dependent_files_count > 0) {
PageLayout::postSuccess(sprintf(
_('Eintrag mit ID "%s" wurde gelöscht. Alle Dateien, welche diesen Eintrag verwendeten, nutzen nun den Eintrag mit ID "%s"!'),
htmlReady($this->entry_id),
htmlReady($this->other_entry_id)
));
} else {
PageLayout::postSuccess(sprintf(
_('Eintrag mit ID "%s" wurde gelöscht!'),
htmlReady($this->entry_id)
));
}
} else {
if ($this->dependent_files_count > 0) {
PageLayout::postError(sprintf(
_('Fehler beim Löschen von Eintrag mit ID "%s"! Alle Dateien, welche diesen Eintrag verwendeten, nutzen nun den Eintrag mit ID "%s"!'),
htmlReady($this->entry_id),
htmlReady($this->other_entry_id)
));
} else {
PageLayout::postError(sprintf(
_('Fehler beim Löschen von Eintrag mit ID "%s"!'),
htmlReady($this->entry_id)
));
}
}
$this->redirect('admin/content_terms_of_use/index');
return;
}
// form not submitted: If files depend on it,
// another entry must be selected for these files.
if ($this->dependent_files_count > 0) {
$this->other_entries = ContentTermsOfUse::findBySql(
'id <> :entry_id ORDER BY position ASC, id ASC',
[':entry_id' => $this->entry_id]
);
}
}
}
|