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
|
<?php
/**
* banner.php - controller class for the banner administration
*
* 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 Nico Müller <nico.mueller@uni-oldenburg.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @package admin
* @since 2.4
*/
class Admin_BannerController extends AuthenticatedController
{
protected $_autobind = true;
/**
* Common tasks for all actions.
*/
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
URLHelper::removeLinkParam('cid');
// user must have root permission
$GLOBALS['perm']->check('root');
// set navigation
Navigation::activateItem('/admin/locations/banner');
//pagelayout
PageLayout::setTitle(_('Verwaltung der Banner'));
// Define banner target types
$this->target_types = [
'url' => _('URL'),
'seminar' => _('Veranstaltung'),
'inst' => _('Einrichtung'),
'user' => _('Person'),
'none' => _('Kein Verweis'),
];
// Define banner priorities
$this->priorities = [
0 => '0 (' . _('nicht anzeigen') . ')',
1 => '1 (' . _('sehr niedrig') . ')',
2 => '2',
3 => '3',
4 => '4',
5 => '5',
6 => '6',
7 => '7',
8 => '8',
9 => '9',
10 => '10 (' . _('sehr hoch') . ')',
];
$this->roles = BannerRoles::getAvailableRoles();
$this->rolesStats = RolePersistence::getStatistics();
//Infobox
$this->setSidebar();
}
/**
* Administration view for banner
*/
public function index_action()
{
$this->banners = Banner::getAllBanners();
}
public function info_action(Banner $banner)
{
if ($banner->isNew()) {
throw new Exception(sprintf(_('Es existiert kein Banner mit der Id "%s"'), $banner->id));
}
$this->assigned = BannerRoles::getRoles($banner->id);
}
/**
* Displays edit form and performs according actions upon submit
*
* @param Banner $banner Banner object
*/
public function edit_action(Banner $banner = null)
{
if ($banner->isNew()) {
PageLayout::setTitle(_('Neues Banner anlegen'));
} else {
PageLayout::setTitle(_('Banner bearbeiten'));
}
$this->assigned = BannerRoles::getRoles($banner->id);
$this->roles = BannerRoles::getAvailableRoles($banner->id);
// edit banner input
if (Request::submitted('speichern')) {
$banner_path = Request::get('banner_path');
$banner_mobile_path = Request::get('banner_mobile_path');
$description = Request::get('description');
$alttext = Request::get('alttext');
$target_type = Request::get('target_type');
$priority = Request::int('priority');
//add the right target
if ($target_type == 'url') {
$target = Request::get('target');
} else if ($target_type == 'inst') {
$target = Request::option('institut');
} else if ($target_type == 'user') {
$target = Request::username('user');
} else if ($target_type == 'seminar') {
$target = Request::option('seminar');
} else {
$target = Request::get('target');
}
$errors = [];
//upload file
$upload = $_FILES['imgfile'];
if (!empty($upload['name'])) {
$banner_path = $this->bannerupload($upload['tmp_name'], $upload['size'], $upload['name'], $errors);
}
if(!$banner_path){
$errors[] = _('Es wurde kein Bild ausgewählt.');
}
// upload mobile banner file
$upload = $_FILES['imgfile_mobile'];
if (!empty($upload['name'])) {
$banner_mobile_path = $this->bannerupload($upload['tmp_name'], $upload['size'], $upload['name'], $errors);
}
$startdate = strtotime(Request::get('start_date', 0));
$enddate = strtotime(Request::get('end_date', 0));
if (!$target && $target_type != 'none') {
$errors[] = _('Es wurde kein Verweisziel angegeben.');
} else {
switch ($target_type) {
case 'url':
if (!preg_match('~^(https?|ftp)://~i', $target)) {
$errors[] = _('Das Verweisziel muss eine gültige URL sein (incl. http://).');
}
break;
case 'inst':
if (Institute::find($target) === null) {
$errors[] = _('Die angegebene Einrichtung existiert nicht. '
.'Bitte geben Sie eine gültige Einrichtungs-ID ein.');
}
break;
case 'user':
if (User::findByUsername($target) === null) {
$errors[] = _('Der angegebene Benutzername existiert nicht.') ;
}
break;
case 'seminar':
try {
Seminar::getInstance($target);
} catch (Exception $e) {
$errors[] = _('Die angegebene Veranstaltung existiert nicht. '
.'Bitte geben Sie eine gültige Veranstaltungs-ID ein.');
}
break;
case 'none':
$target = '';
break;
}
}
if (count($errors) > 0) {
PageLayout::postError(_('Es sind folgende Fehler aufgetreten:'), $errors);
$this->redirect('admin/banner');
} else {
$banner->banner_path = $banner_path;
$banner->banner_mobile_path = $banner_mobile_path;
$banner->description = $description;
$banner->alttext = $alttext;
$banner->target_type = $target_type;
$banner->target = $target;
$banner->startdate = $startdate;
$banner->enddate = $enddate;
$banner->priority = $priority;
$banner->store();
$assignedroles = Request::intArray('assignedroles');
BannerRoles::update($banner->ad_id,$assignedroles);
PageLayout::postSuccess(_('Der Banner wurde erfolgreich gespeichert.'));
$this->redirect('admin/banner');
}
}
if (!$banner->isNew()) {
if ($banner->target_type == 'seminar') {
$seminar_name = get_object_name($banner->target, 'sem');
$this->seminar = QuickSearch::get('seminar', new StandardSearch('Seminar_id'))
->setInputStyle('width: 240px')
->defaultValue($banner->target,$seminar_name['name'])
->render();
}
if ($banner->target_type == 'user') {
$this->user = QuickSearch::get('user', new StandardSearch('username'))
->setInputStyle('width: 240px')
->defaultValue($banner->target, $banner->target)
->render();
}
if ($banner->target_type == 'inst') {
$institut_name = get_object_name($banner->target, 'inst');
$this->institut = QuickSearch::get('institut', new StandardSearch('Institut_id'))
->setInputStyle('width: 240px')
->defaultValue($banner->target, $institut_name['name'])
->render();
}
}
}
/**
* Resets the click and view counter for the given banner
*
* @param Banner $banner
*/
public function reset_action(Banner $banner)
{
$banner->views = 0;
$banner->clicks = 0;
$banner->store();
$message = _('Die Klick- und Viewzahlen des Banners wurden zurückgesetzt');
PageLayout::postSuccess($message);
$this->redirect('admin/banner');
}
/**
*
* @param Banner $banner
*/
public function delete_action(Banner $banner)
{
if (Request::int('delete') == 1) {
$banner->delete();
PageLayout::postSuccess(_('Das Banner wurde erfolgreich gelöscht!'));
} elseif (!Request::get('back')) {
$this->flash['delete'] = ['banner_id' => $banner->id];
}
$this->redirect('admin/banner');
}
/**
* Upload a new picture
*
* @param String $img temporary upload path
* @param Int $img_size size of a image
* @param String $img_name name of the image
* @todo Relocate this function into the model?
*/
private function bannerupload($img, $img_size, $img_name, &$errors = [])
{
if (!$img_name) { //keine Datei ausgewählt!
return false;
}
//Dateiendung bestimmen
$dot = mb_strrpos($img_name, '.');
if ($dot) {
$l = mb_strlen($img_name) - $dot;
$ext = mb_strtolower(mb_substr($img_name, $dot + 1, $l));
}
//passende Endung ?
if (!in_array($ext, words('gif jpeg jpg png'))) {
$errors[] = sprintf(_('Der Dateityp der Bilddatei ist falsch (%s).<br>'
.'Es sind nur die Dateiendungen .gif, .png und .jpg erlaubt!')
, htmlReady($ext));
return false;
}
//na dann kopieren wir mal...
$uploaddir = $GLOBALS['DYNAMIC_CONTENT_PATH'] . '/banner';
$md5hash = md5($img_name . time());
$banner_path = $md5hash . '.' . $ext;
$newfile = $uploaddir . '/' . $banner_path;
if(!@move_uploaded_file($img, $newfile)) {
$errors[] = _('Es ist ein Fehler beim Kopieren der Datei aufgetreten. Das Bild wurde nicht hochgeladen!');
return true;
}
chmod($newfile, 0666 & ~umask()); // set permissions for uploaded file
return $banner_path;
}
/**
* Extends this controller with neccessary sidebar
*
* @param String $view Currently viewed group
*/
protected function setSidebar()
{
$sidebar = Sidebar::Get();
$actions = new ActionsWidget();
$actions->addLink(
_('Neues Banner anlegen'),
$this->url_for('admin/banner/edit'),
Icon::create('add')
)->asDialog('size=auto');
$sidebar->addWidget($actions);
}
}
|