* @author Hermann Schröder * @author Michael Riehemann * @license GPL2 or any later version * @category Stud.IP * @package admin * @since 3.2 */ class Admin_HolidaysController extends AuthenticatedController { /** * common tasks for all actions * * @param String $action Action that has been called * @param Array $args List of arguments */ public function before_filter (&$action, &$args) { parent::before_filter($action, $args); // user must have root permission $GLOBALS['perm']->check('root'); //setting title and navigation PageLayout::setTitle(_('Verwaltung von Ferien')); Navigation::activateItem('/admin/locations/holidays'); // Extract and bind filter option $this->filter = Request::option('filter'); if ($this->filter) { URLHelper::addLinkParam('filter', $this->filter); } $this->setSidebar($action); } /** * Display all informations about the holidays */ public function index_action() { $this->holidays = array_reverse(SemesterHoliday::getAll()); // Filter data? if ($this->filter === 'current') { $this->holidays = array_filter($this->holidays, function ($holiday) { return $holiday->ende > time(); }); } elseif ($this->filter === 'past') { $this->holidays = array_filter($this->holidays, function ($holiday) { return $holiday->ende <= time(); }); } } /** * This method edits existing holidays or creates new holidays * * @param mixed $id Id of the holiday or null to create one */ public function edit_action($id = null) { $this->holiday = new SemesterHoliday($id); PageLayout::setTitle($this->holiday->isNew() ? _('Ferien anlegen') : _('Ferien bearbeiten')); if (Request::isPost()) { CSRFProtection::verifyUnsafeRequest(); $this->holiday->name = Request::get('name'); $this->holiday->description = Request::get('description'); $this->holiday->beginn = $this->getTimeStamp('beginn'); $this->holiday->ende = $this->getTimeStamp('ende', '23:59:59'); $errors = []; if (!$this->holiday->name) { $errors[] = _('Bitte geben Sie einen Namen ein.'); } if (!$this->holiday->beginn) { $errors[] = _('Bitte geben Sie einen Ferienbeginn ein.'); } if (!$this->holiday->ende) { $errors[] = _('Bitte geben Sie ein Ferienende ein.'); } if ($this->holiday->beginn > $this->holiday->ende) { $errors[] = _('Das Ferienende liegt vor dem Beginn.'); } if (!empty($errors)) { PageLayout::postMessage(MessageBox::error(_('Ihre eingegebenen Daten sind ungültig.'), $errors)); } elseif ($this->holiday->isDirty() && !$this->holiday->store()) { PageLayout::postMessage(MessageBox::error(_('Die Ferien konnten nicht gespeichert werden.'))); } else { PageLayout::postMessage(MessageBox::success(_('Die Ferien wurden erfolgreich gespeichert.'))); $this->relocate('admin/holidays'); } } } /** * This method deletes a holiday or a bundle of holidays. * * @param string $id Id of the holiday (or 'bulk' for a bulk operation) */ public function delete_action($id) { $ids = $id === 'bulk' ? Request::optionArray('ids') : [$id]; if (count($ids)) { $holidays = SemesterHoliday::findMany($ids); foreach ($holidays as $holiday) { $holiday->delete(); } PageLayout::postMessage(MessageBox::success(_('Die Ferien wurden erfolgreich gelöscht'))); } $this->redirect('admin/holidays'); } public function holidays_action(): void { $this->holidays = Holidays::getHolidays(true, true); $this->customized = Config::get()->CUSTOMIZED_HOLIDAYS; } public function store_holidays_action(): void { CSRFProtection::verifyUnsafeRequest(); Config::get()->store( 'CUSTOMIZED_HOLIDAYS', Request::intArray('holidays') ); PageLayout::postSuccess(_('Die Änderungen wurden gespeichert.')); $this->redirect($this->holidaysURL()); } /** * Checks a string if it is a valid date and returns the according * unix timestamp if valid. * * @param string $name Parameter name to extract from request * @param string $time Optional time segment * @return int|false Unix timestamp or false if not valid */ private function getTimeStamp($name, $time = '0:00:00') { $date = Request::get($name); if ($date) { list($day, $month, $year) = explode('.', $date); if (checkdate($month, $day, $year)) { return strtotime($date . ' ' . $time); } } return false; } /** * Adds the content to sidebar */ private function setSidebar(string $action): void { $sidebar = Sidebar::Get(); $is_vacation_view = !in_array($action, ['holidays', 'store_holidays']); $views = $sidebar->addWidget(new ViewsWidget()); $views->addLink( _('Ferien'), $this->indexURL() )->setActive($is_vacation_view); $views->addLink( _('Feiertage'), $this->holidaysURL() )->setActive(!$is_vacation_view); if (!$is_vacation_view) { return; } $views = $sidebar->addWidget(new ViewsWidget()); $views->setTitle(_('Ansichtseinstellungen')); $views->addLink( _('Alle Einträge'), $this->indexURL(['filter' => null]) )->setActive(!$this->filter); $views->addLink( _('Aktuelle/zukünftige Einträge'), $this->indexURL(['filter' => 'current']) )->setActive($this->filter === 'current'); $views->addLink( _('Vergangene Einträge'), $this->indexURL(['filter' => 'past']) )->setActive($this->filter === 'past'); $links = $sidebar->addWidget(new ActionsWidget()); $links->addLink( _('Neue Ferien anlegen'), $this->editURL(['filter' => null]), Icon::create('add') )->asDialog('size=auto'); } }