MDL-69491 themes: No 'fontsize' setting in Boost causes unit conflict.
[moodle.git] / theme / boost / lib.php
CommitLineData
d6d695b8
FM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Theme functions.
19 *
2ede86d7 20 * @package theme_boost
d6d695b8
FM
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Post process the CSS tree.
29 *
30 * @param string $tree The CSS tree.
31 * @param theme_config $theme The theme config object.
32 */
98f1034c 33function theme_boost_css_tree_post_processor($tree, $theme) {
2ede86d7 34 $prefixer = new theme_boost\autoprefixer($tree);
d6d695b8
FM
35 $prefixer->prefix();
36}
d48dbde9 37
d48dbde9
FM
38/**
39 * Inject additional SCSS.
40 *
41 * @param theme_config $theme The theme config object.
42 * @return string
43 */
2ede86d7 44function theme_boost_get_extra_scss($theme) {
6961f78d
CM
45 $content = '';
46 $imageurl = $theme->setting_file_url('backgroundimage', 'backgroundimage');
47
48 // Sets the background image, and its settings.
49 if (!empty($imageurl)) {
50 $content .= 'body { ';
3ec69c2e 51 $content .= "background-image: url('$imageurl'); background-size: cover;";
6961f78d
CM
52 $content .= ' }';
53 }
54
55 // Always return the background image with the scss when we have it.
56 return !empty($theme->settings->scss) ? $theme->settings->scss . ' ' . $content : $content;
57}
58
59/**
60 * Serves any files associated with the theme settings.
61 *
62 * @param stdClass $course
63 * @param stdClass $cm
64 * @param context $context
65 * @param string $filearea
66 * @param array $args
67 * @param bool $forcedownload
68 * @param array $options
69 * @return bool
70 */
71function theme_boost_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
72 if ($context->contextlevel == CONTEXT_SYSTEM && ($filearea === 'logo' || $filearea === 'backgroundimage')) {
73 $theme = theme_config::load('boost');
74 // By default, theme files must be cache-able by both browsers and proxies.
75 if (!array_key_exists('cacheability', $options)) {
76 $options['cacheability'] = 'public';
77 }
78 return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
79 } else {
80 send_file_not_found();
81 }
d48dbde9
FM
82}
83
41b973bc
FM
84/**
85 * Returns the main SCSS content.
86 *
87 * @param theme_config $theme The theme config object.
88 * @return string
89 */
90function theme_boost_get_main_scss_content($theme) {
91 global $CFG;
92
93 $scss = '';
94 $filename = !empty($theme->settings->preset) ? $theme->settings->preset : null;
95 $fs = get_file_storage();
96
97 $context = context_system::instance();
98 if ($filename == 'default.scss') {
99 $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
100 } else if ($filename == 'plain.scss') {
101 $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/plain.scss');
102 } else if ($filename && ($presetfile = $fs->get_file($context->id, 'theme_boost', 'preset', 0, '/', $filename))) {
103 $scss .= $presetfile->get_content();
104 } else {
105 // Safety fallback - maybe new installs etc.
106 $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
107 }
108
109 return $scss;
110}
111
af9edb2e
BB
112/**
113 * Get compiled css.
114 *
115 * @return string compiled css
116 */
117function theme_boost_get_precompiled_css() {
118 global $CFG;
119 return file_get_contents($CFG->dirroot . '/theme/boost/style/moodle.css');
120}
121
d48dbde9 122/**
258143e3 123 * Get SCSS to prepend.
d48dbde9
FM
124 *
125 * @param theme_config $theme The theme config object.
126 * @return array
127 */
258143e3 128function theme_boost_get_pre_scss($theme) {
76132d1e
DW
129 global $CFG;
130
258143e3 131 $scss = '';
d48dbde9
FM
132 $configurable = [
133 // Config key => [variableName, ...].
3ec69c2e 134 'brandcolor' => ['primary'],
d48dbde9
FM
135 ];
136
258143e3 137 // Prepend variables first.
d48dbde9 138 foreach ($configurable as $configkey => $targets) {
41b973bc 139 $value = isset($theme->settings->{$configkey}) ? $theme->settings->{$configkey} : null;
d48dbde9
FM
140 if (empty($value)) {
141 continue;
142 }
258143e3
FM
143 array_map(function($target) use (&$scss, $value) {
144 $scss .= '$' . $target . ': ' . $value . ";\n";
d48dbde9
FM
145 }, (array) $targets);
146 }
147
258143e3
FM
148 // Prepend pre-scss.
149 if (!empty($theme->settings->scsspre)) {
150 $scss .= $theme->settings->scsspre;
d48dbde9
FM
151 }
152
258143e3 153 return $scss;
d48dbde9 154}