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