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