Merge branch 'MDL-74064-master' of https://github.com/HuongNV13/moodle
[moodle.git] / theme / boost / lib.php
blob7e98100f6d8936d9a96a23266351b0972a7012d3
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.
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/>.
17 /**
18 * Theme functions.
20 * @package theme_boost
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Post process the CSS tree.
30 * @param string $tree The CSS tree.
31 * @param theme_config $theme The theme config object.
33 function theme_boost_css_tree_post_processor($tree, $theme) {
34 error_log('theme_boost_css_tree_post_processor() is deprecated. Required' .
35 'prefixes for Bootstrap are now in theme/boost/scss/moodle/prefixes.scss');
36 $prefixer = new theme_boost\autoprefixer($tree);
37 $prefixer->prefix();
40 /**
41 * Inject additional SCSS.
43 * @param theme_config $theme The theme config object.
44 * @return string
46 function theme_boost_get_extra_scss($theme) {
47 $content = '';
48 $imageurl = $theme->setting_file_url('backgroundimage', 'backgroundimage');
50 // Sets the background image, and its settings.
51 if (!empty($imageurl)) {
52 $content .= '@media (min-width: 768px) {';
53 $content .= 'body { ';
54 $content .= "background-image: url('$imageurl'); background-size: cover;";
55 $content .= ' } }';
58 // Sets the login background image.
59 $loginbackgroundimageurl = $theme->setting_file_url('loginbackgroundimage', 'loginbackgroundimage');
60 if (!empty($loginbackgroundimageurl)) {
61 $content .= 'body.pagelayout-login #page { ';
62 $content .= "background-image: url('$loginbackgroundimageurl'); background-size: cover;";
63 $content .= ' }';
66 // Always return the background image with the scss when we have it.
67 return !empty($theme->settings->scss) ? $theme->settings->scss . ' ' . $content : $content;
70 /**
71 * Serves any files associated with the theme settings.
73 * @param stdClass $course
74 * @param stdClass $cm
75 * @param context $context
76 * @param string $filearea
77 * @param array $args
78 * @param bool $forcedownload
79 * @param array $options
80 * @return bool
82 function theme_boost_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
83 if ($context->contextlevel == CONTEXT_SYSTEM && ($filearea === 'logo' || $filearea === 'backgroundimage' ||
84 $filearea === 'loginbackgroundimage')) {
85 $theme = theme_config::load('boost');
86 // By default, theme files must be cache-able by both browsers and proxies.
87 if (!array_key_exists('cacheability', $options)) {
88 $options['cacheability'] = 'public';
90 return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
91 } else {
92 send_file_not_found();
96 /**
97 * Returns the main SCSS content.
99 * @param theme_config $theme The theme config object.
100 * @return string
102 function theme_boost_get_main_scss_content($theme) {
103 global $CFG;
105 $scss = '';
106 $filename = !empty($theme->settings->preset) ? $theme->settings->preset : null;
107 $fs = get_file_storage();
109 $context = context_system::instance();
110 if ($filename == 'default.scss') {
111 $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
112 } else if ($filename == 'plain.scss') {
113 $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/plain.scss');
114 } else if ($filename && ($presetfile = $fs->get_file($context->id, 'theme_boost', 'preset', 0, '/', $filename))) {
115 $scss .= $presetfile->get_content();
116 } else {
117 // Safety fallback - maybe new installs etc.
118 $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
121 return $scss;
125 * Get compiled css.
127 * @return string compiled css
129 function theme_boost_get_precompiled_css() {
130 global $CFG;
131 return file_get_contents($CFG->dirroot . '/theme/boost/style/moodle.css');
135 * Get SCSS to prepend.
137 * @param theme_config $theme The theme config object.
138 * @return array
140 function theme_boost_get_pre_scss($theme) {
141 global $CFG;
143 $scss = '';
144 $configurable = [
145 // Config key => [variableName, ...].
146 'brandcolor' => ['primary'],
149 // Prepend variables first.
150 foreach ($configurable as $configkey => $targets) {
151 $value = isset($theme->settings->{$configkey}) ? $theme->settings->{$configkey} : null;
152 if (empty($value)) {
153 continue;
155 array_map(function($target) use (&$scss, $value) {
156 $scss .= '$' . $target . ': ' . $value . ";\n";
157 }, (array) $targets);
160 // Prepend pre-scss.
161 if (!empty($theme->settings->scsspre)) {
162 $scss .= $theme->settings->scsspre;
165 return $scss;