MDL-32683 use slashargument urls for theme images
[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
SH
39 *
40 * This method deletes the phsyical directory that is used to cache the theme
41 * files used for serving.
3d3fae72 42 * Because it deletes the main theme cache directoy all themes are reset by
473dd811 43 * this function.
78946b9b
PS
44 */
45function theme_reset_all_caches() {
46 global $CFG;
47 require_once("$CFG->libdir/filelib.php");
48
49 set_config('themerev', empty($CFG->themerev) ? 1 : $CFG->themerev+1);
365bec4c 50 fulldelete("$CFG->cachedir/theme");
78946b9b
PS
51}
52
53/**
54 * Enable or disable theme designer mode.
473dd811 55 *
78946b9b 56 * @param bool $state
78946b9b
PS
57 */
58function theme_set_designer_mod($state) {
59 theme_reset_all_caches();
60 set_config('themedesignermode', (int)!empty($state));
61}
62
63/**
64 * Returns current theme revision number.
473dd811 65 *
78946b9b 66 * @return int
571fa828 67 */
78946b9b
PS
68function theme_get_revision() {
69 global $CFG;
70
71 if (empty($CFG->themedesignermode)) {
72 if (empty($CFG->themerev)) {
73 return -1;
74 } else {
75 return $CFG->themerev;
76 }
77
78 } else {
79 return -1;
80 }
81}
82
571fa828 83
571fa828 84/**
fdeb7fa1 85 * This class represents the configuration variables of a Moodle theme.
86 *
87 * All the variables with access: public below (with a few exceptions that are marked)
3d3fae72 88 * are the properties you can set in your themes config.php file.
fdeb7fa1 89 *
90 * There are also some methods and protected variables that are part of the inner
3d3fae72 91 * workings of Moodle's themes system. If you are just editing a themes config.php
fa1afe32 92 * file, you can just ignore those, and the following information for developers.
ebebf55c 93 *
94 * Normally, to create an instance of this class, you should use the
95 * {@link theme_config::load()} factory method to load a themes config.php file.
fa1afe32 96 * However, normally you don't need to bother, because moodle_page (that is, $PAGE)
fdeb7fa1 97 * will create one for you, accessible as $PAGE->theme.
571fa828 98 *
99 * @copyright 2009 Tim Hunt
473dd811
SH
100 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
101 * @since Moodle 2.0
f8129210 102 * @package core
76be40cc 103 * @category output
571fa828 104 */
ebebf55c 105class theme_config {
473dd811 106
5f0baa43 107 /**
76be40cc 108 * @var string Default theme, used when requested theme not found.
5f0baa43 109 */
90723839 110 const DEFAULT_THEME = 'standard';
5f0baa43 111
ebebf55c 112 /**
76be40cc 113 * @var array You can base your theme on other themes by linking to the other theme as
78946b9b 114 * parents. This lets you use the CSS and layouts from the other themes
f8129210 115 * (see {@link theme_config::$layouts}).
fdeb7fa1 116 * That makes it easy to create a new theme that is similar to another one
3d3fae72 117 * but with a few changes. In this themes CSS you only need to override
fdeb7fa1 118 * those rules you want to change.
fdeb7fa1 119 */
78946b9b 120 public $parents;
571fa828 121
fdeb7fa1 122 /**
76be40cc 123 * @var array The names of all the stylesheets from this theme that you would
78946b9b 124 * like included, in order. Give the names of the files without .css.
fdeb7fa1 125 */
78946b9b 126 public $sheets = array();
571fa828 127
fdeb7fa1 128 /**
76be40cc 129 * @var array The names of all the stylesheets from parents that should be excluded.
78946b9b
PS
130 * true value may be used to specify all parents or all themes from one parent.
131 * If no value specified value from parent theme used.
fdeb7fa1 132 */
78946b9b 133 public $parents_exclude_sheets = null;
571fa828 134
fdeb7fa1 135 /**
76be40cc 136 * @var array List of plugin sheets to be excluded.
78946b9b 137 * If no value specified value from parent theme used.
fdeb7fa1 138 */
78946b9b 139 public $plugins_exclude_sheets = null;
571fa828 140
fdeb7fa1 141 /**
76be40cc 142 * @var array List of style sheets that are included in the text editor bodies.
78946b9b 143 * Sheets from parent themes are used automatically and can not be excluded.
fdeb7fa1 144 */
78946b9b 145 public $editor_sheets = array();
571fa828 146
38aafea2 147 /**
76be40cc 148 * @var array The names of all the javascript files this theme that you would
04c01408 149 * like included from head, in order. Give the names of the files without .js.
38aafea2
PS
150 */
151 public $javascripts = array();
152
04c01408 153 /**
76be40cc 154 * @var array The names of all the javascript files this theme that you would
04c01408 155 * like included from footer, in order. Give the names of the files without .js.
04c01408
PS
156 */
157 public $javascripts_footer = array();
4399b9d5 158
38aafea2 159 /**
76be40cc
SH
160 * @var array The names of all the javascript files from parents that should
161 * be excluded. true value may be used to specify all parents or all themes
162 * from one parent.
38aafea2 163 * If no value specified value from parent theme used.
38aafea2
PS
164 */
165 public $parents_exclude_javascripts = null;
166
fdeb7fa1 167 /**
76be40cc 168 * @var array Which file to use for each page layout.
fdeb7fa1 169 *
78946b9b
PS
170 * This is an array of arrays. The keys of the outer array are the different layouts.
171 * Pages in Moodle are using several different layouts like 'normal', 'course', 'home',
172 * 'popup', 'form', .... The most reliable way to get a complete list is to look at
173 * {@link http://cvs.moodle.org/moodle/theme/base/config.php?view=markup the base theme config.php file}.
d4a03c00 174 * That file also has a good example of how to set this setting.
fdeb7fa1 175 *
78946b9b 176 * For each layout, the value in the outer array is an array that describes
d4a03c00 177 * how you want that type of page to look. For example
178 * <pre>
179 * $THEME->layouts = array(
39df78bf 180 * // Most pages - if we encounter an unknown or a missing page type, this one is used.
191b267b 181 * 'standard' => array(
78946b9b
PS
182 * 'theme' = 'mytheme',
183 * 'file' => 'normal.php',
d4a03c00 184 * 'regions' => array('side-pre', 'side-post'),
185 * 'defaultregion' => 'side-post'
186 * ),
187 * // The site home page.
188 * 'home' => array(
78946b9b
PS
189 * 'theme' = 'mytheme',
190 * 'file' => 'home.php',
d4a03c00 191 * 'regions' => array('side-pre', 'side-post'),
192 * 'defaultregion' => 'side-post'
193 * ),
194 * // ...
195 * );
196 * </pre>
fdeb7fa1 197 *
78946b9b
PS
198 * 'theme' name of the theme where is the layout located
199 * 'file' is the layout file to use for this type of page.
200 * layout files are stored in layout subfolder
d4a03c00 201 * 'regions' This lists the regions on the page where blocks may appear. For
202 * each region you list here, your layout file must include a call to
203 * <pre>
204 * echo $OUTPUT->blocks_for_region($regionname);
205 * </pre>
206 * or equivalent so that the blocks are actually visible.
fdeb7fa1 207 *
d4a03c00 208 * 'defaultregion' If the list of regions is non-empty, then you must pick
209 * one of the one of them as 'default'. This has two meanings. First, this is
210 * where new blocks are added. Second, if there are any blocks associated with
fa1afe32 211 * the page, but in non-existent regions, they appear here. (Imaging, for example,
d4a03c00 212 * that someone added blocks using a different theme that used different region
213 * names, and then switched to this theme.)
fdeb7fa1 214 */
d4a03c00 215 public $layouts = array();
571fa828 216
fdeb7fa1 217 /**
76be40cc
SH
218 * @var string Name of the renderer factory class to use. Must implement the
219 * {@link renderer_factory} interface.
fdeb7fa1 220 *
221 * This is an advanced feature. Moodle output is generated by 'renderers',
222 * you can customise the HTML that is output by writing custom renderers,
223 * and then you need to specify 'renderer factory' so that Moodle can find
224 * your renderers.
225 *
226 * There are some renderer factories supplied with Moodle. Please follow these
227 * links to see what they do.
228 * <ul>
229 * <li>{@link standard_renderer_factory} - the default.</li>
230 * <li>{@link theme_overridden_renderer_factory} - use this if you want to write
78946b9b 231 * your own custom renderers in a lib.php file in this theme (or the parent theme).</li>
fdeb7fa1 232 * </ul>
fdeb7fa1 233 */
ebebf55c 234 public $rendererfactory = 'standard_renderer_factory';
ebebf55c 235
fdeb7fa1 236 /**
76be40cc 237 * @var string Function to do custom CSS post-processing.
fdeb7fa1 238 *
78946b9b
PS
239 * This is an advanced feature. If you want to do custom post-processing on the
240 * CSS before it is output (for example, to replace certain variable names
241 * with particular values) you can give the name of a function here.
fdeb7fa1 242 */
78946b9b 243 public $csspostprocess = null;
571fa828 244
245 /**
76be40cc 246 * @var string Accessibility: Right arrow-like character is
78946b9b
PS
247 * used in the breadcrumb trail, course navigation menu
248 * (previous/next activity), calendar, and search forum block.
249 * If the theme does not set characters, appropriate defaults
250 * are set automatically. Please DO NOT
251 * use &lt; &gt; &raquo; - these are confusing for blind users.
78946b9b
PS
252 */
253 public $rarrow = null;
254
255 /**
76be40cc 256 * @var string Accessibility: Right arrow-like character is
78946b9b
PS
257 * used in the breadcrumb trail, course navigation menu
258 * (previous/next activity), calendar, and search forum block.
259 * If the theme does not set characters, appropriate defaults
260 * are set automatically. Please DO NOT
261 * use &lt; &gt; &raquo; - these are confusing for blind users.
571fa828 262 */
78946b9b
PS
263 public $larrow = null;
264
03276af6 265 /**
76be40cc 266 * @var bool Some themes may want to disable ajax course editing.
03276af6
DM
267 */
268 public $enablecourseajax = true;
78946b9b
PS
269
270 //==Following properties are not configurable from theme config.php==
571fa828 271
fdeb7fa1 272 /**
76be40cc 273 * @var string The name of this theme. Set automatically when this theme is
78946b9b 274 * loaded. This can not be set in theme config.php
fdeb7fa1 275 */
78946b9b 276 public $name;
fdeb7fa1 277
278 /**
76be40cc 279 * @var string The folder where this themes files are stored. This is set
78946b9b 280 * automatically. This can not be set in theme config.php
fdeb7fa1 281 */
78946b9b 282 public $dir;
fdeb7fa1 283
284 /**
76be40cc 285 * @var stdClass Theme settings stored in config_plugins table.
78946b9b 286 * This can not be set in theme config.php
78946b9b
PS
287 */
288 public $setting = null;
289
d2c394f3 290 /**
76be40cc 291 * @var bool If set to true and the theme enables the dock then blocks will be able
d2c394f3 292 * to be moved to the special dock
d2c394f3
SH
293 */
294 public $enable_dock = false;
295
4d56ee95 296 /**
76be40cc 297 * @var bool If set to true then this theme will not be shown in the theme selector unless
4d56ee95 298 * theme designer mode is turned on.
4d56ee95
SH
299 */
300 public $hidefromselector = false;
301
78946b9b 302 /**
76be40cc 303 * @var renderer_factory Instance of the renderer_factory implementation
fdeb7fa1 304 * we are using. Implementation detail.
305 */
306 protected $rf = null;
307
308 /**
76be40cc 309 * @var array List of parent config objects.
78946b9b
PS
310 **/
311 protected $parent_configs = array();
fdeb7fa1 312
571fa828 313 /**
ebebf55c 314 * Load the config.php file for a particular theme, and return an instance
315 * of this class. (That is, this is a factory method.)
316 *
317 * @param string $themename the name of the theme.
318 * @return theme_config an instance of this class.
571fa828 319 */
ebebf55c 320 public static function load($themename) {
321 global $CFG;
571fa828 322
78946b9b
PS
323 // load theme settings from db
324 try {
325 $settings = get_config('theme_'.$themename);
326 } catch (dml_exception $e) {
da0c0e25 327 // most probably moodle tables not created yet
365a5941 328 $settings = new stdClass();
78946b9b 329 }
ebebf55c 330
78946b9b
PS
331 if ($config = theme_config::find_theme_config($themename, $settings)) {
332 return new theme_config($config);
5f0baa43
PS
333
334 } else if ($themename == theme_config::DEFAULT_THEME) {
335 throw new coding_exception('Default theme '.theme_config::DEFAULT_THEME.' not available or broken!');
336
78946b9b
PS
337 } else {
338 // bad luck, the requested theme has some problems - admin see details in theme config
5f0baa43 339 return new theme_config(theme_config::find_theme_config(theme_config::DEFAULT_THEME, $settings));
78946b9b
PS
340 }
341 }
ebebf55c 342
78946b9b
PS
343 /**
344 * Theme diagnostic code. It is very problematic to send debug output
345 * to the actual CSS file, instead this functions is supposed to
ae5b6801 346 * diagnose given theme and highlights all potential problems.
78946b9b
PS
347 * This information should be available from the theme selection page
348 * or some other debug page for theme designers.
349 *
350 * @param string $themename
351 * @return array description of problems
352 */
353 public static function diagnose($themename) {
5ef719e7 354 //TODO: MDL-21108
78946b9b
PS
355 return array();
356 }
357
358 /**
359 * Private constructor, can be called only from the factory method.
360 * @param stdClass $config
361 */
362 private function __construct($config) {
363 global $CFG; //needed for included lib.php files
364
365 $this->settings = $config->settings;
366 $this->name = $config->name;
367 $this->dir = $config->dir;
368
369 if ($this->name != 'base') {
370 $baseconfig = theme_config::find_theme_config('base', $this->settings);
371 } else {
372 $baseconfig = $config;
571fa828 373 }
ebebf55c 374
fef27578 375 $configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts', 'javascripts_footer',
fcd2cbaf
PS
376 'parents_exclude_javascripts', 'layouts', 'enable_dock', 'enablecourseajax',
377 'rendererfactory', 'csspostprocess', 'editor_sheets', 'rarrow', 'larrow', 'hidefromselector');
ebebf55c 378
78946b9b
PS
379 foreach ($config as $key=>$value) {
380 if (in_array($key, $configurable)) {
381 $this->$key = $value;
382 }
383 }
384
385 // verify all parents and load configs and renderers
386 foreach ($this->parents as $parent) {
387 if ($parent == 'base') {
388 $parent_config = $baseconfig;
389 } else if (!$parent_config = theme_config::find_theme_config($parent, $this->settings)) {
390 // this is not good - better exclude faulty parents
391 continue;
392 }
393 $libfile = $parent_config->dir.'/lib.php';
394 if (is_readable($libfile)) {
395 // theme may store various function here
396 include_once($libfile);
397 }
398 $renderersfile = $parent_config->dir.'/renderers.php';
399 if (is_readable($renderersfile)) {
400 // may contain core and plugin renderers and renderer factory
401 include_once($renderersfile);
402 }
403 $this->parent_configs[$parent] = $parent_config;
404 $rendererfile = $parent_config->dir.'/renderers.php';
405 if (is_readable($rendererfile)) {
406 // may contain core and plugin renderers and renderer factory
407 include_once($rendererfile);
408 }
409 }
410 $libfile = $this->dir.'/lib.php';
411 if (is_readable($libfile)) {
412 // theme may store various function here
413 include_once($libfile);
414 }
415 $rendererfile = $this->dir.'/renderers.php';
416 if (is_readable($rendererfile)) {
417 // may contain core and plugin renderers and renderer factory
418 include_once($rendererfile);
6fc267a0
BJ
419 } else {
420 // check if renderers.php file is missnamed renderer.php
421 if (is_readable($this->dir.'/renderer.php')) {
422 debugging('Developer hint: '.$this->dir.'/renderer.php should be renamed to ' . $this->dir."/renderers.php.
11c0ecce 423 See: http://docs.moodle.org/dev/Output_renderers#Theme_renderers.", DEBUG_DEVELOPER);
6fc267a0 424 }
78946b9b 425 }
4399b9d5 426
78946b9b
PS
427 // cascade all layouts properly
428 foreach ($baseconfig->layouts as $layout=>$value) {
429 if (!isset($this->layouts[$layout])) {
430 foreach ($this->parent_configs as $parent_config) {
431 if (isset($parent_config->layouts[$layout])) {
432 $this->layouts[$layout] = $parent_config->layouts[$layout];
433 continue 2;
434 }
435 }
436 $this->layouts[$layout] = $value;
437 }
438 }
439
440 //fix arrows if needed
441 $this->check_theme_arrows();
571fa828 442 }
443
f8129210 444 /**
78946b9b
PS
445 * Checks if arrows $THEME->rarrow, $THEME->larrow have been set (theme/-/config.php).
446 * If not it applies sensible defaults.
447 *
448 * Accessibility: right and left arrow Unicode characters for breadcrumb, calendar,
449 * search forum block, etc. Important: these are 'silent' in a screen-reader
450 * (unlike &gt; &raquo;), and must be accompanied by text.
34a2777c 451 */
78946b9b
PS
452 private function check_theme_arrows() {
453 if (!isset($this->rarrow) and !isset($this->larrow)) {
454 // Default, looks good in Win XP/IE 6, Win/Firefox 1.5, Win/Netscape 8...
455 // Also OK in Win 9x/2K/IE 5.x
456 $this->rarrow = '&#x25BA;';
457 $this->larrow = '&#x25C4;';
458 if (empty($_SERVER['HTTP_USER_AGENT'])) {
459 $uagent = '';
ebebf55c 460 } else {
78946b9b
PS
461 $uagent = $_SERVER['HTTP_USER_AGENT'];
462 }
463 if (false !== strpos($uagent, 'Opera')
464 || false !== strpos($uagent, 'Mac')) {
465 // Looks good in Win XP/Mac/Opera 8/9, Mac/Firefox 2, Camino, Safari.
466 // Not broken in Mac/IE 5, Mac/Netscape 7 (?).
467 $this->rarrow = '&#x25B6;';
468 $this->larrow = '&#x25C0;';
469 }
470 elseif (false !== strpos($uagent, 'Konqueror')) {
471 $this->rarrow = '&rarr;';
472 $this->larrow = '&larr;';
473 }
474 elseif (isset($_SERVER['HTTP_ACCEPT_CHARSET'])
475 && false === stripos($_SERVER['HTTP_ACCEPT_CHARSET'], 'utf-8')) {
476 // (Win/IE 5 doesn't set ACCEPT_CHARSET, but handles Unicode.)
477 // To be safe, non-Unicode browsers!
478 $this->rarrow = '&gt;';
479 $this->larrow = '&lt;';
480 }
481
f8129210 482 // RTL support - in RTL languages, swap r and l arrows
78946b9b
PS
483 if (right_to_left()) {
484 $t = $this->rarrow;
485 $this->rarrow = $this->larrow;
486 $this->larrow = $t;
ebebf55c 487 }
ebebf55c 488 }
78946b9b 489 }
ebebf55c 490
78946b9b
PS
491 /**
492 * Returns output renderer prefixes, these are used when looking
71c03ac1 493 * for the overridden renderers in themes.
473dd811 494 *
78946b9b
PS
495 * @return array
496 */
497 public function renderer_prefixes() {
498 global $CFG; // just in case the included files need it
499
596af93a 500 $prefixes = array('theme_'.$this->name);
78946b9b
PS
501
502 foreach ($this->parent_configs as $parent) {
503 $prefixes[] = 'theme_'.$parent->name;
504 }
505
506 return $prefixes;
34a2777c 507 }
508
571fa828 509 /**
78946b9b 510 * Returns the stylesheet URL of this editor content
473dd811 511 *
78946b9b
PS
512 * @param bool $encoded false means use & and true use &amp; in URLs
513 * @return string
571fa828 514 */
78946b9b
PS
515 public function editor_css_url($encoded=true) {
516 global $CFG;
517
518 $rev = theme_get_revision();
519
520 if ($rev > -1) {
521 $params = array('theme'=>$this->name,'rev'=>$rev, 'type'=>'editor');
522 return new moodle_url($CFG->httpswwwroot.'/theme/styles.php', $params);
523 } else {
524 $params = array('theme'=>$this->name, 'type'=>'editor');
525 return new moodle_url($CFG->httpswwwroot.'/theme/styles_debug.php', $params);
571fa828 526 }
571fa828 527 }
528
529 /**
78946b9b 530 * Returns the content of the CSS to be used in editor content
473dd811 531 *
78946b9b 532 * @return string
571fa828 533 */
71be124d 534 public function editor_css_files() {
78946b9b
PS
535 global $CFG;
536
71be124d 537 $files = array();
78946b9b
PS
538
539 // first editor plugins
540 $plugins = get_plugin_list('editor');
541 foreach ($plugins as $plugin=>$fulldir) {
542 $sheetfile = "$fulldir/editor_styles.css";
543 if (is_readable($sheetfile)) {
71be124d 544 $files['plugin_'.$plugin] = $sheetfile;
78946b9b
PS
545 }
546 }
547 // then parent themes
548 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
549 if (empty($parent_config->editor_sheets)) {
550 continue;
551 }
552 foreach ($parent_config->editor_sheets as $sheet) {
ca194849 553 $sheetfile = "$parent_config->dir/style/$sheet.css";
78946b9b 554 if (is_readable($sheetfile)) {
71be124d 555 $files['parent_'.$parent_config->name.'_'.$sheet] = $sheetfile;
78946b9b
PS
556 }
557 }
558 }
559 // finally this theme
560 if (!empty($this->editor_sheets)) {
561 foreach ($this->editor_sheets as $sheet) {
71be124d 562 $sheetfile = "$this->dir/style/$sheet.css";
78946b9b 563 if (is_readable($sheetfile)) {
71be124d 564 $files['theme_'.$sheet] = $sheetfile;
78946b9b
PS
565 }
566 }
567 }
568
71be124d 569 return $files;
571fa828 570 }
571
572 /**
78946b9b 573 * Get the stylesheet URL of this theme
473dd811
SH
574 *
575 * @param moodle_page $page Not used... deprecated?
38aafea2 576 * @return array of moodle_url
571fa828 577 */
efaa4c08 578 public function css_urls(moodle_page $page) {
78946b9b
PS
579 global $CFG;
580
581 $rev = theme_get_revision();
582
efaa4c08 583 $urls = array();
efaa4c08 584
78946b9b 585 if ($rev > -1) {
a6338a13 586 if (check_browser_version('MSIE', 5)) {
537740cb
SH
587 // We need to split the CSS files for IE
588 $urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev, 'type'=>'plugins'));
589 $urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev, 'type'=>'parents'));
590 $urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev, 'type'=>'theme'));
591 } else {
592 $urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev));
78946b9b 593 }
78946b9b 594 } else {
7829cf79
PS
595 // find out the current CSS and cache it now for 5 seconds
596 // the point is to construct the CSS only once and pass it through the
597 // dataroot to the script that actually serves the sheets
d6b64d15
PS
598 if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
599 define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
600 }
365bec4c 601 $candidatesheet = "$CFG->cachedir/theme/$this->name/designer.ser";
7829cf79
PS
602 if (!file_exists($candidatesheet)) {
603 $css = $this->css_content();
c426ef3a 604 check_dir_exists(dirname($candidatesheet));
7829cf79
PS
605 file_put_contents($candidatesheet, serialize($css));
606
d6b64d15 607 } else if (filemtime($candidatesheet) > time() - THEME_DESIGNER_CACHE_LIFETIME) {
7829cf79
PS
608 if ($css = file_get_contents($candidatesheet)) {
609 $css = unserialize($css);
610 } else {
611 unlink($candidatesheet);
612 $css = $this->css_content();
613 }
614
615 } else {
616 unlink($candidatesheet);
617 $css = $this->css_content();
618 file_put_contents($candidatesheet, serialize($css));
619 }
620
c724f835 621 $baseurl = $CFG->httpswwwroot.'/theme/styles_debug.php';
c09e1a2c
PS
622
623 if (check_browser_version('MSIE', 5)) {
624 // lalala, IE does not allow more than 31 linked CSS files from main document
71be124d 625 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'plugins'));
a6338a13
SH
626 foreach ($css['parents'] as $parent=>$sheets) {
627 // We need to serve parents individually otherwise we may easily exceed the style limit IE imposes (4096)
71be124d 628 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'ie', 'subtype'=>'parents', 'sheet'=>$parent));
a6338a13 629 }
71be124d 630 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'theme'));
c09e1a2c
PS
631
632 } else {
633 foreach ($css['plugins'] as $plugin=>$unused) {
634 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'plugin', 'subtype'=>$plugin));
635 }
636 foreach ($css['parents'] as $parent=>$sheets) {
637 foreach ($sheets as $sheet=>$unused2) {
638 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'parent', 'subtype'=>$parent, 'sheet'=>$sheet));
639 }
640 }
641 foreach ($css['theme'] as $sheet=>$unused) {
642 $urls[] = new moodle_url($baseurl, array('sheet'=>$sheet, 'theme'=>$this->name, 'type'=>'theme')); // sheet first in order to make long urls easier to read
78946b9b 643 }
78946b9b 644 }
78946b9b 645 }
efaa4c08
PS
646
647 return $urls;
571fa828 648 }
34a2777c 649
ebebf55c 650 /**
045f492c 651 * Returns an array of organised CSS files required for this output
473dd811 652 *
045f492c 653 * @return array
ebebf55c 654 */
045f492c
SH
655 public function css_files() {
656 $cssfiles = array('plugins'=>array(), 'parents'=>array(), 'theme'=>array());
78946b9b 657
8c96c3cb 658 // get all plugin sheets
045f492c 659 $excludes = $this->resolve_excludes('plugins_exclude_sheets');
78946b9b
PS
660 if ($excludes !== true) {
661 foreach (get_plugin_types() as $type=>$unused) {
045f492c 662 if ($type === 'theme' || (!empty($excludes[$type]) and $excludes[$type] === true)) {
78946b9b
PS
663 continue;
664 }
665 $plugins = get_plugin_list($type);
666 foreach ($plugins as $plugin=>$fulldir) {
667 if (!empty($excludes[$type]) and is_array($excludes[$type])
668 and in_array($plugin, $excludes[$type])) {
669 continue;
670 }
f8bb9666
SH
671
672 $plugincontent = '';
78946b9b
PS
673 $sheetfile = "$fulldir/styles.css";
674 if (is_readable($sheetfile)) {
045f492c 675 $cssfiles['plugins'][$type.'_'.$plugin] = $sheetfile;
f8bb9666
SH
676 }
677 $sheetthemefile = "$fulldir/styles_{$this->name}.css";
678 if (is_readable($sheetthemefile)) {
045f492c 679 $cssfiles['plugins'][$type.'_'.$plugin.'_'.$this->name] = $sheetthemefile;
f8bb9666 680 }
78946b9b
PS
681 }
682 }
683 }
34a2777c 684
78946b9b 685 // find out wanted parent sheets
045f492c 686 $excludes = $this->resolve_excludes('parents_exclude_sheets');
78946b9b
PS
687 if ($excludes !== true) {
688 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
689 $parent = $parent_config->name;
045f492c 690 if (empty($parent_config->sheets) || (!empty($excludes[$parent]) and $excludes[$parent] === true)) {
78946b9b
PS
691 continue;
692 }
693 foreach ($parent_config->sheets as $sheet) {
694 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
695 and in_array($sheet, $excludes[$parent])) {
696 continue;
697 }
698 $sheetfile = "$parent_config->dir/style/$sheet.css";
699 if (is_readable($sheetfile)) {
045f492c 700 $cssfiles['parents'][$parent][$sheet] = $sheetfile;
78946b9b
PS
701 }
702 }
703 }
704 }
34a2777c 705
78946b9b
PS
706 // current theme sheets
707 if (is_array($this->sheets)) {
708 foreach ($this->sheets as $sheet) {
709 $sheetfile = "$this->dir/style/$sheet.css";
710 if (is_readable($sheetfile)) {
045f492c 711 $cssfiles['theme'][$sheet] = $sheetfile;
78946b9b 712 }
ebebf55c 713 }
78946b9b
PS
714 }
715
045f492c
SH
716 return $cssfiles;
717 }
718
719 /**
720 * Returns the content of the one huge CSS merged from all style sheets.
473dd811 721 *
045f492c
SH
722 * @return string
723 */
724 public function css_content() {
71be124d
SH
725 $files = array_merge($this->css_files(), array('editor'=>$this->editor_css_files()));
726 $css = $this->css_files_get_contents($files, array());
78946b9b
PS
727 return $css;
728 }
34a2777c 729
045f492c
SH
730 /**
731 * Given an array of file paths or a single file path loads the contents of
732 * the CSS file, processes it then returns it in the same structure it was given.
733 *
f8129210 734 * Can be used recursively on the results of {@link css_files}
045f492c
SH
735 *
736 * @param array|string $file An array of file paths or a single file path
71c03ac1 737 * @param array $keys An array of previous array keys [recursive addition]
045f492c
SH
738 * @return The converted array or the contents of the single file ($file type)
739 */
740 protected function css_files_get_contents($file, array $keys) {
741 if (is_array($file)) {
742 foreach ($file as $key=>$f) {
743 $file[$key] = $this->css_files_get_contents($f, array_merge($keys, array($key)));
744 }
745 return $file;
746 } else {
747 $comment = '/** Path: '.implode(' ', $keys).' **/'."\n";
748 return $comment.$this->post_process(file_get_contents($file));
749 }
750 }
751
e68c5f36
PS
752
753 /**
473dd811
SH
754 * Generate a URL to the file that serves theme JavaScript files.
755 *
71c03ac1 756 * @param bool $inhead true means head url, false means footer
e68c5f36
PS
757 * @return moodle_url
758 */
baeb20bb 759 public function javascript_url($inhead) {
e68c5f36
PS
760 global $CFG;
761
762 $rev = theme_get_revision();
e68c5f36 763 $params = array('theme'=>$this->name,'rev'=>$rev);
baeb20bb
PS
764 $params['type'] = $inhead ? 'head' : 'footer';
765
766 return new moodle_url($CFG->httpswwwroot.'/theme/javascript.php', $params);
e68c5f36
PS
767 }
768
473dd811
SH
769 /**
770 * Get the URL's for the JavaScript files used by this theme.
771 * They won't be served directly, instead they'll be mediated through
772 * theme/javascript.php.
773 *
774 * @param string $type Either javascripts_footer, or javascripts
775 * @return array
776 */
045f492c
SH
777 public function javascript_files($type) {
778 if ($type === 'footer') {
779 $type = 'javascripts_footer';
780 } else {
f856106b 781 $type = 'javascripts';
045f492c 782 }
04c01408 783
358c13cb
PS
784 $js = array();
785 // find out wanted parent javascripts
045f492c 786 $excludes = $this->resolve_excludes('parents_exclude_javascripts');
358c13cb
PS
787 if ($excludes !== true) {
788 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
789 $parent = $parent_config->name;
04c01408 790 if (empty($parent_config->$type)) {
358c13cb
PS
791 continue;
792 }
793 if (!empty($excludes[$parent]) and $excludes[$parent] === true) {
794 continue;
795 }
04c01408 796 foreach ($parent_config->$type as $javascript) {
358c13cb
PS
797 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
798 and in_array($javascript, $excludes[$parent])) {
799 continue;
800 }
801 $javascriptfile = "$parent_config->dir/javascript/$javascript.js";
802 if (is_readable($javascriptfile)) {
045f492c 803 $js[] = $javascriptfile;
358c13cb
PS
804 }
805 }
806 }
807 }
808
809 // current theme javascripts
04c01408
PS
810 if (is_array($this->$type)) {
811 foreach ($this->$type as $javascript) {
358c13cb
PS
812 $javascriptfile = "$this->dir/javascript/$javascript.js";
813 if (is_readable($javascriptfile)) {
045f492c
SH
814 $js[] = $javascriptfile;
815 }
816 }
817 }
818
819 return $js;
820 }
821
822 /**
3d3fae72 823 * Resolves an exclude setting to the themes setting is applicable or the
045f492c
SH
824 * setting of its closest parent.
825 *
826 * @param string $variable The name of the setting the exclude setting to resolve
f8129210 827 * @param string $default
045f492c
SH
828 * @return mixed
829 */
f8129210 830 protected function resolve_excludes($variable, $default = null) {
045f492c
SH
831 $setting = $default;
832 if (is_array($this->{$variable}) or $this->{$variable} === true) {
833 $setting = $this->{$variable};
834 } else {
835 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
836 if (!isset($parent_config->{$variable})) {
837 continue;
838 }
839 if (is_array($parent_config->{$variable}) or $parent_config->{$variable} === true) {
840 $setting = $parent_config->{$variable};
841 break;
358c13cb
PS
842 }
843 }
844 }
045f492c
SH
845 return $setting;
846 }
358c13cb 847
045f492c
SH
848 /**
849 * Returns the content of the one huge javascript file merged from all theme javascript files.
473dd811 850 *
f8129210 851 * @param bool $type
045f492c
SH
852 * @return string
853 */
854 public function javascript_content($type) {
855 $jsfiles = $this->javascript_files($type);
856 $js = '';
857 foreach ($jsfiles as $jsfile) {
858 $js .= file_get_contents($jsfile)."\n";
859 }
860 return $js;
e68c5f36
PS
861 }
862
473dd811
SH
863 /**
864 * Post processes CSS.
865 *
866 * This method post processes all of the CSS before it is served for this theme.
867 * This is done so that things such as image URL's can be swapped in and to
868 * run any specific CSS post process method the theme has requested.
3d3fae72 869 * This allows themes to use CSS settings.
473dd811
SH
870 *
871 * @param string $css The CSS to process.
872 * @return string The processed CSS.
873 */
045f492c 874 public function post_process($css) {
df06c1c4
PS
875 global $CFG;
876
78946b9b
PS
877 // now resolve all image locations
878 if (preg_match_all('/\[\[pix:([a-z_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) {
879 $replaced = array();
880 foreach ($matches as $match) {
881 if (isset($replaced[$match[0]])) {
882 continue;
883 }
884 $replaced[$match[0]] = true;
885 $imagename = $match[2];
886 $component = rtrim($match[1], '|');
b9bc2019 887 $imageurl = $this->pix_url($imagename, $component)->out(false);
df06c1c4 888 // we do not need full url because the image.php is always in the same dir
9d473266 889 $imageurl = preg_replace('|^http.?://[^/]+|', '', $imageurl);
df06c1c4 890 $css = str_replace($match[0], $imageurl, $css);
ebebf55c 891 }
34a2777c 892 }
17a6649b 893
78946b9b
PS
894 // now resolve all theme settings or do any other postprocessing
895 $csspostprocess = $this->csspostprocess;
896 if (function_exists($csspostprocess)) {
897 $css = $csspostprocess($css, $this);
fdeb7fa1 898 }
899
78946b9b 900 return $css;
ebebf55c 901 }
17a6649b 902
ebebf55c 903 /**
78946b9b
PS
904 * Return the URL for an image
905 *
906 * @param string $imagename the name of the icon.
f8129210 907 * @param string $component specification of one plugin like in get_string()
78946b9b 908 * @return moodle_url
fdeb7fa1 909 */
c39e5ba2 910 public function pix_url($imagename, $component) {
fdeb7fa1 911 global $CFG;
78946b9b 912
9d473266
PS
913 $params = array('theme'=>$this->name);
914
915 if (empty($component) or $component === 'moodle' or $component === 'core') {
916 $params['component'] = 'core';
917 } else {
918 $params['component'] = $component;
919 }
78946b9b
PS
920
921 $rev = theme_get_revision();
922 if ($rev != -1) {
923 $params['rev'] = $rev;
924 }
9d473266
PS
925
926 $params['image'] = $imagename;
927
928 if (!empty($CFG->slasharguments) and $rev > 0) {
929 $url = new moodle_url("$CFG->httpswwwroot/theme/image.php");
930 $url->set_slashargument('/'.$params['theme'].'/'.$params['component'].'/'.$params['rev'].'/'.$params['image'], 'noparam', true);
931 } else {
932 $url = new moodle_url("$CFG->httpswwwroot/theme/image.php", $params);
fdeb7fa1 933 }
78946b9b 934
9d473266 935 return $url;
fdeb7fa1 936 }
937
938 /**
78946b9b
PS
939 * Resolves the real image location.
940 * @param string $image name of image, may contain relative path
941 * @param string $component
942 * @return string full file path
fdeb7fa1 943 */
78946b9b
PS
944 public function resolve_image_location($image, $component) {
945 global $CFG;
946
947 if ($component === 'moodle' or $component === 'core' or empty($component)) {
948 if ($imagefile = $this->image_exists("$this->dir/pix_core/$image")) {
949 return $imagefile;
950 }
951 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
952 if ($imagefile = $this->image_exists("$parent_config->dir/pix_core/$image")) {
953 return $imagefile;
954 }
955 }
f930cf6b
EM
956 if ($imagefile = $this->image_exists("$CFG->dataroot/pix/$image")) {
957 return $imagefile;
958 }
78946b9b
PS
959 if ($imagefile = $this->image_exists("$CFG->dirroot/pix/$image")) {
960 return $imagefile;
961 }
962 return null;
963
964 } else if ($component === 'theme') { //exception
965 if ($image === 'favicon') {
966 return "$this->dir/pix/favicon.ico";
967 }
968 if ($imagefile = $this->image_exists("$this->dir/pix/$image")) {
969 return $imagefile;
970 }
971 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
972 if ($imagefile = $this->image_exists("$parent_config->dir/pix/$image")) {
973 return $imagefile;
974 }
975 }
976 return null;
977
78946b9b
PS
978 } else {
979 if (strpos($component, '_') === false) {
980 $component = 'mod_'.$component;
981 }
982 list($type, $plugin) = explode('_', $component, 2);
983
984 if ($imagefile = $this->image_exists("$this->dir/pix_plugins/$type/$plugin/$image")) {
985 return $imagefile;
986 }
987 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
988 if ($imagefile = $this->image_exists("$parent_config->dir/pix_plugins/$type/$plugin/$image")) {
989 return $imagefile;
990 }
991 }
f930cf6b
EM
992 if ($imagefile = $this->image_exists("$CFG->dataroot/pix_plugins/$type/$plugin/$image")) {
993 return $imagefile;
994 }
78946b9b
PS
995 $dir = get_plugin_directory($type, $plugin);
996 if ($imagefile = $this->image_exists("$dir/pix/$image")) {
997 return $imagefile;
998 }
999 return null;
fdeb7fa1 1000 }
fdeb7fa1 1001 }
1002
d4a03c00 1003 /**
78946b9b 1004 * Checks if file with any image extension exists.
473dd811 1005 *
78946b9b
PS
1006 * @param string $filepath
1007 * @return string image name with extension
d4a03c00 1008 */
78946b9b
PS
1009 private static function image_exists($filepath) {
1010 if (file_exists("$filepath.gif")) {
1011 return "$filepath.gif";
1012 } else if (file_exists("$filepath.png")) {
1013 return "$filepath.png";
1014 } else if (file_exists("$filepath.jpg")) {
1015 return "$filepath.jpg";
1016 } else if (file_exists("$filepath.jpeg")) {
1017 return "$filepath.jpeg";
d4a03c00 1018 } else {
78946b9b 1019 return false;
d4a03c00 1020 }
1021 }
1022
fdeb7fa1 1023 /**
78946b9b 1024 * Loads the theme config from config.php file.
473dd811 1025 *
78946b9b 1026 * @param string $themename
473dd811
SH
1027 * @param stdClass $settings from config_plugins table
1028 * @return stdClass The theme configuration
fdeb7fa1 1029 */
78946b9b
PS
1030 private static function find_theme_config($themename, $settings) {
1031 // We have to use the variable name $THEME (upper case) because that
1032 // is what is used in theme config.php files.
fdeb7fa1 1033
78946b9b
PS
1034 if (!$dir = theme_config::find_theme_location($themename)) {
1035 return null;
fdeb7fa1 1036 }
2f67a9b3 1037
365a5941 1038 $THEME = new stdClass();
78946b9b
PS
1039 $THEME->name = $themename;
1040 $THEME->dir = $dir;
1041 $THEME->settings = $settings;
1042
1043 global $CFG; // just in case somebody tries to use $CFG in theme config
1044 include("$THEME->dir/config.php");
1045
1046 // verify the theme configuration is OK
1047 if (!is_array($THEME->parents)) {
1048 // parents option is mandatory now
1049 return null;
fdeb7fa1 1050 }
1051
78946b9b 1052 return $THEME;
fdeb7fa1 1053 }
1054
d4a03c00 1055 /**
78946b9b
PS
1056 * Finds the theme location and verifies the theme has all needed files
1057 * and is not obsoleted.
473dd811 1058 *
78946b9b
PS
1059 * @param string $themename
1060 * @return string full dir path or null if not found
d4a03c00 1061 */
78946b9b
PS
1062 private static function find_theme_location($themename) {
1063 global $CFG;
1064
1065 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
1066 $dir = "$CFG->dirroot/theme/$themename";
d4a03c00 1067
3dd1d7e7
DM
1068 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
1069 $dir = "$CFG->themedir/$themename";
1070
7d875874 1071 } else {
78946b9b 1072 return null;
d4a03c00 1073 }
78946b9b
PS
1074
1075 if (file_exists("$dir/styles.php")) {
1076 //legacy theme - needs to be upgraded - upgrade info is displayed on the admin settings page
1077 return null;
1078 }
2f67a9b3 1079
78946b9b 1080 return $dir;
d4a03c00 1081 }
1082
1d13c75c 1083 /**
78946b9b 1084 * Get the renderer for a part of Moodle for this theme.
473dd811 1085 *
78946b9b 1086 * @param moodle_page $page the page we are rendering
f8129210 1087 * @param string $component the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
78946b9b 1088 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
c927e35c 1089 * @param string $target one of rendering target constants
78946b9b 1090 * @return renderer_base the requested renderer.
1d13c75c 1091 */
c927e35c 1092 public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
78946b9b 1093 if (is_null($this->rf)) {
c927e35c 1094 $classname = $this->rendererfactory;
78946b9b 1095 $this->rf = new $classname($this);
1d13c75c 1096 }
78946b9b 1097
c927e35c 1098 return $this->rf->get_renderer($page, $component, $subtype, $target);
1d13c75c 1099 }
1100
fdeb7fa1 1101 /**
78946b9b 1102 * Get the information from {@link $layouts} for this type of page.
473dd811 1103 *
78946b9b
PS
1104 * @param string $pagelayout the the page layout name.
1105 * @return array the appropriate part of {@link $layouts}.
fdeb7fa1 1106 */
78946b9b
PS
1107 protected function layout_info_for_page($pagelayout) {
1108 if (array_key_exists($pagelayout, $this->layouts)) {
1109 return $this->layouts[$pagelayout];
1110 } else {
191b267b
PS
1111 debugging('Invalid page layout specified: ' . $pagelayout);
1112 return $this->layouts['standard'];
fdeb7fa1 1113 }
1114 }
1115
1116 /**
78946b9b
PS
1117 * Given the settings of this theme, and the page pagelayout, return the
1118 * full path of the page layout file to use.
1119 *
1120 * Used by {@link core_renderer::header()}.
1121 *
1122 * @param string $pagelayout the the page layout name.
1123 * @return string Full path to the lyout file to use
ebebf55c 1124 */
78946b9b 1125 public function layout_file($pagelayout) {
ebebf55c 1126 global $CFG;
fdeb7fa1 1127
78946b9b
PS
1128 $layoutinfo = $this->layout_info_for_page($pagelayout);
1129 $layoutfile = $layoutinfo['file'];
fdeb7fa1 1130
84db3ea2
SH
1131 if (array_key_exists('theme', $layoutinfo)) {
1132 $themes = array($layoutinfo['theme']);
1133 } else {
1134 $themes = array_merge(array($this->name),$this->parents);
1135 }
4399b9d5 1136
84db3ea2
SH
1137 foreach ($themes as $theme) {
1138 if ($dir = $this->find_theme_location($theme)) {
1139 $path = "$dir/layout/$layoutfile";
1140
1141 // Check the template exists, return general base theme template if not.
1142 if (is_readable($path)) {
1143 return $path;
1144 }
78946b9b 1145 }
34a2777c 1146 }
1147
191b267b 1148 debugging('Can not find layout file for: ' . $pagelayout);
78946b9b
PS
1149 // fallback to standard normal layout
1150 return "$CFG->dirroot/theme/base/layout/general.php";
1151 }
ebebf55c 1152
78946b9b
PS
1153 /**
1154 * Returns auxiliary page layout options specified in layout configuration array.
473dd811 1155 *
78946b9b
PS
1156 * @param string $pagelayout
1157 * @return array
1158 */
1159 public function pagelayout_options($pagelayout) {
1160 $info = $this->layout_info_for_page($pagelayout);
1161 if (!empty($info['options'])) {
1162 return $info['options'];
34a2777c 1163 }
78946b9b
PS
1164 return array();
1165 }
34a2777c 1166
78946b9b
PS
1167 /**
1168 * Inform a block_manager about the block regions this theme wants on this
1169 * page layout.
473dd811 1170 *
78946b9b
PS
1171 * @param string $pagelayout the general type of the page.
1172 * @param block_manager $blockmanager the block_manger to set up.
78946b9b
PS
1173 */
1174 public function setup_blocks($pagelayout, $blockmanager) {
1175 $layoutinfo = $this->layout_info_for_page($pagelayout);
1176 if (!empty($layoutinfo['regions'])) {
1177 $blockmanager->add_regions($layoutinfo['regions']);
1178 $blockmanager->set_default_region($layoutinfo['defaultregion']);
ebebf55c 1179 }
34a2777c 1180 }
1181
473dd811
SH
1182 /**
1183 * Gets the visible name for the requested block region.
1184 *
1185 * @param string $region The region name to get
1186 * @param string $theme The theme the region belongs to (may come from the parent theme)
1187 * @return string
1188 */
cadc0d28
TH
1189 protected function get_region_name($region, $theme) {
1190 $regionstring = get_string('region-' . $region, 'theme_' . $theme);
1191 // A name exists in this theme, so use it
1192 if (substr($regionstring, 0, 1) != '[') {
1193 return $regionstring;
1194 }
1195
1196 // Otherwise, try to find one elsewhere
1197 // Check parents, if any
1198 foreach ($this->parents as $parentthemename) {
1199 $regionstring = get_string('region-' . $region, 'theme_' . $parentthemename);
1200 if (substr($regionstring, 0, 1) != '[') {
1201 return $regionstring;
1202 }
1203 }
1204
1205 // Last resort, try the base theme for names
1206 return get_string('region-' . $region, 'theme_base');
1207 }
1208
ebebf55c 1209 /**
78946b9b 1210 * Get the list of all block regions known to this theme in all templates.
473dd811 1211 *
78946b9b 1212 * @return array internal region name => human readable name.
ebebf55c 1213 */
78946b9b
PS
1214 public function get_all_block_regions() {
1215 $regions = array();
78946b9b
PS
1216 foreach ($this->layouts as $layoutinfo) {
1217 foreach ($layoutinfo['regions'] as $region) {
a9535f79 1218 $regions[$region] = $this->get_region_name($region, $this->name);
ebebf55c 1219 }
34a2777c 1220 }
78946b9b 1221 return $regions;
34a2777c 1222 }
d609d962
SH
1223
1224 /**
1225 * Returns the human readable name of the theme
1226 *
1227 * @return string
1228 */
1229 public function get_theme_name() {
1230 return get_string('pluginname', 'theme_'.$this->name);
1231 }
ebebf55c 1232}
34a2777c 1233
ebebf55c 1234/**
d9c8f425 1235 * This class keeps track of which HTML tags are currently open.
1236 *
1237 * This makes it much easier to always generate well formed XHTML output, even
1238 * if execution terminates abruptly. Any time you output some opening HTML
1239 * without the matching closing HTML, you should push the necessary close tags
1240 * onto the stack.
ebebf55c 1241 *
1242 * @copyright 2009 Tim Hunt
473dd811
SH
1243 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1244 * @since Moodle 2.0
f8129210 1245 * @package core
76be40cc 1246 * @category output
ebebf55c 1247 */
d9c8f425 1248class xhtml_container_stack {
473dd811
SH
1249
1250 /**
76be40cc 1251 * @var array Stores the list of open containers.
473dd811 1252 */
d9c8f425 1253 protected $opencontainers = array();
473dd811 1254
fa1afe32 1255 /**
76be40cc 1256 * @var array In developer debug mode, stores a stack trace of all opens and
d9c8f425 1257 * closes, so we can output helpful error messages when there is a mismatch.
fa1afe32 1258 */
d9c8f425 1259 protected $log = array();
473dd811 1260
fa1afe32 1261 /**
76be40cc
SH
1262 * @var boolean Store whether we are developer debug mode. We need this in
1263 * several places including in the destructor where we may not have access to $CFG.
fa1afe32 1264 */
d9c8f425 1265 protected $isdebugging;
34a2777c 1266
473dd811
SH
1267 /**
1268 * Constructor
1269 */
d9c8f425 1270 public function __construct() {
1271 $this->isdebugging = debugging('', DEBUG_DEVELOPER);
ebebf55c 1272 }
34a2777c 1273
fa1afe32 1274 /**
d9c8f425 1275 * Push the close HTML for a recently opened container onto the stack.
473dd811 1276 *
d9c8f425 1277 * @param string $type The type of container. This is checked when {@link pop()}
1278 * is called and must match, otherwise a developer debug warning is output.
1279 * @param string $closehtml The HTML required to close the container.
fa1afe32 1280 */
d9c8f425 1281 public function push($type, $closehtml) {
1282 $container = new stdClass;
1283 $container->type = $type;
1284 $container->closehtml = $closehtml;
1285 if ($this->isdebugging) {
1286 $this->log('Open', $type);
3aaa27f4 1287 }
d9c8f425 1288 array_push($this->opencontainers, $container);
ebebf55c 1289 }
34a2777c 1290
fa1afe32 1291 /**
d9c8f425 1292 * Pop the HTML for the next closing container from the stack. The $type
1293 * must match the type passed when the container was opened, otherwise a
1294 * warning will be output.
473dd811 1295 *
d9c8f425 1296 * @param string $type The type of container.
1297 * @return string the HTML required to close the container.
fa1afe32 1298 */
d9c8f425 1299 public function pop($type) {
1300 if (empty($this->opencontainers)) {
1301 debugging('<p>There are no more open containers. This suggests there is a nesting problem.</p>' .
1302 $this->output_log(), DEBUG_DEVELOPER);
1303 return;
3aaa27f4 1304 }
ebebf55c 1305
d9c8f425 1306 $container = array_pop($this->opencontainers);
1307 if ($container->type != $type) {
1308 debugging('<p>The type of container to be closed (' . $container->type .
1309 ') does not match the type of the next open container (' . $type .
1310 '). This suggests there is a nesting problem.</p>' .
1311 $this->output_log(), DEBUG_DEVELOPER);
ebebf55c 1312 }
d9c8f425 1313 if ($this->isdebugging) {
1314 $this->log('Close', $type);
e8775320 1315 }
d9c8f425 1316 return $container->closehtml;
ebebf55c 1317 }
e8775320 1318
fa1afe32 1319 /**
d9c8f425 1320 * Close all but the last open container. This is useful in places like error
1321 * handling, where you want to close all the open containers (apart from <body>)
1322 * before outputting the error message.
473dd811 1323 *
d9c8f425 1324 * @param bool $shouldbenone assert that the stack should be empty now - causes a
1325 * developer debug warning if it isn't.
1326 * @return string the HTML required to close any open containers inside <body>.
fa1afe32 1327 */
d9c8f425 1328 public function pop_all_but_last($shouldbenone = false) {
1329 if ($shouldbenone && count($this->opencontainers) != 1) {
1330 debugging('<p>Some HTML tags were opened in the body of the page but not closed.</p>' .
1331 $this->output_log(), DEBUG_DEVELOPER);
1332 }
1333 $output = '';
1334 while (count($this->opencontainers) > 1) {
1335 $container = array_pop($this->opencontainers);
1336 $output .= $container->closehtml;
e8775320 1337 }
d9c8f425 1338 return $output;
e8775320 1339 }
34a2777c 1340
ebebf55c 1341 /**
d9c8f425 1342 * You can call this function if you want to throw away an instance of this
1343 * class without properly emptying the stack (for example, in a unit test).
1344 * Calling this method stops the destruct method from outputting a developer
1345 * debug warning. After calling this method, the instance can no longer be used.
ebebf55c 1346 */
d9c8f425 1347 public function discard() {
1348 $this->opencontainers = null;
ebebf55c 1349 }
d9c8f425 1350
fa1afe32 1351 /**
d9c8f425 1352 * Adds an entry to the log.
473dd811 1353 *
d9c8f425 1354 * @param string $action The name of the action
1355 * @param string $type The type of action
fa1afe32 1356 */
d9c8f425 1357 protected function log($action, $type) {
1358 $this->log[] = '<li>' . $action . ' ' . $type . ' at:' .
1359 format_backtrace(debug_backtrace()) . '</li>';
ebebf55c 1360 }
34a2777c 1361
fa1afe32 1362 /**
d9c8f425 1363 * Outputs the log's contents as a HTML list.
473dd811 1364 *
d9c8f425 1365 * @return string HTML list of the log
fa1afe32 1366 */
d9c8f425 1367 protected function output_log() {
1368 return '<ul>' . implode("\n", $this->log) . '</ul>';
34a2777c 1369 }
6fc267a0 1370}