Merge branch 'w12_MDL-38329_m23_webkit' of git://github.com/skodak/moodle into MOODLE...
[moodle.git] / theme / index.php
blobce2a3d10fd108b243fa567399a466f98f2c67545
1 <?php
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.
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/>.
18 /**
19 * This page prvides the Administration -> ... -> Theme selector UI.
22 require_once(dirname(__FILE__) . '/../config.php');
23 require_once($CFG->libdir . '/adminlib.php');
25 $choose = optional_param('choose', '', PARAM_PLUGIN);
26 $reset = optional_param('reset', 0, PARAM_BOOL);
27 $device = optional_param('device', '', PARAM_TEXT);
28 $unsettheme = optional_param('unsettheme', 0, PARAM_BOOL);
30 admin_externalpage_setup('themeselector');
32 if (!empty($device)) {
33 // Make sure the device requested is valid
34 $devices = get_device_type_list();
35 if (!in_array($device, $devices)) {
36 // The provided device isn't a valid device throw an error
37 print_error('invaliddevicetype');
41 unset($SESSION->theme);
43 if ($reset and confirm_sesskey()) {
44 theme_reset_all_caches();
46 } else if ($choose && $device && !$unsettheme && confirm_sesskey()) {
47 // Load the theme to make sure it is valid.
48 $theme = theme_config::load($choose);
49 // Get the config argument for the chosen device.
50 $themename = get_device_cfg_var_name($device);
51 set_config($themename, $theme->name);
53 // Create a new page for the display of the themes readme.
54 // This ensures that the readme page is shown using the new theme.
55 $confirmpage = new moodle_page();
56 $confirmpage->set_context($PAGE->context);
57 $confirmpage->set_url($PAGE->url);
58 $confirmpage->set_pagelayout($PAGE->pagelayout);
59 $confirmpage->set_pagetype($PAGE->pagetype);
60 $confirmpage->set_title($PAGE->title);
61 $confirmpage->set_heading($PAGE->heading);
63 // Get the core renderer for the new theme.
64 $output = $confirmpage->get_renderer('core');
66 echo $output->header();
67 echo $output->heading(get_string('themesaved'));
68 echo $output->box_start();
69 echo format_text(get_string('choosereadme', 'theme_'.$theme->name), FORMAT_MOODLE);
70 echo $output->box_end();
71 echo $output->continue_button($CFG->wwwroot . '/theme/index.php');
72 echo $output->footer();
73 exit;
74 } else if ($device && $unsettheme && confirm_sesskey() && ($device != 'default')) {
75 //Unset the theme and continue.
76 unset_config(get_device_cfg_var_name($device));
77 $device = '';
80 // Otherwise, show either a list of devices, or is enabledevicedetection set to no or a
81 // device is specified show a list of themes.
83 $table = new html_table();
84 $table->data = array();
85 $heading = '';
86 if (!empty($CFG->enabledevicedetection) && empty($device)) {
87 $heading = get_string('selectdevice', 'admin');
88 // Display a list of devices that a user can select a theme for.
90 $strthemenotselected = get_string('themenoselected', 'admin');
91 $strthemeselect = get_string('themeselect', 'admin');
93 // Display the device selection screen
94 $table->id = 'admindeviceselector';
95 $table->head = array(get_string('devicetype', 'admin'), get_string('currenttheme', 'admin'), get_string('info'));
97 $devices = get_device_type_list();
98 foreach ($devices as $device) {
100 $headingthemename = ''; // To output the picked theme name when needed
101 $themename = get_selected_theme_for_device_type($device);
102 if (!$themename && $device == 'default') {
103 $themename = theme_config::DEFAULT_THEME;
106 $screenshotcell = $strthemenotselected;
107 $unsetthemebutton = '';
108 if ($themename) {
109 // Check the theme exists
110 $themename = clean_param($themename, PARAM_THEME);
111 if (empty($themename)) {
112 // Likely the theme has been deleted
113 unset_config(get_device_cfg_var_name($device));
114 } else {
115 $strthemename = get_string('pluginname', 'theme_'.$themename);
116 // link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes, not the current one
117 $screenshoturl = new moodle_url('/theme/image.php', array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
118 // Contents of the screenshot/preview cell.
119 $screenshotcell = html_writer::empty_tag('img', array('src' => $screenshoturl, 'alt' => $strthemename));
120 // Show the name of the picked theme
121 $headingthemename = $OUTPUT->heading($strthemename, 3);
123 // If not default device then show option to unset theme.
124 if ($device != 'default') {
125 $unsetthemestr = get_string('unsettheme', 'admin');
126 $unsetthemeurl = new moodle_url('/theme/index.php', array('device' => $device, 'sesskey' => sesskey(), 'unsettheme' => true));
127 $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
128 $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
132 $deviceurl = new moodle_url('/theme/index.php', array('device' => $device, 'sesskey' => sesskey()));
133 $select = new single_button($deviceurl, $strthemeselect, 'get');
135 $table->data[] = array(
136 $OUTPUT->heading(ucfirst($device), 3),
137 $screenshotcell,
138 $headingthemename . $OUTPUT->render($select) . $unsetthemebutton
141 } else {
142 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
143 // of themes to select.
144 $heading = get_string('selecttheme', 'admin', $device);
145 if (empty($device)) {
146 // if $CFG->enabledevicedetection is off this will return 'default'
147 $device = get_device_type();
150 $table->id = 'adminthemeselector';
151 $table->head = array(get_string('theme'), get_string('info'));
153 $themes = get_plugin_list('theme');
155 foreach ($themes as $themename => $themedir) {
157 // Load the theme config.
158 try {
159 $theme = theme_config::load($themename);
160 } catch (Exception $e) {
161 // Bad theme, just skip it for now.
162 continue;
164 if ($themename !== $theme->name) {
165 //obsoleted or broken theme, just skip for now
166 continue;
168 if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
169 // The theme doesn't want to be shown in the theme selector and as theme
170 // designer mode is switched off we will respect that decision.
171 continue;
173 $strthemename = get_string('pluginname', 'theme_'.$themename);
175 // Build the table row, and also a list of items to go in the second cell.
176 $row = array();
177 $infoitems = array();
178 $rowclasses = array();
180 // Set up bools whether this theme is chosen either main or legacy
181 $ischosentheme = ($themename == get_selected_theme_for_device_type($device));
183 if ($ischosentheme) {
184 // Is the chosen main theme
185 $rowclasses[] = 'selectedtheme';
188 // link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes, not the current one
189 $screenshotpath = new moodle_url('/theme/image.php', array('theme'=>$themename, 'image'=>'screenshot', 'component'=>'theme'));
190 // Contents of the first screenshot/preview cell.
191 $row[] = html_writer::empty_tag('img', array('src'=>$screenshotpath, 'alt'=>$strthemename));
192 // Contents of the second cell.
193 $infocell = $OUTPUT->heading($strthemename, 3);
195 // Button to choose this as the main theme or unset this theme for
196 // devices other then default
197 if (($ischosentheme) && ($device != 'default')) {
198 $unsetthemestr = get_string('unsettheme', 'admin');
199 $unsetthemeurl = new moodle_url('/theme/index.php', array('device' => $device, 'unsettheme' => true, 'sesskey' => sesskey()));
200 $unsetbutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
201 $infocell .= $OUTPUT->render($unsetbutton);
202 } else if ((!$ischosentheme)) {
203 $setthemestr = get_string('usetheme');
204 $setthemeurl = new moodle_url('/theme/index.php', array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey()));
205 $setthemebutton = new single_button($setthemeurl, $setthemestr, 'get');
206 $infocell .= $OUTPUT->render($setthemebutton);
209 $row[] = $infocell;
211 $table->data[$themename] = $row;
212 $table->rowclasses[$themename] = join(' ', $rowclasses);
215 echo $OUTPUT->header('themeselector');
216 echo $OUTPUT->heading($heading);
218 echo $OUTPUT->single_button(new moodle_url('index.php', array('sesskey' => sesskey(), 'reset' => 1, 'device' => $device)), get_string('themeresetcaches', 'admin'));
220 echo html_writer::table($table);
222 echo $OUTPUT->footer();