on-demand release 3.1beta+
[moodle.git] / theme / index.php
CommitLineData
b7009474 1<?php
b7009474 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
574909ef
PS
18 * This page provides the Administration -> ... -> Theme selector UI.
19 *
20 * @package core
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
b7009474 22 */
23
24require_once(dirname(__FILE__) . '/../config.php');
25require_once($CFG->libdir . '/adminlib.php');
26
aff24313 27$choose = optional_param('choose', '', PARAM_PLUGIN);
78946b9b 28$reset = optional_param('reset', 0, PARAM_BOOL);
37959dd4 29$device = optional_param('device', '', PARAM_TEXT);
bff67f49 30$unsettheme = optional_param('unsettheme', 0, PARAM_BOOL);
b7009474 31
32admin_externalpage_setup('themeselector');
33
e5824bb9 34if (!empty($device)) {
c3d2fbf9
SH
35 // Make sure the device requested is valid.
36 $devices = core_useragent::get_device_type_list();
e5824bb9 37 if (!in_array($device, $devices)) {
c3d2fbf9 38 // The provided device isn't a valid device throw an error.
e5824bb9
SH
39 print_error('invaliddevicetype');
40 }
41}
42
b7009474 43unset($SESSION->theme);
44
78946b9b
PS
45if ($reset and confirm_sesskey()) {
46 theme_reset_all_caches();
47
f1ecad60 48} else if ($choose && $device && !theme_is_device_locked($device) && !$unsettheme && confirm_sesskey()) {
e5824bb9
SH
49 // Load the theme to make sure it is valid.
50 $theme = theme_config::load($choose);
f1ecad60 51
e5824bb9 52 // Get the config argument for the chosen device.
c3d2fbf9 53 $themename = core_useragent::get_device_type_cfg_var_name($device);
37959dd4 54 set_config($themename, $theme->name);
8ba55c01 55
752fdfbd
SH
56 // Create a new page for the display of the themes readme.
57 // This ensures that the readme page is shown using the new theme.
58 $confirmpage = new moodle_page();
59 $confirmpage->set_context($PAGE->context);
60 $confirmpage->set_url($PAGE->url);
61 $confirmpage->set_pagelayout($PAGE->pagelayout);
62 $confirmpage->set_pagetype($PAGE->pagetype);
63 $confirmpage->set_title($PAGE->title);
64 $confirmpage->set_heading($PAGE->heading);
65
66 // Get the core renderer for the new theme.
67 $output = $confirmpage->get_renderer('core');
68
69 echo $output->header();
e5824bb9 70 echo $output->heading(get_string('themesaved'));
752fdfbd 71 echo $output->box_start();
37959dd4 72 echo format_text(get_string('choosereadme', 'theme_'.$theme->name), FORMAT_MOODLE);
752fdfbd 73 echo $output->box_end();
0e01784c 74 echo $output->continue_button($CFG->wwwroot . '/theme/index.php');
752fdfbd 75 echo $output->footer();
b7009474 76 exit;
f1ecad60 77} else if ($device && !theme_is_device_locked($device) && $unsettheme && confirm_sesskey() && ($device != 'default')) {
c3d2fbf9
SH
78 // Unset the theme and continue.
79 unset_config(core_useragent::get_device_type_cfg_var_name($device));
bff67f49 80 $device = '';
b7009474 81}
82
37959dd4
AF
83// Otherwise, show either a list of devices, or is enabledevicedetection set to no or a
84// device is specified show a list of themes.
85
fd09397e 86$table = new html_table();
e5824bb9 87$table->data = array();
604cb179 88$heading = '';
7bff5441 89if (!empty($CFG->enabledevicedetection) && empty($device)) {
604cb179 90 $heading = get_string('selectdevice', 'admin');
e5824bb9
SH
91 // Display a list of devices that a user can select a theme for.
92
93 $strthemenotselected = get_string('themenoselected', 'admin');
94 $strthemeselect = get_string('themeselect', 'admin');
95
c3d2fbf9 96 // Display the device selection screen.
e5824bb9 97 $table->id = 'admindeviceselector';
604cb179 98 $table->head = array(get_string('devicetype', 'admin'), get_string('currenttheme', 'admin'), get_string('info'));
37959dd4 99
c3d2fbf9 100 $devices = core_useragent::get_device_type_list();
32b0c696 101 foreach ($devices as $thedevice) {
37959dd4 102
c3d2fbf9
SH
103 $headingthemename = ''; // To output the picked theme name when needed.
104 $themename = core_useragent::get_device_type_theme($thedevice);
32b0c696 105 if (!$themename && $thedevice == 'default') {
37959dd4
AF
106 $themename = theme_config::DEFAULT_THEME;
107 }
f1ecad60 108 $themelocked = theme_is_device_locked($thedevice);
37959dd4 109
e5824bb9 110 $screenshotcell = $strthemenotselected;
bff67f49 111 $unsetthemebutton = '';
37959dd4 112 if ($themename) {
c3d2fbf9 113 // Check the theme exists.
e5824bb9
SH
114 $themename = clean_param($themename, PARAM_THEME);
115 if (empty($themename)) {
c3d2fbf9
SH
116 // Likely the theme has been deleted.
117 unset_config(core_useragent::get_device_type_cfg_var_name($thedevice));
e5824bb9
SH
118 } else {
119 $strthemename = get_string('pluginname', 'theme_'.$themename);
c3d2fbf9
SH
120 // Link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes,
121 // not the current one.
122 $screenshoturl = new moodle_url('/theme/image.php',
123 array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
e5824bb9
SH
124 // Contents of the screenshot/preview cell.
125 $screenshotcell = html_writer::empty_tag('img', array('src' => $screenshoturl, 'alt' => $strthemename));
c3d2fbf9 126 // Show the name of the picked theme.
43c741b9 127 $headingthemename = $OUTPUT->heading($strthemename, 3);
e5824bb9 128 }
bff67f49 129 // If not default device then show option to unset theme.
f1ecad60 130 if ($thedevice != 'default' && !$themelocked) {
bff67f49 131 $unsetthemestr = get_string('unsettheme', 'admin');
c3d2fbf9
SH
132 $unsetthemeurl = new moodle_url('/theme/index.php',
133 array('device' => $thedevice, 'sesskey' => sesskey(), 'unsettheme' => true));
bff67f49
RT
134 $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
135 $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
136 }
37959dd4
AF
137 }
138
32b0c696 139 $deviceurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey()));
f1ecad60
RW
140
141 $select = '';
142 if (!$themelocked) {
143 $select = $OUTPUT->render(new single_button($deviceurl, $strthemeselect, 'get'));
144 }
145
146 $lockwarning = '';
147 if ($themelocked) {
148 $lockwarning = html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
149 }
37959dd4 150
e5824bb9 151 $table->data[] = array(
32b0c696 152 $OUTPUT->heading(ucfirst($thedevice), 3),
e5824bb9 153 $screenshotcell,
f1ecad60 154 $headingthemename . $lockwarning . $select . $unsetthemebutton
e5824bb9 155 );
78946b9b 156 }
e5824bb9
SH
157} else {
158 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
159 // of themes to select.
604cb179 160 $heading = get_string('selecttheme', 'admin', $device);
e5824bb9 161 if (empty($device)) {
c3d2fbf9
SH
162 // If $CFG->enabledevicedetection is off this will return 'default'.
163 $device = core_useragent::get_device_type();
ee8df661 164 }
b7009474 165
f1ecad60 166 $themelocked = theme_is_device_locked($device);
e5824bb9
SH
167 $table->id = 'adminthemeselector';
168 $table->head = array(get_string('theme'), get_string('info'));
37959dd4 169
f1ecad60
RW
170 $themes = array();
171 if ($themelocked) {
172 $heading = get_string('currenttheme', 'admin');
173 $themename = theme_get_locked_theme_for_device($device);
174 $themedirectory = core_component::get_plugin_directory('theme', $themename);
175 $themes[$themename] = $themedirectory;
176 } else {
177 $themes = core_component::get_plugin_list('theme');
178 }
78946b9b 179
e5824bb9 180 foreach ($themes as $themename => $themedir) {
37959dd4 181
e5824bb9
SH
182 // Load the theme config.
183 try {
184 $theme = theme_config::load($themename);
185 } catch (Exception $e) {
186 // Bad theme, just skip it for now.
187 continue;
188 }
189 if ($themename !== $theme->name) {
c3d2fbf9 190 // Obsoleted or broken theme, just skip for now.
e5824bb9
SH
191 continue;
192 }
e625feaf 193 if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
e5824bb9
SH
194 // The theme doesn't want to be shown in the theme selector and as theme
195 // designer mode is switched off we will respect that decision.
196 continue;
197 }
198 $strthemename = get_string('pluginname', 'theme_'.$themename);
74df4357 199
e5824bb9
SH
200 // Build the table row, and also a list of items to go in the second cell.
201 $row = array();
202 $infoitems = array();
203 $rowclasses = array();
b7009474 204
c3d2fbf9
SH
205 // Set up bools whether this theme is chosen either main or legacy.
206 $ischosentheme = ($themename == core_useragent::get_device_type_theme($device));
b7009474 207
e5824bb9 208 if ($ischosentheme) {
c3d2fbf9 209 // Is the chosen main theme.
e5824bb9
SH
210 $rowclasses[] = 'selectedtheme';
211 }
0d399dc5 212
c3d2fbf9
SH
213 // Link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes,
214 // not the current one.
215 $screenshotpath = new moodle_url('/theme/image.php',
216 array('theme'=>$themename, 'image'=>'screenshot', 'component'=>'theme'));
e5824bb9
SH
217 // Contents of the first screenshot/preview cell.
218 $row[] = html_writer::empty_tag('img', array('src'=>$screenshotpath, 'alt'=>$strthemename));
219 // Contents of the second cell.
220 $infocell = $OUTPUT->heading($strthemename, 3);
ebb19aba 221
f1ecad60
RW
222 if ($themelocked) {
223 $infocell .= html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
224 }
225
c3d2fbf9 226 // Button to choose this as the main theme or unset this theme for devices other then default.
f1ecad60
RW
227 if (!$themelocked) {
228 if (($ischosentheme) && ($device != 'default')) {
229 $unsetthemestr = get_string('unsettheme', 'admin');
230 $unsetthemeurl = new moodle_url('/theme/index.php',
231 array('device' => $device, 'unsettheme' => true, 'sesskey' => sesskey()));
232 $unsetbutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
233 $infocell .= $OUTPUT->render($unsetbutton);
234 } else if ((!$ischosentheme)) {
235 $setthemestr = get_string('usetheme');
236 $setthemeurl = new moodle_url('/theme/index.php',
237 array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey()));
238 $setthemebutton = new single_button($setthemeurl, $setthemestr, 'get');
239 $infocell .= $OUTPUT->render($setthemebutton);
240 }
36ff8d05 241 }
b7009474 242
e5824bb9 243 $row[] = $infocell;
edda2b9a 244
e5824bb9
SH
245 $table->data[$themename] = $row;
246 $table->rowclasses[$themename] = join(' ', $rowclasses);
247 }
b7009474 248}
604cb179
RT
249echo $OUTPUT->header('themeselector');
250echo $OUTPUT->heading($heading);
251
32b0c696
GB
252$params = array('sesskey' => sesskey(), 'reset' => 1);
253if (!empty($device)) {
254 $params['device'] = $device;
255}
256echo $OUTPUT->single_button(new moodle_url('index.php', $params), get_string('themeresetcaches', 'admin'));
b7009474 257
16be8974 258echo html_writer::table($table);
b7009474 259
e625feaf 260echo $OUTPUT->footer();