MDL-52214 core: Fix the is_web_crawler() regression
[moodle.git] / theme / index.php
blob788e3e8998cd903dd97db915679063ae6ef5943a
1 <?php
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.
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/>.
17 /**
18 * This page provides the Administration -> ... -> Theme selector UI.
20 * @package core
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 require_once(dirname(__FILE__) . '/../config.php');
25 require_once($CFG->libdir . '/adminlib.php');
27 $choose = optional_param('choose', '', PARAM_PLUGIN);
28 $reset = optional_param('reset', 0, PARAM_BOOL);
29 $device = optional_param('device', '', PARAM_TEXT);
30 $unsettheme = optional_param('unsettheme', 0, PARAM_BOOL);
32 admin_externalpage_setup('themeselector');
34 if (!empty($device)) {
35 // Make sure the device requested is valid.
36 $devices = core_useragent::get_device_type_list();
37 if (!in_array($device, $devices)) {
38 // The provided device isn't a valid device throw an error.
39 print_error('invaliddevicetype');
43 unset($SESSION->theme);
45 if ($reset and confirm_sesskey()) {
46 theme_reset_all_caches();
48 } else if ($choose && $device && !theme_is_device_locked($device) && !$unsettheme && confirm_sesskey()) {
49 // Load the theme to make sure it is valid.
50 $theme = theme_config::load($choose);
52 // Get the config argument for the chosen device.
53 $themename = core_useragent::get_device_type_cfg_var_name($device);
54 set_config($themename, $theme->name);
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);
66 // Get the core renderer for the new theme.
67 $output = $confirmpage->get_renderer('core');
69 echo $output->header();
70 echo $output->heading(get_string('themesaved'));
71 echo $output->box_start();
72 echo format_text(get_string('choosereadme', 'theme_'.$theme->name), FORMAT_MOODLE);
73 echo $output->box_end();
74 echo $output->continue_button($CFG->wwwroot . '/theme/index.php');
75 echo $output->footer();
76 exit;
77 } else if ($device && !theme_is_device_locked($device) && $unsettheme && confirm_sesskey() && ($device != 'default')) {
78 // Unset the theme and continue.
79 unset_config(core_useragent::get_device_type_cfg_var_name($device));
80 $device = '';
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.
86 $table = new html_table();
87 $table->data = array();
88 $heading = '';
89 if (!empty($CFG->enabledevicedetection) && empty($device)) {
90 $heading = get_string('selectdevice', 'admin');
91 // Display a list of devices that a user can select a theme for.
93 $strthemenotselected = get_string('themenoselected', 'admin');
94 $strthemeselect = get_string('themeselect', 'admin');
96 // Display the device selection screen.
97 $table->id = 'admindeviceselector';
98 $table->head = array(get_string('devicetype', 'admin'), get_string('currenttheme', 'admin'), get_string('info'));
100 $devices = core_useragent::get_device_type_list();
101 foreach ($devices as $thedevice) {
103 $headingthemename = ''; // To output the picked theme name when needed.
104 $themename = core_useragent::get_device_type_theme($thedevice);
105 if (!$themename && $thedevice == 'default') {
106 $themename = theme_config::DEFAULT_THEME;
108 $themelocked = theme_is_device_locked($thedevice);
110 $screenshotcell = $strthemenotselected;
111 $unsetthemebutton = '';
112 if ($themename) {
113 // Check the theme exists.
114 $themename = clean_param($themename, PARAM_THEME);
115 if (empty($themename)) {
116 // Likely the theme has been deleted.
117 unset_config(core_useragent::get_device_type_cfg_var_name($thedevice));
118 } else {
119 $strthemename = get_string('pluginname', 'theme_'.$themename);
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'));
124 // Contents of the screenshot/preview cell.
125 $screenshotcell = html_writer::empty_tag('img', array('src' => $screenshoturl, 'alt' => $strthemename));
126 // Show the name of the picked theme.
127 $headingthemename = $OUTPUT->heading($strthemename, 3);
129 // If not default device then show option to unset theme.
130 if ($thedevice != 'default' && !$themelocked) {
131 $unsetthemestr = get_string('unsettheme', 'admin');
132 $unsetthemeurl = new moodle_url('/theme/index.php',
133 array('device' => $thedevice, 'sesskey' => sesskey(), 'unsettheme' => true));
134 $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
135 $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
139 $deviceurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey()));
141 $select = '';
142 if (!$themelocked) {
143 $select = $OUTPUT->render(new single_button($deviceurl, $strthemeselect, 'get'));
146 $lockwarning = '';
147 if ($themelocked) {
148 $lockwarning = html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
151 $table->data[] = array(
152 $OUTPUT->heading(ucfirst($thedevice), 3),
153 $screenshotcell,
154 $headingthemename . $lockwarning . $select . $unsetthemebutton
157 } else {
158 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
159 // of themes to select.
160 $heading = get_string('selecttheme', 'admin', $device);
161 if (empty($device)) {
162 // If $CFG->enabledevicedetection is off this will return 'default'.
163 $device = core_useragent::get_device_type();
166 $themelocked = theme_is_device_locked($device);
167 $table->id = 'adminthemeselector';
168 $table->head = array(get_string('theme'), get_string('info'));
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');
180 foreach ($themes as $themename => $themedir) {
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;
189 if ($themename !== $theme->name) {
190 // Obsoleted or broken theme, just skip for now.
191 continue;
193 if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
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;
198 $strthemename = get_string('pluginname', 'theme_'.$themename);
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();
205 // Set up bools whether this theme is chosen either main or legacy.
206 $ischosentheme = ($themename == core_useragent::get_device_type_theme($device));
208 if ($ischosentheme) {
209 // Is the chosen main theme.
210 $rowclasses[] = 'selectedtheme';
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'));
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);
222 if ($themelocked) {
223 $infocell .= html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
226 // Button to choose this as the main theme or unset this theme for devices other then default.
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);
243 $row[] = $infocell;
245 $table->data[$themename] = $row;
246 $table->rowclasses[$themename] = join(' ', $rowclasses);
249 echo $OUTPUT->header('themeselector');
250 echo $OUTPUT->heading($heading);
252 $params = array('sesskey' => sesskey(), 'reset' => 1);
253 if (!empty($device)) {
254 $params['device'] = $device;
256 echo $OUTPUT->single_button(new moodle_url('index.php', $params), get_string('themeresetcaches', 'admin'));
258 echo html_writer::table($table);
260 echo $OUTPUT->footer();