MDL-21178 formslib element modgrade does not use deprecated link_to_popup_window...
[moodle.git] / lib / outputlib.php
CommitLineData
571fa828 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
78946b9b
PS
18/**
19 * Functions for generating the HTML that Moodle should output.
20 *
21 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
22 * for an overview.
23 *
24 * @package moodlecore
25 * @copyright 2009 Tim Hunt
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
f8065dd2 29/**
30 * This constant is used for html attributes which need to have an empty
31 * value and still be output by the renderers (e.g. alt="");
32 *
33 * @constant @EMPTY@
34 */
35define('HTML_ATTR_EMPTY', '@EMPTY@');
571fa828 36
d9c8f425 37require_once($CFG->libdir.'/outputcomponents.php');
38require_once($CFG->libdir.'/outputactions.php');
39require_once($CFG->libdir.'/outputfactories.php');
d9c8f425 40require_once($CFG->libdir.'/outputrenderers.php');
41
571fa828 42/**
78946b9b
PS
43 * Invalidate all server and client side caches.
44 * @return void
45 */
46function theme_reset_all_caches() {
47 global $CFG;
48 require_once("$CFG->libdir/filelib.php");
49
50 set_config('themerev', empty($CFG->themerev) ? 1 : $CFG->themerev+1);
51 fulldelete("$CFG->dataroot/cache/theme");
52}
53
54/**
55 * Enable or disable theme designer mode.
56 * @param bool $state
57 * @return void
58 */
59function theme_set_designer_mod($state) {
60 theme_reset_all_caches();
61 set_config('themedesignermode', (int)!empty($state));
62}
63
64/**
65 * Returns current theme revision number.
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
100 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
101 * @since Moodle 2.0
102 */
ebebf55c 103class theme_config {
104 /**
78946b9b
PS
105 * You can base your theme on other themes by linking to the other theme as
106 * parents. This lets you use the CSS and layouts from the other themes
107 * (see {@link $layouts}).
fdeb7fa1 108 * That makes it easy to create a new theme that is similar to another one
109 * but with a few changes. In this theme's CSS you only need to override
110 * those rules you want to change.
fdeb7fa1 111 *
78946b9b 112 * @var array
fdeb7fa1 113 */
78946b9b 114 public $parents;
571fa828 115
fdeb7fa1 116 /**
78946b9b
PS
117 * The names of all the stylesheets from this theme that you would
118 * like included, in order. Give the names of the files without .css.
fdeb7fa1 119 *
78946b9b 120 * @var array
fdeb7fa1 121 */
78946b9b 122 public $sheets = array();
571fa828 123
fdeb7fa1 124 /**
78946b9b
PS
125 * The names of all the stylesheets from parents that should be expcluded.
126 * true value may be used to specify all parents or all themes from one parent.
127 * If no value specified value from parent theme used.
fdeb7fa1 128 *
78946b9b 129 * @var array or arrays, true means all, null means use value from parent
fdeb7fa1 130 */
78946b9b 131 public $parents_exclude_sheets = null;
571fa828 132
fdeb7fa1 133 /**
78946b9b
PS
134 * List of plugin sheets to be excluded.
135 * If no value specified value from parent theme used.
136 *
137 * @var array of full plugin names, null means use value from parent
fdeb7fa1 138 */
78946b9b 139 public $plugins_exclude_sheets = null;
571fa828 140
fdeb7fa1 141 /**
78946b9b
PS
142 * List of style sheets that are included in the text editor bodies.
143 * Sheets from parent themes are used automatically and can not be excluded.
fdeb7fa1 144 *
78946b9b 145 * @var array
fdeb7fa1 146 */
78946b9b 147 public $editor_sheets = array();
571fa828 148
38aafea2
PS
149 /**
150 * The names of all the javascript files this theme that you would
151 * like included, in order. Give the names of the files without .js.
152 *
153 * @var array
154 */
155 public $javascripts = array();
156
157 /**
158 * The names of all the javascript files from parents that should be expcluded.
159 * true value may be used to specify all parents or all themes from one parent.
160 * If no value specified value from parent theme used.
161 *
162 * @var array or arrays, true means all, null means use value from parent
163 */
164 public $parents_exclude_javascripts = null;
165
fdeb7fa1 166 /**
78946b9b 167 * Which file to use for each page layout.
fdeb7fa1 168 *
78946b9b
PS
169 * This is an array of arrays. The keys of the outer array are the different layouts.
170 * Pages in Moodle are using several different layouts like 'normal', 'course', 'home',
171 * 'popup', 'form', .... The most reliable way to get a complete list is to look at
172 * {@link http://cvs.moodle.org/moodle/theme/base/config.php?view=markup the base theme config.php file}.
d4a03c00 173 * That file also has a good example of how to set this setting.
fdeb7fa1 174 *
78946b9b 175 * For each layout, the value in the outer array is an array that describes
d4a03c00 176 * how you want that type of page to look. For example
177 * <pre>
178 * $THEME->layouts = array(
78946b9b 179 * // Most pages - if we encounter an unknown or amissing page type, this one is used.
d4a03c00 180 * 'normal' => array(
78946b9b
PS
181 * 'theme' = 'mytheme',
182 * 'file' => 'normal.php',
d4a03c00 183 * 'regions' => array('side-pre', 'side-post'),
184 * 'defaultregion' => 'side-post'
185 * ),
186 * // The site home page.
187 * 'home' => array(
78946b9b
PS
188 * 'theme' = 'mytheme',
189 * 'file' => 'home.php',
d4a03c00 190 * 'regions' => array('side-pre', 'side-post'),
191 * 'defaultregion' => 'side-post'
192 * ),
193 * // ...
194 * );
195 * </pre>
fdeb7fa1 196 *
78946b9b
PS
197 * 'theme' name of the theme where is the layout located
198 * 'file' is the layout file to use for this type of page.
199 * layout files are stored in layout subfolder
d4a03c00 200 * 'regions' This lists the regions on the page where blocks may appear. For
201 * each region you list here, your layout file must include a call to
202 * <pre>
203 * echo $OUTPUT->blocks_for_region($regionname);
204 * </pre>
205 * or equivalent so that the blocks are actually visible.
fdeb7fa1 206 *
d4a03c00 207 * 'defaultregion' If the list of regions is non-empty, then you must pick
208 * one of the one of them as 'default'. This has two meanings. First, this is
209 * where new blocks are added. Second, if there are any blocks associated with
fa1afe32 210 * the page, but in non-existent regions, they appear here. (Imaging, for example,
d4a03c00 211 * that someone added blocks using a different theme that used different region
212 * names, and then switched to this theme.)
fdeb7fa1 213 *
214 * @var array
215 */
d4a03c00 216 public $layouts = array();
571fa828 217
fdeb7fa1 218 /**
219 * With this you can control the colours of the big MP3 player
220 * that is used for MP3 resources.
221 *
222 * @var string
223 */
ebebf55c 224 public $resource_mp3player_colors = 'bgColour=000000&btnColour=ffffff&btnBorderColour=cccccc&iconColour=000000&iconOverColour=00cc00&trackColour=cccccc&handleColour=ffffff&loaderColour=ffffff&font=Arial&fontColour=3333FF&buffer=10&waitForPlay=no&autoPlay=yes';
571fa828 225
fdeb7fa1 226 /**
78946b9b
PS
227 * With this you can control the colours of the small MP3 player
228 * that is used elsewhere.
229 *
fdeb7fa1 230 * @var string
231 */
ebebf55c 232 public $filter_mediaplugin_colors = 'bgColour=000000&btnColour=ffffff&btnBorderColour=cccccc&iconColour=000000&iconOverColour=00cc00&trackColour=cccccc&handleColour=ffffff&loaderColour=ffffff&waitForPlay=yes';
233
fdeb7fa1 234 /**
235 * Name of the renderer factory class to use.
236 *
237 * This is an advanced feature. Moodle output is generated by 'renderers',
238 * you can customise the HTML that is output by writing custom renderers,
239 * and then you need to specify 'renderer factory' so that Moodle can find
240 * your renderers.
241 *
242 * There are some renderer factories supplied with Moodle. Please follow these
243 * links to see what they do.
244 * <ul>
245 * <li>{@link standard_renderer_factory} - the default.</li>
246 * <li>{@link theme_overridden_renderer_factory} - use this if you want to write
78946b9b 247 * your own custom renderers in a lib.php file in this theme (or the parent theme).</li>
fdeb7fa1 248 * </ul>
249 *
250 * @var string name of a class implementing the {@link renderer_factory} interface.
251 */
ebebf55c 252 public $rendererfactory = 'standard_renderer_factory';
ebebf55c 253
fdeb7fa1 254 /**
78946b9b 255 * Function to do custom CSS post-processing.
fdeb7fa1 256 *
78946b9b
PS
257 * This is an advanced feature. If you want to do custom post-processing on the
258 * CSS before it is output (for example, to replace certain variable names
259 * with particular values) you can give the name of a function here.
fdeb7fa1 260 *
78946b9b 261 * @var string the name of a function.
fdeb7fa1 262 */
78946b9b 263 public $csspostprocess = null;
571fa828 264
265 /**
78946b9b
PS
266 * Accessibility: Right arrow-like character is
267 * used in the breadcrumb trail, course navigation menu
268 * (previous/next activity), calendar, and search forum block.
269 * If the theme does not set characters, appropriate defaults
270 * are set automatically. Please DO NOT
271 * use &lt; &gt; &raquo; - these are confusing for blind users.
fdeb7fa1 272 *
78946b9b
PS
273 * @var string
274 */
275 public $rarrow = null;
276
277 /**
278 * Accessibility: Right arrow-like character is
279 * used in the breadcrumb trail, course navigation menu
280 * (previous/next activity), calendar, and search forum block.
281 * If the theme does not set characters, appropriate defaults
282 * are set automatically. Please DO NOT
283 * use &lt; &gt; &raquo; - these are confusing for blind users.
ebebf55c 284 *
78946b9b 285 * @var string
571fa828 286 */
78946b9b
PS
287 public $larrow = null;
288
289
290 //==Following properties are not configurable from theme config.php==
571fa828 291
fdeb7fa1 292 /**
78946b9b
PS
293 * The name of this theme. Set automatically when this theme is
294 * loaded. This can not be set in theme config.php
295 * @var string
fdeb7fa1 296 */
78946b9b 297 public $name;
fdeb7fa1 298
299 /**
78946b9b
PS
300 * the folder where this themes files are stored. This is set
301 * automatically. This can not be set in theme config.php
302 * @var string
fdeb7fa1 303 */
78946b9b 304 public $dir;
fdeb7fa1 305
306 /**
78946b9b
PS
307 * Theme settings stored in config_plugins table.
308 * This can not be set in theme config.php
309 * @var object
310 */
311 public $setting = null;
312
313 /**
314 * Instance of the renderer_factory implementation
fdeb7fa1 315 * we are using. Implementation detail.
78946b9b 316 * @var renderer_factory
fdeb7fa1 317 */
318 protected $rf = null;
319
320 /**
78946b9b
PS
321 * List of parent config objects.
322 * @var array list of parent configs
323 **/
324 protected $parent_configs = array();
fdeb7fa1 325
571fa828 326 /**
ebebf55c 327 * Load the config.php file for a particular theme, and return an instance
328 * of this class. (That is, this is a factory method.)
329 *
330 * @param string $themename the name of the theme.
331 * @return theme_config an instance of this class.
571fa828 332 */
ebebf55c 333 public static function load($themename) {
334 global $CFG;
571fa828 335
78946b9b
PS
336 // load theme settings from db
337 try {
338 $settings = get_config('theme_'.$themename);
339 } catch (dml_exception $e) {
da0c0e25 340 // most probably moodle tables not created yet
78946b9b
PS
341 $settings = new object();
342 }
ebebf55c 343
78946b9b
PS
344 if ($config = theme_config::find_theme_config($themename, $settings)) {
345 return new theme_config($config);
346 } else {
347 // bad luck, the requested theme has some problems - admin see details in theme config
da0c0e25 348 return new theme_config(theme_config::find_theme_config('standard', $settings)); // TODO: use some other default MDL-21149
78946b9b
PS
349 }
350 }
ebebf55c 351
78946b9b
PS
352 /**
353 * Theme diagnostic code. It is very problematic to send debug output
354 * to the actual CSS file, instead this functions is supposed to
355 * diagnose given theme and highlisht all potential problems.
356 * This information should be available from the theme selection page
357 * or some other debug page for theme designers.
358 *
359 * @param string $themename
360 * @return array description of problems
361 */
362 public static function diagnose($themename) {
5ef719e7 363 //TODO: MDL-21108
78946b9b
PS
364 return array();
365 }
366
367 /**
368 * Private constructor, can be called only from the factory method.
369 * @param stdClass $config
370 */
371 private function __construct($config) {
372 global $CFG; //needed for included lib.php files
373
374 $this->settings = $config->settings;
375 $this->name = $config->name;
376 $this->dir = $config->dir;
377
378 if ($this->name != 'base') {
379 $baseconfig = theme_config::find_theme_config('base', $this->settings);
380 } else {
381 $baseconfig = $config;
571fa828 382 }
ebebf55c 383
e68c5f36 384 $configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts',
38aafea2 385 'parents_exclude_javascripts', 'layouts', 'resource_mp3player_colors',
78946b9b 386 'filter_mediaplugin_colors', 'rendererfactory', 'csspostprocess', 'editor_sheets', 'rarrow', 'larrow');
ebebf55c 387
78946b9b
PS
388 foreach ($config as $key=>$value) {
389 if (in_array($key, $configurable)) {
390 $this->$key = $value;
391 }
392 }
393
394 // verify all parents and load configs and renderers
395 foreach ($this->parents as $parent) {
396 if ($parent == 'base') {
397 $parent_config = $baseconfig;
398 } else if (!$parent_config = theme_config::find_theme_config($parent, $this->settings)) {
399 // this is not good - better exclude faulty parents
400 continue;
401 }
402 $libfile = $parent_config->dir.'/lib.php';
403 if (is_readable($libfile)) {
404 // theme may store various function here
405 include_once($libfile);
406 }
407 $renderersfile = $parent_config->dir.'/renderers.php';
408 if (is_readable($renderersfile)) {
409 // may contain core and plugin renderers and renderer factory
410 include_once($renderersfile);
411 }
412 $this->parent_configs[$parent] = $parent_config;
413 $rendererfile = $parent_config->dir.'/renderers.php';
414 if (is_readable($rendererfile)) {
415 // may contain core and plugin renderers and renderer factory
416 include_once($rendererfile);
417 }
418 }
419 $libfile = $this->dir.'/lib.php';
420 if (is_readable($libfile)) {
421 // theme may store various function here
422 include_once($libfile);
423 }
424 $rendererfile = $this->dir.'/renderers.php';
425 if (is_readable($rendererfile)) {
426 // may contain core and plugin renderers and renderer factory
427 include_once($rendererfile);
428 }
429
430 // cascade all layouts properly
431 foreach ($baseconfig->layouts as $layout=>$value) {
432 if (!isset($this->layouts[$layout])) {
433 foreach ($this->parent_configs as $parent_config) {
434 if (isset($parent_config->layouts[$layout])) {
435 $this->layouts[$layout] = $parent_config->layouts[$layout];
436 continue 2;
437 }
438 }
439 $this->layouts[$layout] = $value;
440 }
441 }
442
443 //fix arrows if needed
444 $this->check_theme_arrows();
571fa828 445 }
446
78946b9b
PS
447 /*
448 * Checks if arrows $THEME->rarrow, $THEME->larrow have been set (theme/-/config.php).
449 * If not it applies sensible defaults.
450 *
451 * Accessibility: right and left arrow Unicode characters for breadcrumb, calendar,
452 * search forum block, etc. Important: these are 'silent' in a screen-reader
453 * (unlike &gt; &raquo;), and must be accompanied by text.
34a2777c 454 */
78946b9b
PS
455 private function check_theme_arrows() {
456 if (!isset($this->rarrow) and !isset($this->larrow)) {
457 // Default, looks good in Win XP/IE 6, Win/Firefox 1.5, Win/Netscape 8...
458 // Also OK in Win 9x/2K/IE 5.x
459 $this->rarrow = '&#x25BA;';
460 $this->larrow = '&#x25C4;';
461 if (empty($_SERVER['HTTP_USER_AGENT'])) {
462 $uagent = '';
ebebf55c 463 } else {
78946b9b
PS
464 $uagent = $_SERVER['HTTP_USER_AGENT'];
465 }
466 if (false !== strpos($uagent, 'Opera')
467 || false !== strpos($uagent, 'Mac')) {
468 // Looks good in Win XP/Mac/Opera 8/9, Mac/Firefox 2, Camino, Safari.
469 // Not broken in Mac/IE 5, Mac/Netscape 7 (?).
470 $this->rarrow = '&#x25B6;';
471 $this->larrow = '&#x25C0;';
472 }
473 elseif (false !== strpos($uagent, 'Konqueror')) {
474 $this->rarrow = '&rarr;';
475 $this->larrow = '&larr;';
476 }
477 elseif (isset($_SERVER['HTTP_ACCEPT_CHARSET'])
478 && false === stripos($_SERVER['HTTP_ACCEPT_CHARSET'], 'utf-8')) {
479 // (Win/IE 5 doesn't set ACCEPT_CHARSET, but handles Unicode.)
480 // To be safe, non-Unicode browsers!
481 $this->rarrow = '&gt;';
482 $this->larrow = '&lt;';
483 }
484
485 /// RTL support - in RTL languages, swap r and l arrows
486 if (right_to_left()) {
487 $t = $this->rarrow;
488 $this->rarrow = $this->larrow;
489 $this->larrow = $t;
ebebf55c 490 }
ebebf55c 491 }
78946b9b 492 }
ebebf55c 493
78946b9b
PS
494 /**
495 * Returns output renderer prefixes, these are used when looking
496 * for the overriden renderers in themes.
497 * @return array
498 */
499 public function renderer_prefixes() {
500 global $CFG; // just in case the included files need it
501
502 $prefixes = array();
503
504 foreach ($this->parent_configs as $parent) {
505 $prefixes[] = 'theme_'.$parent->name;
506 }
507
508 return $prefixes;
34a2777c 509 }
510
571fa828 511 /**
78946b9b
PS
512 * Returns the stylesheet URL of this editor content
513 * @param bool $encoded false means use & and true use &amp; in URLs
514 * @return string
571fa828 515 */
78946b9b
PS
516 public function editor_css_url($encoded=true) {
517 global $CFG;
518
519 $rev = theme_get_revision();
520
521 if ($rev > -1) {
522 $params = array('theme'=>$this->name,'rev'=>$rev, 'type'=>'editor');
523 return new moodle_url($CFG->httpswwwroot.'/theme/styles.php', $params);
524 } else {
525 $params = array('theme'=>$this->name, 'type'=>'editor');
526 return new moodle_url($CFG->httpswwwroot.'/theme/styles_debug.php', $params);
571fa828 527 }
571fa828 528 }
529
530 /**
78946b9b
PS
531 * Returns the content of the CSS to be used in editor content
532 * @return string
571fa828 533 */
78946b9b
PS
534 public function editor_css_content() {
535 global $CFG;
536
537 $css = '';
538
539 // first editor plugins
540 $plugins = get_plugin_list('editor');
541 foreach ($plugins as $plugin=>$fulldir) {
542 $sheetfile = "$fulldir/editor_styles.css";
543 if (is_readable($sheetfile)) {
544 $css .= "/*** Editor $plugin content CSS ***/\n\n" . file_get_contents($sheetfile) . "\n\n";
545 }
546 }
547 // then parent themes
548 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
549 if (empty($parent_config->editor_sheets)) {
550 continue;
551 }
552 foreach ($parent_config->editor_sheets as $sheet) {
553 $sheetfile = "$parent_config->dir/$sheet.css";
554 if (is_readable($sheetfile)) {
555 $css .= "/*** Parent theme $parent/$sheet ***/\n\n" . file_get_contents($sheetfile) . "\n\n";
556 }
557 }
558 }
559 // finally this theme
560 if (!empty($this->editor_sheets)) {
561 foreach ($this->editor_sheets as $sheet) {
562 $sheetfile = "$this->dir/$sheet.css";
563 if (is_readable($sheetfile)) {
564 $css .= "/*** Theme $sheet ***/\n\n" . file_get_contents($sheetfile) . "\n\n";
565 }
566 }
567 }
568
569 return $this->post_process($css);
571fa828 570 }
571
572 /**
78946b9b
PS
573 * Get the stylesheet URL of this theme
574 * @param bool $encoded false means use & and true use &amp; in URLs
38aafea2 575 * @return array of moodle_url
571fa828 576 */
78946b9b
PS
577 public function css_urls() {
578 global $CFG;
579
580 $rev = theme_get_revision();
581
582 if ($rev > -1) {
583 $params = array('theme'=>$this->name,'rev'=>$rev);
584 if (check_browser_version('MSIE', 5) and !check_browser_version('MSIE', 8)) {
585 $params['type'] = 'ie';
586 }
587 return array(new moodle_url($CFG->httpswwwroot.'/theme/styles.php', $params));
588
589 } else {
590 // this is painfully slow, but it should not matter much ;-)
591 $css = $this->css_content();
592 $url = $CFG->httpswwwroot.'/theme/styles_debug.php';
593 $urls = array();
594 $urls[] = new moodle_url($url, array('theme'=>$this->name,'type'=>'yui'));
595 foreach ($css['plugins'] as $plugin=>$unused) {
596 $urls[] = new moodle_url($url, array('theme'=>$this->name,'type'=>'plugin', 'subtype'=>$plugin));
597 }
598 foreach ($css['parents'] as $parent=>$sheets) {
599 foreach ($sheets as $sheet=>$unused2) {
600 $urls[] = new moodle_url($url, array('theme'=>$this->name,'type'=>'parent', 'subtype'=>$parent, 'sheet'=>$sheet));
601 }
602 }
603 foreach ($css['theme'] as $sheet=>$unused) {
604 $urls[] = new moodle_url($url, array('theme'=>$this->name,'type'=>'theme', 'sheet'=>$sheet));
605 }
606 return $urls;
607 }
571fa828 608 }
34a2777c 609
ebebf55c 610 /**
78946b9b
PS
611 * Returns the content of the one huge CSS merged from all style sheets.
612 * @return string
ebebf55c 613 */
78946b9b 614 public function css_content() {
34a2777c 615 global $CFG;
616
78946b9b 617 $css = array('yui'=>array(), 'plugins'=>array(), 'parents'=>array(), 'theme'=>array());
34a2777c 618
78946b9b 619 //YUI sheets
39302c79
PS
620 $yui_sheets = "/*** YUI reset sheets and grids ***/\n\n";
621 $yui_sheets .= file_get_contents("$CFG->libdir/yui/$CFG->yui3version/cssreset/reset-min.css");
622 $yui_sheets .= file_get_contents("$CFG->libdir/yui/$CFG->yui3version/cssbase/base-min.css");
623 $yui_sheets .= file_get_contents("$CFG->libdir/yui/$CFG->yui3version/cssfonts/fonts-min.css");
624 $yui_sheets .= file_get_contents("$CFG->libdir/yui/$CFG->yui3version/cssgrids/grids-min.css");
625
626 $yui_sheets .= "/*** Standard YUI sheets ***/\n\n";
bf62f187 627 $items = new DirectoryIterator("$CFG->libdir/yui/$CFG->yui2version/assets/skins/sam");
78946b9b
PS
628 foreach ($items as $item) {
629 if ($item->isDot() or !$item->isFile()) {
630 continue;
631 }
632 $filename = $item->getFilename();
633 if (substr($filename, -4) !== '.css') {
634 continue;
635 }
bf62f187 636 $yui_sheets .= file_get_contents("$CFG->libdir/yui/$CFG->yui2version/assets/skins/sam/$filename");
78946b9b
PS
637 }
638 unset($item);
639 unset($items);
640 $yui_sheets = preg_replace('/([a-z-]+)\.(png|gif)/', '[[pix:yui|$1]]', $yui_sheets);
641 $css['yui'][] = $this->post_process($yui_sheets);
642
643 // get plugin sheets
644 $excludes = null;
645 if (is_array($this->plugins_exclude_sheets) or $this->plugins_exclude_sheets === true) {
646 $excludes = $this->plugins_exclude_sheets;
647 } else {
648 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
649 if (!isset($parent_config->plugins_exclude_sheets)) {
650 continue;
651 }
652 if (is_array($parent_config->plugins_exclude_sheets) or $parent_config->plugins_exclude_sheets === true) {
653 $excludes = $parent_config->plugins_exclude_sheets;
654 break;
655 }
656 }
ebebf55c 657 }
78946b9b
PS
658 if ($excludes !== true) {
659 foreach (get_plugin_types() as $type=>$unused) {
660 if ($type === 'theme') {
661 continue;
662 }
663 if (!empty($excludes[$type]) and $excludes[$type] === true) {
664 continue;
665 }
666 $plugins = get_plugin_list($type);
667 foreach ($plugins as $plugin=>$fulldir) {
668 if (!empty($excludes[$type]) and is_array($excludes[$type])
669 and in_array($plugin, $excludes[$type])) {
670 continue;
671 }
672 $sheetfile = "$fulldir/styles.css";
673 if (is_readable($sheetfile)) {
674 $css['plugins'][$type.'_'.$plugin] = $this->post_process("/*** Standard plugin $type/$plugin ***/\n\n" . file_get_contents($sheetfile));
675 }
676 }
677 }
34a2777c 678 }
679
78946b9b
PS
680 // find out wanted parent sheets
681 $excludes = null;
682 if (is_array($this->parents_exclude_sheets) or $this->parents_exclude_sheets === true) {
683 $excludes = $this->parents_exclude_sheets;
684 } else {
685 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
686 if (!isset($parent_config->parents_exclude_sheets)) {
687 continue;
688 }
689 if (is_array($parent_config->parents_exclude_sheets) or $parent_config->parents_exclude_sheets === true) {
690 $excludes = $parent_config->parents_exclude_sheets;
691 break;
692 }
693 }
694 }
695 if ($excludes !== true) {
696 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
697 $parent = $parent_config->name;
698 if (empty($parent_config->sheets)) {
699 continue;
700 }
701 if (!empty($excludes[$parent]) and $excludes[$parent] === true) {
702 continue;
703 }
704 foreach ($parent_config->sheets as $sheet) {
705 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
706 and in_array($sheet, $excludes[$parent])) {
707 continue;
708 }
709 $sheetfile = "$parent_config->dir/style/$sheet.css";
710 if (is_readable($sheetfile)) {
711 $css['parents'][$parent][$sheet] = $this->post_process("/*** Parent theme $parent/$sheet ***/\n\n" . file_get_contents($sheetfile));
712 }
713 }
714 }
715 }
34a2777c 716
78946b9b
PS
717 // current theme sheets
718 if (is_array($this->sheets)) {
719 foreach ($this->sheets as $sheet) {
720 $sheetfile = "$this->dir/style/$sheet.css";
721 if (is_readable($sheetfile)) {
722 $css['theme'][$sheet] = $this->post_process("/*** This theme $sheet ***/\n\n" . file_get_contents($sheetfile));
723 }
ebebf55c 724 }
78946b9b
PS
725 }
726
727 return $css;
728 }
34a2777c 729
e68c5f36
PS
730
731 /**
732 * Get the javascript URL of this theme
733 * @param bool $encoded false means use & and true use &amp; in URLs
734 * @return moodle_url
735 */
736 public function javascript_url() {
737 global $CFG;
738
739 $rev = theme_get_revision();
740
741 $params = array('theme'=>$this->name,'rev'=>$rev);
742 return new moodle_url($CFG->httpswwwroot.'/theme/javascripts.php', $params);
743 }
744
745 /**
746 * Returns the content of the one huge javascript file merged from all theme javascript files.
747 * @return string
748 */
749 public function javascript_content() {
750 //TODO: load contents of all theme JS files
751 return '/*not yet fully implemented*/';
752 }
753
78946b9b
PS
754 protected function post_process($css) {
755 // now resolve all image locations
756 if (preg_match_all('/\[\[pix:([a-z_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) {
757 $replaced = array();
758 foreach ($matches as $match) {
759 if (isset($replaced[$match[0]])) {
760 continue;
761 }
762 $replaced[$match[0]] = true;
763 $imagename = $match[2];
764 $component = rtrim($match[1], '|');
c39e5ba2 765 $css = str_replace($match[0], $this->pix_url($imagename, $component)->out(false, array(), false), $css);
ebebf55c 766 }
34a2777c 767 }
17a6649b 768
78946b9b
PS
769 // now resolve all theme settings or do any other postprocessing
770 $csspostprocess = $this->csspostprocess;
771 if (function_exists($csspostprocess)) {
772 $css = $csspostprocess($css, $this);
fdeb7fa1 773 }
774
78946b9b 775 return $css;
ebebf55c 776 }
17a6649b 777
ebebf55c 778 /**
78946b9b
PS
779 * Return the URL for an image
780 *
781 * @param string $imagename the name of the icon.
782 * @param string $component, specification of one plugin like in get_string()
783 * @return moodle_url
fdeb7fa1 784 */
c39e5ba2 785 public function pix_url($imagename, $component) {
fdeb7fa1 786 global $CFG;
78946b9b
PS
787
788 $params = array('theme'=>$this->name, 'image'=>$imagename);
789
790 $rev = theme_get_revision();
791 if ($rev != -1) {
792 $params['rev'] = $rev;
793 }
794 if (!empty($component) and $component !== 'moodle'and $component !== 'core') {
795 $params['component'] = $component;
fdeb7fa1 796 }
78946b9b
PS
797
798 return new moodle_url("$CFG->httpswwwroot/theme/image.php", $params);
fdeb7fa1 799 }
800
801 /**
78946b9b
PS
802 * Resolves the real image location.
803 * @param string $image name of image, may contain relative path
804 * @param string $component
805 * @return string full file path
fdeb7fa1 806 */
78946b9b
PS
807 public function resolve_image_location($image, $component) {
808 global $CFG;
809
810 if ($component === 'moodle' or $component === 'core' or empty($component)) {
811 if ($imagefile = $this->image_exists("$this->dir/pix_core/$image")) {
812 return $imagefile;
813 }
814 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
815 if ($imagefile = $this->image_exists("$parent_config->dir/pix_core/$image")) {
816 return $imagefile;
817 }
818 }
819 if ($imagefile = $this->image_exists("$CFG->dirroot/pix/$image")) {
820 return $imagefile;
821 }
822 return null;
823
824 } else if ($component === 'theme') { //exception
825 if ($image === 'favicon') {
826 return "$this->dir/pix/favicon.ico";
827 }
828 if ($imagefile = $this->image_exists("$this->dir/pix/$image")) {
829 return $imagefile;
830 }
831 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
832 if ($imagefile = $this->image_exists("$parent_config->dir/pix/$image")) {
833 return $imagefile;
834 }
835 }
836 return null;
837
838 } else if ($component === 'yui') {
839 // yui CSS files are parsed automatically and altered on the fly
840 if ($imagefile = $this->image_exists("$this->dir/pix_yui/$image")) {
841 return $imagefile;
842 }
843 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
844 if ($imagefile = $this->image_exists("$parent_config->dir/pix_yui/$image")) {
845 return $imagefile;
846 }
847 }
bf62f187 848 if ($imagefile = $this->image_exists("$CFG->libdir/yui/$CFG->yui2version/assets/skins/sam/$image")) {
78946b9b
PS
849 return $imagefile;
850 }
851 return null;
852
853 } else {
854 if (strpos($component, '_') === false) {
855 $component = 'mod_'.$component;
856 }
857 list($type, $plugin) = explode('_', $component, 2);
858
859 if ($imagefile = $this->image_exists("$this->dir/pix_plugins/$type/$plugin/$image")) {
860 return $imagefile;
861 }
862 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
863 if ($imagefile = $this->image_exists("$parent_config->dir/pix_plugins/$type/$plugin/$image")) {
864 return $imagefile;
865 }
866 }
867 $dir = get_plugin_directory($type, $plugin);
868 if ($imagefile = $this->image_exists("$dir/pix/$image")) {
869 return $imagefile;
870 }
871 return null;
fdeb7fa1 872 }
fdeb7fa1 873 }
874
d4a03c00 875 /**
78946b9b
PS
876 * Checks if file with any image extension exists.
877 * @param string $filepath
878 * @return string image name with extension
d4a03c00 879 */
78946b9b
PS
880 private static function image_exists($filepath) {
881 if (file_exists("$filepath.gif")) {
882 return "$filepath.gif";
883 } else if (file_exists("$filepath.png")) {
884 return "$filepath.png";
885 } else if (file_exists("$filepath.jpg")) {
886 return "$filepath.jpg";
887 } else if (file_exists("$filepath.jpeg")) {
888 return "$filepath.jpeg";
d4a03c00 889 } else {
78946b9b 890 return false;
d4a03c00 891 }
892 }
893
fdeb7fa1 894 /**
78946b9b
PS
895 * Loads the theme config from config.php file.
896 * @param string $themename
897 * @param object $settings from config_plugins table
898 * @return object
fdeb7fa1 899 */
78946b9b
PS
900 private static function find_theme_config($themename, $settings) {
901 // We have to use the variable name $THEME (upper case) because that
902 // is what is used in theme config.php files.
fdeb7fa1 903
78946b9b
PS
904 if (!$dir = theme_config::find_theme_location($themename)) {
905 return null;
fdeb7fa1 906 }
2f67a9b3 907
78946b9b
PS
908 $THEME = new object();
909 $THEME->name = $themename;
910 $THEME->dir = $dir;
911 $THEME->settings = $settings;
912
913 global $CFG; // just in case somebody tries to use $CFG in theme config
914 include("$THEME->dir/config.php");
915
916 // verify the theme configuration is OK
917 if (!is_array($THEME->parents)) {
918 // parents option is mandatory now
919 return null;
fdeb7fa1 920 }
921
78946b9b 922 return $THEME;
fdeb7fa1 923 }
924
d4a03c00 925 /**
78946b9b
PS
926 * Finds the theme location and verifies the theme has all needed files
927 * and is not obsoleted.
928 * @param string $themename
929 * @return string full dir path or null if not found
d4a03c00 930 */
78946b9b
PS
931 private static function find_theme_location($themename) {
932 global $CFG;
933
934 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
935 $dir = "$CFG->dirroot/theme/$themename";
d4a03c00 936
7d875874 937 } else {
78946b9b 938 return null;
d4a03c00 939 }
78946b9b
PS
940
941 if (file_exists("$dir/styles.php")) {
942 //legacy theme - needs to be upgraded - upgrade info is displayed on the admin settings page
943 return null;
944 }
2f67a9b3 945
78946b9b 946 return $dir;
d4a03c00 947 }
948
1d13c75c 949 /**
78946b9b 950 * Get the renderer for a part of Moodle for this theme.
78946b9b 951 * @param moodle_page $page the page we are rendering
56cbc53b 952 * @param string $module the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
78946b9b 953 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
c927e35c 954 * @param string $target one of rendering target constants
78946b9b 955 * @return renderer_base the requested renderer.
1d13c75c 956 */
c927e35c 957 public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
78946b9b 958 if (is_null($this->rf)) {
c927e35c 959 $classname = $this->rendererfactory;
78946b9b 960 $this->rf = new $classname($this);
1d13c75c 961 }
78946b9b 962
c927e35c 963 return $this->rf->get_renderer($page, $component, $subtype, $target);
1d13c75c 964 }
965
fdeb7fa1 966 /**
78946b9b
PS
967 * Get the information from {@link $layouts} for this type of page.
968 * @param string $pagelayout the the page layout name.
969 * @return array the appropriate part of {@link $layouts}.
fdeb7fa1 970 */
78946b9b
PS
971 protected function layout_info_for_page($pagelayout) {
972 if (array_key_exists($pagelayout, $this->layouts)) {
973 return $this->layouts[$pagelayout];
974 } else {
975 return $this->layouts['normal'];
fdeb7fa1 976 }
977 }
978
979 /**
78946b9b
PS
980 * Given the settings of this theme, and the page pagelayout, return the
981 * full path of the page layout file to use.
982 *
983 * Used by {@link core_renderer::header()}.
984 *
985 * @param string $pagelayout the the page layout name.
986 * @return string Full path to the lyout file to use
ebebf55c 987 */
78946b9b 988 public function layout_file($pagelayout) {
ebebf55c 989 global $CFG;
fdeb7fa1 990
78946b9b
PS
991 $layoutinfo = $this->layout_info_for_page($pagelayout);
992 $layoutfile = $layoutinfo['file'];
993 $theme = $layoutinfo['theme'];
fdeb7fa1 994
78946b9b
PS
995 if ($dir = $this->find_theme_location($theme)) {
996 $path = "$dir/layout/$layoutfile";
fdeb7fa1 997
78946b9b
PS
998 // Check the template exists, return general base theme template if not.
999 if (is_readable($path)) {
1000 return $path;
1001 }
34a2777c 1002 }
1003
78946b9b
PS
1004 // fallback to standard normal layout
1005 return "$CFG->dirroot/theme/base/layout/general.php";
1006 }
ebebf55c 1007
78946b9b
PS
1008 /**
1009 * Returns auxiliary page layout options specified in layout configuration array.
1010 * @param string $pagelayout
1011 * @return array
1012 */
1013 public function pagelayout_options($pagelayout) {
1014 $info = $this->layout_info_for_page($pagelayout);
1015 if (!empty($info['options'])) {
1016 return $info['options'];
34a2777c 1017 }
78946b9b
PS
1018 return array();
1019 }
34a2777c 1020
78946b9b
PS
1021 /**
1022 * Inform a block_manager about the block regions this theme wants on this
1023 * page layout.
1024 * @param string $pagelayout the general type of the page.
1025 * @param block_manager $blockmanager the block_manger to set up.
1026 * @return void
1027 */
1028 public function setup_blocks($pagelayout, $blockmanager) {
1029 $layoutinfo = $this->layout_info_for_page($pagelayout);
1030 if (!empty($layoutinfo['regions'])) {
1031 $blockmanager->add_regions($layoutinfo['regions']);
1032 $blockmanager->set_default_region($layoutinfo['defaultregion']);
ebebf55c 1033 }
34a2777c 1034 }
1035
ebebf55c 1036 /**
78946b9b
PS
1037 * Get the list of all block regions known to this theme in all templates.
1038 * @return array internal region name => human readable name.
ebebf55c 1039 */
78946b9b
PS
1040 public function get_all_block_regions() {
1041 $regions = array();
1042 //TODO: this is weird because the regions from different layouts override each other
1043 foreach ($this->layouts as $layoutinfo) {
1044 foreach ($layoutinfo['regions'] as $region) {
1045 $regions[$region] = get_string('region-' . $region, 'theme_' . $layoutinfo['theme']);
ebebf55c 1046 }
34a2777c 1047 }
78946b9b 1048 return $regions;
34a2777c 1049 }
ebebf55c 1050}
34a2777c 1051
78946b9b 1052
ebebf55c 1053/**
d9c8f425 1054 * This class keeps track of which HTML tags are currently open.
1055 *
1056 * This makes it much easier to always generate well formed XHTML output, even
1057 * if execution terminates abruptly. Any time you output some opening HTML
1058 * without the matching closing HTML, you should push the necessary close tags
1059 * onto the stack.
ebebf55c 1060 *
1061 * @copyright 2009 Tim Hunt
1062 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1063 * @since Moodle 2.0
1064 */
d9c8f425 1065class xhtml_container_stack {
1066 /** @var array stores the list of open containers. */
1067 protected $opencontainers = array();
fa1afe32 1068 /**
d9c8f425 1069 * @var array in developer debug mode, stores a stack trace of all opens and
1070 * closes, so we can output helpful error messages when there is a mismatch.
fa1afe32 1071 */
d9c8f425 1072 protected $log = array();
fa1afe32 1073 /**
d9c8f425 1074 * Store whether we are developer debug mode. We need this in several places
1075 * including in the destructor where we may no thave access to $CFG.
1076 * @var boolean
fa1afe32 1077 */
d9c8f425 1078 protected $isdebugging;
34a2777c 1079
d9c8f425 1080 public function __construct() {
1081 $this->isdebugging = debugging('', DEBUG_DEVELOPER);
ebebf55c 1082 }
34a2777c 1083
fa1afe32 1084 /**
d9c8f425 1085 * Push the close HTML for a recently opened container onto the stack.
1086 * @param string $type The type of container. This is checked when {@link pop()}
1087 * is called and must match, otherwise a developer debug warning is output.
1088 * @param string $closehtml The HTML required to close the container.
1089 * @return void
fa1afe32 1090 */
d9c8f425 1091 public function push($type, $closehtml) {
1092 $container = new stdClass;
1093 $container->type = $type;
1094 $container->closehtml = $closehtml;
1095 if ($this->isdebugging) {
1096 $this->log('Open', $type);
3aaa27f4 1097 }
d9c8f425 1098 array_push($this->opencontainers, $container);
ebebf55c 1099 }
34a2777c 1100
fa1afe32 1101 /**
d9c8f425 1102 * Pop the HTML for the next closing container from the stack. The $type
1103 * must match the type passed when the container was opened, otherwise a
1104 * warning will be output.
1105 * @param string $type The type of container.
1106 * @return string the HTML required to close the container.
fa1afe32 1107 */
d9c8f425 1108 public function pop($type) {
1109 if (empty($this->opencontainers)) {
1110 debugging('<p>There are no more open containers. This suggests there is a nesting problem.</p>' .
1111 $this->output_log(), DEBUG_DEVELOPER);
1112 return;
3aaa27f4 1113 }
ebebf55c 1114
d9c8f425 1115 $container = array_pop($this->opencontainers);
1116 if ($container->type != $type) {
1117 debugging('<p>The type of container to be closed (' . $container->type .
1118 ') does not match the type of the next open container (' . $type .
1119 '). This suggests there is a nesting problem.</p>' .
1120 $this->output_log(), DEBUG_DEVELOPER);
ebebf55c 1121 }
d9c8f425 1122 if ($this->isdebugging) {
1123 $this->log('Close', $type);
e8775320 1124 }
d9c8f425 1125 return $container->closehtml;
ebebf55c 1126 }
e8775320 1127
fa1afe32 1128 /**
d9c8f425 1129 * Close all but the last open container. This is useful in places like error
1130 * handling, where you want to close all the open containers (apart from <body>)
1131 * before outputting the error message.
1132 * @param bool $shouldbenone assert that the stack should be empty now - causes a
1133 * developer debug warning if it isn't.
1134 * @return string the HTML required to close any open containers inside <body>.
fa1afe32 1135 */
d9c8f425 1136 public function pop_all_but_last($shouldbenone = false) {
1137 if ($shouldbenone && count($this->opencontainers) != 1) {
1138 debugging('<p>Some HTML tags were opened in the body of the page but not closed.</p>' .
1139 $this->output_log(), DEBUG_DEVELOPER);
1140 }
1141 $output = '';
1142 while (count($this->opencontainers) > 1) {
1143 $container = array_pop($this->opencontainers);
1144 $output .= $container->closehtml;
e8775320 1145 }
d9c8f425 1146 return $output;
e8775320 1147 }
34a2777c 1148
ebebf55c 1149 /**
d9c8f425 1150 * You can call this function if you want to throw away an instance of this
1151 * class without properly emptying the stack (for example, in a unit test).
1152 * Calling this method stops the destruct method from outputting a developer
1153 * debug warning. After calling this method, the instance can no longer be used.
1154 * @return void
ebebf55c 1155 */
d9c8f425 1156 public function discard() {
1157 $this->opencontainers = null;
ebebf55c 1158 }
d9c8f425 1159
fa1afe32 1160 /**
d9c8f425 1161 * Adds an entry to the log.
1162 * @param string $action The name of the action
1163 * @param string $type The type of action
1164 * @return void
fa1afe32 1165 */
d9c8f425 1166 protected function log($action, $type) {
1167 $this->log[] = '<li>' . $action . ' ' . $type . ' at:' .
1168 format_backtrace(debug_backtrace()) . '</li>';
ebebf55c 1169 }
34a2777c 1170
fa1afe32 1171 /**
d9c8f425 1172 * Outputs the log's contents as a HTML list.
1173 * @return string HTML list of the log
fa1afe32 1174 */
d9c8f425 1175 protected function output_log() {
1176 return '<ul>' . implode("\n", $this->log) . '</ul>';
34a2777c 1177 }
1178}