MDL-21124 towards full JS support in themes
[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
38aafea2
PS
384 $configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts',
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
38aafea2
PS
610 /**
611 * Get the javascript URL of this theme
612 * @param bool $encoded false means use & and true use &amp; in URLs
613 * @return moodle_url
614 */
615 public function javascript_url() {
616 global $CFG;
617
618 $rev = theme_get_revision();
619
620 $params = array('theme'=>$this->name,'rev'=>$rev);
621 return new moodle_url($CFG->httpswwwroot.'/theme/javascripts.php', $params);
622 }
623
ebebf55c 624 /**
78946b9b
PS
625 * Returns the content of the one huge CSS merged from all style sheets.
626 * @return string
ebebf55c 627 */
78946b9b 628 public function css_content() {
34a2777c 629 global $CFG;
630
78946b9b 631 $css = array('yui'=>array(), 'plugins'=>array(), 'parents'=>array(), 'theme'=>array());
34a2777c 632
78946b9b 633 //YUI sheets
39302c79
PS
634 $yui_sheets = "/*** YUI reset sheets and grids ***/\n\n";
635 $yui_sheets .= file_get_contents("$CFG->libdir/yui/$CFG->yui3version/cssreset/reset-min.css");
636 $yui_sheets .= file_get_contents("$CFG->libdir/yui/$CFG->yui3version/cssbase/base-min.css");
637 $yui_sheets .= file_get_contents("$CFG->libdir/yui/$CFG->yui3version/cssfonts/fonts-min.css");
638 $yui_sheets .= file_get_contents("$CFG->libdir/yui/$CFG->yui3version/cssgrids/grids-min.css");
639
640 $yui_sheets .= "/*** Standard YUI sheets ***/\n\n";
bf62f187 641 $items = new DirectoryIterator("$CFG->libdir/yui/$CFG->yui2version/assets/skins/sam");
78946b9b
PS
642 foreach ($items as $item) {
643 if ($item->isDot() or !$item->isFile()) {
644 continue;
645 }
646 $filename = $item->getFilename();
647 if (substr($filename, -4) !== '.css') {
648 continue;
649 }
bf62f187 650 $yui_sheets .= file_get_contents("$CFG->libdir/yui/$CFG->yui2version/assets/skins/sam/$filename");
78946b9b
PS
651 }
652 unset($item);
653 unset($items);
654 $yui_sheets = preg_replace('/([a-z-]+)\.(png|gif)/', '[[pix:yui|$1]]', $yui_sheets);
655 $css['yui'][] = $this->post_process($yui_sheets);
656
657 // get plugin sheets
658 $excludes = null;
659 if (is_array($this->plugins_exclude_sheets) or $this->plugins_exclude_sheets === true) {
660 $excludes = $this->plugins_exclude_sheets;
661 } else {
662 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
663 if (!isset($parent_config->plugins_exclude_sheets)) {
664 continue;
665 }
666 if (is_array($parent_config->plugins_exclude_sheets) or $parent_config->plugins_exclude_sheets === true) {
667 $excludes = $parent_config->plugins_exclude_sheets;
668 break;
669 }
670 }
ebebf55c 671 }
78946b9b
PS
672 if ($excludes !== true) {
673 foreach (get_plugin_types() as $type=>$unused) {
674 if ($type === 'theme') {
675 continue;
676 }
677 if (!empty($excludes[$type]) and $excludes[$type] === true) {
678 continue;
679 }
680 $plugins = get_plugin_list($type);
681 foreach ($plugins as $plugin=>$fulldir) {
682 if (!empty($excludes[$type]) and is_array($excludes[$type])
683 and in_array($plugin, $excludes[$type])) {
684 continue;
685 }
686 $sheetfile = "$fulldir/styles.css";
687 if (is_readable($sheetfile)) {
688 $css['plugins'][$type.'_'.$plugin] = $this->post_process("/*** Standard plugin $type/$plugin ***/\n\n" . file_get_contents($sheetfile));
689 }
690 }
691 }
34a2777c 692 }
693
78946b9b
PS
694 // find out wanted parent sheets
695 $excludes = null;
696 if (is_array($this->parents_exclude_sheets) or $this->parents_exclude_sheets === true) {
697 $excludes = $this->parents_exclude_sheets;
698 } else {
699 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
700 if (!isset($parent_config->parents_exclude_sheets)) {
701 continue;
702 }
703 if (is_array($parent_config->parents_exclude_sheets) or $parent_config->parents_exclude_sheets === true) {
704 $excludes = $parent_config->parents_exclude_sheets;
705 break;
706 }
707 }
708 }
709 if ($excludes !== true) {
710 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
711 $parent = $parent_config->name;
712 if (empty($parent_config->sheets)) {
713 continue;
714 }
715 if (!empty($excludes[$parent]) and $excludes[$parent] === true) {
716 continue;
717 }
718 foreach ($parent_config->sheets as $sheet) {
719 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
720 and in_array($sheet, $excludes[$parent])) {
721 continue;
722 }
723 $sheetfile = "$parent_config->dir/style/$sheet.css";
724 if (is_readable($sheetfile)) {
725 $css['parents'][$parent][$sheet] = $this->post_process("/*** Parent theme $parent/$sheet ***/\n\n" . file_get_contents($sheetfile));
726 }
727 }
728 }
729 }
34a2777c 730
78946b9b
PS
731 // current theme sheets
732 if (is_array($this->sheets)) {
733 foreach ($this->sheets as $sheet) {
734 $sheetfile = "$this->dir/style/$sheet.css";
735 if (is_readable($sheetfile)) {
736 $css['theme'][$sheet] = $this->post_process("/*** This theme $sheet ***/\n\n" . file_get_contents($sheetfile));
737 }
ebebf55c 738 }
78946b9b
PS
739 }
740
741 return $css;
742 }
34a2777c 743
78946b9b
PS
744 protected function post_process($css) {
745 // now resolve all image locations
746 if (preg_match_all('/\[\[pix:([a-z_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) {
747 $replaced = array();
748 foreach ($matches as $match) {
749 if (isset($replaced[$match[0]])) {
750 continue;
751 }
752 $replaced[$match[0]] = true;
753 $imagename = $match[2];
754 $component = rtrim($match[1], '|');
c39e5ba2 755 $css = str_replace($match[0], $this->pix_url($imagename, $component)->out(false, array(), false), $css);
ebebf55c 756 }
34a2777c 757 }
17a6649b 758
78946b9b
PS
759 // now resolve all theme settings or do any other postprocessing
760 $csspostprocess = $this->csspostprocess;
761 if (function_exists($csspostprocess)) {
762 $css = $csspostprocess($css, $this);
fdeb7fa1 763 }
764
78946b9b 765 return $css;
ebebf55c 766 }
17a6649b 767
ebebf55c 768 /**
78946b9b
PS
769 * Return the URL for an image
770 *
771 * @param string $imagename the name of the icon.
772 * @param string $component, specification of one plugin like in get_string()
773 * @return moodle_url
fdeb7fa1 774 */
c39e5ba2 775 public function pix_url($imagename, $component) {
fdeb7fa1 776 global $CFG;
78946b9b
PS
777
778 $params = array('theme'=>$this->name, 'image'=>$imagename);
779
780 $rev = theme_get_revision();
781 if ($rev != -1) {
782 $params['rev'] = $rev;
783 }
784 if (!empty($component) and $component !== 'moodle'and $component !== 'core') {
785 $params['component'] = $component;
fdeb7fa1 786 }
78946b9b
PS
787
788 return new moodle_url("$CFG->httpswwwroot/theme/image.php", $params);
fdeb7fa1 789 }
790
791 /**
78946b9b
PS
792 * Resolves the real image location.
793 * @param string $image name of image, may contain relative path
794 * @param string $component
795 * @return string full file path
fdeb7fa1 796 */
78946b9b
PS
797 public function resolve_image_location($image, $component) {
798 global $CFG;
799
800 if ($component === 'moodle' or $component === 'core' or empty($component)) {
801 if ($imagefile = $this->image_exists("$this->dir/pix_core/$image")) {
802 return $imagefile;
803 }
804 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
805 if ($imagefile = $this->image_exists("$parent_config->dir/pix_core/$image")) {
806 return $imagefile;
807 }
808 }
809 if ($imagefile = $this->image_exists("$CFG->dirroot/pix/$image")) {
810 return $imagefile;
811 }
812 return null;
813
814 } else if ($component === 'theme') { //exception
815 if ($image === 'favicon') {
816 return "$this->dir/pix/favicon.ico";
817 }
818 if ($imagefile = $this->image_exists("$this->dir/pix/$image")) {
819 return $imagefile;
820 }
821 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
822 if ($imagefile = $this->image_exists("$parent_config->dir/pix/$image")) {
823 return $imagefile;
824 }
825 }
826 return null;
827
828 } else if ($component === 'yui') {
829 // yui CSS files are parsed automatically and altered on the fly
830 if ($imagefile = $this->image_exists("$this->dir/pix_yui/$image")) {
831 return $imagefile;
832 }
833 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
834 if ($imagefile = $this->image_exists("$parent_config->dir/pix_yui/$image")) {
835 return $imagefile;
836 }
837 }
bf62f187 838 if ($imagefile = $this->image_exists("$CFG->libdir/yui/$CFG->yui2version/assets/skins/sam/$image")) {
78946b9b
PS
839 return $imagefile;
840 }
841 return null;
842
843 } else {
844 if (strpos($component, '_') === false) {
845 $component = 'mod_'.$component;
846 }
847 list($type, $plugin) = explode('_', $component, 2);
848
849 if ($imagefile = $this->image_exists("$this->dir/pix_plugins/$type/$plugin/$image")) {
850 return $imagefile;
851 }
852 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
853 if ($imagefile = $this->image_exists("$parent_config->dir/pix_plugins/$type/$plugin/$image")) {
854 return $imagefile;
855 }
856 }
857 $dir = get_plugin_directory($type, $plugin);
858 if ($imagefile = $this->image_exists("$dir/pix/$image")) {
859 return $imagefile;
860 }
861 return null;
fdeb7fa1 862 }
fdeb7fa1 863 }
864
d4a03c00 865 /**
78946b9b
PS
866 * Checks if file with any image extension exists.
867 * @param string $filepath
868 * @return string image name with extension
d4a03c00 869 */
78946b9b
PS
870 private static function image_exists($filepath) {
871 if (file_exists("$filepath.gif")) {
872 return "$filepath.gif";
873 } else if (file_exists("$filepath.png")) {
874 return "$filepath.png";
875 } else if (file_exists("$filepath.jpg")) {
876 return "$filepath.jpg";
877 } else if (file_exists("$filepath.jpeg")) {
878 return "$filepath.jpeg";
d4a03c00 879 } else {
78946b9b 880 return false;
d4a03c00 881 }
882 }
883
fdeb7fa1 884 /**
78946b9b
PS
885 * Loads the theme config from config.php file.
886 * @param string $themename
887 * @param object $settings from config_plugins table
888 * @return object
fdeb7fa1 889 */
78946b9b
PS
890 private static function find_theme_config($themename, $settings) {
891 // We have to use the variable name $THEME (upper case) because that
892 // is what is used in theme config.php files.
fdeb7fa1 893
78946b9b
PS
894 if (!$dir = theme_config::find_theme_location($themename)) {
895 return null;
fdeb7fa1 896 }
2f67a9b3 897
78946b9b
PS
898 $THEME = new object();
899 $THEME->name = $themename;
900 $THEME->dir = $dir;
901 $THEME->settings = $settings;
902
903 global $CFG; // just in case somebody tries to use $CFG in theme config
904 include("$THEME->dir/config.php");
905
906 // verify the theme configuration is OK
907 if (!is_array($THEME->parents)) {
908 // parents option is mandatory now
909 return null;
fdeb7fa1 910 }
911
78946b9b 912 return $THEME;
fdeb7fa1 913 }
914
d4a03c00 915 /**
78946b9b
PS
916 * Finds the theme location and verifies the theme has all needed files
917 * and is not obsoleted.
918 * @param string $themename
919 * @return string full dir path or null if not found
d4a03c00 920 */
78946b9b
PS
921 private static function find_theme_location($themename) {
922 global $CFG;
923
924 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
925 $dir = "$CFG->dirroot/theme/$themename";
d4a03c00 926
7d875874 927 } else {
78946b9b 928 return null;
d4a03c00 929 }
78946b9b
PS
930
931 if (file_exists("$dir/styles.php")) {
932 //legacy theme - needs to be upgraded - upgrade info is displayed on the admin settings page
933 return null;
934 }
2f67a9b3 935
78946b9b 936 return $dir;
d4a03c00 937 }
938
1d13c75c 939 /**
78946b9b 940 * Get the renderer for a part of Moodle for this theme.
78946b9b 941 * @param moodle_page $page the page we are rendering
56cbc53b 942 * @param string $module the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
78946b9b 943 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
c927e35c 944 * @param string $target one of rendering target constants
78946b9b 945 * @return renderer_base the requested renderer.
1d13c75c 946 */
c927e35c 947 public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
78946b9b 948 if (is_null($this->rf)) {
c927e35c 949 $classname = $this->rendererfactory;
78946b9b 950 $this->rf = new $classname($this);
1d13c75c 951 }
78946b9b 952
c927e35c 953 return $this->rf->get_renderer($page, $component, $subtype, $target);
1d13c75c 954 }
955
fdeb7fa1 956 /**
78946b9b
PS
957 * Get the information from {@link $layouts} for this type of page.
958 * @param string $pagelayout the the page layout name.
959 * @return array the appropriate part of {@link $layouts}.
fdeb7fa1 960 */
78946b9b
PS
961 protected function layout_info_for_page($pagelayout) {
962 if (array_key_exists($pagelayout, $this->layouts)) {
963 return $this->layouts[$pagelayout];
964 } else {
965 return $this->layouts['normal'];
fdeb7fa1 966 }
967 }
968
969 /**
78946b9b
PS
970 * Given the settings of this theme, and the page pagelayout, return the
971 * full path of the page layout file to use.
972 *
973 * Used by {@link core_renderer::header()}.
974 *
975 * @param string $pagelayout the the page layout name.
976 * @return string Full path to the lyout file to use
ebebf55c 977 */
78946b9b 978 public function layout_file($pagelayout) {
ebebf55c 979 global $CFG;
fdeb7fa1 980
78946b9b
PS
981 $layoutinfo = $this->layout_info_for_page($pagelayout);
982 $layoutfile = $layoutinfo['file'];
983 $theme = $layoutinfo['theme'];
fdeb7fa1 984
78946b9b
PS
985 if ($dir = $this->find_theme_location($theme)) {
986 $path = "$dir/layout/$layoutfile";
fdeb7fa1 987
78946b9b
PS
988 // Check the template exists, return general base theme template if not.
989 if (is_readable($path)) {
990 return $path;
991 }
34a2777c 992 }
993
78946b9b
PS
994 // fallback to standard normal layout
995 return "$CFG->dirroot/theme/base/layout/general.php";
996 }
ebebf55c 997
78946b9b
PS
998 /**
999 * Returns auxiliary page layout options specified in layout configuration array.
1000 * @param string $pagelayout
1001 * @return array
1002 */
1003 public function pagelayout_options($pagelayout) {
1004 $info = $this->layout_info_for_page($pagelayout);
1005 if (!empty($info['options'])) {
1006 return $info['options'];
34a2777c 1007 }
78946b9b
PS
1008 return array();
1009 }
34a2777c 1010
78946b9b
PS
1011 /**
1012 * Inform a block_manager about the block regions this theme wants on this
1013 * page layout.
1014 * @param string $pagelayout the general type of the page.
1015 * @param block_manager $blockmanager the block_manger to set up.
1016 * @return void
1017 */
1018 public function setup_blocks($pagelayout, $blockmanager) {
1019 $layoutinfo = $this->layout_info_for_page($pagelayout);
1020 if (!empty($layoutinfo['regions'])) {
1021 $blockmanager->add_regions($layoutinfo['regions']);
1022 $blockmanager->set_default_region($layoutinfo['defaultregion']);
ebebf55c 1023 }
34a2777c 1024 }
1025
ebebf55c 1026 /**
78946b9b
PS
1027 * Get the list of all block regions known to this theme in all templates.
1028 * @return array internal region name => human readable name.
ebebf55c 1029 */
78946b9b
PS
1030 public function get_all_block_regions() {
1031 $regions = array();
1032 //TODO: this is weird because the regions from different layouts override each other
1033 foreach ($this->layouts as $layoutinfo) {
1034 foreach ($layoutinfo['regions'] as $region) {
1035 $regions[$region] = get_string('region-' . $region, 'theme_' . $layoutinfo['theme']);
ebebf55c 1036 }
34a2777c 1037 }
78946b9b 1038 return $regions;
34a2777c 1039 }
ebebf55c 1040}
34a2777c 1041
78946b9b 1042
ebebf55c 1043/**
d9c8f425 1044 * This class keeps track of which HTML tags are currently open.
1045 *
1046 * This makes it much easier to always generate well formed XHTML output, even
1047 * if execution terminates abruptly. Any time you output some opening HTML
1048 * without the matching closing HTML, you should push the necessary close tags
1049 * onto the stack.
ebebf55c 1050 *
1051 * @copyright 2009 Tim Hunt
1052 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1053 * @since Moodle 2.0
1054 */
d9c8f425 1055class xhtml_container_stack {
1056 /** @var array stores the list of open containers. */
1057 protected $opencontainers = array();
fa1afe32 1058 /**
d9c8f425 1059 * @var array in developer debug mode, stores a stack trace of all opens and
1060 * closes, so we can output helpful error messages when there is a mismatch.
fa1afe32 1061 */
d9c8f425 1062 protected $log = array();
fa1afe32 1063 /**
d9c8f425 1064 * Store whether we are developer debug mode. We need this in several places
1065 * including in the destructor where we may no thave access to $CFG.
1066 * @var boolean
fa1afe32 1067 */
d9c8f425 1068 protected $isdebugging;
34a2777c 1069
d9c8f425 1070 public function __construct() {
1071 $this->isdebugging = debugging('', DEBUG_DEVELOPER);
ebebf55c 1072 }
34a2777c 1073
fa1afe32 1074 /**
d9c8f425 1075 * Push the close HTML for a recently opened container onto the stack.
1076 * @param string $type The type of container. This is checked when {@link pop()}
1077 * is called and must match, otherwise a developer debug warning is output.
1078 * @param string $closehtml The HTML required to close the container.
1079 * @return void
fa1afe32 1080 */
d9c8f425 1081 public function push($type, $closehtml) {
1082 $container = new stdClass;
1083 $container->type = $type;
1084 $container->closehtml = $closehtml;
1085 if ($this->isdebugging) {
1086 $this->log('Open', $type);
3aaa27f4 1087 }
d9c8f425 1088 array_push($this->opencontainers, $container);
ebebf55c 1089 }
34a2777c 1090
fa1afe32 1091 /**
d9c8f425 1092 * Pop the HTML for the next closing container from the stack. The $type
1093 * must match the type passed when the container was opened, otherwise a
1094 * warning will be output.
1095 * @param string $type The type of container.
1096 * @return string the HTML required to close the container.
fa1afe32 1097 */
d9c8f425 1098 public function pop($type) {
1099 if (empty($this->opencontainers)) {
1100 debugging('<p>There are no more open containers. This suggests there is a nesting problem.</p>' .
1101 $this->output_log(), DEBUG_DEVELOPER);
1102 return;
3aaa27f4 1103 }
ebebf55c 1104
d9c8f425 1105 $container = array_pop($this->opencontainers);
1106 if ($container->type != $type) {
1107 debugging('<p>The type of container to be closed (' . $container->type .
1108 ') does not match the type of the next open container (' . $type .
1109 '). This suggests there is a nesting problem.</p>' .
1110 $this->output_log(), DEBUG_DEVELOPER);
ebebf55c 1111 }
d9c8f425 1112 if ($this->isdebugging) {
1113 $this->log('Close', $type);
e8775320 1114 }
d9c8f425 1115 return $container->closehtml;
ebebf55c 1116 }
e8775320 1117
fa1afe32 1118 /**
d9c8f425 1119 * Close all but the last open container. This is useful in places like error
1120 * handling, where you want to close all the open containers (apart from <body>)
1121 * before outputting the error message.
1122 * @param bool $shouldbenone assert that the stack should be empty now - causes a
1123 * developer debug warning if it isn't.
1124 * @return string the HTML required to close any open containers inside <body>.
fa1afe32 1125 */
d9c8f425 1126 public function pop_all_but_last($shouldbenone = false) {
1127 if ($shouldbenone && count($this->opencontainers) != 1) {
1128 debugging('<p>Some HTML tags were opened in the body of the page but not closed.</p>' .
1129 $this->output_log(), DEBUG_DEVELOPER);
1130 }
1131 $output = '';
1132 while (count($this->opencontainers) > 1) {
1133 $container = array_pop($this->opencontainers);
1134 $output .= $container->closehtml;
e8775320 1135 }
d9c8f425 1136 return $output;
e8775320 1137 }
34a2777c 1138
ebebf55c 1139 /**
d9c8f425 1140 * You can call this function if you want to throw away an instance of this
1141 * class without properly emptying the stack (for example, in a unit test).
1142 * Calling this method stops the destruct method from outputting a developer
1143 * debug warning. After calling this method, the instance can no longer be used.
1144 * @return void
ebebf55c 1145 */
d9c8f425 1146 public function discard() {
1147 $this->opencontainers = null;
ebebf55c 1148 }
d9c8f425 1149
fa1afe32 1150 /**
d9c8f425 1151 * Adds an entry to the log.
1152 * @param string $action The name of the action
1153 * @param string $type The type of action
1154 * @return void
fa1afe32 1155 */
d9c8f425 1156 protected function log($action, $type) {
1157 $this->log[] = '<li>' . $action . ' ' . $type . ' at:' .
1158 format_backtrace(debug_backtrace()) . '</li>';
ebebf55c 1159 }
34a2777c 1160
fa1afe32 1161 /**
d9c8f425 1162 * Outputs the log's contents as a HTML list.
1163 * @return string HTML list of the log
fa1afe32 1164 */
d9c8f425 1165 protected function output_log() {
1166 return '<ul>' . implode("\n", $this->log) . '</ul>';
34a2777c 1167 }
1168}