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