aboutsummaryrefslogtreecommitdiff
path: root/lib/models/LoginBackground.class.php
blob: df7fb9e5c1ff77eb5019f871d4a6e29d42008bca (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
<?php
/**
 * LoginBackground.class.php
 * model class for table loginbackgrounds
 *
 * 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      Thomas Hackl <thomas.hackl@uni-passau.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 *
 * @property string background_id database column
 * @property string id alias column for background_id
 * @property string filename database column
 * @property string active database column
 * @property string in_release database column
 */
class LoginBackground extends SimpleORMap
{
    /**
     * Configures this model.
     *
     * @param Array $config
     */
    protected static function configure($config = [])
    {
        $config['db_table'] = 'loginbackgrounds';

        $config['registered_callbacks']['after_delete'][] = function ($pic) {
            if (file_exists($pic->getPath())) {
                unlink($pic->getPath());
            }
        };

        parent::configure($config);
    }

    /**
     * @return string The full URL to this picture.
     */
    public function getURL()
    {
        return URLHelper::getURL(
            $GLOBALS['DYNAMIC_CONTENT_URL']
                . self::getRelativePath()
                . "/{$this->id}.{$this->getExtension()}",
            null,
            true
        );
    }

    /**
     * @return string the full file system path to this picture.
     */
    public function getPath()
    {
        return self::getPictureDirectory() . "/{$this->id}.{$this->getExtension()}";
    }

    /**
     * @return int The file size in bytes.
     */
    public function getFilesize()
    {
        if (file_exists($this->getPath())) {
            return filesize($this->getPath());
        }
        return false;
    }

    /**
     * @return int The picture dimensions as provided by getimagesize.
     */
    public function getDimensions()
    {
        if (file_exists($this->getPath())) {
            return getimagesize($this->getPath());
        }
        return false;
    }

    /**
     * @return string The picture's extension
     */
    public function getExtension()
    {
        return pathinfo($this->filename, PATHINFO_EXTENSION);
    }

    /**
     * Provides a random picture for the given view.
     * @param string $view one of 'desktop', 'mobile'.
     * @return LoginBackground One of the available pictures.
     */
    public static function getRandomPicture($view = 'desktop')
    {
        if (!in_array($view, ['desktop', 'mobile'])) {
            throw new Exception('Unknown view mode');
        }

        $pic = self::findOneBySQL("{$view} = 1 ORDER BY RAND() LIMIT 1");
        return $pic;
    }

    /**
     * @return string The relative path to the Stud.IP web root.
     */
    public static function getRelativePath()
    {
        return '/loginbackgrounds';
    }

    /**
     * @return The directory where all available background pictures live.
     */
    public static function getPictureDirectory()
    {
        return $GLOBALS['DYNAMIC_CONTENT_PATH'] . self::getRelativePath();
    }

}