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