From 653cfb58ca06423ab8636eab7a224c504d027636 Mon Sep 17 00:00:00 2001 From: Sebastian Biller <s.biller@tu-braunschweig.de> Date: Tue, 25 Jan 2022 12:21:22 +0000 Subject: [PATCH] fixes #409 --- app/controllers/admin/banner.php | 209 +++++------------- app/views/admin/banner/datetime-picker.php | 18 -- app/views/admin/banner/edit.php | 237 ++++++++++++--------- app/views/admin/banner/info.php | 29 ++- app/views/admin/banner/new.php | 100 --------- db/migrations/5.1.15_tic_409.php | 36 ++++ lib/models/Banner.class.php | 19 +- lib/models/BannerRoles.class.php | 120 +++++++++++ 8 files changed, 376 insertions(+), 392 deletions(-) delete mode 100644 app/views/admin/banner/datetime-picker.php delete mode 100644 app/views/admin/banner/new.php create mode 100644 db/migrations/5.1.15_tic_409.php create mode 100644 lib/models/BannerRoles.class.php diff --git a/app/controllers/admin/banner.php b/app/controllers/admin/banner.php index c13b8e8c258..5199bbc26fe 100644 --- a/app/controllers/admin/banner.php +++ b/app/controllers/admin/banner.php @@ -59,6 +59,9 @@ class Admin_BannerController extends AuthenticatedController 10 => '10 (' . _('sehr hoch') . ')', ]; + $this->roles = BannerRoles::getAvailableRoles(); + $this->rolesStats = RolePersistence::getStatistics(); + //Infobox $this->setSidebar(); } @@ -76,6 +79,8 @@ class Admin_BannerController extends AuthenticatedController if ($banner->isNew()) { throw new Exception(sprintf(_('Es existiert kein Banner mit der Id "%s"'), $banner->id)); } + + $this->assigned = BannerRoles::getRoles($banner->id); } /** @@ -83,12 +88,17 @@ class Admin_BannerController extends AuthenticatedController * * @param Banner $banner Banner object */ - public function edit_action(Banner $banner) + public function edit_action(Banner $banner = null) { if ($banner->isNew()) { - throw new Exception(sprintf(_('Es existiert kein Banner mit der Id "%s"'), $banner->id)); + 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'); @@ -118,143 +128,13 @@ class Admin_BannerController extends AuthenticatedController $banner_path = $this->bannerupload($upload['tmp_name'], $upload['size'], $upload['name'], $errors); } - if (!$target && $target_type != 'none') { - $errors[] = _('Es wurde kein Verweisziel angegeben.'); - } - - $startDate = explode('.',Request::get('start_date')); - if (($x = $this->valid_date(Request::int('start_hour'), Request::int('start_minute'), $startDate[0],$startDate[1], $startDate[2])) == -1) { - $errors[] = _('Bitte geben Sie einen gültiges Startdatum ein.'); - } else { - $startdate = $x; - } - - $endDate = explode('.',Request::get('end_date')); - if (($x = $this->valid_date(Request::int('end_hour'), Request::int('end_minute'), $endDate[0], $endDate[1], $endDate[2])) == -1) { - $errors[] = _('Bitte geben Sie einen gültiges Enddatum ein.'); - } else { - $enddate = $x; - } - - 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 Username 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); - } else { - $banner->banner_path = $banner_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(); - - PageLayout::postSuccess(_('Der Banner wurde erfolgreich gespeichert.')); - $this->redirect('admin/banner'); - } - } - - 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(); - } - } - - /** - * Create a new banner - */ - public function new_action() - { - // add new banner input - if (Request::submitted('anlegen')) { - $description = Request::get('description'); - $alttext = Request::get('alttext'); - $target_type = Request::option('target_type'); - - //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'); - } - - $priority = Request::int('priority'); - - $errors = []; - - $upload = $_FILES['imgfile']; - if (!$upload['name']) { + if(!$banner_path){ $errors[] = _('Es wurde kein Bild ausgewählt.'); - } else { - $banner_path = $this->bannerupload($upload['tmp_name'], $upload['size'], $upload['name'], $errors); - } - $startDate = explode('.',Request::get('start_date')); - if (($x = $this->valid_date(Request::int('start_hour'), Request::int('start_minute'), $startDate[0],$startDate[1], $startDate[2])) == -1) { - $errors[] = _('Bitte geben Sie einen gültiges Startdatum ein.'); - } else { - $startdate = $x; - } - $endDate = explode('.',Request::get('end_date')); - if (($x = $this->valid_date(Request::int('end_hour'), Request::int('end_minute'), $endDate[0], $endDate[1], $endDate[2])) == -1) { - $errors[] = _('Bitte geben Sie einen gültiges Enddatum ein.'); - } else { - $enddate = $x; } + $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 { @@ -267,7 +147,7 @@ class Admin_BannerController extends AuthenticatedController case 'inst': if (Institute::find($target) === null) { $errors[] = _('Die angegebene Einrichtung existiert nicht. ' - .'Bitte geben Sie eine gültige Einrichtungs-ID ein.'); + .'Bitte geben Sie eine gültige Einrichtungs-ID ein.'); } break; case 'user': @@ -280,7 +160,7 @@ class Admin_BannerController extends AuthenticatedController Seminar::getInstance($target); } catch (Exception $e) { $errors[] = _('Die angegebene Veranstaltung existiert nicht. ' - .'Bitte geben Sie eine gültige Veranstaltungs-ID ein.'); + .'Bitte geben Sie eine gültige Veranstaltungs-ID ein.'); } break; case 'none': @@ -291,8 +171,8 @@ class Admin_BannerController extends AuthenticatedController if (count($errors) > 0) { PageLayout::postError(_('Es sind folgende Fehler aufgetreten:'), $errors); + $this->redirect('admin/banner'); } else { - $banner = new Banner(); $banner->banner_path = $banner_path; $banner->description = $description; $banner->alttext = $alttext; @@ -303,10 +183,38 @@ class Admin_BannerController extends AuthenticatedController $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(); + } + } } /** @@ -384,27 +292,6 @@ class Admin_BannerController extends AuthenticatedController return $banner_path; } - /** - * Validate the date - * - * @param Int $h hour - * @param Int $m minute - * @param Int $d day - * @param Int $mo month - * @param Int $y year - * - */ - protected function valid_date($h, $m, $d, $mo, $y) - { - if ((int)$h + (int)$m + (int)$d + (int)$mo + (int)$y === 0) { - return 0; // 0= forever - } - - // mktime returns -1 if date is invalid (and does some strange - // conversion which might be considered as a bug...) - return mktime($h ,$m, 0, $mo, $d, $y); - } - /** * Extends this controller with neccessary sidebar * @@ -417,7 +304,7 @@ class Admin_BannerController extends AuthenticatedController $actions = new ActionsWidget(); $actions->addLink( _('Neues Banner anlegen'), - $this->url_for('admin/banner/new'), + $this->url_for('admin/banner/edit'), Icon::create('add') )->asDialog('size=auto'); diff --git a/app/views/admin/banner/datetime-picker.php b/app/views/admin/banner/datetime-picker.php deleted file mode 100644 index c8d69d896f5..00000000000 --- a/app/views/admin/banner/datetime-picker.php +++ /dev/null @@ -1,18 +0,0 @@ -<div class="hgroup"> - <input size="11" name="<?=$prefix?>date" id="<?=$prefix?>date" - value="<?if($timestamp) : ?><?=date('d.m.Y',$timestamp)?><?endif;?>" - placeholder ="TT.MM.JJJJ" class="no-hint" - <?if($disabled) : ?>disabled<?endif;?>> - - <input type="number" name="<?= $prefix ?>hour" placeholder="<?= _('hh') ?>" - value="<? if ($timestamp) echo date('G', $timestamp) ?>" - size="2" maxlength="2" style="width: 4em;" class="no-hint" - <? if ($disabled) echo 'disabled'; ?>> : - <input type="number" name="<?= $prefix ?>minute" placeholder="<?= _('mm') ?>" - value="<? if ($timestamp) echo date('i', $timestamp) ?>" - size="2" maxlength="2" style="width: 4em;" class="no-hint" - <? if ($disabled) echo 'disabled'; ?>> - <script> - jQuery('#<?=$prefix?>date').datepicker(); - </script> -</div> diff --git a/app/views/admin/banner/edit.php b/app/views/admin/banner/edit.php index 668fde90af7..0c0108259bf 100644 --- a/app/views/admin/banner/edit.php +++ b/app/views/admin/banner/edit.php @@ -1,103 +1,133 @@ <? use Studip\Button, Studip\LinkButton; ?> -<form action="<?= $controller->edit($banner) ?>" method="post" enctype="multipart/form-data" class="default"> +<form action="<?= $controller->url_for('admin/banner/edit', $banner->id) ?>" method="post" enctype="multipart/form-data" class="default"> <?= CSRFProtection::tokenTag() ?> - <table class="default"> - <tbody> - <tr> - <td class="nohover"> - <? if ($banner['banner_path']): ?> - <?= $banner->toImg() ?> - <? else: ?> - <?= _('Noch kein Bild hochgeladen') ?> - <? endif; ?><br> - - <label class="file-upload"> - <?= _('Bilddatei auswählen') ?> - <input id="imgfile" name="imgfile" type="file" accept="image/*"><br> - <input type="hidden" name="banner_path" value="<?= $banner['banner_path'] ?>"><br> - </label> - </td> - - <td class="nohover" style="vertical-align: top"> - <fieldset> - <legend> - <?= _('Banner bearbeiten') ?> - </legend> - - <label> - <?= _('Beschreibung:') ?> - <input type="text" id="description" name="description" - value="<?= htmlReady($banner['description']) ?>" - size="40" maxlen="254"> - </label> - - <label> - <?= _('Alternativtext:') ?> - - <input type="text" id="alttext" name="alttext" - value="<?= htmlReady($banner['alttext']) ?>" - size="40" maxlen="254"> - </label> - - <label> - <?= _("Verweis-Typ:") ?> - - <input name="target_type" type="hidden" size="8" value="<?=$banner['target_type']?>"> - <select name="target_type" disabled="disabled"> - <? foreach ($target_types as $key => $label): ?> - <option value="<?= $key ?>" <? if ($banner['target_type'] == $key) echo 'selected'; ?>> - <?= $label ?> - </option> - <? endforeach; ?> - </select> - </label> - - <label> - <?= _("Verweis-Ziel:") ?> - - <? if (in_array($banner['target_type'], words('none url'))): ?> - <input type="text" name="target" size="40" maxlen="254" value="<?= htmlReady($banner['target']) ?>"> - <? elseif ($banner['target_type'] == "seminar") :?> - <?= $seminar ?> - <? elseif ($banner['target_type'] == "inst") :?> - <?= $institut ?> - <? else: ?> - <?= $user ?> - <? endif; ?> - </label> - - <label> - <?= _('Anzeigen ab:') ?> - - <?= $this->render_partial('admin/banner/datetime-picker', [ - 'prefix' => 'start_', - 'timestamp' => $banner['startdate']]) ?> - </label> - - <label> - <?= _('Anzeigen bis:') ?> - - <?= $this->render_partial('admin/banner/datetime-picker', [ - 'prefix' => 'end_', - 'timestamp' => $banner['enddate']]) ?> - </label> - - <label> - <?= _('Priorität:')?> - - <select id="priority" name="priority"> - <? foreach ($priorities as $key => $label): ?> - <option value="<?= $key ?>" <? if ($banner['priority'] == $key) echo 'selected'; ?>> - <?= $label ?> - </option> - <? endforeach; ?> - </select> - </label> - </fieldset> - </td> - </tr> - </tbody> - </table> + + <fieldset> + + <label> + <? if ($banner['banner_path']) : ?> + <?= $banner->toImg(['style' => 'max-width:500px']) ?> + <? else : ?> + <?= _('Noch kein Bild hochgeladen') ?> + <? endif; ?><br> + + <label class="file-upload"> + <?= _('Bilddatei auswählen') ?> + <input id="imgfile" name="imgfile" type="file" accept="image/*"> + <input type="hidden" name="banner_path" value="<?= $banner['banner_path'] ?>"> + </label> + </label> + + <label> + <?= _('Beschreibung:') ?> + <input type="text" id="description" name="description" value="<?= htmlReady($banner['description']) ?>" size="40" maxlen="254"> + </label> + + <label> + <?= _('Alternativtext:') ?> + + <input type="text" id="alttext" name="alttext" value="<?= htmlReady($banner['alttext']) ?>" size="40" maxlen="254"> + </label> + + <label> + <?= _("Verweis-Typ:") ?> + + <? if (!$banner->isNew()) : ?> + <input name="target_type" type="hidden" size="8" value="<?= $banner['target_type'] ?>"> + <? endif; ?> + <select id="target_type" name="target_type" <?= $banner->isNew() ?: 'disabled' ?>> + <? foreach ($target_types as $key => $label) : ?> + <option value="<?= $key ?>" <? if ($banner['target_type'] == $key) echo 'selected'; ?>> + <?= $label ?> + </option> + <? endforeach; ?> + </select> + </label> + + <label> + <?= _("Verweis-Ziel:") ?> + + <? if ($banner->isNew()) : ?> + <input type="url" class="target-url" name="target" placeholder="<?= _('URL eingeben') ?>" value="<?= htmlReady($this->flash['request']['target']) ?>" style="width: 240px;" maxlen="254"> + + <?= QuickSearch::get('seminar', new StandardSearch('Seminar_id')) + ->setInputStyle('width: 240px') + ->setInputClass('target-seminar') + ->render() ?> + + <?= QuickSearch::get('institut', new StandardSearch('Institut_id')) + ->setInputStyle('width: 240px') + ->setInputClass('target-inst') + ->render() ?> + + <?= QuickSearch::get('user', new StandardSearch('username')) + ->setInputStyle('width: 240px') + ->setInputClass('target-user') + ->render() ?> + + <span class="target-none"><?= _('Kein Verweisziel') ?></span> + <? else : ?> + <? if (in_array($banner['target_type'], words('none url'))) : ?> + <input type="text" name="target" size="40" maxlen="254" value="<?= htmlReady($banner['target']) ?>"> + <? elseif ($banner['target_type'] == "seminar") : ?> + <?= $seminar ?> + <? elseif ($banner['target_type'] == "inst") : ?> + <?= $institut ?> + <? else : ?> + <?= $user ?> + <? endif; ?> + <? endif; ?> + </label> + + <label> + <?= _('Anzeigen ab:') ?> + + <input type="text" size="20" name="start_date" id="start_date" value="<?= $banner['startdate'] ? + date('d.m.Y H:i', $banner['startdate']) : '' ?>" data-datetime-picker> + </label> + + <label> + <?= _('Anzeigen bis:') ?> + + <input type="text" size="20" name="end_date" id="end_date" value="<?= $banner['enddate'] ? + date('d.m.Y H:i', $banner['enddate']) : '' ?>" data-datetime-picker> + </label> + + <label> + <?= _('Priorität:') ?> + + <select id="priority" name="priority"> + <? foreach ($priorities as $key => $label) : ?> + <option value="<?= $key ?>" <? if ($banner['priority'] == $key) echo 'selected'; ?>> + <?= $label ?> + </option> + <? endforeach; ?> + </select> + </label> + + <label> + <?= _('Sichtbarkeit:') ?> + + <select id="assignedroles" class="nested-select" name="assignedroles[]" multiple> + <? if ($assigned) : ?> + <? foreach ($assigned as $assignedrole) : ?> + <option value="<?= $assignedrole->getRoleid() ?>" selected> + <?= htmlReady($assignedrole->getRolename()) ?> + <? if ($assignedrole->getSystemtype()) : ?>[<?= _('Systemrolle') ?>]<? endif ?> + (<?= $rolesStats[$assignedrole->getRoleid()]['explicit'] + $rolesStats[$assignedrole->getRoleid()]['implicit'] ?>) + </option> + <? endforeach ?> + <? endif ?> + <? foreach ($roles as $role) : ?> + <option value="<?= $role->getRoleid() ?>"> + <?= htmlReady($role->getRolename()) ?> + <? if ($role->getSystemtype()) : ?>[<?= _('Systemrolle') ?>]<? endif ?> + (<?= $rolesStats[$role->getRoleid()]['explicit'] + $rolesStats[$role->getRoleid()]['implicit'] ?>) + </option> + <? endforeach ?> + </select> + </label> + </fieldset> <footer data-dialog-button> <?= Studip\Button::create(_('Speichern'), 'speichern') ?> @@ -107,3 +137,14 @@ ) ?> </footer> </form> + +<? if ($banner->isNew()) : ?> + <script type="text/javascript"> + jQuery(function($) { + $('#target_type').change(function() { + var target = $(this).val(); + $(this).closest('label').next().find('[class^="target"]').hide().filter('.target-' + target).show(); + }).change(); + }); + </script> +<? endif; ?> \ No newline at end of file diff --git a/app/views/admin/banner/info.php b/app/views/admin/banner/info.php index 2720d5ffe86..74e8c730337 100644 --- a/app/views/admin/banner/info.php +++ b/app/views/admin/banner/info.php @@ -41,26 +41,22 @@ <td><?= _('Verweis-Ziel:') ?></td> <td> <input type="text" readonly - value="<?= htmlReady($this->edit['target']) ?>" - size="40" maxlen="254"> + value="<?= htmlReady($banner['target']) ?>" + size="40" maxlen="254" disabled> </td> </tr> <tr> <td><?= _('Anzeigen ab:')?></td> <td> - <?= $this->render_partial('admin/banner/datetime-picker', [ - 'prefix' => 'start_', - 'timestamp' => $banner['startdate'], - 'disabled' => true]) ?> + <input type="text" size="40" name="start_date" id="start_date" value="<?= $banner['startdate'] ? + date('d.m.Y H:i', $banner['startdate']) : '' ?>" data-datetime-picker disabled> </td> </tr> <tr> <td><?= _('Anzeigen bis:') ?></td> <td> - <?= $this->render_partial('admin/banner/datetime-picker', [ - 'prefix' => 'end_', - 'timestamp' => $banner['enddate'], - 'disabled' => true]) ?> + <input type="text" size="40" name="end_date" id="end_date" value="<?= $banner['enddate'] ? + date('d.m.Y H:i', $banner['enddate']) : '' ?>" data-datetime-picker disabled> </td> </tr> <tr> @@ -75,6 +71,19 @@ </select> </td> </tr> + <tr> + <td><?= _('Sichtbarkeit:')?></td> + <td> + <select multiple name="assignedroles[]" size="10" style="width: 100%" disabled> + <? foreach ($assigned as $assignedrole): ?> + <option value="<?= $assignedrole->getRoleid() ?>"> + <?= htmlReady($assignedrole->getRolename()) ?> + <? if ($assignedrole->getSystemtype()): ?>[<?= _('Systemrolle') ?>]<? endif ?> + </option> + <? endforeach ?> + </select> + </td> + </tr> </tbody> </table> diff --git a/app/views/admin/banner/new.php b/app/views/admin/banner/new.php deleted file mode 100644 index 5e27cf05869..00000000000 --- a/app/views/admin/banner/new.php +++ /dev/null @@ -1,100 +0,0 @@ -<form action="<?= $controller->new() ?>" method="post" enctype="multipart/form-data" class="default"> - <fieldset> - <legend> - <?= _('Neues Banner anlegen') ?> - </legend> - - <label class="file-upload"> - <?= _('Bilddatei auswählen') ?> - <input id="imgfile" name="imgfile" type="file" accept="image/*"> - </label> - - <label> - <?= _('Beschreibung') ?> - - <input type="text" id="description" name="description" - value="<?= htmlReady($this->flash['request']['description']) ?>" - style="width: 240px;" maxlen="254"> - </label> - - <label> - <?= _('Alternativtext') ?> - - <input type="text" id="alttext" name="alttext" - value="<?= htmlReady($this->flash['request']['alttext']) ?>" - style="width: 240px;" maxlen="254"> - </label> - - <label> - <?= _('Verweis-Typ') ?> - - <select id="target_type" name="target_type"> - <? foreach ($target_types as $key => $label): ?> - <option value="<?= $key ?>"><?= $label ?></option> - <? endforeach; ?> - </select> - </label> - - - <label> - <?= _('Verweis-Ziel') ?> - - <input type="url" class="target-url" name="target" - placeholder="<?= _('URL eingeben') ?>" - value="<?= htmlReady($this->flash['request']['target']) ?>" - style="width: 240px;" maxlen="254"> - - <?= QuickSearch::get('seminar', new StandardSearch('Seminar_id')) - ->setInputStyle('width: 240px') - ->setInputClass('target-seminar') - ->render() ?> - - <?= QuickSearch::get('institut', new StandardSearch('Institut_id')) - ->setInputStyle('width: 240px') - ->setInputClass('target-inst') - ->render() ?> - - <?= QuickSearch::get('user', new StandardSearch('username')) - ->setInputStyle('width: 240px') - ->setInputClass('target-user') - ->render() ?> - - <span class="target-none"><?= _('Kein Verweisziel') ?></span> - </label> - - <label> - <?= _('Anzeigen ab') ?> - <?= $this->render_partial('admin/banner/datetime-picker', ['prefix' => 'start_']) ?> - </label> - - - <label> - <?= _('Anzeigen bis')?> - <?= $this->render_partial('admin/banner/datetime-picker', ['prefix' => 'end_']) ?> - </label> - - <label> - <?= _('Priorität')?> - - <select name="priority"> - <? foreach ($priorities as $key => $label): ?> - <option value="<?= $key ?>"><?= $label ?></option> - <? endforeach; ?> - </select> - </label> - </fieldset> - - <footer data-dialog-button> - <?= Studip\Button::createAccept(_('Anlegen'), 'anlegen') ?> - <?= Studip\LinkButton::createCancel(_('Abbrechen'), $controller->indexURL()) ?> - </footer> -</form> - -<script type="text/javascript"> -jQuery(function ($) { - $('#target_type').change(function () { - var target = $(this).val(); - $(this).closest('label').next().find('[class^="target"]').hide().filter('.target-' + target).show(); - }).change(); -}); -</script> diff --git a/db/migrations/5.1.15_tic_409.php b/db/migrations/5.1.15_tic_409.php new file mode 100644 index 00000000000..7e4611b9f15 --- /dev/null +++ b/db/migrations/5.1.15_tic_409.php @@ -0,0 +1,36 @@ +<?php +class tic409 extends Migration +{ + public function description() + { + return "create BannerRoles table"; + } + + public function up() + { + $query = 'CREATE TABLE IF NOT EXISTS `banner_roles` ( + `ad_id` CHAR(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL, + `roleid` int(10) NOT NULL, + PRIMARY KEY (`ad_id`, `roleid`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC'; + + DBManager::get()->exec($query); + + $query = "INSERT IGNORE INTO `config` (`field`, `value`, `type`, `range`, `mkdate`, `chdate`, `description`) + VALUES (:name, :value, :type, :range, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), :description)"; + + $statement = DBManager::get()->prepare($query); + $statement->execute([ + ':name' => 'BANNER_ONLY_SYSTEM_ROLES', + ':description' => 'Über diese Option wird die Auswahl der rollenspezifischen Banner auf Systemrollen begrenzt', + ':range' => 'global', + ':type' => 'boolean', + ':value' => '1' + ]); + } + + public function down() + { + DBManager::get()->exec('DROP TABLE IF EXISTS `banner_roles`'); + } +} diff --git a/lib/models/Banner.class.php b/lib/models/Banner.class.php index 3f6611d0e78..8c283e540d8 100644 --- a/lib/models/Banner.class.php +++ b/lib/models/Banner.class.php @@ -35,6 +35,13 @@ class Banner extends SimpleORMap protected static function configure($config = []) { $config['db_table'] = 'banner_ads'; + + $config['has_many']['banner_roles'] = [ + 'class_name' => BannerRoles::class, + 'assoc_foreign_key' => 'ad_id', + 'on_delete' => 'delete' + ]; + parent::configure($config); } @@ -60,11 +67,13 @@ class Banner extends SimpleORMap $sum = 0; // collect banners to consider, build banners array while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { - $sum += pow(2, $row['priority']); - $banners[] = [ - 'ad_id' => $row['ad_id'], - 'offset' => $sum - ]; + if (BannerRoles::checkUserAccess($row['ad_id'])) { + $sum += pow(2, $row['priority']); + $banners[] = [ + 'ad_id' => $row['ad_id'], + 'offset' => $sum + ]; + } } // draw random number and select banner diff --git a/lib/models/BannerRoles.class.php b/lib/models/BannerRoles.class.php new file mode 100644 index 00000000000..82f5722ae3b --- /dev/null +++ b/lib/models/BannerRoles.class.php @@ -0,0 +1,120 @@ +<?php + +/** + * BannerRoles.class.php - model class for the banner roles + * + * 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 Sebastian Biller <s.biller@tu-braunschweig.de> + * @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2 + * @category Stud.IP + * @package admin + * @since 5.1 + * + * @property string ad_id database column + * @property int roleid database column + */ + +class BannerRoles extends SimpleORMap +{ + protected static function configure($config = []) + { + $config['db_table'] = 'banner_roles'; + + $config['belongs_to']['banner_ads'] = [ + 'class_name' => Banner::class, + 'foreign_key' => 'ad_id', + ]; + + parent::configure($config); + } + + public static function checkUserAccess($ad_id, $user_id = null) + { + $user_id = $user_id ?: $GLOBALS['user']->id; + $banner_roles = self::getRoles($ad_id); + $user_roles = RolePersistence::getAssignedRoles($user_id, true); + + if (!$banner_roles) { + return true; + } + + foreach ($banner_roles as $banner_role) { + foreach ($user_roles as $user_role) { + if ($banner_role->getRoleid() === $user_role->getRoleid()) { + return true; + } + } + } + + return false; + } + + public static function getRoles($ad_id) + { + $banner_roles = self::findByad_id($ad_id); + $banner_role_ids = []; + foreach ($banner_roles as $banner_role) { + $banner_role_ids[] = $banner_role['roleid']; + } + + $only_system_roles = Config::get()->BANNER_ONLY_SYSTEM_ROLES; + $roles = RolePersistence::getAllRoles(); + $re = []; + foreach ($banner_role_ids as $role_id) { + if (isset($roles[$role_id])) { + if ($only_system_roles && !$roles[$role_id]->getSystemtype()) { + continue; + } + $re[$role_id] = $roles[$role_id]; + } + } + return $re; + } + + public static function getAvailableRoles($ad_id = null) + { + $banner_role_ids = []; + if ($ad_id) { + $banner_roles = self::findByad_id($ad_id); + foreach ($banner_roles as $banner_role) { + $banner_role_ids[] = $banner_role['roleid']; + } + } + + $only_system_roles = Config::get()->BANNER_ONLY_SYSTEM_ROLES; + $roles = RolePersistence::getAllRoles(); + $rolesStats = RolePersistence::getStatistics(); + $re = []; + foreach ($roles as $key => $role) { + if (!in_array($key, $banner_role_ids)) { + if ($only_system_roles && !$role->getSystemtype()) { + continue; + } + if ($rolesStats[$role->getRoleid()]['explicit'] + $rolesStats[$role->getRoleid()]['implicit'] == 0) { + continue; + } + $re[$key] = $role; + } + } + + return $re; + } + + public static function update($ad_id, $new_roles) + { + self::deleteByAd_id($ad_id); + + if ($new_roles) { + foreach ($new_roles as $new_role) { + $BannerRoles = new self(); + $BannerRoles->ad_id = $ad_id; + $BannerRoles->roleid = $new_role; + $BannerRoles->store(); + } + } + } +} -- GitLab