aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/admin/holidays.php
blob: 8abdefc24fdb53c7d7ca82bed23896524f1c5891 (plain)
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
<?php
/**
 * holidays.php - controller class for the holidays administration
 *
 * @author    Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @author    Hermann Schröder <hermann.schroeder@uni-oldenburg.de>
 * @author    Michael Riehemann <michael.riehemann@uni-oldenburg.de>
 * @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();
    }

    /**
     * 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');
    }

    /**
     * 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 mixed 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()
    {
        $sidebar = Sidebar::Get();

        $views = new ViewsWidget();
        $views->addLink(_('Alle Einträge'),
                        $this->url_for('admin/holidays', ['filter' => null]))
              ->setActive(!$this->filter);
        $views->addLink(_('Aktuelle/zukünftige Einträge'),
                        $this->url_for('admin/holidays', ['filter' => 'current']))
              ->setActive($this->filter === 'current');
        $views->addLink(_('Vergangene Einträge'),
                        $this->url_for('admin/holidays', ['filter' => 'past']))
              ->setActive($this->filter === 'past');
        $sidebar->addWidget($views);

        $links = new ActionsWidget();
        $links->addLink(_('Neue Ferien anlegen'),
                        $this->url_for('admin/holidays/edit', ['filter' => null]),
                        Icon::create('add', 'clickable'))
              ->asDialog('size=auto');
        $sidebar->addWidget($links);
    }
}