aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/admin/loginstyle.php
blob: f998b3a6a6677b648a92f1bd8a3e88844e2f3291 (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
<?php
/**
 * loginstyle.php - controller class for administration of login background pics
 *
 * @author    Thomas Hackl <thomas.hackl@uni-passau.de>
 * @license   GPL2 or any later version
 * @category  Stud.IP
 * @package   admin
 * @since     4.0
 */

class Admin_LoginStyleController 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(_('Hintergrundbilder für den Startbildschirm'));
        Navigation::activateItem('/admin/locations/loginstyle');

        // Setup sidebar
        $this->setSidebar();
    }

    /**
     * Display all available background pictures
     */
    public function index_action()
    {
        $this->pictures = LoginBackground::findBySQL("1 ORDER BY `background_id`");
    }

    /**
     * Provides a form for uploading a new picture.
     */
    public function newpic_action()
    {
    }

    /**
     * Adds a new picture ass possible login background.
     */
    public function add_action()
    {
        CSRFProtection::verifyRequest();
        $success = 0;
        foreach ($_FILES['pictures']['name'] as $index => $filename) {
            if ($_FILES['pictures']['error'][$index] !== UPLOAD_ERR_OK) {
                continue;
            }

            $extension = pathinfo($filename, PATHINFO_EXTENSION);
            $extension = strtolower($extension);
            if (!in_array($extension, ['gif', 'jpeg', 'jpg', 'png'])) {
                continue;
            }

            $entry = new LoginBackground();
            $entry->filename = $filename;
            $entry->desktop = Request::int('desktop', 0);
            $entry->mobile = Request::int('mobile', 0);
            if ($entry->store()) {
                $destination = LoginBackground::getPictureDirectory() . DIRECTORY_SEPARATOR
                             . $entry->id . '.' . $extension;
                if (move_uploaded_file($_FILES['pictures']['tmp_name'][$index], $destination)) {
                    $success++;
                } else {
                    $entry->delete();
                }
            }
        }

        if ($success > 0) {
            PageLayout::postSuccess(sprintf(ngettext(
                'Ein Bild wurde hochgeladen.',
                '%u Bilder wurden hochgeladen',
                $success
            ), $success));
        }

        $fail = count($_FILES['pictures']['name']) - $success;
        if ($fail > 0) {
            PageLayout::postError(sprintf(ngettext(
                'Ein Bild konnte nicht hochgeladen werden.',
                '%u Bilder konnten nicht hochgeladen werden.',
                $fail
            ), $fail));
        }
        $this->relocate('admin/loginstyle');
    }

    /**
     * Deletes the given picture.
     * @param $id the picture to delete
     */
    public function delete_action($id)
    {
        $pic = LoginBackground::find($id);
        if ($pic->in_release) {
            PageLayout::postError(_('Dieses Bild wird vom System mitgeliefert und kann daher nicht gelöscht werden.'));
        } elseif ($pic->delete()) {
            PageLayout::postSuccess(_('Das Bild wurde gelöscht.'));
        } else {
            PageLayout::postError(_('Das Bild konnte nicht gelöscht werden.'));
        }

        $this->relocate('admin/loginstyle');
    }

    /**
     * (De-)activate the given picture for given view.
     * @param $id the picture to change activation for
     * @param $view one of 'desktop', 'mobile', view to (de-) activate picture for
     * @param $newStatus new activation status for given view.
     */
    public function activation_action($id, $view, $newStatus)
    {
        $pic = LoginBackground::find($id);
        $pic->$view = $newStatus;
        if ($pic->store()) {
            PageLayout::postSuccess(_('Der Aktivierungsstatus wurde gespeichert.'));
        } else {
            PageLayout::postSuccess(_('Der Aktivierungsstatus konnte nicht gespeichert werden.'));
        }
        $this->relocate('admin/loginstyle');
    }

    /**
     * Adds the content to sidebar
     */
    protected function setSidebar()
    {
        $sidebar = Sidebar::get();

        $links = new ActionsWidget();
        $links->addLink(
            _('Bild hinzufügen'),
            $this->url_for('admin/loginstyle/newpic'),
            Icon::create('add', 'clickable')
        )->asDialog('size=auto');
        $sidebar->addWidget($links);
    }
}