aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Theme.php
blob: 824d8cdb06ac4a17010e37f82a06ab120a040dc2 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php

/**
 * Theme.php - Stud.IP Theme Model Class
 *
 * @author    Ron Lucke <lucke@elan-ev.de>
 * @license   GPL2 or any later version
 *
 * @property int $id
 * @property bool $active
 * @property string $name
 * @property string $origin
 * @property string $version
 * @property string $studip_min_version
 * @property string $studip_max_version
 * @property string $author
 * @property string $description
 * @property string $type
 * @property JSONArrayObject $values
 * @property int $mkdate database column
 * @property int $chdate database column
 */

 class Theme extends SimpleORMap
 {
    public const COLOR_KEY_CATEGORIES = [
        'brand' => [
            '--color--brand-primary' => '#28497c',
            '--color--brand-primary-contrast' => '#ffffff',
            '--color--brand-secondary' => '#28497c',
            '--color--brand-secondary-contrast' => '#ffffff',
        ],
        'general' => [
            '--color--global-background' => '#ffffff',
        ],
        'text' => [
            '--color--font-primary' => '#101010',
            '--color--font-secondary' => '#3c454e',
            '--color--font-inactive' => '#676767',
            '--color--font-inverted' => '#ffffff',
        ],
        'navigation' => [
            '--color--main-navigation-item' => '#28497c',
        ],
        'sidebar' => [
            '--color--sidebar-item' => '#28497c',
            '--color--sidebar-item-hover' => '#101010',
        ],
        'content' => [
            '--color--highlight' => '#28497c',
            '--color--highlight-hover' => '#d60000',
            '--color--content-link' => '#28497c',
            '--color--content-link-hover' => '#d60000',
        ],
    ];

    protected static function configure($config = [])
    {
        $config['db_table'] = 'themes';
        $config['serialized_fields']['values'] = JSONArrayObject::class;

        $config['registered_callbacks']['after_store'][] = function (Theme $theme): void {
            if (
                $theme->isFieldDirty('active')
                || (
                    $theme->active
                    && $theme->isFieldDirty('values')
                )
            ) {
                self::loadActiveThemes(true);
                self::getThemeAsset()->writeContent(self::getActiveCSS());
            }
        };

        parent::configure($config);
    }

    public static function getThemeAsset(): PluginAsset
    {
        $asset = new PluginAsset('studip-theme');
        if ($asset->isNew()) {
            $asset->plugin_id = 0;
            $asset->type = 'css';
            $asset->filename = 'theme.css';
            $asset->storagename = 'theme.css';
            $asset->store();

            $asset->writeContent(self::getActiveCSS());
        }
        return $asset;
    }

    /**
     * @return static[]
     */
    public static function getActiveThemes(): array
    {
        return [
            'light' => self::getActiveLightTheme(),
            'dark' => self::getActiveDarkTheme(),
            'high-contrast' => self::getActiveHighContrastTheme(),
        ];
    }

    public static function getDownloadURL(): string
    {
        $asset = self::getThemeAsset();
        return URLHelper::getLink(
            "assets.php/css/{$asset->id}#{$asset->filename}",
            ['v' => $asset->chdate],
            true
        );

    }

    public static function getActiveCSS(): string
    {
        $css = '';
        foreach (self::getActiveThemes() as $theme) {
            if ($theme) {
                $css .= $theme->render() . PHP_EOL;
            }
        }
        return $css;
    }

    public static function getActiveLightTheme(): ?static
    {
        return self::loadActiveThemes()['light'];
    }

    public static function getActiveDarkTheme(): ?static
    {
        return self::loadActiveThemes()['dark'];
    }

    public static function getActiveHighContrastTheme(): ?static
    {
        return self::loadActiveThemes()['high-contrast'];
    }

    protected static ?array $active_themes = null;

    public static function loadActiveThemes(bool $force = false): array
    {
        if ($force || self::$active_themes === null) {
            self::$active_themes = [
                'light'         => null,
                'dark'          => null,
                'high-contrast' => null,
            ];
            self::findEachBySQL(
                function (self $theme): void {
                    self::$active_themes[$theme->type] = $theme;
                },
                'active = 1'
            );
        }
        return self::$active_themes;
    }

    public static function getColorKeyCategories(): array
    {
        return self::COLOR_KEY_CATEGORIES;
    }

    public function render(): string
    {
        $lines = [];

        $indent = '    ';
        if ($this->type === 'dark') {
            $lines[] = '@media (prefers-color-scheme: dark) {';
        } elseif ($this->type === 'high-contrast') {
            $lines[] = '@media (prefers-contrast: more) {';
        } else {
            $indent = '';
        }

        $lines[] = $indent . ':root {';
        foreach ($this->values as $name => $value) {

            if ($value !== '') {
                $lines[] = $indent . "    {$name}: {$value};";
            }
        }
        $lines[] = $indent . '}';

        if ($indent !== '') {
            $lines[] = '}';
        }
        return implode(PHP_EOL, $lines);
    }
 }