Commit | Line | Data |
---|---|---|
571fa828 | 1 | <?php |
571fa828 | 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 | ||
78946b9b PS |
17 | /** |
18 | * Functions for generating the HTML that Moodle should output. | |
19 | * | |
20 | * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML | |
21 | * for an overview. | |
22 | * | |
473dd811 SH |
23 | * @copyright 2009 Tim Hunt |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
f8129210 | 25 | * @package core |
76be40cc | 26 | * @category output |
78946b9b PS |
27 | */ |
28 | ||
78bfb562 PS |
29 | defined('MOODLE_INTERNAL') || die(); |
30 | ||
d9c8f425 | 31 | require_once($CFG->libdir.'/outputcomponents.php'); |
32 | require_once($CFG->libdir.'/outputactions.php'); | |
33 | require_once($CFG->libdir.'/outputfactories.php'); | |
d9c8f425 | 34 | require_once($CFG->libdir.'/outputrenderers.php'); |
0bb38e8c | 35 | require_once($CFG->libdir.'/outputrequirementslib.php'); |
d9c8f425 | 36 | |
571fa828 | 37 | /** |
5dd40ec4 | 38 | * Returns current theme revision number. |
473dd811 | 39 | * |
5dd40ec4 | 40 | * @return int |
78946b9b | 41 | */ |
5dd40ec4 RW |
42 | function theme_get_revision() { |
43 | global $CFG; | |
44 | ||
45 | if (empty($CFG->themedesignermode)) { | |
46 | if (empty($CFG->themerev)) { | |
47 | // This only happens during install. It doesn't matter what themerev we use as long as it's positive. | |
48 | return 1; | |
49 | } else { | |
50 | return $CFG->themerev; | |
51 | } | |
52 | ||
53 | } else { | |
54 | return -1; | |
55 | } | |
56 | } | |
57 | ||
58 | /** | |
59 | * Returns current theme sub revision number. This is the revision for | |
60 | * this theme exclusively, not the global theme revision. | |
61 | * | |
62 | * @param string $themename The non-frankenstyle name of the theme | |
63 | * @return int | |
64 | */ | |
65 | function theme_get_sub_revision_for_theme($themename) { | |
66 | global $CFG; | |
67 | ||
68 | if (empty($CFG->themedesignermode)) { | |
69 | $pluginname = "theme_{$themename}"; | |
63470cf3 | 70 | $revision = during_initial_install() ? null : get_config($pluginname, 'themerev'); |
5dd40ec4 RW |
71 | |
72 | if (empty($revision)) { | |
73 | // This only happens during install. It doesn't matter what themerev we use as long as it's positive. | |
74 | return 1; | |
75 | } else { | |
76 | return $revision; | |
77 | } | |
78 | } else { | |
79 | return -1; | |
80 | } | |
81 | } | |
82 | ||
83 | /** | |
84 | * Calculates and returns the next theme revision number. | |
85 | * | |
86 | * @return int | |
87 | */ | |
88 | function theme_get_next_revision() { | |
89 | global $CFG; | |
78946b9b | 90 | |
f11db1a6 PS |
91 | $next = time(); |
92 | if (isset($CFG->themerev) and $next <= $CFG->themerev and $CFG->themerev - $next < 60*60) { | |
93 | // This resolves problems when reset is requested repeatedly within 1s, | |
94 | // the < 1h condition prevents accidental switching to future dates | |
95 | // because we might not recover from it. | |
96 | $next = $CFG->themerev+1; | |
97 | } | |
98 | ||
5dd40ec4 RW |
99 | return $next; |
100 | } | |
101 | ||
102 | /** | |
103 | * Calculates and returns the next theme revision number. | |
104 | * | |
105 | * @param string $themename The non-frankenstyle name of the theme | |
106 | * @return int | |
107 | */ | |
108 | function theme_get_next_sub_revision_for_theme($themename) { | |
109 | global $CFG; | |
110 | ||
111 | $next = time(); | |
112 | $current = theme_get_sub_revision_for_theme($themename); | |
113 | if ($next <= $current and $current - $next < 60 * 60) { | |
114 | // This resolves problems when reset is requested repeatedly within 1s, | |
115 | // the < 1h condition prevents accidental switching to future dates | |
116 | // because we might not recover from it. | |
117 | $next = $current + 1; | |
118 | } | |
119 | ||
120 | return $next; | |
121 | } | |
122 | ||
123 | /** | |
124 | * Sets the current theme revision number. | |
125 | * | |
126 | * @param int $revision The new theme revision number | |
127 | */ | |
128 | function theme_set_revision($revision) { | |
129 | set_config('themerev', $revision); | |
130 | } | |
131 | ||
132 | /** | |
133 | * Sets the current theme revision number for a specific theme. | |
134 | * This does not affect the global themerev value. | |
135 | * | |
136 | * @param string $themename The non-frankenstyle name of the theme | |
137 | * @param int $revision The new theme revision number | |
138 | */ | |
139 | function theme_set_sub_revision_for_theme($themename, $revision) { | |
140 | set_config('themerev', $revision, "theme_{$themename}"); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Get the path to a theme config.php file. | |
145 | * | |
146 | * @param string $themename The non-frankenstyle name of the theme to check | |
147 | */ | |
148 | function theme_get_config_file_path($themename) { | |
149 | global $CFG; | |
150 | ||
151 | if (file_exists("{$CFG->dirroot}/theme/{$themename}/config.php")) { | |
152 | return "{$CFG->dirroot}/theme/{$themename}/config.php"; | |
153 | } else if (!empty($CFG->themedir) and file_exists("{$CFG->themedir}/{$themename}/config.php")) { | |
154 | return "{$CFG->themedir}/{$themename}/config.php"; | |
155 | } else { | |
156 | return null; | |
157 | } | |
158 | } | |
159 | ||
160 | /** | |
161 | * Get the path to the local cached CSS file. | |
162 | * | |
163 | * @param string $themename The non-frankenstyle theme name. | |
164 | * @param int $globalrevision The global theme revision. | |
165 | * @param int $themerevision The theme specific revision. | |
166 | * @param string $direction Either 'ltr' or 'rtl' (case sensitive). | |
167 | */ | |
168 | function theme_get_css_filename($themename, $globalrevision, $themerevision, $direction) { | |
169 | global $CFG; | |
170 | ||
171 | $path = "{$CFG->localcachedir}/theme/{$globalrevision}/{$themename}/css"; | |
172 | $filename = $direction == 'rtl' ? "all-rtl_{$themerevision}" : "all_{$themerevision}"; | |
173 | return "{$path}/{$filename}.css"; | |
174 | } | |
175 | ||
176 | /** | |
177 | * Generates and saves the CSS files for the given theme configs. | |
178 | * | |
179 | * @param theme_config[] $themeconfigs An array of theme_config instances. | |
180 | * @param array $directions Must be a subset of ['rtl', 'ltr']. | |
181 | * @param bool $cache Should the generated files be stored in local cache. | |
182 | */ | |
183 | function theme_build_css_for_themes($themeconfigs = [], $directions = ['rtl', 'ltr'], $cache = true) { | |
184 | global $CFG; | |
185 | ||
186 | if (empty($themeconfigs)) { | |
187 | return; | |
188 | } | |
189 | ||
190 | require_once("{$CFG->libdir}/csslib.php"); | |
191 | ||
192 | $themescss = []; | |
193 | $themerev = theme_get_revision(); | |
194 | // Make sure the local cache directory exists. | |
195 | make_localcache_directory('theme'); | |
196 | ||
197 | foreach ($themeconfigs as $themeconfig) { | |
198 | $themecss = []; | |
199 | $oldrevision = theme_get_sub_revision_for_theme($themeconfig->name); | |
200 | $newrevision = theme_get_next_sub_revision_for_theme($themeconfig->name); | |
201 | ||
202 | // First generate all the new css. | |
203 | foreach ($directions as $direction) { | |
b3480499 DW |
204 | // Lock it on. Technically we should build all themes for SVG and no SVG - but ie9 is out of support. |
205 | $themeconfig->force_svg_use(true); | |
5dd40ec4 RW |
206 | $themeconfig->set_rtl_mode(($direction === 'rtl')); |
207 | ||
208 | $themecss[$direction] = $themeconfig->get_css_content(); | |
209 | if ($cache) { | |
b3480499 | 210 | $themeconfig->set_css_content_cache($themecss[$direction]); |
5dd40ec4 RW |
211 | $filename = theme_get_css_filename($themeconfig->name, $themerev, $newrevision, $direction); |
212 | css_store_css($themeconfig, $filename, $themecss[$direction]); | |
213 | } | |
214 | } | |
215 | $themescss[] = $themecss; | |
216 | ||
217 | if ($cache) { | |
218 | // Only update the theme revision after we've successfully created the | |
219 | // new CSS cache. | |
220 | theme_set_sub_revision_for_theme($themeconfig->name, $newrevision); | |
221 | ||
222 | // Now purge old files. We must purge all old files in the local cache | |
223 | // because we've incremented the theme sub revision. This will leave any | |
224 | // files with the old revision inaccessbile so we might as well removed | |
225 | // them from disk. | |
226 | foreach (['ltr', 'rtl'] as $direction) { | |
227 | $oldcss = theme_get_css_filename($themeconfig->name, $themerev, $oldrevision, $direction); | |
228 | if (file_exists($oldcss)) { | |
229 | unlink($oldcss); | |
230 | } | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
235 | return $themescss; | |
236 | } | |
237 | ||
238 | /** | |
239 | * Invalidate all server and client side caches. | |
240 | * | |
241 | * This method deletes the physical directory that is used to cache the theme | |
242 | * files used for serving. | |
243 | * Because it deletes the main theme cache directory all themes are reset by | |
244 | * this function. | |
245 | */ | |
246 | function theme_reset_all_caches() { | |
247 | global $CFG, $PAGE; | |
a990e4fa | 248 | require_once("{$CFG->libdir}/filelib.php"); |
5dd40ec4 RW |
249 | |
250 | $next = theme_get_next_revision(); | |
251 | theme_set_revision($next); | |
fe7b75f8 | 252 | |
98245a84 PS |
253 | if (!empty($CFG->themedesignermode)) { |
254 | $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'core', 'themedesigner'); | |
255 | $cache->purge(); | |
256 | } | |
257 | ||
4cc2f33b SL |
258 | // Purge compiled post processed css. |
259 | cache::make('core', 'postprocessedcss')->purge(); | |
260 | ||
a990e4fa MH |
261 | // Delete all old theme localcaches. |
262 | $themecachedirs = glob("{$CFG->localcachedir}/theme/*", GLOB_ONLYDIR); | |
263 | foreach ($themecachedirs as $localcachedir) { | |
264 | fulldelete($localcachedir); | |
265 | } | |
266 | ||
fe7b75f8 PS |
267 | if ($PAGE) { |
268 | $PAGE->reload_theme(); | |
269 | } | |
78946b9b PS |
270 | } |
271 | ||
272 | /** | |
273 | * Enable or disable theme designer mode. | |
473dd811 | 274 | * |
78946b9b | 275 | * @param bool $state |
78946b9b PS |
276 | */ |
277 | function theme_set_designer_mod($state) { | |
78946b9b | 278 | set_config('themedesignermode', (int)!empty($state)); |
98245a84 PS |
279 | // Reset caches after switching mode so that any designer mode caches get purged too. |
280 | theme_reset_all_caches(); | |
78946b9b PS |
281 | } |
282 | ||
f1ecad60 RW |
283 | /** |
284 | * Checks if the given device has a theme defined in config.php. | |
285 | * | |
286 | * @return bool | |
287 | */ | |
288 | function theme_is_device_locked($device) { | |
289 | global $CFG; | |
290 | $themeconfigname = core_useragent::get_device_type_cfg_var_name($device); | |
291 | return isset($CFG->config_php_settings[$themeconfigname]); | |
292 | } | |
293 | ||
294 | /** | |
295 | * Returns the theme named defined in config.php for the given device. | |
296 | * | |
297 | * @return string or null | |
298 | */ | |
299 | function theme_get_locked_theme_for_device($device) { | |
300 | global $CFG; | |
301 | ||
302 | if (!theme_is_device_locked($device)) { | |
303 | return null; | |
304 | } | |
305 | ||
306 | $themeconfigname = core_useragent::get_device_type_cfg_var_name($device); | |
307 | return $CFG->config_php_settings[$themeconfigname]; | |
308 | } | |
571fa828 | 309 | |
571fa828 | 310 | /** |
fdeb7fa1 | 311 | * This class represents the configuration variables of a Moodle theme. |
312 | * | |
313 | * All the variables with access: public below (with a few exceptions that are marked) | |
3d3fae72 | 314 | * are the properties you can set in your themes config.php file. |
fdeb7fa1 | 315 | * |
316 | * There are also some methods and protected variables that are part of the inner | |
3d3fae72 | 317 | * workings of Moodle's themes system. If you are just editing a themes config.php |
fa1afe32 | 318 | * file, you can just ignore those, and the following information for developers. |
ebebf55c | 319 | * |
320 | * Normally, to create an instance of this class, you should use the | |
321 | * {@link theme_config::load()} factory method to load a themes config.php file. | |
fa1afe32 | 322 | * However, normally you don't need to bother, because moodle_page (that is, $PAGE) |
fdeb7fa1 | 323 | * will create one for you, accessible as $PAGE->theme. |
571fa828 | 324 | * |
325 | * @copyright 2009 Tim Hunt | |
473dd811 SH |
326 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
327 | * @since Moodle 2.0 | |
f8129210 | 328 | * @package core |
76be40cc | 329 | * @category output |
571fa828 | 330 | */ |
ebebf55c | 331 | class theme_config { |
473dd811 | 332 | |
5f0baa43 | 333 | /** |
76be40cc | 334 | * @var string Default theme, used when requested theme not found. |
5f0baa43 | 335 | */ |
e56b58e3 | 336 | const DEFAULT_THEME = 'boost'; |
5f0baa43 | 337 | |
41b973bc FM |
338 | /** The key under which the SCSS file is stored amongst the CSS files. */ |
339 | const SCSS_KEY = '__SCSS__'; | |
340 | ||
ebebf55c | 341 | /** |
76be40cc | 342 | * @var array You can base your theme on other themes by linking to the other theme as |
78946b9b | 343 | * parents. This lets you use the CSS and layouts from the other themes |
f8129210 | 344 | * (see {@link theme_config::$layouts}). |
fdeb7fa1 | 345 | * That makes it easy to create a new theme that is similar to another one |
3d3fae72 | 346 | * but with a few changes. In this themes CSS you only need to override |
fdeb7fa1 | 347 | * those rules you want to change. |
fdeb7fa1 | 348 | */ |
78946b9b | 349 | public $parents; |
571fa828 | 350 | |
fdeb7fa1 | 351 | /** |
76be40cc | 352 | * @var array The names of all the stylesheets from this theme that you would |
78946b9b | 353 | * like included, in order. Give the names of the files without .css. |
fdeb7fa1 | 354 | */ |
78946b9b | 355 | public $sheets = array(); |
571fa828 | 356 | |
fdeb7fa1 | 357 | /** |
76be40cc | 358 | * @var array The names of all the stylesheets from parents that should be excluded. |
78946b9b PS |
359 | * true value may be used to specify all parents or all themes from one parent. |
360 | * If no value specified value from parent theme used. | |
fdeb7fa1 | 361 | */ |
78946b9b | 362 | public $parents_exclude_sheets = null; |
571fa828 | 363 | |
fdeb7fa1 | 364 | /** |
76be40cc | 365 | * @var array List of plugin sheets to be excluded. |
78946b9b | 366 | * If no value specified value from parent theme used. |
fdeb7fa1 | 367 | */ |
78946b9b | 368 | public $plugins_exclude_sheets = null; |
571fa828 | 369 | |
fdeb7fa1 | 370 | /** |
76be40cc | 371 | * @var array List of style sheets that are included in the text editor bodies. |
78946b9b | 372 | * Sheets from parent themes are used automatically and can not be excluded. |
fdeb7fa1 | 373 | */ |
78946b9b | 374 | public $editor_sheets = array(); |
571fa828 | 375 | |
0e9911e1 AN |
376 | /** |
377 | * @var bool Whether a fallback version of the stylesheet will be used | |
378 | * whilst the final version is generated. | |
379 | */ | |
380 | public $usefallback = false; | |
381 | ||
38aafea2 | 382 | /** |
76be40cc | 383 | * @var array The names of all the javascript files this theme that you would |
04c01408 | 384 | * like included from head, in order. Give the names of the files without .js. |
38aafea2 PS |
385 | */ |
386 | public $javascripts = array(); | |
387 | ||
04c01408 | 388 | /** |
76be40cc | 389 | * @var array The names of all the javascript files this theme that you would |
04c01408 | 390 | * like included from footer, in order. Give the names of the files without .js. |
04c01408 PS |
391 | */ |
392 | public $javascripts_footer = array(); | |
4399b9d5 | 393 | |
38aafea2 | 394 | /** |
76be40cc SH |
395 | * @var array The names of all the javascript files from parents that should |
396 | * be excluded. true value may be used to specify all parents or all themes | |
397 | * from one parent. | |
38aafea2 | 398 | * If no value specified value from parent theme used. |
38aafea2 PS |
399 | */ |
400 | public $parents_exclude_javascripts = null; | |
401 | ||
fdeb7fa1 | 402 | /** |
76be40cc | 403 | * @var array Which file to use for each page layout. |
fdeb7fa1 | 404 | * |
78946b9b PS |
405 | * This is an array of arrays. The keys of the outer array are the different layouts. |
406 | * Pages in Moodle are using several different layouts like 'normal', 'course', 'home', | |
407 | * 'popup', 'form', .... The most reliable way to get a complete list is to look at | |
408 | * {@link http://cvs.moodle.org/moodle/theme/base/config.php?view=markup the base theme config.php file}. | |
d4a03c00 | 409 | * That file also has a good example of how to set this setting. |
fdeb7fa1 | 410 | * |
78946b9b | 411 | * For each layout, the value in the outer array is an array that describes |
d4a03c00 | 412 | * how you want that type of page to look. For example |
413 | * <pre> | |
414 | * $THEME->layouts = array( | |
39df78bf | 415 | * // Most pages - if we encounter an unknown or a missing page type, this one is used. |
191b267b | 416 | * 'standard' => array( |
78946b9b PS |
417 | * 'theme' = 'mytheme', |
418 | * 'file' => 'normal.php', | |
d4a03c00 | 419 | * 'regions' => array('side-pre', 'side-post'), |
420 | * 'defaultregion' => 'side-post' | |
421 | * ), | |
422 | * // The site home page. | |
423 | * 'home' => array( | |
78946b9b PS |
424 | * 'theme' = 'mytheme', |
425 | * 'file' => 'home.php', | |
d4a03c00 | 426 | * 'regions' => array('side-pre', 'side-post'), |
427 | * 'defaultregion' => 'side-post' | |
428 | * ), | |
429 | * // ... | |
430 | * ); | |
431 | * </pre> | |
fdeb7fa1 | 432 | * |
78946b9b PS |
433 | * 'theme' name of the theme where is the layout located |
434 | * 'file' is the layout file to use for this type of page. | |
435 | * layout files are stored in layout subfolder | |
d4a03c00 | 436 | * 'regions' This lists the regions on the page where blocks may appear. For |
437 | * each region you list here, your layout file must include a call to | |
438 | * <pre> | |
439 | * echo $OUTPUT->blocks_for_region($regionname); | |
440 | * </pre> | |
441 | * or equivalent so that the blocks are actually visible. | |
fdeb7fa1 | 442 | * |
d4a03c00 | 443 | * 'defaultregion' If the list of regions is non-empty, then you must pick |
444 | * one of the one of them as 'default'. This has two meanings. First, this is | |
445 | * where new blocks are added. Second, if there are any blocks associated with | |
fa1afe32 | 446 | * the page, but in non-existent regions, they appear here. (Imaging, for example, |
d4a03c00 | 447 | * that someone added blocks using a different theme that used different region |
448 | * names, and then switched to this theme.) | |
fdeb7fa1 | 449 | */ |
d4a03c00 | 450 | public $layouts = array(); |
571fa828 | 451 | |
fdeb7fa1 | 452 | /** |
76be40cc SH |
453 | * @var string Name of the renderer factory class to use. Must implement the |
454 | * {@link renderer_factory} interface. | |
fdeb7fa1 | 455 | * |
456 | * This is an advanced feature. Moodle output is generated by 'renderers', | |
457 | * you can customise the HTML that is output by writing custom renderers, | |
458 | * and then you need to specify 'renderer factory' so that Moodle can find | |
459 | * your renderers. | |
460 | * | |
461 | * There are some renderer factories supplied with Moodle. Please follow these | |
462 | * links to see what they do. | |
463 | * <ul> | |
464 | * <li>{@link standard_renderer_factory} - the default.</li> | |
465 | * <li>{@link theme_overridden_renderer_factory} - use this if you want to write | |
78946b9b | 466 | * your own custom renderers in a lib.php file in this theme (or the parent theme).</li> |
fdeb7fa1 | 467 | * </ul> |
fdeb7fa1 | 468 | */ |
ebebf55c | 469 | public $rendererfactory = 'standard_renderer_factory'; |
ebebf55c | 470 | |
fdeb7fa1 | 471 | /** |
76be40cc | 472 | * @var string Function to do custom CSS post-processing. |
fdeb7fa1 | 473 | * |
78946b9b PS |
474 | * This is an advanced feature. If you want to do custom post-processing on the |
475 | * CSS before it is output (for example, to replace certain variable names | |
476 | * with particular values) you can give the name of a function here. | |
fdeb7fa1 | 477 | */ |
78946b9b | 478 | public $csspostprocess = null; |
571fa828 | 479 | |
99a6118f FM |
480 | /** |
481 | * @var string Function to do custom CSS post-processing on a parsed CSS tree. | |
482 | * | |
483 | * This is an advanced feature. If you want to do custom post-processing on the | |
484 | * CSS before it is output, you can provide the name of the function here. The | |
485 | * function will receive a CSS tree document as first parameter, and the theme_config | |
486 | * object as second parameter. A return value is not required, the tree can | |
487 | * be edited in place. | |
488 | */ | |
493ef66e | 489 | public $csstreepostprocessor = null; |
99a6118f | 490 | |
571fa828 | 491 | /** |
76be40cc | 492 | * @var string Accessibility: Right arrow-like character is |
78946b9b PS |
493 | * used in the breadcrumb trail, course navigation menu |
494 | * (previous/next activity), calendar, and search forum block. | |
495 | * If the theme does not set characters, appropriate defaults | |
496 | * are set automatically. Please DO NOT | |
497 | * use < > » - these are confusing for blind users. | |
78946b9b PS |
498 | */ |
499 | public $rarrow = null; | |
500 | ||
501 | /** | |
06844353 | 502 | * @var string Accessibility: Left arrow-like character is |
78946b9b PS |
503 | * used in the breadcrumb trail, course navigation menu |
504 | * (previous/next activity), calendar, and search forum block. | |
505 | * If the theme does not set characters, appropriate defaults | |
506 | * are set automatically. Please DO NOT | |
507 | * use < > » - these are confusing for blind users. | |
571fa828 | 508 | */ |
78946b9b PS |
509 | public $larrow = null; |
510 | ||
7b2eff28 DB |
511 | /** |
512 | * @var string Accessibility: Up arrow-like character is used in | |
513 | * the book heirarchical navigation. | |
514 | * If the theme does not set characters, appropriate defaults | |
515 | * are set automatically. Please DO NOT | |
516 | * use ^ - this is confusing for blind users. | |
517 | */ | |
518 | public $uarrow = null; | |
519 | ||
7a05bc92 JP |
520 | /** |
521 | * @var string Accessibility: Down arrow-like character. | |
522 | * If the theme does not set characters, appropriate defaults | |
523 | * are set automatically. | |
524 | */ | |
525 | public $darrow = null; | |
526 | ||
03276af6 | 527 | /** |
76be40cc | 528 | * @var bool Some themes may want to disable ajax course editing. |
03276af6 DM |
529 | */ |
530 | public $enablecourseajax = true; | |
78946b9b | 531 | |
13725b37 PS |
532 | /** |
533 | * @var string Determines served document types | |
534 | * - 'html5' the only officially supported doctype in Moodle | |
535 | * - 'xhtml5' may be used in development for validation (not intended for production servers!) | |
536 | * - 'xhtml' XHTML 1.0 Strict for legacy themes only | |
537 | */ | |
538 | public $doctype = 'html5'; | |
539 | ||
0f73f3ab | 540 | /** |
9d1402ab | 541 | * @var string requiredblocks If set to a string, will list the block types that cannot be deleted. Defaults to |
0f73f3ab DW |
542 | * navigation and settings. |
543 | */ | |
9d1402ab | 544 | public $requiredblocks = false; |
0f73f3ab | 545 | |
78946b9b | 546 | //==Following properties are not configurable from theme config.php== |
571fa828 | 547 | |
fdeb7fa1 | 548 | /** |
76be40cc | 549 | * @var string The name of this theme. Set automatically when this theme is |
78946b9b | 550 | * loaded. This can not be set in theme config.php |
fdeb7fa1 | 551 | */ |
78946b9b | 552 | public $name; |
fdeb7fa1 | 553 | |
554 | /** | |
76be40cc | 555 | * @var string The folder where this themes files are stored. This is set |
78946b9b | 556 | * automatically. This can not be set in theme config.php |
fdeb7fa1 | 557 | */ |
78946b9b | 558 | public $dir; |
fdeb7fa1 | 559 | |
560 | /** | |
76be40cc | 561 | * @var stdClass Theme settings stored in config_plugins table. |
78946b9b | 562 | * This can not be set in theme config.php |
78946b9b | 563 | */ |
5dd40ec4 | 564 | public $settings = null; |
78946b9b | 565 | |
d2c394f3 | 566 | /** |
76be40cc | 567 | * @var bool If set to true and the theme enables the dock then blocks will be able |
d2c394f3 | 568 | * to be moved to the special dock |
d2c394f3 SH |
569 | */ |
570 | public $enable_dock = false; | |
571 | ||
4d56ee95 | 572 | /** |
76be40cc | 573 | * @var bool If set to true then this theme will not be shown in the theme selector unless |
4d56ee95 | 574 | * theme designer mode is turned on. |
4d56ee95 SH |
575 | */ |
576 | public $hidefromselector = false; | |
577 | ||
97dbc8f2 PS |
578 | /** |
579 | * @var array list of YUI CSS modules to be included on each page. This may be used | |
580 | * to remove cssreset and use cssnormalise module instead. | |
581 | */ | |
582 | public $yuicssmodules = array('cssreset', 'cssfonts', 'cssgrids', 'cssbase'); | |
583 | ||
36b77e3a SH |
584 | /** |
585 | * An associative array of block manipulations that should be made if the user is using an rtl language. | |
586 | * The key is the original block region, and the value is the block region to change to. | |
587 | * This is used when displaying blocks for regions only. | |
588 | * @var array | |
589 | */ | |
e2ad11d8 | 590 | public $blockrtlmanipulations = array(); |
36b77e3a | 591 | |
78946b9b | 592 | /** |
76be40cc | 593 | * @var renderer_factory Instance of the renderer_factory implementation |
fdeb7fa1 | 594 | * we are using. Implementation detail. |
595 | */ | |
596 | protected $rf = null; | |
597 | ||
598 | /** | |
76be40cc | 599 | * @var array List of parent config objects. |
78946b9b PS |
600 | **/ |
601 | protected $parent_configs = array(); | |
fdeb7fa1 | 602 | |
436dbeec SH |
603 | /** |
604 | * Used to determine whether we can serve SVG images or not. | |
605 | * @var bool | |
606 | */ | |
607 | private $usesvg = null; | |
608 | ||
1d987cae FM |
609 | /** |
610 | * Whether in RTL mode or not. | |
611 | * @var bool | |
612 | */ | |
613 | protected $rtlmode = false; | |
614 | ||
d433cf37 FM |
615 | /** |
616 | * The LESS file to compile. When set, the theme will attempt to compile the file itself. | |
617 | * @var bool | |
618 | */ | |
619 | public $lessfile = false; | |
620 | ||
65b8336e | 621 | /** |
41b973bc FM |
622 | * The SCSS file to compile (without .scss), located in the scss/ folder of the theme. |
623 | * Or a Closure, which receives the theme_config as argument and must | |
624 | * return the SCSS content. This setting takes precedence over self::$lessfile. | |
625 | * @var string|Closure | |
626 | */ | |
627 | public $scss = false; | |
628 | ||
629 | /** | |
630 | * Local cache of the SCSS property. | |
631 | * @var false|array | |
65b8336e | 632 | */ |
41b973bc | 633 | protected $scsscache = null; |
65b8336e | 634 | |
d433cf37 FM |
635 | /** |
636 | * The name of the function to call to get the LESS code to inject. | |
637 | * @var string | |
638 | */ | |
639 | public $extralesscallback = null; | |
640 | ||
65b8336e FM |
641 | /** |
642 | * The name of the function to call to get the SCSS code to inject. | |
643 | * @var string | |
644 | */ | |
645 | public $extrascsscallback = null; | |
646 | ||
d433cf37 FM |
647 | /** |
648 | * The name of the function to call to get extra LESS variables. | |
649 | * @var string | |
650 | */ | |
651 | public $lessvariablescallback = null; | |
652 | ||
65b8336e | 653 | /** |
258143e3 | 654 | * The name of the function to call to get SCSS to prepend. |
65b8336e FM |
655 | * @var string |
656 | */ | |
258143e3 | 657 | public $prescsscallback = null; |
65b8336e | 658 | |
225c418f SH |
659 | /** |
660 | * Sets the render method that should be used for rendering custom block regions by scripts such as my/index.php | |
661 | * Defaults to {@link core_renderer::blocks_for_region()} | |
662 | * @var string | |
663 | */ | |
664 | public $blockrendermethod = null; | |
665 | ||
a26ce248 DW |
666 | /** |
667 | * Remember the results of icon remapping for the current page. | |
668 | * @var array | |
669 | */ | |
670 | public $remapiconcache = []; | |
671 | ||
af9edb2e BB |
672 | /** |
673 | * The name of the function to call to get precompiled CSS. | |
674 | * @var string | |
675 | */ | |
676 | public $precompiledcsscallback = null; | |
677 | ||
571fa828 | 678 | /** |
ebebf55c | 679 | * Load the config.php file for a particular theme, and return an instance |
680 | * of this class. (That is, this is a factory method.) | |
681 | * | |
682 | * @param string $themename the name of the theme. | |
683 | * @return theme_config an instance of this class. | |
571fa828 | 684 | */ |
ebebf55c | 685 | public static function load($themename) { |
686 | global $CFG; | |
571fa828 | 687 | |
78946b9b PS |
688 | // load theme settings from db |
689 | try { | |
690 | $settings = get_config('theme_'.$themename); | |
691 | } catch (dml_exception $e) { | |
da0c0e25 | 692 | // most probably moodle tables not created yet |
365a5941 | 693 | $settings = new stdClass(); |
78946b9b | 694 | } |
ebebf55c | 695 | |
78946b9b PS |
696 | if ($config = theme_config::find_theme_config($themename, $settings)) { |
697 | return new theme_config($config); | |
5f0baa43 PS |
698 | |
699 | } else if ($themename == theme_config::DEFAULT_THEME) { | |
700 | throw new coding_exception('Default theme '.theme_config::DEFAULT_THEME.' not available or broken!'); | |
701 | ||
2211f504 | 702 | } else if ($config = theme_config::find_theme_config($CFG->theme, $settings)) { |
ea049ee6 TH |
703 | debugging('This page should be using theme ' . $themename . |
704 | ' which cannot be initialised. Falling back to the site theme ' . $CFG->theme, DEBUG_NORMAL); | |
2211f504 MA |
705 | return new theme_config($config); |
706 | ||
78946b9b PS |
707 | } else { |
708 | // bad luck, the requested theme has some problems - admin see details in theme config | |
ea049ee6 TH |
709 | debugging('This page should be using theme ' . $themename . |
710 | ' which cannot be initialised. Nor can the site theme ' . $CFG->theme . | |
711 | '. Falling back to ' . theme_config::DEFAULT_THEME, DEBUG_NORMAL); | |
5f0baa43 | 712 | return new theme_config(theme_config::find_theme_config(theme_config::DEFAULT_THEME, $settings)); |
78946b9b PS |
713 | } |
714 | } | |
ebebf55c | 715 | |
78946b9b PS |
716 | /** |
717 | * Theme diagnostic code. It is very problematic to send debug output | |
718 | * to the actual CSS file, instead this functions is supposed to | |
ae5b6801 | 719 | * diagnose given theme and highlights all potential problems. |
78946b9b PS |
720 | * This information should be available from the theme selection page |
721 | * or some other debug page for theme designers. | |
722 | * | |
723 | * @param string $themename | |
724 | * @return array description of problems | |
725 | */ | |
726 | public static function diagnose($themename) { | |
5ef719e7 | 727 | //TODO: MDL-21108 |
78946b9b PS |
728 | return array(); |
729 | } | |
730 | ||
731 | /** | |
732 | * Private constructor, can be called only from the factory method. | |
733 | * @param stdClass $config | |
734 | */ | |
735 | private function __construct($config) { | |
736 | global $CFG; //needed for included lib.php files | |
737 | ||
738 | $this->settings = $config->settings; | |
739 | $this->name = $config->name; | |
740 | $this->dir = $config->dir; | |
741 | ||
4028ffd8 MM |
742 | if ($this->name != self::DEFAULT_THEME) { |
743 | $baseconfig = self::find_theme_config(self::DEFAULT_THEME, $this->settings); | |
744 | } else { | |
745 | $baseconfig = $config; | |
746 | } | |
ebebf55c | 747 | |
7b2eff28 | 748 | $configurable = array( |
0e9911e1 | 749 | 'parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'usefallback', |
7b2eff28 | 750 | 'javascripts', 'javascripts_footer', 'parents_exclude_javascripts', |
9757c656 | 751 | 'layouts', 'enablecourseajax', 'requiredblocks', |
341070d5 | 752 | 'rendererfactory', 'csspostprocess', 'editor_sheets', 'editor_scss', 'rarrow', 'larrow', 'uarrow', 'darrow', |
7b2eff28 | 753 | 'hidefromselector', 'doctype', 'yuicssmodules', 'blockrtlmanipulations', |
65b8336e | 754 | 'lessfile', 'extralesscallback', 'lessvariablescallback', 'blockrendermethod', |
af9edb2e BB |
755 | 'scss', 'extrascsscallback', 'prescsscallback', 'csstreepostprocessor', 'addblockposition', |
756 | 'iconsystem', 'precompiledcsscallback'); | |
ebebf55c | 757 | |
78946b9b PS |
758 | foreach ($config as $key=>$value) { |
759 | if (in_array($key, $configurable)) { | |
760 | $this->$key = $value; | |
761 | } | |
762 | } | |
763 | ||
764 | // verify all parents and load configs and renderers | |
765 | foreach ($this->parents as $parent) { | |
644fcbb6 | 766 | if (!$parent_config = theme_config::find_theme_config($parent, $this->settings)) { |
78946b9b PS |
767 | // this is not good - better exclude faulty parents |
768 | continue; | |
769 | } | |
770 | $libfile = $parent_config->dir.'/lib.php'; | |
771 | if (is_readable($libfile)) { | |
772 | // theme may store various function here | |
773 | include_once($libfile); | |
774 | } | |
775 | $renderersfile = $parent_config->dir.'/renderers.php'; | |
776 | if (is_readable($renderersfile)) { | |
777 | // may contain core and plugin renderers and renderer factory | |
778 | include_once($renderersfile); | |
779 | } | |
780 | $this->parent_configs[$parent] = $parent_config; | |
78946b9b PS |
781 | } |
782 | $libfile = $this->dir.'/lib.php'; | |
783 | if (is_readable($libfile)) { | |
784 | // theme may store various function here | |
785 | include_once($libfile); | |
786 | } | |
787 | $rendererfile = $this->dir.'/renderers.php'; | |
788 | if (is_readable($rendererfile)) { | |
789 | // may contain core and plugin renderers and renderer factory | |
790 | include_once($rendererfile); | |
6fc267a0 BJ |
791 | } else { |
792 | // check if renderers.php file is missnamed renderer.php | |
793 | if (is_readable($this->dir.'/renderer.php')) { | |
794 | debugging('Developer hint: '.$this->dir.'/renderer.php should be renamed to ' . $this->dir."/renderers.php. | |
11c0ecce | 795 | See: http://docs.moodle.org/dev/Output_renderers#Theme_renderers.", DEBUG_DEVELOPER); |
6fc267a0 | 796 | } |
78946b9b | 797 | } |
4399b9d5 | 798 | |
78946b9b PS |
799 | // cascade all layouts properly |
800 | foreach ($baseconfig->layouts as $layout=>$value) { | |
801 | if (!isset($this->layouts[$layout])) { | |
802 | foreach ($this->parent_configs as $parent_config) { | |
803 | if (isset($parent_config->layouts[$layout])) { | |
804 | $this->layouts[$layout] = $parent_config->layouts[$layout]; | |
805 | continue 2; | |
806 | } | |
807 | } | |
808 | $this->layouts[$layout] = $value; | |
809 | } | |
810 | } | |
811 | ||
812 | //fix arrows if needed | |
813 | $this->check_theme_arrows(); | |
571fa828 | 814 | } |
815 | ||
63c88397 PS |
816 | /** |
817 | * Let the theme initialise the page object (usually $PAGE). | |
818 | * | |
819 | * This may be used for example to request jQuery in add-ons. | |
820 | * | |
821 | * @param moodle_page $page | |
822 | */ | |
823 | public function init_page(moodle_page $page) { | |
824 | $themeinitfunction = 'theme_'.$this->name.'_page_init'; | |
825 | if (function_exists($themeinitfunction)) { | |
826 | $themeinitfunction($page); | |
827 | } | |
828 | } | |
829 | ||
f8129210 | 830 | /** |
7a05bc92 | 831 | * Checks if arrows $THEME->rarrow, $THEME->larrow, $THEME->uarrow, $THEME->darrow have been set (theme/-/config.php). |
78946b9b PS |
832 | * If not it applies sensible defaults. |
833 | * | |
834 | * Accessibility: right and left arrow Unicode characters for breadcrumb, calendar, | |
835 | * search forum block, etc. Important: these are 'silent' in a screen-reader | |
836 | * (unlike > »), and must be accompanied by text. | |
34a2777c | 837 | */ |
78946b9b PS |
838 | private function check_theme_arrows() { |
839 | if (!isset($this->rarrow) and !isset($this->larrow)) { | |
840 | // Default, looks good in Win XP/IE 6, Win/Firefox 1.5, Win/Netscape 8... | |
841 | // Also OK in Win 9x/2K/IE 5.x | |
842 | $this->rarrow = '►'; | |
843 | $this->larrow = '◄'; | |
7b2eff28 | 844 | $this->uarrow = '▲'; |
7a05bc92 | 845 | $this->darrow = '▼'; |
78946b9b PS |
846 | if (empty($_SERVER['HTTP_USER_AGENT'])) { |
847 | $uagent = ''; | |
ebebf55c | 848 | } else { |
78946b9b PS |
849 | $uagent = $_SERVER['HTTP_USER_AGENT']; |
850 | } | |
851 | if (false !== strpos($uagent, 'Opera') | |
852 | || false !== strpos($uagent, 'Mac')) { | |
853 | // Looks good in Win XP/Mac/Opera 8/9, Mac/Firefox 2, Camino, Safari. | |
854 | // Not broken in Mac/IE 5, Mac/Netscape 7 (?). | |
64489778 DP |
855 | $this->rarrow = '▶︎'; |
856 | $this->larrow = '◀︎'; | |
78946b9b | 857 | } |
f04721e2 | 858 | elseif ((false !== strpos($uagent, 'Konqueror')) |
0179d6be JF |
859 | || (false !== strpos($uagent, 'Android'))) { |
860 | // The fonts on Android don't include the characters required for this to work as expected. | |
861 | // So we use the same ones Konqueror uses. | |
78946b9b PS |
862 | $this->rarrow = '→'; |
863 | $this->larrow = '←'; | |
7b2eff28 | 864 | $this->uarrow = '↑'; |
7a05bc92 | 865 | $this->darrow = '↓'; |
78946b9b PS |
866 | } |
867 | elseif (isset($_SERVER['HTTP_ACCEPT_CHARSET']) | |
868 | && false === stripos($_SERVER['HTTP_ACCEPT_CHARSET'], 'utf-8')) { | |
869 | // (Win/IE 5 doesn't set ACCEPT_CHARSET, but handles Unicode.) | |
870 | // To be safe, non-Unicode browsers! | |
871 | $this->rarrow = '>'; | |
872 | $this->larrow = '<'; | |
7b2eff28 | 873 | $this->uarrow = '^'; |
7a05bc92 | 874 | $this->darrow = 'v'; |
78946b9b PS |
875 | } |
876 | ||
f8129210 | 877 | // RTL support - in RTL languages, swap r and l arrows |
78946b9b PS |
878 | if (right_to_left()) { |
879 | $t = $this->rarrow; | |
880 | $this->rarrow = $this->larrow; | |
881 | $this->larrow = $t; | |
ebebf55c | 882 | } |
ebebf55c | 883 | } |
78946b9b | 884 | } |
ebebf55c | 885 | |
78946b9b PS |
886 | /** |
887 | * Returns output renderer prefixes, these are used when looking | |
71c03ac1 | 888 | * for the overridden renderers in themes. |
473dd811 | 889 | * |
78946b9b PS |
890 | * @return array |
891 | */ | |
892 | public function renderer_prefixes() { | |
893 | global $CFG; // just in case the included files need it | |
894 | ||
596af93a | 895 | $prefixes = array('theme_'.$this->name); |
78946b9b PS |
896 | |
897 | foreach ($this->parent_configs as $parent) { | |
898 | $prefixes[] = 'theme_'.$parent->name; | |
899 | } | |
900 | ||
901 | return $prefixes; | |
34a2777c | 902 | } |
903 | ||
571fa828 | 904 | /** |
78946b9b | 905 | * Returns the stylesheet URL of this editor content |
473dd811 | 906 | * |
78946b9b | 907 | * @param bool $encoded false means use & and true use & in URLs |
1eb2b517 | 908 | * @return moodle_url |
571fa828 | 909 | */ |
78946b9b PS |
910 | public function editor_css_url($encoded=true) { |
911 | global $CFG; | |
78946b9b | 912 | $rev = theme_get_revision(); |
78946b9b | 913 | if ($rev > -1) { |
bb8bb8e4 RW |
914 | $themesubrevision = theme_get_sub_revision_for_theme($this->name); |
915 | ||
916 | // Provide the sub revision to allow us to invalidate cached theme CSS | |
917 | // on a per theme basis, rather than globally. | |
918 | if ($themesubrevision && $themesubrevision > 0) { | |
919 | $rev .= "_{$themesubrevision}"; | |
920 | } | |
921 | ||
7eb50b32 | 922 | $url = new moodle_url("/theme/styles.php"); |
7070b7f2 | 923 | if (!empty($CFG->slasharguments)) { |
7070b7f2 | 924 | $url->set_slashargument('/'.$this->name.'/'.$rev.'/editor', 'noparam', true); |
7070b7f2 | 925 | } else { |
1eb2b517 | 926 | $url->params(array('theme'=>$this->name,'rev'=>$rev, 'type'=>'editor')); |
7070b7f2 | 927 | } |
78946b9b PS |
928 | } else { |
929 | $params = array('theme'=>$this->name, 'type'=>'editor'); | |
7eb50b32 | 930 | $url = new moodle_url('/theme/styles_debug.php', $params); |
571fa828 | 931 | } |
1eb2b517 | 932 | return $url; |
571fa828 | 933 | } |
934 | ||
935 | /** | |
78946b9b | 936 | * Returns the content of the CSS to be used in editor content |
473dd811 | 937 | * |
98245a84 | 938 | * @return array |
571fa828 | 939 | */ |
71be124d | 940 | public function editor_css_files() { |
71be124d | 941 | $files = array(); |
78946b9b | 942 | |
98245a84 | 943 | // First editor plugins. |
bd3b3bba | 944 | $plugins = core_component::get_plugin_list('editor'); |
78946b9b PS |
945 | foreach ($plugins as $plugin=>$fulldir) { |
946 | $sheetfile = "$fulldir/editor_styles.css"; | |
947 | if (is_readable($sheetfile)) { | |
71be124d | 948 | $files['plugin_'.$plugin] = $sheetfile; |
78946b9b PS |
949 | } |
950 | } | |
98245a84 PS |
951 | // Then parent themes - base first, the immediate parent last. |
952 | foreach (array_reverse($this->parent_configs) as $parent_config) { | |
78946b9b PS |
953 | if (empty($parent_config->editor_sheets)) { |
954 | continue; | |
955 | } | |
956 | foreach ($parent_config->editor_sheets as $sheet) { | |
ca194849 | 957 | $sheetfile = "$parent_config->dir/style/$sheet.css"; |
78946b9b | 958 | if (is_readable($sheetfile)) { |
71be124d | 959 | $files['parent_'.$parent_config->name.'_'.$sheet] = $sheetfile; |
78946b9b PS |
960 | } |
961 | } | |
962 | } | |
98245a84 | 963 | // Finally this theme. |
78946b9b PS |
964 | if (!empty($this->editor_sheets)) { |
965 | foreach ($this->editor_sheets as $sheet) { | |
71be124d | 966 | $sheetfile = "$this->dir/style/$sheet.css"; |
78946b9b | 967 | if (is_readable($sheetfile)) { |
71be124d | 968 | $files['theme_'.$sheet] = $sheetfile; |
78946b9b PS |
969 | } |
970 | } | |
971 | } | |
972 | ||
71be124d | 973 | return $files; |
571fa828 | 974 | } |
975 | ||
341070d5 MH |
976 | /** |
977 | * Compiles and returns the content of the SCSS to be used in editor content | |
978 | * | |
979 | * @return string Compiled CSS from the editor SCSS | |
980 | */ | |
981 | public function editor_scss_to_css() { | |
982 | $css = ''; | |
983 | ||
984 | if (!empty($this->editor_scss)) { | |
985 | $compiler = new core_scss(); | |
986 | ||
987 | foreach ($this->editor_scss as $filename) { | |
988 | $compiler->set_file("{$this->dir}/scss/{$filename}.scss"); | |
989 | ||
990 | try { | |
991 | $css .= $compiler->to_css(); | |
992 | } catch (\Exception $e) { | |
993 | debugging('Error while compiling editor SCSS: ' . $e->getMessage(), DEBUG_DEVELOPER); | |
994 | } | |
995 | } | |
996 | } | |
997 | ||
998 | return $css; | |
999 | } | |
1000 | ||
571fa828 | 1001 | /** |
98245a84 | 1002 | * Get the stylesheet URL of this theme. |
473dd811 SH |
1003 | * |
1004 | * @param moodle_page $page Not used... deprecated? | |
98245a84 | 1005 | * @return moodle_url[] |
571fa828 | 1006 | */ |
efaa4c08 | 1007 | public function css_urls(moodle_page $page) { |
78946b9b PS |
1008 | global $CFG; |
1009 | ||
1010 | $rev = theme_get_revision(); | |
1011 | ||
efaa4c08 | 1012 | $urls = array(); |
efaa4c08 | 1013 | |
383b89a1 | 1014 | $svg = $this->use_svg_icons(); |
06a9e103 | 1015 | $separate = (core_useragent::is_ie() && !core_useragent::check_ie_version('10')); |
383b89a1 | 1016 | |
78946b9b | 1017 | if ($rev > -1) { |
1d987cae | 1018 | $filename = right_to_left() ? 'all-rtl' : 'all'; |
7eb50b32 | 1019 | $url = new moodle_url("/theme/styles.php"); |
5dd40ec4 RW |
1020 | $themesubrevision = theme_get_sub_revision_for_theme($this->name); |
1021 | ||
1022 | // Provide the sub revision to allow us to invalidate cached theme CSS | |
1023 | // on a per theme basis, rather than globally. | |
1024 | if ($themesubrevision && $themesubrevision > 0) { | |
1025 | $rev .= "_{$themesubrevision}"; | |
1026 | } | |
1027 | ||
1eb2b517 SH |
1028 | if (!empty($CFG->slasharguments)) { |
1029 | $slashargs = ''; | |
1030 | if (!$svg) { | |
1031 | // We add a simple /_s to the start of the path. | |
1032 | // The underscore is used to ensure that it isn't a valid theme name. | |
1033 | $slashargs .= '/_s'.$slashargs; | |
1034 | } | |
1d987cae | 1035 | $slashargs .= '/'.$this->name.'/'.$rev.'/'.$filename; |
1eb2b517 SH |
1036 | if ($separate) { |
1037 | $slashargs .= '/chunk0'; | |
1038 | } | |
1039 | $url->set_slashargument($slashargs, 'noparam', true); | |
537740cb | 1040 | } else { |
e584e6ae | 1041 | $params = array('theme' => $this->name, 'rev' => $rev, 'type' => $filename); |
1eb2b517 SH |
1042 | if (!$svg) { |
1043 | // We add an SVG param so that we know not to serve SVG images. | |
1044 | // We do this because all modern browsers support SVG and this param will one day be removed. | |
1045 | $params['svg'] = '0'; | |
7070b7f2 | 1046 | } |
1eb2b517 SH |
1047 | if ($separate) { |
1048 | $params['chunk'] = '0'; | |
1049 | } | |
1050 | $url->params($params); | |
78946b9b | 1051 | } |
1eb2b517 SH |
1052 | $urls[] = $url; |
1053 | ||
78946b9b | 1054 | } else { |
7eb50b32 | 1055 | $baseurl = new moodle_url('/theme/styles_debug.php'); |
c09e1a2c | 1056 | |
08823e5f | 1057 | $css = $this->get_css_files(true); |
383b89a1 SH |
1058 | if (!$svg) { |
1059 | // We add an SVG param so that we know not to serve SVG images. | |
1060 | // We do this because all modern browsers support SVG and this param will one day be removed. | |
1061 | $baseurl->param('svg', '0'); | |
1062 | } | |
1d987cae FM |
1063 | if (right_to_left()) { |
1064 | $baseurl->param('rtl', 1); | |
1065 | } | |
06a9e103 FM |
1066 | if ($separate) { |
1067 | // We might need to chunk long files. | |
1068 | $baseurl->param('chunk', '0'); | |
1069 | } | |
378b3eac | 1070 | if (core_useragent::is_ie()) { |
98245a84 | 1071 | // Lalala, IE does not allow more than 31 linked CSS files from main document. |
71be124d | 1072 | $urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'plugins')); |
a6338a13 | 1073 | foreach ($css['parents'] as $parent=>$sheets) { |
98245a84 | 1074 | // We need to serve parents individually otherwise we may easily exceed the style limit IE imposes (4096). |
71be124d | 1075 | $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'ie', 'subtype'=>'parents', 'sheet'=>$parent)); |
a6338a13 | 1076 | } |
41b973bc | 1077 | if ($this->get_scss_property()) { |
65b8336e FM |
1078 | // No need to define the type as IE here. |
1079 | $urls[] = new moodle_url($baseurl, array('theme' => $this->name, 'type' => 'scss')); | |
1080 | } else if (!empty($this->lessfile)) { | |
08823e5f | 1081 | // No need to define the type as IE here. |
06a9e103 | 1082 | $urls[] = new moodle_url($baseurl, array('theme' => $this->name, 'type' => 'less')); |
29d73d27 | 1083 | } |
71be124d | 1084 | $urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'theme')); |
c09e1a2c PS |
1085 | |
1086 | } else { | |
1087 | foreach ($css['plugins'] as $plugin=>$unused) { | |
1088 | $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'plugin', 'subtype'=>$plugin)); | |
1089 | } | |
1090 | foreach ($css['parents'] as $parent=>$sheets) { | |
1091 | foreach ($sheets as $sheet=>$unused2) { | |
1092 | $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'parent', 'subtype'=>$parent, 'sheet'=>$sheet)); | |
1093 | } | |
1094 | } | |
08823e5f | 1095 | foreach ($css['theme'] as $sheet => $filename) { |
41b973bc | 1096 | if ($sheet === self::SCSS_KEY) { |
65b8336e FM |
1097 | // This is the theme SCSS file. |
1098 | $urls[] = new moodle_url($baseurl, array('theme' => $this->name, 'type' => 'scss')); | |
1099 | } else if ($sheet === $this->lessfile) { | |
08823e5f FM |
1100 | // This is the theme LESS file. |
1101 | $urls[] = new moodle_url($baseurl, array('theme' => $this->name, 'type' => 'less')); | |
1102 | } else { | |
d433cf37 FM |
1103 | // Sheet first in order to make long urls easier to read. |
1104 | $urls[] = new moodle_url($baseurl, array('sheet'=>$sheet, 'theme'=>$this->name, 'type'=>'theme')); | |
c09e1a2c | 1105 | } |
78946b9b | 1106 | } |
78946b9b | 1107 | } |
78946b9b | 1108 | } |
efaa4c08 | 1109 | |
66bd0f6c BB |
1110 | // Allow themes to change the css url to something like theme/mytheme/mycss.php. |
1111 | component_callback('theme_' . $this->name, 'alter_css_urls', [&$urls]); | |
efaa4c08 | 1112 | return $urls; |
571fa828 | 1113 | } |
34a2777c | 1114 | |
ebebf55c | 1115 | /** |
98245a84 | 1116 | * Get the whole css stylesheet for production mode. |
473dd811 | 1117 | * |
31f28b6a PÅ |
1118 | * NOTE: this method is not expected to be used from any addons. |
1119 | * | |
d52ebf11 | 1120 | * @return string CSS markup compressed |
98245a84 PS |
1121 | */ |
1122 | public function get_css_content() { | |
98245a84 PS |
1123 | |
1124 | $csscontent = ''; | |
08823e5f FM |
1125 | foreach ($this->get_css_files(false) as $type => $value) { |
1126 | foreach ($value as $identifier => $val) { | |
98245a84 PS |
1127 | if (is_array($val)) { |
1128 | foreach ($val as $v) { | |
1129 | $csscontent .= file_get_contents($v) . "\n"; | |
1130 | } | |
1131 | } else { | |
41b973bc | 1132 | if ($type === 'theme' && $identifier === self::SCSS_KEY) { |
65b8336e | 1133 | // We need the content from SCSS because this is the SCSS file from the theme. |
af9edb2e BB |
1134 | if ($compiled = $this->get_css_content_from_scss(false)) { |
1135 | $csscontent .= $compiled; | |
1136 | } else { | |
1137 | // The compiler failed so default back to any precompiled css that might | |
1138 | // exist. | |
1139 | $csscontent .= $this->get_precompiled_css_content(); | |
1140 | } | |
65b8336e | 1141 | } else if ($type === 'theme' && $identifier === $this->lessfile) { |
08823e5f FM |
1142 | // We need the content from LESS because this is the LESS file from the theme. |
1143 | $csscontent .= $this->get_css_content_from_less(false); | |
d433cf37 FM |
1144 | } else { |
1145 | $csscontent .= file_get_contents($val) . "\n"; | |
98245a84 | 1146 | } |
98245a84 PS |
1147 | } |
1148 | } | |
1149 | } | |
1150 | $csscontent = $this->post_process($csscontent); | |
d52ebf11 | 1151 | $csscontent = core_minify::css($csscontent); |
98245a84 PS |
1152 | |
1153 | return $csscontent; | |
1154 | } | |
4cc2f33b SL |
1155 | /** |
1156 | * Set post processed CSS content cache. | |
1157 | * | |
1158 | * @param string $csscontent The post processed CSS content. | |
1159 | * @return bool True if the content was successfully cached. | |
1160 | */ | |
1161 | public function set_css_content_cache($csscontent) { | |
1162 | ||
1163 | $cache = cache::make('core', 'postprocessedcss'); | |
1164 | $key = $this->get_css_cache_key(); | |
1165 | ||
1166 | return $cache->set($key, $csscontent); | |
1167 | } | |
1168 | ||
0e9911e1 AN |
1169 | /** |
1170 | * Return whether the post processed CSS content has been cached. | |
1171 | * | |
1172 | * @return bool Whether the post-processed CSS is available in the cache. | |
1173 | */ | |
1174 | public function has_css_cached_content() { | |
1175 | ||
1176 | $key = $this->get_css_cache_key(); | |
1177 | $cache = cache::make('core', 'postprocessedcss'); | |
1178 | ||
1179 | return $cache->has($key); | |
1180 | } | |
1181 | ||
4cc2f33b SL |
1182 | /** |
1183 | * Return cached post processed CSS content. | |
1184 | * | |
1185 | * @return bool|string The cached css content or false if not found. | |
1186 | */ | |
1187 | public function get_css_cached_content() { | |
1188 | ||
1189 | $key = $this->get_css_cache_key(); | |
1190 | $cache = cache::make('core', 'postprocessedcss'); | |
1191 | ||
1192 | return $cache->get($key); | |
1193 | } | |
1194 | ||
1195 | /** | |
1196 | * Generate the css content cache key. | |
1197 | * | |
1198 | * @return string The post processed css cache key. | |
1199 | */ | |
1200 | public function get_css_cache_key() { | |
1201 | $nosvg = (!$this->use_svg_icons()) ? 'nosvg_' : ''; | |
1202 | $rtlmode = ($this->rtlmode == true) ? 'rtl' : 'ltr'; | |
1203 | ||
1204 | return $nosvg . $this->name . '_' . $rtlmode; | |
1205 | } | |
98245a84 PS |
1206 | |
1207 | /** | |
1208 | * Get the theme designer css markup, | |
1209 | * the parameters are coming from css_urls(). | |
1210 | * | |
31f28b6a PÅ |
1211 | * NOTE: this method is not expected to be used from any addons. |
1212 | * | |
98245a84 PS |
1213 | * @param string $type |
1214 | * @param string $subtype | |
1215 | * @param string $sheet | |
1216 | * @return string CSS markup | |
ebebf55c | 1217 | */ |
98245a84 | 1218 | public function get_css_content_debug($type, $subtype, $sheet) { |
98245a84 | 1219 | |
65b8336e FM |
1220 | if ($type === 'scss') { |
1221 | // The SCSS file of the theme is requested. | |
1222 | $csscontent = $this->get_css_content_from_scss(true); | |
1223 | if ($csscontent !== false) { | |
1d987cae | 1224 | return $this->post_process($csscontent); |
65b8336e FM |
1225 | } |
1226 | return ''; | |
1227 | } else if ($type === 'less') { | |
1228 | // The LESS file of the theme is requested. | |
08823e5f FM |
1229 | $csscontent = $this->get_css_content_from_less(true); |
1230 | if ($csscontent !== false) { | |
1d987cae | 1231 | return $this->post_process($csscontent); |
08823e5f FM |
1232 | } |
1233 | return ''; | |
1234 | } | |
1235 | ||
98245a84 PS |
1236 | $cssfiles = array(); |
1237 | $css = $this->get_css_files(true); | |
1238 | ||
1239 | if ($type === 'ie') { | |
1240 | // IE is a sloppy browser with weird limits, sorry. | |
1241 | if ($subtype === 'plugins') { | |
1242 | $cssfiles = $css['plugins']; | |
1243 | ||
1244 | } else if ($subtype === 'parents') { | |
1245 | if (empty($sheet)) { | |
1246 | // Do not bother with the empty parent here. | |
1247 | } else { | |
1248 | // Build up the CSS for that parent so we can serve it as one file. | |
1249 | foreach ($css[$subtype][$sheet] as $parent => $css) { | |
1250 | $cssfiles[] = $css; | |
1251 | } | |
1252 | } | |
1253 | } else if ($subtype === 'theme') { | |
1254 | $cssfiles = $css['theme']; | |
da283760 | 1255 | foreach ($cssfiles as $key => $value) { |
41b973bc | 1256 | if (in_array($key, [$this->lessfile, self::SCSS_KEY])) { |
65b8336e FM |
1257 | // Remove the LESS/SCSS file from the theme CSS files. |
1258 | // The LESS/SCSS files use the type 'less' or 'scss', not 'ie'. | |
da283760 FM |
1259 | unset($cssfiles[$key]); |
1260 | } | |
1261 | } | |
98245a84 PS |
1262 | } |
1263 | ||
1264 | } else if ($type === 'plugin') { | |
1265 | if (isset($css['plugins'][$subtype])) { | |
1266 | $cssfiles[] = $css['plugins'][$subtype]; | |
1267 | } | |
1268 | ||
1269 | } else if ($type === 'parent') { | |
1270 | if (isset($css['parents'][$subtype][$sheet])) { | |
1271 | $cssfiles[] = $css['parents'][$subtype][$sheet]; | |
1272 | } | |
1273 | ||
1274 | } else if ($type === 'theme') { | |
1275 | if (isset($css['theme'][$sheet])) { | |
1276 | $cssfiles[] = $css['theme'][$sheet]; | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | $csscontent = ''; | |
1281 | foreach ($cssfiles as $file) { | |
1282 | $contents = file_get_contents($file); | |
1283 | $contents = $this->post_process($contents); | |
1284 | $comment = "/** Path: $type $subtype $sheet.' **/\n"; | |
1285 | $stats = ''; | |
98245a84 PS |
1286 | $csscontent .= $comment.$stats.$contents."\n\n"; |
1287 | } | |
1288 | ||
1289 | return $csscontent; | |
1290 | } | |
1291 | ||
1292 | /** | |
1293 | * Get the whole css stylesheet for editor iframe. | |
31f28b6a PÅ |
1294 | * |
1295 | * NOTE: this method is not expected to be used from any addons. | |
1296 | * | |
98245a84 PS |
1297 | * @return string CSS markup |
1298 | */ | |
1299 | public function get_css_content_editor() { | |
98245a84 | 1300 | $css = ''; |
341070d5 MH |
1301 | $cssfiles = $this->editor_css_files(); |
1302 | ||
1303 | // If editor has static CSS, include it. | |
98245a84 PS |
1304 | foreach ($cssfiles as $file) { |
1305 | $css .= file_get_contents($file)."\n"; | |
1306 | } | |
341070d5 MH |
1307 | |
1308 | // If editor has SCSS, compile and include it. | |
1309 | if (($convertedscss = $this->editor_scss_to_css())) { | |
1310 | $css .= $convertedscss; | |
1311 | } | |
1312 | ||
1313 | $output = $this->post_process($css); | |
1314 | ||
1315 | return $output; | |
98245a84 PS |
1316 | } |
1317 | ||
1318 | /** | |
1319 | * Returns an array of organised CSS files required for this output. | |
1320 | * | |
1321 | * @param bool $themedesigner | |
1322 | * @return array nested array of file paths | |
1323 | */ | |
1324 | protected function get_css_files($themedesigner) { | |
1325 | global $CFG; | |
1326 | ||
1327 | $cache = null; | |
08823e5f | 1328 | $cachekey = 'cssfiles'; |
98245a84 PS |
1329 | if ($themedesigner) { |
1330 | require_once($CFG->dirroot.'/lib/csslib.php'); | |
1331 | // We need some kind of caching here because otherwise the page navigation becomes | |
1332 | // way too slow in theme designer mode. Feel free to create full cache definition later... | |
1333 | $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'core', 'themedesigner', array('theme' => $this->name)); | |
d433cf37 | 1334 | if ($files = $cache->get($cachekey)) { |
98245a84 PS |
1335 | if ($files['created'] > time() - THEME_DESIGNER_CACHE_LIFETIME) { |
1336 | unset($files['created']); | |
1337 | return $files; | |
1338 | } | |
1339 | } | |
1340 | } | |
1341 | ||
045f492c | 1342 | $cssfiles = array('plugins'=>array(), 'parents'=>array(), 'theme'=>array()); |
78946b9b | 1343 | |
98245a84 | 1344 | // Get all plugin sheets. |
045f492c | 1345 | $excludes = $this->resolve_excludes('plugins_exclude_sheets'); |
78946b9b | 1346 | if ($excludes !== true) { |
46f6f7f2 | 1347 | foreach (core_component::get_plugin_types() as $type=>$unused) { |
045f492c | 1348 | if ($type === 'theme' || (!empty($excludes[$type]) and $excludes[$type] === true)) { |
78946b9b PS |
1349 | continue; |
1350 | } | |
bd3b3bba | 1351 | $plugins = core_component::get_plugin_list($type); |
78946b9b PS |
1352 | foreach ($plugins as $plugin=>$fulldir) { |
1353 | if (!empty($excludes[$type]) and is_array($excludes[$type]) | |
22c5c6e6 | 1354 | and in_array($plugin, $excludes[$type])) { |
78946b9b PS |
1355 | continue; |
1356 | } | |
f8bb9666 | 1357 | |
08823e5f | 1358 | // Get the CSS from the plugin. |
78946b9b PS |
1359 | $sheetfile = "$fulldir/styles.css"; |
1360 | if (is_readable($sheetfile)) { | |
045f492c | 1361 | $cssfiles['plugins'][$type.'_'.$plugin] = $sheetfile; |
f8bb9666 | 1362 | } |
22c5c6e6 FM |
1363 | |
1364 | // Create a list of candidate sheets from parents (direct parent last) and current theme. | |
1365 | $candidates = array(); | |
1366 | foreach (array_reverse($this->parent_configs) as $parent_config) { | |
1367 | $candidates[] = $parent_config->name; | |
f8bb9666 | 1368 | } |
22c5c6e6 FM |
1369 | $candidates[] = $this->name; |
1370 | ||
1371 | // Add the sheets found. | |
1372 | foreach ($candidates as $candidate) { | |
1373 | $sheetthemefile = "$fulldir/styles_{$candidate}.css"; | |
1374 | if (is_readable($sheetthemefile)) { | |
1375 | $cssfiles['plugins'][$type.'_'.$plugin.'_'.$candidate] = $sheetthemefile; | |
1376 | } | |
78946b9b PS |
1377 | } |
1378 | } | |
1379 | } | |
22c5c6e6 | 1380 | } |
34a2777c | 1381 | |
98245a84 | 1382 | // Find out wanted parent sheets. |
045f492c | 1383 | $excludes = $this->resolve_excludes('parents_exclude_sheets'); |
78946b9b | 1384 | if ($excludes !== true) { |
98245a84 | 1385 | foreach (array_reverse($this->parent_configs) as $parent_config) { // Base first, the immediate parent last. |
78946b9b | 1386 | $parent = $parent_config->name; |
045f492c | 1387 | if (empty($parent_config->sheets) || (!empty($excludes[$parent]) and $excludes[$parent] === true)) { |
78946b9b PS |
1388 | continue; |
1389 | } | |
1390 | foreach ($parent_config->sheets as $sheet) { | |
d433cf37 FM |
1391 | if (!empty($excludes[$parent]) && is_array($excludes[$parent]) |
1392 | && in_array($sheet, $excludes[$parent])) { | |
78946b9b PS |
1393 | continue; |
1394 | } | |
d433cf37 FM |
1395 | |
1396 | // We never refer to the parent LESS files. | |
78946b9b PS |
1397 | $sheetfile = "$parent_config->dir/style/$sheet.css"; |
1398 | if (is_readable($sheetfile)) { | |
045f492c | 1399 | $cssfiles['parents'][$parent][$sheet] = $sheetfile; |
78946b9b PS |
1400 | } |
1401 | } | |
1402 | } | |
1403 | } | |
34a2777c | 1404 | |
41b973bc | 1405 | |
d433cf37 | 1406 | // Current theme sheets and less file. |
65b8336e | 1407 | // We first add the SCSS, or LESS file because we want the CSS ones to |
41b973bc | 1408 | // be included after the SCSS/LESS code. However, if both the LESS file |
65b8336e | 1409 | // and a CSS file share the same name, the CSS file is ignored. |
41b973bc FM |
1410 | if ($this->get_scss_property()) { |
1411 | $cssfiles['theme'][self::SCSS_KEY] = true; | |
65b8336e | 1412 | } else if (!empty($this->lessfile)) { |
d433cf37 FM |
1413 | $sheetfile = "{$this->dir}/less/{$this->lessfile}.less"; |
1414 | if (is_readable($sheetfile)) { | |
1415 | $cssfiles['theme'][$this->lessfile] = $sheetfile; | |
1416 | } | |
1417 | } | |
78946b9b PS |
1418 | if (is_array($this->sheets)) { |
1419 | foreach ($this->sheets as $sheet) { | |
1420 | $sheetfile = "$this->dir/style/$sheet.css"; | |
d433cf37 | 1421 | if (is_readable($sheetfile) && !isset($cssfiles['theme'][$sheet])) { |
045f492c | 1422 | $cssfiles['theme'][$sheet] = $sheetfile; |
78946b9b | 1423 | } |
ebebf55c | 1424 | } |
78946b9b PS |
1425 | } |
1426 | ||
98245a84 PS |
1427 | if ($cache) { |
1428 | $files = $cssfiles; | |
1429 | $files['created'] = time(); | |
d433cf37 | 1430 | $cache->set($cachekey, $files); |
045f492c | 1431 | } |
98245a84 PS |
1432 | return $cssfiles; |
1433 | } | |
e68c5f36 | 1434 | |
d433cf37 | 1435 | /** |
08823e5f | 1436 | * Return the CSS content generated from LESS the file. |
d433cf37 FM |
1437 | * |
1438 | * @param bool $themedesigner True if theme designer is enabled. | |
1439 | * @return bool|string Return false when the compilation failed. Else the compiled string. | |
1440 | */ | |
1441 | protected function get_css_content_from_less($themedesigner) { | |
a75f1dbc | 1442 | global $CFG; |
d433cf37 FM |
1443 | |
1444 | $lessfile = $this->lessfile; | |
1445 | if (!$lessfile || !is_readable($this->dir . '/less/' . $lessfile . '.less')) { | |
1446 | throw new coding_exception('The theme did not define a LESS file, or it is not readable.'); | |
1447 | } | |
1448 | ||
1081de20 | 1449 | // We might need more memory/time to do this, so let's play safe. |
d433cf37 | 1450 | raise_memory_limit(MEMORY_EXTRA); |
1081de20 | 1451 | core_php_time_limit::raise(300); |
d433cf37 FM |
1452 | |
1453 | // Files list. | |
08823e5f | 1454 | $files = $this->get_css_files($themedesigner); |
d433cf37 | 1455 | |
08823e5f FM |
1456 | // Get the LESS file path. |
1457 | $themelessfile = $files['theme'][$lessfile]; | |
d433cf37 | 1458 | |
5d504395 AN |
1459 | // Setup compiler options. |
1460 | $options = array( | |
d433cf37 | 1461 | // We need to set the import directory to where $lessfile is. |
08823e5f | 1462 | 'import_dirs' => array(dirname($themelessfile) => '/'), |
d433cf37 FM |
1463 | // Always disable default caching. |
1464 | 'cache_method' => false, | |
1465 | // Disable the relative URLs, we have post_process() to handle that. | |
1466 | 'relativeUrls' => false, | |
5d504395 AN |
1467 | ); |
1468 | ||
1469 | if ($themedesigner) { | |
1470 | // Add the sourceMap inline to ensure that it is atomically generated. | |
1471 | $options['sourceMap'] = true; | |
a75f1dbc DM |
1472 | $options['sourceMapBasepath'] = $CFG->dirroot; |
1473 | $options['sourceMapRootpath'] = $CFG->wwwroot; | |
5d504395 AN |
1474 | } |
1475 | ||
1476 | // Instantiate the compiler. | |
1477 | $compiler = new core_lessc($options); | |
d433cf37 FM |
1478 | |
1479 | try { | |
08823e5f | 1480 | $compiler->parse_file_content($themelessfile); |
d433cf37 FM |
1481 | |
1482 | // Get the callbacks. | |
1483 | $compiler->parse($this->get_extra_less_code()); | |
1484 | $compiler->ModifyVars($this->get_less_variables()); | |
1485 | ||
1486 | // Compile the CSS. | |
1487 | $compiled = $compiler->getCss(); | |
1488 | ||
d433cf37 FM |
1489 | } catch (Less_Exception_Parser $e) { |
1490 | $compiled = false; | |
1491 | debugging('Error while compiling LESS ' . $lessfile . ' file: ' . $e->getMessage(), DEBUG_DEVELOPER); | |
1492 | } | |
1493 | ||
1494 | // Try to save memory. | |
1495 | $compiler = null; | |
1496 | unset($compiler); | |
1497 | ||
1498 | return $compiled; | |
1499 | } | |
1500 | ||
65b8336e FM |
1501 | /** |
1502 | * Return the CSS content generated from the SCSS file. | |
1503 | * | |
1504 | * @param bool $themedesigner True if theme designer is enabled. | |
1505 | * @return bool|string Return false when the compilation failed. Else the compiled string. | |
1506 | */ | |
1507 | protected function get_css_content_from_scss($themedesigner) { | |
1508 | global $CFG; | |
1509 | ||
41b973bc FM |
1510 | list($paths, $scss) = $this->get_scss_property(); |
1511 | if (!$scss) { | |
65b8336e FM |
1512 | throw new coding_exception('The theme did not define a SCSS file, or it is not readable.'); |
1513 | } | |
1514 | ||
1081de20 | 1515 | // We might need more memory/time to do this, so let's play safe. |
65b8336e | 1516 | raise_memory_limit(MEMORY_EXTRA); |
1081de20 | 1517 | core_php_time_limit::raise(300); |
65b8336e | 1518 | |
65b8336e FM |
1519 | // Set-up the compiler. |
1520 | $compiler = new core_scss(); | |
258143e3 | 1521 | $compiler->prepend_raw_scss($this->get_pre_scss_code()); |
41b973bc FM |
1522 | if (is_string($scss)) { |
1523 | $compiler->set_file($scss); | |
1524 | } else { | |
1525 | $compiler->append_raw_scss($scss($this)); | |
1526 | $compiler->setImportPaths($paths); | |
1527 | } | |
65b8336e | 1528 | $compiler->append_raw_scss($this->get_extra_scss_code()); |
65b8336e FM |
1529 | |
1530 | try { | |
1531 | // Compile! | |
1532 | $compiled = $compiler->to_css(); | |
65b8336e | 1533 | |
eadebb24 | 1534 | } catch (\Exception $e) { |
65b8336e | 1535 | $compiled = false; |
41b973bc | 1536 | debugging('Error while compiling SCSS: ' . $e->getMessage(), DEBUG_DEVELOPER); |
65b8336e FM |
1537 | } |
1538 | ||
1539 | // Try to save memory. | |
1540 | $compiler = null; | |
1541 | unset($compiler); | |
1542 | ||
1543 | return $compiled; | |
1544 | } | |
1545 | ||
af9edb2e BB |
1546 | /** |
1547 | * Return the precompiled CSS if the precompiledcsscallback exists. | |
1548 | * | |
1549 | * @return string Return compiled css. | |
1550 | */ | |
1551 | public function get_precompiled_css_content() { | |
1552 | $configs = [$this] + $this->parent_configs; | |
1553 | $css = ''; | |
1554 | ||
1555 | foreach ($configs as $config) { | |
1556 | if (isset($config->precompiledcsscallback)) { | |
1557 | $function = $config->precompiledcsscallback; | |
1558 | if (function_exists($function)) { | |
1559 | $css .= $function($this); | |
1560 | } | |
1561 | } | |
1562 | } | |
1563 | return $css; | |
1564 | } | |
1565 | ||
95b06c13 DW |
1566 | /** |
1567 | * Get the icon system to use. | |
1568 | * | |
1569 | * @return string | |
1570 | */ | |
1571 | public function get_icon_system() { | |
1572 | ||
1573 | // Getting all the candidate functions. | |
1574 | $system = false; | |
1575 | if (isset($this->iconsystem) && \core\output\icon_system::is_valid_system($this->iconsystem)) { | |
1576 | return $this->iconsystem; | |
1577 | } | |
1578 | foreach ($this->parent_configs as $parent_config) { | |
1579 | if (isset($parent_config->iconsystem) && \core\output\icon_system::is_valid_system($parent_config->iconsystem)) { | |
1580 | return $parent_config->iconsystem; | |
1581 | } | |
1582 | } | |
8857c715 | 1583 | return \core\output\icon_system::STANDARD; |
95b06c13 DW |
1584 | } |
1585 | ||
d433cf37 FM |
1586 | /** |
1587 | * Return extra LESS variables to use when compiling. | |
1588 | * | |
1589 | * @return array Where keys are the variable names (omitting the @), and the values are the value. | |
1590 | */ | |
1591 | protected function get_less_variables() { | |
1592 | $variables = array(); | |
1593 | ||
1594 | // Getting all the candidate functions. | |
1595 | $candidates = array(); | |
1596 | foreach ($this->parent_configs as $parent_config) { | |
1597 | if (!isset($parent_config->lessvariablescallback)) { | |
1598 | continue; | |
1599 | } | |
1600 | $candidates[] = $parent_config->lessvariablescallback; | |
1601 | } | |
1602 | $candidates[] = $this->lessvariablescallback; | |
1603 | ||
1604 | // Calling the functions. | |
1605 | foreach ($candidates as $function) { | |
1606 | if (function_exists($function)) { | |
1607 | $vars = $function($this); | |
1608 | if (!is_array($vars)) { | |
1609 | debugging('Callback ' . $function . ' did not return an array() as expected', DEBUG_DEVELOPER); | |
1610 | continue; | |
1611 | } | |
1612 | $variables = array_merge($variables, $vars); | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | return $variables; | |
1617 | } | |
1618 | ||
65b8336e | 1619 | /** |
258143e3 FM |
1620 | * Return extra LESS code to add when compiling. |
1621 | * | |
1622 | * This is intended to be used by themes to inject some LESS code | |
1623 | * before it gets compiled. If you want to inject variables you | |
1624 | * should use {@link self::get_less_variables()}. | |
65b8336e | 1625 | * |
258143e3 | 1626 | * @return string The LESS code to inject. |
65b8336e | 1627 | */ |
258143e3 FM |
1628 | protected function get_extra_less_code() { |
1629 | $content = ''; | |
65b8336e FM |
1630 | |
1631 | // Getting all the candidate functions. | |
1632 | $candidates = array(); | |
1633 | foreach ($this->parent_configs as $parent_config) { | |
258143e3 | 1634 | if (!isset($parent_config->extralesscallback)) { |
65b8336e FM |
1635 | continue; |
1636 | } | |
258143e3 | 1637 | $candidates[] = $parent_config->extralesscallback; |
65b8336e | 1638 | } |
258143e3 | 1639 | $candidates[] = $this->extralesscallback; |
65b8336e FM |
1640 | |
1641 | // Calling the functions. | |
1642 | foreach ($candidates as $function) { | |
1643 | if (function_exists($function)) { | |
258143e3 | 1644 | $content .= "\n/** Extra LESS from $function **/\n" . $function($this) . "\n"; |
65b8336e FM |
1645 | } |
1646 | } | |
1647 | ||
258143e3 | 1648 | return $content; |
65b8336e FM |
1649 | } |
1650 | ||
d433cf37 | 1651 | /** |
258143e3 | 1652 | * Return extra SCSS code to add when compiling. |
d433cf37 | 1653 | * |
258143e3 | 1654 | * This is intended to be used by themes to inject some SCSS code |
d433cf37 | 1655 | * before it gets compiled. If you want to inject variables you |
258143e3 | 1656 | * should use {@link self::get_scss_variables()}. |
d433cf37 | 1657 | * |
258143e3 | 1658 | * @return string The SCSS code to inject. |
d433cf37 | 1659 | */ |
258143e3 | 1660 | protected function get_extra_scss_code() { |
d433cf37 FM |
1661 | $content = ''; |
1662 | ||
1663 | // Getting all the candidate functions. | |
1664 | $candidates = array(); | |
1665 | foreach ($this->parent_configs as $parent_config) { | |
258143e3 | 1666 | if (!isset($parent_config->extrascsscallback)) { |
d433cf37 FM |
1667 | continue; |
1668 | } | |
258143e3 | 1669 | $candidates[] = $parent_config->extrascsscallback; |
d433cf37 | 1670 | } |
258143e3 | 1671 | $candidates[] = $this->extrascsscallback; |
d433cf37 FM |
1672 | |
1673 | // Calling the functions. | |
1674 | foreach ($candidates as $function) { | |
1675 | if (function_exists($function)) { | |
258143e3 | 1676 | $content .= "\n/** Extra SCSS from $function **/\n" . $function($this) . "\n"; |
d433cf37 FM |
1677 | } |
1678 | } | |
1679 | ||
1680 | return $content; | |
1681 | } | |
1682 | ||
65b8336e | 1683 | /** |
258143e3 | 1684 | * SCSS code to prepend when compiling. |
65b8336e | 1685 | * |
258143e3 | 1686 | * This is intended to be used by themes to inject SCSS code before it gets compiled. |
65b8336e FM |
1687 | * |
1688 | * @return string The SCSS code to inject. | |
1689 | */ | |
258143e3 | 1690 | protected function get_pre_scss_code() { |
65b8336e FM |
1691 | $content = ''; |
1692 | ||
1693 | // Getting all the candidate functions. | |
1694 | $candidates = array(); | |
1695 | foreach ($this->parent_configs as $parent_config) { | |
258143e3 | 1696 | if (!isset($parent_config->prescsscallback)) { |
65b8336e FM |
1697 | continue; |
1698 | } | |
258143e3 | 1699 | $candidates[] = $parent_config->prescsscallback; |
65b8336e | 1700 | } |
258143e3 | 1701 | $candidates[] = $this->prescsscallback; |
65b8336e FM |
1702 | |
1703 | // Calling the functions. | |
1704 | foreach ($candidates as $function) { | |
1705 | if (function_exists($function)) { | |
258143e3 | 1706 | $content .= "\n/** Pre-SCSS from $function **/\n" . $function($this) . "\n"; |
65b8336e FM |
1707 | } |
1708 | } | |
1709 | ||
1710 | return $content; | |
1711 | } | |
1712 | ||
41b973bc FM |
1713 | /** |
1714 | * Get the SCSS property. | |
1715 | * | |
1716 | * This resolves whether a SCSS file (or content) has to be used when generating | |
1717 | * the stylesheet for the theme. It will look at parents themes and check the | |
1718 | * SCSS properties there. | |
1719 | * | |
1720 | * @return False when SCSS is not used. | |
1721 | * An array with the import paths, and the path to the SCSS file or Closure as second. | |
1722 | */ | |
1723 | public function get_scss_property() { | |
1724 | if ($this->scsscache === null) { | |
1725 | $configs = [$this] + $this->parent_configs; | |
1726 | $scss = null; | |
1727 | ||
1728 | foreach ($configs as $config) { | |
1729 | $path = "{$config->dir}/scss"; | |
1730 | ||
1731 | // We collect the SCSS property until we've found one. | |
1732 | if (empty($scss) && !empty($config->scss)) { | |
1733 | $candidate = is_string($config->scss) ? "{$path}/{$config->scss}.scss" : $config->scss; | |
1734 | if ($candidate instanceof Closure) { | |
1735 | $scss = $candidate; | |
1736 | } else if (is_string($candidate) && is_readable($candidate)) { | |
1737 | $scss = $candidate; | |
1738 | } | |
1739 | } | |
1740 | ||
1741 | // We collect the import paths once we've found a SCSS property. | |
1742 | if ($scss && is_dir($path)) { | |
1743 | $paths[] = $path; | |
1744 | } | |
1745 | ||
1746 | } | |
1747 | ||
1748 | $this->scsscache = $scss !== null ? [$paths, $scss] : false; | |
1749 | } | |
1750 | ||
1751 | return $this->scsscache; | |
1752 | } | |
1753 | ||
e68c5f36 | 1754 | /** |
473dd811 SH |
1755 | * Generate a URL to the file that serves theme JavaScript files. |
1756 | * | |
d7656956 ARN |
1757 | * If we determine that the theme has no relevant files, then we return |
1758 | * early with a null value. | |
1759 | * | |
71c03ac1 | 1760 | * @param bool $inhead true means head url, false means footer |
d7656956 | 1761 | * @return moodle_url|null |
e68c5f36 | 1762 | */ |
baeb20bb | 1763 | public function javascript_url($inhead) { |
e68c5f36 PS |
1764 | global $CFG; |
1765 | ||
1766 | $rev = theme_get_revision(); | |
e68c5f36 | 1767 | $params = array('theme'=>$this->name,'rev'=>$rev); |
baeb20bb PS |
1768 | $params['type'] = $inhead ? 'head' : 'footer'; |
1769 | ||
d7656956 ARN |
1770 | // Return early if there are no files to serve |
1771 | if (count($this->javascript_files($params['type'])) === 0) { | |
1772 | return null; | |
1773 | } | |
1774 | ||
ecbad2ad | 1775 | if (!empty($CFG->slasharguments) and $rev > 0) { |
7eb50b32 | 1776 | $url = new moodle_url("/theme/javascript.php"); |
ecbad2ad PS |
1777 | $url->set_slashargument('/'.$this->name.'/'.$rev.'/'.$params['type'], 'noparam', true); |
1778 | return $url; | |
1779 | } else { | |
7eb50b32 | 1780 | return new moodle_url('/theme/javascript.php', $params); |
ecbad2ad | 1781 | } |
e68c5f36 PS |
1782 | } |
1783 | ||
473dd811 SH |
1784 | /** |
1785 | * Get the URL's for the JavaScript files used by this theme. | |
1786 | * They won't be served directly, instead they'll be mediated through | |
1787 | * theme/javascript.php. | |
1788 | * | |
1789 | * @param string $type Either javascripts_footer, or javascripts | |
1790 | * @return array | |
1791 | */ | |
045f492c SH |
1792 | public function javascript_files($type) { |
1793 | if ($type === 'footer') { | |
1794 | $type = 'javascripts_footer'; | |
1795 | } else { | |
f856106b | 1796 | $type = 'javascripts'; |
045f492c | 1797 | } |
04c01408 | 1798 | |
358c13cb PS |
1799 | $js = array(); |
1800 | // find out wanted parent javascripts | |
045f492c | 1801 | $excludes = $this->resolve_excludes('parents_exclude_javascripts'); |
358c13cb PS |
1802 | if ($excludes !== true) { |
1803 | foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last | |
1804 | $parent = $parent_config->name; | |
04c01408 | 1805 | if (empty($parent_config->$type)) { |
358c13cb PS |
1806 | continue; |
1807 | } | |
1808 | if (!empty($excludes[$parent]) and $excludes[$parent] === true) { | |
1809 | continue; | |
1810 | } | |
04c01408 | 1811 | foreach ($parent_config->$type as $javascript) { |
358c13cb PS |
1812 | if (!empty($excludes[$parent]) and is_array($excludes[$parent]) |
1813 | and in_array($javascript, $excludes[$parent])) { | |
1814 | continue; | |
1815 | } | |
1816 | $javascriptfile = "$parent_config->dir/javascript/$javascript.js"; | |
1817 | if (is_readable($javascriptfile)) { | |
045f492c | 1818 | $js[] = $javascriptfile; |
358c13cb PS |
1819 | } |
1820 | } | |
1821 | } | |
1822 | } | |
1823 | ||
1824 | // current theme javascripts | |
04c01408 PS |
1825 | if (is_array($this->$type)) { |
1826 | foreach ($this->$type as $javascript) { | |
358c13cb PS |
1827 | $javascriptfile = "$this->dir/javascript/$javascript.js"; |
1828 | if (is_readable($javascriptfile)) { | |
045f492c SH |
1829 | $js[] = $javascriptfile; |
1830 | } | |
1831 | } | |
1832 | } | |
045f492c SH |
1833 | return $js; |
1834 | } | |
1835 | ||
1836 | /** | |
3d3fae72 | 1837 | * Resolves an exclude setting to the themes setting is applicable or the |
045f492c SH |
1838 | * setting of its closest parent. |
1839 | * | |
1840 | * @param string $variable The name of the setting the exclude setting to resolve | |
f8129210 | 1841 | * @param string $default |
045f492c SH |
1842 | * @return mixed |
1843 | */ | |
f8129210 | 1844 | protected function resolve_excludes($variable, $default = null) { |
045f492c SH |
1845 | $setting = $default; |
1846 | if (is_array($this->{$variable}) or $this->{$variable} === true) { | |
1847 | $setting = $this->{$variable}; | |
1848 | } else { | |
1849 | foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last | |
1850 | if (!isset($parent_config->{$variable})) { | |
1851 | continue; | |
1852 | } | |
1853 | if (is_array($parent_config->{$variable}) or $parent_config->{$variable} === true) { | |
1854 | $setting = $parent_config->{$variable}; | |
1855 | break; | |
358c13cb PS |
1856 | } |
1857 | } | |
1858 | } | |
045f492c SH |
1859 | return $setting; |
1860 | } | |
358c13cb | 1861 | |
045f492c SH |
1862 | /** |
1863 | * Returns the content of the one huge javascript file merged from all theme javascript files. | |
473dd811 | 1864 | * |
f8129210 | 1865 | * @param bool $type |
045f492c SH |
1866 | * @return string |
1867 | */ | |
1868 | public function javascript_content($type) { | |
1869 | $jsfiles = $this->javascript_files($type); | |
1870 | $js = ''; | |
1871 | foreach ($jsfiles as $jsfile) { | |
1872 | $js .= file_get_contents($jsfile)."\n"; | |
1873 | } | |
1874 | return $js; | |
e68c5f36 PS |
1875 | } |
1876 | ||
473dd811 SH |
1877 | /** |
1878 | * Post processes CSS. | |
1879 | * | |
1880 | * This method post processes all of the CSS before it is served for this theme. | |
1881 | * This is done so that things such as image URL's can be swapped in and to | |
1882 | * run any specific CSS post process method the theme has requested. | |
3d3fae72 | 1883 | * This allows themes to use CSS settings. |
473dd811 SH |
1884 | * |
1885 | * @param string $css The CSS to process. | |
1886 | * @return string The processed CSS. | |
1887 | */ | |
045f492c | 1888 | public function post_process($css) { |
78946b9b | 1889 | // now resolve all image locations |
37824e73 | 1890 | if (preg_match_all('/\[\[pix:([a-z0-9_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) { |
78946b9b PS |
1891 | $replaced = array(); |
1892 | foreach ($matches as $match) { | |
1893 | if (isset($replaced[$match[0]])) { | |
1894 | continue; | |
1895 | } | |
1896 | $replaced[$match[0]] = true; | |
1897 | $imagename = $match[2]; | |
1898 | $component = rtrim($match[1], '|'); | |
663640f5 | 1899 | $imageurl = $this->image_url($imagename, $component)->out(false); |
df06c1c4 | 1900 | // we do not need full url because the image.php is always in the same dir |
9d473266 | 1901 | $imageurl = preg_replace('|^http.?://[^/]+|', '', $imageurl); |
df06c1c4 | 1902 | $css = str_replace($match[0], $imageurl, $css); |
ebebf55c | 1903 | } |
34a2777c | 1904 | } |
17a6649b | 1905 | |
9ba6076c | 1906 | // Now resolve all font locations. |
37824e73 | 1907 | if (preg_match_all('/\[\[font:([a-z0-9_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) { |
9ba6076c PS |
1908 | $replaced = array(); |
1909 | foreach ($matches as $match) { | |
1910 | if (isset($replaced[$match[0]])) { | |
1911 | continue; | |
1912 | } | |
1913 | $replaced[$match[0]] = true; | |
1914 | $fontname = $match[2]; | |
1915 | $component = rtrim($match[1], '|'); | |
1916 | $fonturl = $this->font_url($fontname, $component)->out(false); | |
1917 | // We do not need full url because the font.php is always in the same dir. | |
1918 | $fonturl = preg_replace('|^http.?://[^/]+|', '', $fonturl); | |
1919 | $css = str_replace($match[0], $fonturl, $css); | |
1920 | } | |
1921 | } | |
1922 | ||
dc48cc0a AA |
1923 | // Now resolve all theme settings or do any other postprocessing. |
1924 | // This needs to be done before calling core parser, since the parser strips [[settings]] tags. | |
1925 | $csspostprocess = $this->csspostprocess; | |
1926 | if (function_exists($csspostprocess)) { | |
1927 | $css = $csspostprocess($css, $this); | |
1928 | } | |
1929 | ||
1d987cae | 1930 | // Post processing using an object representation of CSS. |
493ef66e FM |
1931 | $treeprocessor = $this->get_css_tree_post_processor(); |
1932 | $needsparsing = !empty($treeprocessor) || !empty($this->rtlmode); | |
1d987cae | 1933 | if ($needsparsing) { |
061a39ac | 1934 | |
1081de20 | 1935 | // We might need more memory/time to do this, so let's play safe. |
061a39ac | 1936 | raise_memory_limit(MEMORY_EXTRA); |
1081de20 | 1937 | core_php_time_limit::raise(300); |
061a39ac | 1938 | |
1d987cae FM |
1939 | $parser = new core_cssparser($css); |
1940 | $csstree = $parser->parse(); | |
1941 | unset($parser); | |
1d987cae | 1942 | |
d8f90ec6 FM |
1943 | if ($this->rtlmode) { |
1944 | $this->rtlize($csstree); | |
1945 | } | |
1946 | ||
493ef66e FM |
1947 | if ($treeprocessor) { |
1948 | $treeprocessor($csstree, $this); | |
d8f90ec6 | 1949 | } |
1d987cae | 1950 | |
1d987cae FM |
1951 | $css = $csstree->render(); |
1952 | unset($csstree); | |
1953 | } | |
1954 | ||
78946b9b | 1955 | return $css; |
ebebf55c | 1956 | } |
17a6649b | 1957 | |
1d987cae FM |
1958 | /** |
1959 | * Flip a stylesheet to RTL. | |
1960 | * | |
1961 | * @param Object $csstree The parsed CSS tree structure to flip. | |
1962 | * @return void | |
1963 | */ | |
1964 | protected function rtlize($csstree) { | |
1965 | $rtlcss = new core_rtlcss($csstree); | |
1966 | $rtlcss->flip(); | |
1967 | } | |
1968 | ||
ebebf55c | 1969 | /** |
c266d256 DW |
1970 | * Return the direct URL for an image from the pix folder. |
1971 | * | |
1972 | * Use this function sparingly and never for icons. For icons use pix_icon or the pix helper in a mustache template. | |
78946b9b | 1973 | * |
088598ba | 1974 | * @deprecated since Moodle 3.3 |
78946b9b | 1975 | * @param string $imagename the name of the icon. |
f8129210 | 1976 | * @param string $component specification of one plugin like in get_string() |
78946b9b | 1977 | * @return moodle_url |
fdeb7fa1 | 1978 | */ |
c39e5ba2 | 1979 | public function pix_url($imagename, $component) { |
088598ba | 1980 | debugging('pix_url is deprecated. Use image_url for images and pix_icon for icons.', DEBUG_DEVELOPER); |
c266d256 DW |
1981 | return $this->image_url($imagename, $component); |
1982 | } | |
1983 | ||
1984 | /** | |
1985 | * Return the direct URL for an image from the pix folder. | |
1986 | * | |
1987 | * Use this function sparingly and never for icons. For icons use pix_icon or the pix helper in a mustache template. | |
1988 | * | |
1989 | * @param string $imagename the name of the icon. | |
1990 | * @param string $component specification of one plugin like in get_string() | |
1991 | * @return moodle_url | |
1992 | */ | |
1993 | public function image_url($imagename, $component) { | |
fdeb7fa1 | 1994 | global $CFG; |
78946b9b | 1995 | |
9d473266 | 1996 | $params = array('theme'=>$this->name); |
436dbeec | 1997 | $svg = $this->use_svg_icons(); |
9d473266 PS |
1998 | |
1999 | if (empty($component) or $component === 'moodle' or $component === 'core') { | |
2000 | $params['component'] = 'core'; | |
2001 | } else { | |
2002 | $params['component'] = $component; | |
2003 | } | |
78946b9b PS |
2004 | |
2005 | $rev = theme_get_revision(); | |
2006 | if ($rev != -1) { | |
2007 | $params['rev'] = $rev; | |
2008 | } | |
9d473266 PS |
2009 | |
2010 | $params['image'] = $imagename; | |
2011 | ||
7eb50b32 | 2012 | $url = new moodle_url("/theme/image.php"); |
9d473266 | 2013 | if (!empty($CFG->slasharguments) and $rev > 0) { |
436dbeec SH |
2014 | $path = '/'.$params['theme'].'/'.$params['component'].'/'.$params['rev'].'/'.$params['image']; |
2015 | if (!$svg) { | |
2016 | // We add a simple /_s to the start of the path. | |
2017 | // The underscore is used to ensure that it isn't a valid theme name. | |
2018 | $path = '/_s'.$path; | |
2019 | } | |
2020 | $url->set_slashargument($path, 'noparam', true); | |
9d473266 | 2021 | } else { |
436dbeec SH |
2022 | if (!$svg) { |
2023 | // We add an SVG param so that we know not to serve SVG images. | |
2024 | // We do this because all modern browsers support SVG and this param will one day be removed. | |
2025 | $params['svg'] = '0'; | |
2026 | } | |
2027 | $url->params($params); | |
fdeb7fa1 | 2028 | } |
78946b9b | 2029 | |
9d473266 | 2030 | return $url; |
fdeb7fa1 | 2031 | } |
2032 | ||
9ba6076c PS |
2033 | /** |
2034 | * Return the URL for a font | |
2035 | * | |
2036 | * @param string $font the name of the font (including extension). | |
2037 | * @param string $component specification of one plugin like in get_string() | |
2038 | * @return moodle_url | |
2039 | */ | |
2040 | public function font_url($font, $component) { | |
2041 | global $CFG; | |
2042 | ||
2043 | $params = array('theme'=>$this->name); | |
2044 | ||
2045 | if (empty($component) or $component === 'moodle' or $component === 'core') { | |
2046 | $params['component'] = 'core'; | |
2047 | } else { | |
2048 | $params['component'] = $component; | |
2049 | } | |
2050 | ||
2051 | $rev = theme_get_revision(); | |
2052 | if ($rev != -1) { | |
2053 | $params['rev'] = $rev; | |
2054 | } | |
2055 | ||
2056 | $params['font'] = $font; | |
2057 | ||
7eb50b32 | 2058 | $url = new moodle_url("/theme/font.php"); |
9ba6076c PS |
2059 | if (!empty($CFG->slasharguments) and $rev > 0) { |
2060 | $path = '/'.$params['theme'].'/'.$params['component'].'/'.$params['rev'].'/'.$params['font']; | |
2061 | $url->set_slashargument($path, 'noparam', true); | |
2062 | } else { | |
2063 | $url->params($params); | |
2064 | } | |
2065 | ||
2066 | return $url; | |
2067 | } | |
2068 | ||
fe7b75f8 PS |
2069 | /** |
2070 | * Returns URL to the stored file via pluginfile.php. | |
2071 | * | |
2072 | * Note the theme must also implement pluginfile.php handler, | |
2073 | * theme revision is used instead of the itemid. | |
2074 | * | |
2075 | * @param string $setting | |
2076 | * @param string $filearea | |
2077 | * @return string protocol relative URL or null if not present | |
2078 | */ | |
2079 | public function setting_file_url($setting, $filearea) { | |
2080 | global $CFG; | |
2081 | ||
2082 | if (empty($this->settings->$setting)) { | |
2083 | return null; | |
2084 | } | |
2085 | ||
2086 | $component = 'theme_'.$this->name; | |
2087 | $itemid = theme_get_revision(); | |
2088 | $filepath = $this->settings->$setting; | |
2089 | $syscontext = context_system::instance(); | |
2090 | ||
2091 | $url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php", "/$syscontext->id/$component/$filearea/$itemid".$filepath); | |
2092 | ||
2093 | // Now this is tricky because the we can not hardcode http or https here, lets use the relative link. | |
2094 | // Note: unfortunately moodle_url does not support //urls yet. | |
2095 | ||
2096 | $url = preg_replace('|^https?://|i', '//', $url->out(false)); | |
2097 | ||
2098 | return $url; | |
2099 | } | |
2100 | ||
2101 | /** | |
2102 | * Serve the theme setting file. | |
2103 | * | |
2104 | * @param string $filearea | |
2105 | * @param array $args | |
2106 | * @param bool $forcedownload | |
2107 | * @param array $options | |
2108 | * @return bool may terminate if file not found or donotdie not specified | |
2109 | */ | |
2110 | public function setting_file_serve($filearea, $args, $forcedownload, $options) { | |
2111 | global $CFG; | |
2112 | require_once("$CFG->libdir/filelib.php"); | |
2113 | ||
2114 | $syscontext = context_system::instance(); | |
2115 | $component = 'theme_'.$this->name; | |
2116 | ||
2117 | $revision = array_shift($args); | |
2118 | if ($revision < 0) { | |
2119 | $lifetime = 0; | |
2120 | } else { | |
2121 | $lifetime = 60*60*24*60; | |
bb8ed60a MS |
2122 | // By default, theme files must be cache-able by both browsers and proxies. |
2123 | if (!array_key_exists('cacheability', $options)) { | |
2124 | $options['cacheability'] = 'public'; | |
2125 | } | |
fe7b75f8 PS |
2126 | } |
2127 | ||
2128 | $fs = get_file_storage(); | |
2129 | $relativepath = implode('/', $args); | |
2130 | ||
2131 | $fullpath = "/{$syscontext->id}/{$component}/{$filearea}/0/{$relativepath}"; | |
2132 | $fullpath = rtrim($fullpath, '/'); | |
2133 | if ($file = $fs->get_file_by_hash(sha1($fullpath))) { | |
2134 | send_stored_file($file, $lifetime, 0, $forcedownload, $options); | |
2135 | return true; | |
2136 | } else { | |
2137 | send_file_not_found(); | |
2138 | } | |
2139 | } | |
2140 | ||
fdeb7fa1 | 2141 | /** |
78946b9b | 2142 | * Resolves the real image location. |
436dbeec SH |
2143 | * |
2144 | * $svg was introduced as an arg in 2.4. It is important because not all supported browsers support the use of SVG | |
2145 | * and we need a way in which to turn it off. | |
2146 | * By default SVG won't be used unless asked for. This is done for two reasons: | |
2147 | * 1. It ensures that we don't serve svg images unless we really want to. The admin has selected to force them, of the users | |
2148 | * browser supports SVG. | |
2149 | * 2. We only serve SVG images from locations we trust. This must NOT include any areas where the image may have been uploaded | |
2150 | * by the user due to security concerns. | |
2151 | * | |
78946b9b PS |
2152 | * @param string $image name of image, may contain relative path |
2153 | * @param string $component | |
62306cdf JD |
2154 | * @param bool $svg|null Should SVG images also be looked for? If null, resorts to $CFG->svgicons if that is set; falls back to |
2155 | * auto-detection of browser support otherwise | |
78946b9b | 2156 | * @return string full file path |
fdeb7fa1 | 2157 | */ |
436dbeec | 2158 | public function resolve_image_location($image, $component, $svg = false) { |
78946b9b PS |
2159 | global $CFG; |
2160 | ||
436dbeec SH |
2161 | if (!is_bool($svg)) { |
2162 | // If $svg isn't a bool then we need to decide for ourselves. | |
2163 | $svg = $this->use_svg_icons(); | |
2164 | } | |
2165 | ||
78946b9b | 2166 | if ($component === 'moodle' or $component === 'core' or empty($component)) { |
436dbeec | 2167 | if ($imagefile = $this->image_exists("$this->dir/pix_core/$image", $svg)) { |
78946b9b PS |
2168 | return $imagefile; |
2169 | } | |
2170 | foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last | |
436dbeec | 2171 | if ($imagefile = $this->image_exists("$parent_config->dir/pix_core/$image", $svg)) { |
78946b9b PS |
2172 | return $imagefile; |
2173 | } | |
2174 | } | |
436dbeec | 2175 | if ($imagefile = $this->image_exists("$CFG->dataroot/pix/$image", $svg)) { |
f930cf6b EM |
2176 | return $imagefile; |
2177 | } | |
436dbeec | 2178 | if ($imagefile = $this->image_exists("$CFG->dirroot/pix/$image", $svg)) { |
78946b9b PS |
2179 | return $imagefile; |
2180 | } | |
2181 | return null; | |
2182 | ||
2183 | } else if ($component === 'theme') { //exception | |
2184 | if ($image === 'favicon') { | |
2185 | return "$this->dir/pix/favicon.ico"; | |
2186 | } | |
436dbeec | 2187 | if ($imagefile = $this->image_exists("$this->dir/pix/$image", $svg)) { |
78946b9b PS |
2188 | return $imagefile; |
2189 | } | |
2190 | foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last | |
436dbeec | 2191 | if ($imagefile = $this->image_exists("$parent_config->dir/pix/$image", $svg)) { |
78946b9b PS |
2192 | return $imagefile; |
2193 | } | |
2194 | } | |
2195 | return null; | |
2196 | ||
78946b9b PS |
2197 | } else { |
2198 | if (strpos($component, '_') === false) { | |
2199 | $component = 'mod_'.$component; | |
2200 | } | |
2201 | list($type, $plugin) = explode('_', $component, 2); | |
2202 | ||
436dbeec | 2203 | if ($imagefile = $this->image_exists("$this->dir/pix_plugins/$type/$plugin/$image", $svg)) { |
78946b9b PS |
2204 | return $imagefile; |
2205 | } | |
2206 | foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last | |
436dbeec | 2207 | if ($imagefile = $this->image_exists("$parent_config->dir/pix_plugins/$type/$plugin/$image", $svg)) { |
78946b9b PS |
2208 | return $imagefile; |
2209 | } | |
2210 | } | |
436dbeec | 2211 | if ($imagefile = $this->image_exists("$CFG->dataroot/pix_plugins/$type/$plugin/$image", $svg)) { |
f930cf6b EM |
2212 | return $imagefile; |
2213 | } | |
1c74b260 | 2214 | $dir = core_component::get_plugin_directory($type, $plugin); |
436dbeec | 2215 | if ($imagefile = $this->image_exists("$dir/pix/$image", $svg)) { |
78946b9b PS |
2216 | return $imagefile; |
2217 | } | |
2218 | return null; | |
fdeb7fa1 | 2219 | } |
fdeb7fa1 | 2220 | } |
2221 | ||
9ba6076c PS |
2222 | /** |
2223 | * Resolves the real font location. | |
2224 | * | |
2225 | * @param string $font name of font file | |
2226 | * @param string $component | |
2227 | * @return string full file path | |
2228 | */ | |
2229 | public function resolve_font_location($font, $component) { | |
2230 | global $CFG; | |
2231 | ||
2232 | if ($component === 'moodle' or $component === 'core' or empty($component)) { | |
2233 | if (file_exists("$this->dir/fonts_core/$font")) { | |
2234 | return "$this->dir/fonts_core/$font"; | |
2235 | } | |
2236 | foreach (array_reverse($this->parent_configs) as $parent_config) { // Base first, the immediate parent last. | |
2237 | if (file_exists("$parent_config->dir/fonts_core/$font")) { | |
2238 | return "$parent_config->dir/fonts_core/$font"; | |
2239 | } | |
2240 | } | |
2241 | if (file_exists("$CFG->dataroot/fonts/$font")) { | |
2242 | return "$CFG->dataroot/fonts/$font"; | |
2243 | } | |
2244 | if (file_exists("$CFG->dirroot/lib/fonts/$font")) { | |
2245 | return "$CFG->dirroot/lib/fonts/$font"; | |
2246 | } | |
2247 | return null; | |
2248 | ||
2249 | } else if ($component === 'theme') { // Exception. | |
2250 | if (file_exists("$this->dir/fonts/$font")) { | |
2251 | return "$this->dir/fonts/$font"; | |
2252 | } | |
2253 | foreach (array_reverse($this->parent_configs) as $parent_config) { // Base first, the immediate parent last. | |
2254 | if (file_exists("$parent_config->dir/fonts/$font")) { | |
2255 | return "$parent_config->dir/fonts/$font"; | |
2256 | } | |
2257 | } | |
2258 | return null; | |
2259 | ||
2260 | } else { | |
2261 | if (strpos($component, '_') === false) { | |
2262 | $component = 'mod_'.$component; | |
2263 | } | |
2264 | list($type, $plugin) = explode('_', $component, 2); | |
2265 | ||
2266 | if (file_exists("$this->dir/fonts_plugins/$type/$plugin/$font")) { | |
2267 | return "$this->dir/fonts_plugins/$type/$plugin/$font"; | |
2268 | } | |
2269 | foreach (array_reverse($this->parent_configs) as $parent_config) { // Base first, the immediate parent last. | |
2270 | if (file_exists("$parent_config->dir/fonts_plugins/$type/$plugin/$font")) { | |
2271 | return "$parent_config->dir/fonts_plugins/$type/$plugin/$font"; | |
2272 | } | |
2273 | } | |
2274 | if (file_exists("$CFG->dataroot/fonts_plugins/$type/$plugin/$font")) { | |
2275 | return "$CFG->dataroot/fonts_plugins/$type/$plugin/$font"; | |
2276 | } | |
2277 | $dir = core_component::get_plugin_directory($type, $plugin); | |
2278 | if (file_exists("$dir/fonts/$font")) { | |
2279 | return "$dir/fonts/$font"; | |
2280 | } | |
2281 | return null; | |
2282 | } | |
2283 | } | |
2284 | ||
436dbeec SH |
2285 | /** |
2286 | * Return true if we should look for SVG images as well. | |
2287 | * | |
436dbeec SH |
2288 | * @return bool |
2289 | */ | |
2290 | public function use_svg_icons() { | |
2291 | global $CFG; | |
2292 | if ($this->usesvg === null) { | |
c3d2fbf9 | 2293 | |
ba2ad431 | 2294 | if (!isset($CFG->svgicons)) { |
c3d2fbf9 | 2295 | $this->usesvg = core_useragent::supports_svg(); |
436dbeec SH |
2296 | } else { |
2297 | // Force them on/off depending upon the setting. | |
ba2ad431 | 2298 | $this->usesvg = (bool)$CFG->svgicons; |
436dbeec SH |
2299 | } |
2300 | } | |
2301 | return $this->usesvg; | |
2302 | } | |
2303 | ||
f7d6a556 SH |
2304 | /** |
2305 | * Forces the usesvg setting to either true or false, avoiding any decision making. | |
2306 | * | |
2307 | * This function should only ever be used when absolutely required, and before any generation of image URL's has occurred. | |
383b89a1 | 2308 | * DO NOT ABUSE THIS FUNCTION... not that you'd want to right ;) |
f7d6a556 SH |
2309 | * |
2310 | * @param bool $setting True to force the use of svg when available, null otherwise. | |
2311 | */ | |
383b89a1 | 2312 | public function force_svg_use($setting) { |
f7d6a556 SH |
2313 | $this->usesvg = (bool)$setting; |
2314 | } | |
2315 | ||
1d987cae FM |
2316 | /** |
2317 | * Set to be in RTL mode. | |
2318 | * | |
2319 | * This will likely be used when post processing the CSS before serving it. | |
2320 | * | |
2321 | * @param bool $inrtl True when in RTL mode. | |
2322 | */ | |
2323 | public function set_rtl_mode($inrtl = true) { | |
2324 | $this->rtlmode = $inrtl; | |
2325 | } | |
2326 | ||
0e9911e1 AN |
2327 | /** |
2328 | * Whether the theme is being served in RTL mode. | |
2329 | * | |
2330 | * @return bool True when in RTL mode. | |
2331 | */ | |
2332 | public function get_rtl_mode() { | |
2333 | return $this->rtlmode; | |
2334 | } | |
2335 | ||
d4a03c00 | 2336 | /** |
78946b9b | 2337 | * Checks if file with any image extension exists. |
473dd811 | 2338 | * |
436dbeec SH |
2339 | * The order to these images was adjusted prior to the release of 2.4 |
2340 | * At that point the were the following image counts in Moodle core: | |
2341 | * | |
2342 | * - png = 667 in pix dirs (1499 total) | |
2343 | * - gif = 385 in pix dirs (606 total) | |
2344 | * - jpg = 62 in pix dirs (74 total) | |
2345 | * - jpeg = 0 in pix dirs (1 total) | |
2346 | * | |
2347 | * There is work in progress to move towards SVG presently hence that has been prioritiesed. | |
2348 | * | |
78946b9b | 2349 | * @param string $filepath |
436dbeec | 2350 | * @param bool $svg If set to true SVG images will also be looked for. |
78946b9b | 2351 | * @return string image name with extension |
d4a03c00 | 2352 | */ |
436dbeec SH |
2353 | private static function image_exists($filepath, $svg = false) { |
2354 | if ($svg && file_exists("$filepath.svg")) { | |
2355 | return "$filepath.svg"; | |
78946b9b PS |
2356 | } else if (file_exists("$filepath.png")) { |
2357 | return "$filepath.png"; | |
436dbeec SH |
2358 | } else if (file_exists("$filepath.gif")) { |
2359 | return "$filepath.gif"; | |
78946b9b PS |
2360 | } else if (file_exists("$filepath.jpg")) { |
2361 | return "$filepath.jpg"; | |
2362 | } else if (file_exists("$filepath.jpeg")) { | |
2363 | return "$filepath.jpeg"; | |
d4a03c00 | 2364 | } else { |
78946b9b | 2365 | return false; |
d4a03c00 | 2366 | } |
2367 | } | |
2368 | ||
fdeb7fa1 | 2369 | /** |
78946b9b | 2370 | * Loads the theme config from config.php file. |
473dd811 | 2371 | * |
78946b9b | 2372 | * @param string $themename |
473dd811 | 2373 | * @param stdClass $settings from config_plugins table |
b0ec47fb | 2374 | * @param boolean $parentscheck true to also check the parents. . |
473dd811 | 2375 | * @return stdClass The theme configuration |
fdeb7fa1 | 2376 | */ |
b0ec47fb | 2377 | private static function find_theme_config($themename, $settings, $parentscheck = true) { |
78946b9b PS |
2378 | // We have to use the variable name $THEME (upper case) because that |
2379 | // is what is used in theme config.php files. | |
fdeb7fa1 | 2380 | |
78946b9b PS |
2381 | if (!$dir = theme_config::find_theme_location($themename)) { |
2382 | return null; | |
fdeb7fa1 | 2383 | } |
2f67a9b3 | 2384 | |
365a5941 | 2385 | $THEME = new stdClass(); |
78946b9b PS |
2386 | $THEME->name = $themename; |
2387 | $THEME->dir = $dir; | |
2388 | $THEME->settings = $settings; | |
2389 | ||
2390 | global $CFG; // just in case somebody tries to use $CFG in theme config | |
2391 | include("$THEME->dir/config.php"); | |
2392 | ||
2393 | // verify the theme configuration is OK | |
2394 | if (!is_array($THEME->parents)) { | |
2395 | // parents option is mandatory now | |
2396 | return null; | |
b0ec47fb JM |
2397 | } else { |
2398 | // We use $parentscheck to only check the direct parents (avoid infinite loop). | |
2399 | if ($parentscheck) { | |
2400 | // Find all parent theme configs. | |
2401 | foreach ($THEME->parents as $parent) { | |
2402 | $parentconfig = theme_config::find_theme_config($parent, $settings, false); | |
2403 | if (empty($parentconfig)) { | |
2404 | return null; | |
2405 | } | |
2406 | } | |
2407 | } | |
fdeb7fa1 | 2408 | } |
2409 | ||
78946b9b | 2410 | return $THEME; |
fdeb7fa1 | 2411 | } |
2412 | ||
d4a03c00 | 2413 | /** |
78946b9b PS |
2414 | * Finds the theme location and verifies the theme has all needed files |
2415 | * and is not obsoleted. | |
473dd811 | 2416 | * |
78946b9b PS |
2417 | * @param string $themename |
2418 | * @return string full dir path or null if not found | |
d4a03c00 | 2419 | */ |
78946b9b PS |
2420 | private static function find_theme_location($themename) { |
2421 | global $CFG; | |
2422 | ||
2423 | if (file_exists("$CFG->dirroot/theme/$themename/config.php")) { | |
2424 | $dir = "$CFG->dirroot/theme/$themename"; | |
d4a03c00 | 2425 | |
3dd1d7e7 DM |
2426 | } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) { |
2427 | $dir = "$CFG->themedir/$themename"; | |
2428 | ||
7d875874 | 2429 | } else { |
78946b9b | 2430 | return null; |
d4a03c00 | 2431 | } |
78946b9b PS |
2432 | |
2433 | if (file_exists("$dir/styles.php")) { | |
2434 | //legacy theme - needs to be upgraded - upgrade info is displayed on the admin settings page | |
2435 | return null; | |
2436 | } | |
2f67a9b3 | 2437 | |
78946b9b | 2438 | return $dir; |
d4a03c00 | 2439 | } |
2440 | ||
1d13c75c | 2441 | /** |
78946b9b | 2442 | * Get the renderer for a part of Moodle for this theme. |
473dd811 | 2443 | * |
78946b9b | 2444 | * @param moodle_page $page the page we are rendering |
f8129210 | 2445 | * @param string $component the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'. |
78946b9b | 2446 | * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news' |
c927e35c | 2447 | * @param string $target one of rendering target constants |
78946b9b | 2448 | * @return renderer_base the requested renderer. |
1d13c75c | 2449 | */ |
c927e35c | 2450 | public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) { |
78946b9b | 2451 | if (is_null($this->rf)) { |
c927e35c | 2452 | $classname = $this->rendererfactory; |
78946b9b | 2453 | $this->rf = new $classname($this); |
1d13c75c | 2454 | } |
78946b9b | 2455 | |
c927e35c | 2456 | return $this->rf->get_renderer($page, $component, $subtype, $target); |
1d13c75c | 2457 | } |
2458 | ||
fdeb7fa1 | 2459 | /** |
78946b9b | 2460 | * Get the information from {@link $layouts} for this type of page. |
473dd811 | 2461 | * |
78946b9b PS |
2462 | * @param string $pagelayout the the page layout name. |
2463 | * @return array the appropriate part of {@link $layouts}. | |
fdeb7fa1 | 2464 | */ |
78946b9b PS |
2465 | protected function layout_info_for_page($pagelayout) { |
2466 | if (array_key_exists($pagelayout, $this->layouts)) { | |
2467 | return $this->layouts[$pagelayout]; | |
2468 | } else { | |
191b267b PS |
2469 | debugging('Invalid page layout specified: ' . $pagelayout); |
2470 | return $this->layouts['standard']; | |
fdeb7fa1 | 2471 | } |
2472 | } | |
2473 | ||
2474 | /** | |
78946b9b PS |
2475 | * Given the settings of this theme, and the page pagelayout, return the |
2476 | * full path of the page layout file to use. | |
2477 | * | |
2478 | * Used by {@link core_renderer::header()}. | |
2479 | * | |
2480 | * @param string $pagelayout the the page layout name. | |
2481 | * @return string Full path to the lyout file to use | |
ebebf55c | 2482 | */ |
78946b9b | 2483 | public function layout_file($pagelayout) { |
ebebf55c | 2484 | global $CFG; |
fdeb7fa1 | 2485 | |
78946b9b PS |
2486 | $layoutinfo = $this->layout_info_for_page($pagelayout); |
2487 | $layoutfile = $layoutinfo['file']; | |
fdeb7fa1 | 2488 | |
84db3ea2 SH |
2489 | if (array_key_exists('theme', $layoutinfo)) { |
2490 | $themes = array($layoutinfo['theme']); | |
2491 | } else { | |
2492 | $themes = array_merge(array($this->name),$this->parents); | |
2493 | } | |
4399b9d5 | 2494 | |
84db3ea2 SH |
2495 | foreach ($themes as $theme) { |
2496 | if ($dir = $this->find_theme_location($theme)) { | |
2497 | $path = "$dir/layout/$layoutfile"; | |
2498 | ||
2499 | // Check the template exists, return general base theme template if not. | |
2500 | if (is_readable($path)) { | |
2501 | return $path; | |
2502 | } | |
78946b9b | 2503 | } |
34a2777c | 2504 | } |
2505 | ||
191b267b | 2506 | debugging('Can not find layout file for: ' . $pagelayout); |
78946b9b PS |
2507 | // fallback to standard normal layout |
2508 | return "$CFG->dirroot/theme/base/layout/general.php"; | |
2509 | } | |
ebebf55c | 2510 | |
78946b9b PS |
2511 | /** |
2512 | * Returns auxiliary page layout options specified in layout configuration array. | |
473dd811 | 2513 | * |
78946b9b PS |
2514 | * @param string $pagelayout |
2515 | * @return array | |
2516 | */ | |
2517 | public function pagelayout_options($pagelayout) { | |
2518 | $info = $this->layout_info_for_page($pagelayout); | |
2519 | if (!empty($info['options'])) { | |
2520 | return $info['options']; | |
34a2777c | 2521 | } |
78946b9b PS |
2522 | return array(); |
2523 | } | |
34a2777c | 2524 | |
78946b9b PS |
2525 | /** |
2526 | * Inform a block_manager about the block regions this theme wants on this | |
2527 | * page layout. | |
473dd811 | 2528 | * |
78946b9b PS |
2529 | * @param string $pagelayout the general type of the page. |
2530 | * @param block_manager $blockmanager the block_manger to set up. | |
78946b9b PS |
2531 | */ |
2532 | public function setup_blocks($pagelayout, $blockmanager) { | |
2533 | $layoutinfo = $this->layout_info_for_page($pagelayout); | |
2534 | if (!empty($layoutinfo['regions'])) { | |
292dcf04 | 2535 | $blockmanager->add_regions($layoutinfo['regions'], false); |
78946b9b | 2536 | $blockmanager->set_default_region($layoutinfo['defaultregion']); |
ebebf55c | 2537 | } |
34a2777c | 2538 | } |
2539 | ||
473dd811 SH |
2540 | /** |
2541 | * Gets the visible name for the requested block region. | |
2542 | * | |
2543 | * @param string $region The region name to get | |
2544 | * @param string $theme The theme the region belongs to (may come from the parent theme) | |
2545 | * @return string | |
2546 | */ | |
cadc0d28 TH |
2547 | protected function get_region_name($region, $theme) { |
2548 | $regionstring = get_string('region-' . $region, 'theme_' . $theme); | |
2549 | // A name exists in this theme, so use it | |
2550 | if (substr($regionstring, 0, 1) != '[') { | |
2551 | return $regionstring; | |
2552 | } | |
2553 | ||
2554 | // Otherwise, try to find one elsewhere | |
2555 | // Check parents, if any | |
2556 | foreach ($this->parents as $parentthemename) { | |
2557 | $regionstring = get_string('region-' . $region, 'theme_' . $parentthemename); | |
2558 | if (substr($regionstring, 0, 1) != '[') { | |
2559 | return $regionstring; | |
2560 | } | |
2561 | } | |
2562 | ||
a5a6df54 MM |
2563 | // Last resort, try the boost theme for names |
2564 | return get_string('region-' . $region, 'theme_boost'); | |
cadc0d28 TH |
2565 | } |
2566 | ||
ebebf55c | 2567 | /** |
78946b9b | 2568 | * Get the list of all block regions known to this theme in all templates. |
473dd811 | 2569 | * |
78946b9b | 2570 | * @return array internal region name => human readable name. |
ebebf55c | 2571 | */ |
78946b9b PS |
2572 | public function get_all_block_regions() { |
2573 | $regions = array(); | |
78946b9b PS |
2574 | foreach ($this->layouts as $layoutinfo) { |
2575 | foreach ($layoutinfo['regions'] as $region) { | |
a9535f79 | 2576 | $regions[$region] = $this->get_region_name($region, $this->name); |
ebebf55c | 2577 | } |
34a2777c | 2578 | } |
78946b9b | 2579 | return $regions; |
34a2777c | 2580 | } |
d609d962 SH |
2581 | |
2582 | /** | |
2583 | * Returns the human readable name of the theme | |
2584 | * | |
2585 | * @return string | |
2586 | */ | |
2587 | public function get_theme_name() { | |
2588 | return get_string('pluginname', 'theme_'.$this->name); | |
2589 | } | |
225c418f SH |
2590 | |
2591 | /** | |
2592 | * Returns the block render method. | |
2593 | * | |
2594 | * It is set by the theme via: | |
2595 | * $THEME->blockrendermethod = '...'; | |
2596 | * | |
2597 | * It can be one of two values, blocks or blocks_for_region. | |
2598 | * It should be set to the method being used by the theme layouts. | |
2599 | * | |
2600 | * @return string | |
2601 | */ | |
2602 | public function get_block_render_method() { | |
2603 | if ($this->blockrendermethod) { | |
2604 | // Return the specified block render method. | |
2605 | return $this->blockrendermethod; | |
2606 | } | |
2607 | // Its not explicitly set, check the parent theme configs. | |
2608 | foreach ($this->parent_configs as $config) { | |
2609 | if (isset($config->blockrendermethod)) { | |
2610 | return $config->blockrendermethod; | |
2611 | } | |
2612 | } | |
2613 | // Default it to blocks. | |
2614 | return 'blocks'; | |
2615 | } | |
493ef66e FM |
2616 | |
2617 | /** | |
2618 | * Get the callable for CSS tree post processing. | |
2619 | * | |
2620 | * @return string|null | |
2621 | */ | |
2622 | public function get_css_tree_post_processor() { | |
2623 | $configs = [$this] + $this->parent_configs; | |
2624 | foreach ($configs as $config) { | |
755b013f | 2625 | if (!empty($config->csstreepostprocessor) && is_callable($config->csstreepostprocessor)) { |
493ef66e FM |
2626 | return $config->csstreepostprocessor; |
2627 | } | |
2628 | } | |
2629 | return null; | |
2630 | } | |
a26ce248 | 2631 | |
ebebf55c | 2632 | } |
34a2777c | 2633 | |
ebebf55c | 2634 | /** |
d9c8f425 | 2635 | * This class keeps track of which HTML tags are currently open. |
2636 | * | |
2637 | * This makes it much easier to always generate well formed XHTML output, even | |
2638 | * if execution terminates abruptly. Any time you output some opening HTML | |
2639 | * without the matching closing HTML, you should push the necessary close tags | |
2640 | * onto the stack. | |
ebebf55c | 2641 | * |
2642 | * @copyright 2009 Tim Hunt | |
473dd811 SH |
2643 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
2644 | * @since Moodle 2.0 | |
f8129210 | 2645 | * @package core |
76be40cc | 2646 | * @category output |
ebebf55c | 2647 | */ |
d9c8f425 | 2648 | class xhtml_container_stack { |
473dd811 SH |
2649 | |
2650 | /** | |
76be40cc | 2651 | * @var array Stores the list of open containers. |
473dd811 | 2652 | */ |
d9c8f425 | 2653 | protected $opencontainers = array(); |
473dd811 | 2654 | |
fa1afe32 | 2655 | /** |
76be40cc | 2656 | * @var array In developer debug mode, stores a stack trace of all opens and |
d9c8f425 | 2657 | * closes, so we can output helpful error messages when there is a mismatch. |
fa1afe32 | 2658 | */ |
d9c8f425 | 2659 | protected $log = array(); |
473dd811 | 2660 | |
fa1afe32 | 2661 | /** |
76be40cc SH |
2662 | * @var boolean Store whether we are developer debug mode. We need this in |
2663 | * several places including in the destructor where we may not have access to $CFG. | |
fa1afe32 | 2664 | */ |
d9c8f425 | 2665 | protected $isdebugging; |
34a2777c | 2666 | |
473dd811 SH |
2667 | /** |
2668 | * Constructor | |
2669 | */ | |
d9c8f425 | 2670 | public function __construct() { |
96f81ea3 PS |
2671 | global $CFG; |
2672 | $this->isdebugging = $CFG->debugdeveloper; | |
ebebf55c | 2673 | } |
34a2777c | 2674 | |
fa1afe32 | 2675 | /** |
d9c8f425 | 2676 | * Push the close HTML for a recently opened container onto the stack. |
473dd811 | 2677 | * |
d9c8f425 | 2678 | * @param string $type The type of container. This is checked when {@link pop()} |
2679 | * is called and must match, otherwise a developer debug warning is output. | |
2680 | * @param string $closehtml The HTML required to close the container. | |
fa1afe32 | 2681 | */ |
d9c8f425 | 2682 | public function push($type, $closehtml) { |
2683 | $container = new stdClass; | |
2684 | $container->type = $type; | |
2685 | $container->closehtml = $closehtml; | |
2686 | if ($this->isdebugging) { | |
2687 | $this->log('Open', $type); | |
3aaa27f4 | 2688 | } |
d9c8f425 | 2689 | array_push($this->opencontainers, $container); |
ebebf55c | 2690 | } |
34a2777c | 2691 | |
fa1afe32 | 2692 | /** |
d9c8f425 | 2693 | * Pop the HTML for the next closing container from the stack. The $type |
2694 | * must match the type passed when the container was opened, otherwise a | |
2695 | * warning will be output. | |
473dd811 | 2696 | * |
d9c8f425 | 2697 | * @param string $type The type of container. |
2698 | * @return string the HTML required to close the container. | |
fa1afe32 | 2699 | */ |
d9c8f425 | 2700 | public function pop($type) { |
2701 | if (empty($this->opencontainers)) { | |
2702 | debugging('<p>There are no more open containers. This suggests there is a nesting problem.</p>' . | |
2703 | $this->output_log(), DEBUG_DEVELOPER); | |
2704 | return; | |
3aaa27f4 | 2705 | } |
ebebf55c | 2706 | |
d9c8f425 | 2707 | $container = array_pop($this->opencontainers); |
2708 | if ($container->type != $type) { | |
2709 | debugging('<p>The type of container to be closed (' . $container->type . | |
2710 | ') does not match the type of the next open container (' . $type . | |
2711 | '). This suggests there is a nesting problem.</p>' . | |
2712 | $this->output_log(), DEBUG_DEVELOPER); | |
ebebf55c | 2713 | } |
d9c8f425 | 2714 | if ($this->isdebugging) { |
2715 | $this->log('Close', $type); | |
e8775320 | 2716 | } |
d9c8f425 | 2717 | return $container->closehtml; |
ebebf55c | 2718 | } |
e8775320 | 2719 | |
fa1afe32 | 2720 | /** |
d9c8f425 | 2721 | * Close all but the last open container. This is useful in places like error |
2722 | * handling, where you want to close all the open containers (apart from <body>) | |
2723 | * before outputting the error message. | |
473dd811 | 2724 | * |
d9c8f425 | 2725 | * @param bool $shouldbenone assert that the stack should be empty now - causes a |
2726 | * developer debug warning if it isn't. | |
2727 | * @return string the HTML required to close any open containers inside <body>. | |
fa1afe32 | 2728 | */ |
d9c8f425 | 2729 | public function pop_all_but_last($shouldbenone = false) { |
2730 | if ($shouldbenone && count($this->opencontainers) != 1) { | |
2731 | debugging('<p>Some HTML tags were opened in the body of the page but not closed.</p>' . | |
2732 | $this->output_log(), DEBUG_DEVELOPER); | |
2733 | } | |
2734 | $output = ''; | |
2735 | while (count($this->opencontainers) > 1) { | |
2736 | $container = array_pop($this->opencontainers); | |
2737 | $output .= $container->closehtml; | |
e8775320 | 2738 | } |
d9c8f425 | 2739 | return $output; |
e8775320 | 2740 | } |
34a2777c | 2741 | |
ebebf55c | 2742 | /** |
d9c8f425 | 2743 | * You can call this function if you want to throw away an instance of this |
2744 | * class without properly emptying the stack (for example, in a unit test). | |
2745 | * Calling this method stops the destruct method from outputting a developer | |
2746 | * debug warning. After calling this method, the instance can no longer be used. | |
ebebf55c | 2747 | */ |
d9c8f425 | 2748 | public function discard() { |
2749 | $this->opencontainers = null; | |
ebebf55c | 2750 | } |
d9c8f425 | 2751 | |
fa1afe32 | 2752 | /** |
d9c8f425 | 2753 | * Adds an entry to the log. |
473dd811 | 2754 | * |
d9c8f425 | 2755 | * @param string $action The name of the action |
2756 | * @param string $type The type of action | |
fa1afe32 | 2757 | */ |
d9c8f425 | 2758 | protected function log($action, $type) { |
2759 | $this->log[] = '<li>' . $action . ' ' . $type . ' at:' . | |
2760 | format_backtrace(debug_backtrace()) . '</li>'; | |
ebebf55c | 2761 | } |
34a2777c | 2762 | |
fa1afe32 | 2763 | /** |
d9c8f425 | 2764 | * Outputs the log's contents as a HTML list. |
473dd811 | 2765 | * |
d9c8f425 | 2766 | * @return string HTML list of the log |
fa1afe32 | 2767 | */ |
d9c8f425 | 2768 | protected function output_log() { |
2769 | return '<ul>' . implode("\n", $this->log) . '</ul>'; | |
34a2777c | 2770 | } |
6fc267a0 | 2771 | } |