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