Moodle release 4.1.3
[moodle.git] / theme / index.php
blob65afd7e015d8d5ac6a94c033ec692d4862cdcbe9
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(__DIR__ . '/../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);
31 $confirmation = optional_param('confirmation', 0, PARAM_BOOL);
33 admin_externalpage_setup('themeselector');
35 if (!empty($device)) {
36 // Make sure the device requested is valid.
37 $devices = core_useragent::get_device_type_list();
38 if (!in_array($device, $devices)) {
39 // The provided device isn't a valid device throw an error.
40 throw new \moodle_exception('invaliddevicetype');
44 unset($SESSION->theme);
46 $PAGE->set_primary_active_tab('siteadminnode');
47 $PAGE->navbar->add(get_string('themeselector', 'admin'), $PAGE->url);
49 if ($reset and confirm_sesskey()) {
50 theme_reset_all_caches();
51 } else if ($choose && $confirmation) {
53 $theme = theme_config::load($choose);
54 echo $OUTPUT->header();
55 echo $OUTPUT->heading(get_string('themesaved'));
56 echo $OUTPUT->box_start();
57 echo format_text(get_string('choosereadme', 'theme_'.$theme->name), FORMAT_MOODLE);
58 echo $OUTPUT->box_end();
59 echo $OUTPUT->continue_button($CFG->wwwroot . '/theme/index.php');
60 echo $OUTPUT->footer();
61 exit;
63 } else if ($choose && $device && !theme_is_device_locked($device) && !$unsettheme && confirm_sesskey()) {
64 // Load the theme to make sure it is valid.
65 $theme = theme_config::load($choose);
67 // Get the config argument for the chosen device.
68 $themename = core_useragent::get_device_type_cfg_var_name($device);
69 set_config($themename, $theme->name);
71 $urlconfirm = new moodle_url('/theme/index.php', array('confirmation' => 1, 'choose' => $choose));
72 redirect($urlconfirm);
73 } else if ($device && !theme_is_device_locked($device) && $unsettheme && confirm_sesskey() && ($device != 'default')) {
74 // Unset the theme and continue.
75 unset_config(core_useragent::get_device_type_cfg_var_name($device));
76 $device = '';
79 // Otherwise, show either a list of devices, or is enabledevicedetection set to no or a
80 // device is specified show a list of themes.
82 $table = new html_table();
83 $table->data = array();
84 $heading = '';
85 if (!empty($CFG->enabledevicedetection) && empty($device)) {
86 $heading = get_string('selectdevice', 'admin');
87 // Display a list of devices that a user can select a theme for.
89 $strthemenotselected = get_string('themenoselected', 'admin');
90 $strthemeselect = get_string('themeselect', 'admin');
92 // Display the device selection screen.
93 $table->id = 'admindeviceselector';
94 $table->head = array(get_string('devicetype', 'admin'), get_string('currenttheme', 'admin'), get_string('info'));
96 $devices = core_useragent::get_device_type_list();
97 foreach ($devices as $thedevice) {
99 $headingthemename = ''; // To output the picked theme name when needed.
100 $themename = core_useragent::get_device_type_theme($thedevice);
101 if (!$themename && $thedevice == 'default') {
102 $themename = theme_config::DEFAULT_THEME;
104 $themelocked = theme_is_device_locked($thedevice);
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(core_useragent::get_device_type_cfg_var_name($thedevice));
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,
117 // not the current one.
118 $screenshoturl = new moodle_url('/theme/image.php',
119 array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
120 // Contents of the screenshot/preview cell.
121 $screenshotcell = html_writer::empty_tag('img', array('class' => 'img-fluid',
122 'src' => $screenshoturl, 'alt' => $strthemename));
123 // Show the name of the picked theme.
124 $headingthemename = $OUTPUT->heading($strthemename, 3);
126 // If not default device then show option to unset theme.
127 if ($thedevice != 'default' && !$themelocked) {
128 $unsetthemestr = get_string('unsettheme', 'admin');
129 $unsetthemeurl = new moodle_url('/theme/index.php',
130 array('device' => $thedevice, 'sesskey' => sesskey(), 'unsettheme' => true));
131 $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
132 $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
136 $deviceurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey()));
138 $select = '';
139 if (!$themelocked) {
140 $select = $OUTPUT->render(new single_button($deviceurl, $strthemeselect, 'get'));
143 $lockwarning = '';
144 if ($themelocked) {
145 $lockwarning = html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
148 $table->data[] = array(
149 $OUTPUT->heading(ucfirst($thedevice), 3),
150 $screenshotcell,
151 $headingthemename . $lockwarning . $select . $unsetthemebutton
154 } else {
155 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
156 // of themes to select.
157 $heading = get_string('selecttheme', 'admin', $device);
158 if (empty($device)) {
159 // If $CFG->enabledevicedetection is off this will return 'default'.
160 $device = core_useragent::get_device_type();
163 $themelocked = theme_is_device_locked($device);
164 $table->id = 'adminthemeselector';
165 $table->head = array(get_string('theme'), get_string('info'));
167 $themes = array();
168 if ($themelocked) {
169 $heading = get_string('currenttheme', 'admin');
170 $themename = theme_get_locked_theme_for_device($device);
171 $themedirectory = core_component::get_plugin_directory('theme', $themename);
172 $themes[$themename] = $themedirectory;
173 } else {
174 $themes = core_component::get_plugin_list('theme');
177 foreach ($themes as $themename => $themedir) {
179 // Load the theme config.
180 try {
181 $theme = theme_config::load($themename);
182 } catch (Exception $e) {
183 // Bad theme, just skip it for now.
184 continue;
186 if ($themename !== $theme->name) {
187 // Obsoleted or broken theme, just skip for now.
188 continue;
190 if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
191 // The theme doesn't want to be shown in the theme selector and as theme
192 // designer mode is switched off we will respect that decision.
193 continue;
195 $strthemename = get_string('pluginname', 'theme_'.$themename);
197 // Build the table row, and also a list of items to go in the second cell.
198 $row = array();
199 $infoitems = array();
200 $rowclasses = array();
202 // Set up bools whether this theme is chosen either main or legacy.
203 $ischosentheme = ($themename == core_useragent::get_device_type_theme($device));
205 if ($ischosentheme) {
206 // Is the chosen main theme.
207 $rowclasses[] = 'selectedtheme';
210 // Link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes,
211 // not the current one.
212 $screenshotpath = new moodle_url('/theme/image.php',
213 array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
214 // Contents of the first screenshot/preview cell.
215 $row[] = html_writer::empty_tag('img', array('class' => 'img-fluid',
216 'src' => $screenshotpath, 'alt' => $strthemename));
217 // Contents of the second cell.
218 $infocell = $OUTPUT->heading($strthemename, 3);
220 if ($themelocked) {
221 $infocell .= html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
224 // Button to choose this as the main theme or unset this theme for devices other then default.
225 if (!$themelocked) {
226 if (($ischosentheme) && ($device != 'default')) {
227 $unsetthemestr = get_string('unsettheme', 'admin');
228 $unsetthemeurl = new moodle_url('/theme/index.php',
229 array('device' => $device, 'unsettheme' => true, 'sesskey' => sesskey()));
230 $unsetbutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
231 $infocell .= $OUTPUT->render($unsetbutton);
232 } else if ((!$ischosentheme)) {
233 $setthemestr = get_string('usetheme');
234 $setthemeurl = new moodle_url('/theme/index.php',
235 array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey()));
236 $setthemebutton = new single_button($setthemeurl, $setthemestr, 'get');
237 $infocell .= $OUTPUT->render($setthemebutton);
241 $row[] = $infocell;
243 $table->data[$themename] = $row;
244 $table->rowclasses[$themename] = join(' ', $rowclasses);
247 echo $OUTPUT->header('themeselector');
248 echo $OUTPUT->heading($heading);
250 $params = array('sesskey' => sesskey(), 'reset' => 1);
251 if (!empty($device)) {
252 $params['device'] = $device;
254 echo $OUTPUT->single_button(new moodle_url('index.php', $params), get_string('themeresetcaches', 'admin'));
256 echo html_writer::table($table);
258 echo $OUTPUT->footer();