Merge branch 'MDL-41891_master' of https://github.com/nadavkav/moodle
[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
bff67f49 48} else if ($choose && $device && !$unsettheme && confirm_sesskey()) {
e5824bb9
SH
49 // Load the theme to make sure it is valid.
50 $theme = theme_config::load($choose);
51 // Get the config argument for the chosen device.
c3d2fbf9 52 $themename = core_useragent::get_device_type_cfg_var_name($device);
37959dd4 53 set_config($themename, $theme->name);
8ba55c01 54
752fdfbd
SH
55 // Create a new page for the display of the themes readme.
56 // This ensures that the readme page is shown using the new theme.
57 $confirmpage = new moodle_page();
58 $confirmpage->set_context($PAGE->context);
59 $confirmpage->set_url($PAGE->url);
60 $confirmpage->set_pagelayout($PAGE->pagelayout);
61 $confirmpage->set_pagetype($PAGE->pagetype);
62 $confirmpage->set_title($PAGE->title);
63 $confirmpage->set_heading($PAGE->heading);
64
65 // Get the core renderer for the new theme.
66 $output = $confirmpage->get_renderer('core');
67
68 echo $output->header();
e5824bb9 69 echo $output->heading(get_string('themesaved'));
752fdfbd 70 echo $output->box_start();
37959dd4 71 echo format_text(get_string('choosereadme', 'theme_'.$theme->name), FORMAT_MOODLE);
752fdfbd 72 echo $output->box_end();
0e01784c 73 echo $output->continue_button($CFG->wwwroot . '/theme/index.php');
752fdfbd 74 echo $output->footer();
b7009474 75 exit;
bff67f49 76} else if ($device && $unsettheme && confirm_sesskey() && ($device != 'default')) {
c3d2fbf9
SH
77 // Unset the theme and continue.
78 unset_config(core_useragent::get_device_type_cfg_var_name($device));
bff67f49 79 $device = '';
b7009474 80}
81
37959dd4
AF
82// Otherwise, show either a list of devices, or is enabledevicedetection set to no or a
83// device is specified show a list of themes.
84
fd09397e 85$table = new html_table();
e5824bb9 86$table->data = array();
604cb179 87$heading = '';
7bff5441 88if (!empty($CFG->enabledevicedetection) && empty($device)) {
604cb179 89 $heading = get_string('selectdevice', 'admin');
e5824bb9
SH
90 // Display a list of devices that a user can select a theme for.
91
92 $strthemenotselected = get_string('themenoselected', 'admin');
93 $strthemeselect = get_string('themeselect', 'admin');
94
c3d2fbf9 95 // Display the device selection screen.
e5824bb9 96 $table->id = 'admindeviceselector';
604cb179 97 $table->head = array(get_string('devicetype', 'admin'), get_string('currenttheme', 'admin'), get_string('info'));
37959dd4 98
c3d2fbf9 99 $devices = core_useragent::get_device_type_list();
32b0c696 100 foreach ($devices as $thedevice) {
37959dd4 101
c3d2fbf9
SH
102 $headingthemename = ''; // To output the picked theme name when needed.
103 $themename = core_useragent::get_device_type_theme($thedevice);
32b0c696 104 if (!$themename && $thedevice == 'default') {
37959dd4
AF
105 $themename = theme_config::DEFAULT_THEME;
106 }
107
e5824bb9 108 $screenshotcell = $strthemenotselected;
bff67f49 109 $unsetthemebutton = '';
37959dd4 110 if ($themename) {
c3d2fbf9 111 // Check the theme exists.
e5824bb9
SH
112 $themename = clean_param($themename, PARAM_THEME);
113 if (empty($themename)) {
c3d2fbf9
SH
114 // Likely the theme has been deleted.
115 unset_config(core_useragent::get_device_type_cfg_var_name($thedevice));
e5824bb9
SH
116 } else {
117 $strthemename = get_string('pluginname', 'theme_'.$themename);
c3d2fbf9
SH
118 // Link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes,
119 // not the current one.
120 $screenshoturl = new moodle_url('/theme/image.php',
121 array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
e5824bb9
SH
122 // Contents of the screenshot/preview cell.
123 $screenshotcell = html_writer::empty_tag('img', array('src' => $screenshoturl, 'alt' => $strthemename));
c3d2fbf9 124 // Show the name of the picked theme.
43c741b9 125 $headingthemename = $OUTPUT->heading($strthemename, 3);
e5824bb9 126 }
bff67f49 127 // If not default device then show option to unset theme.
32b0c696 128 if ($thedevice != 'default') {
bff67f49 129 $unsetthemestr = get_string('unsettheme', 'admin');
c3d2fbf9
SH
130 $unsetthemeurl = new moodle_url('/theme/index.php',
131 array('device' => $thedevice, 'sesskey' => sesskey(), 'unsettheme' => true));
bff67f49
RT
132 $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
133 $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
134 }
37959dd4
AF
135 }
136
32b0c696 137 $deviceurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey()));
e5824bb9 138 $select = new single_button($deviceurl, $strthemeselect, 'get');
37959dd4 139
e5824bb9 140 $table->data[] = array(
32b0c696 141 $OUTPUT->heading(ucfirst($thedevice), 3),
e5824bb9 142 $screenshotcell,
bff67f49 143 $headingthemename . $OUTPUT->render($select) . $unsetthemebutton
e5824bb9 144 );
78946b9b 145 }
e5824bb9
SH
146} else {
147 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
148 // of themes to select.
604cb179 149 $heading = get_string('selecttheme', 'admin', $device);
e5824bb9 150 if (empty($device)) {
c3d2fbf9
SH
151 // If $CFG->enabledevicedetection is off this will return 'default'.
152 $device = core_useragent::get_device_type();
ee8df661 153 }
b7009474 154
e5824bb9
SH
155 $table->id = 'adminthemeselector';
156 $table->head = array(get_string('theme'), get_string('info'));
37959dd4 157
bd3b3bba 158 $themes = core_component::get_plugin_list('theme');
78946b9b 159
e5824bb9 160 foreach ($themes as $themename => $themedir) {
37959dd4 161
e5824bb9
SH
162 // Load the theme config.
163 try {
164 $theme = theme_config::load($themename);
165 } catch (Exception $e) {
166 // Bad theme, just skip it for now.
167 continue;
168 }
169 if ($themename !== $theme->name) {
c3d2fbf9 170 // Obsoleted or broken theme, just skip for now.
e5824bb9
SH
171 continue;
172 }
e625feaf 173 if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
e5824bb9
SH
174 // The theme doesn't want to be shown in the theme selector and as theme
175 // designer mode is switched off we will respect that decision.
176 continue;
177 }
178 $strthemename = get_string('pluginname', 'theme_'.$themename);
74df4357 179
e5824bb9
SH
180 // Build the table row, and also a list of items to go in the second cell.
181 $row = array();
182 $infoitems = array();
183 $rowclasses = array();
b7009474 184
c3d2fbf9
SH
185 // Set up bools whether this theme is chosen either main or legacy.
186 $ischosentheme = ($themename == core_useragent::get_device_type_theme($device));
b7009474 187
e5824bb9 188 if ($ischosentheme) {
c3d2fbf9 189 // Is the chosen main theme.
e5824bb9
SH
190 $rowclasses[] = 'selectedtheme';
191 }
0d399dc5 192
c3d2fbf9
SH
193 // Link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes,
194 // not the current one.
195 $screenshotpath = new moodle_url('/theme/image.php',
196 array('theme'=>$themename, 'image'=>'screenshot', 'component'=>'theme'));
e5824bb9
SH
197 // Contents of the first screenshot/preview cell.
198 $row[] = html_writer::empty_tag('img', array('src'=>$screenshotpath, 'alt'=>$strthemename));
199 // Contents of the second cell.
200 $infocell = $OUTPUT->heading($strthemename, 3);
ebb19aba 201
c3d2fbf9 202 // Button to choose this as the main theme or unset this theme for devices other then default.
36ff8d05
RT
203 if (($ischosentheme) && ($device != 'default')) {
204 $unsetthemestr = get_string('unsettheme', 'admin');
c3d2fbf9
SH
205 $unsetthemeurl = new moodle_url('/theme/index.php',
206 array('device' => $device, 'unsettheme' => true, 'sesskey' => sesskey()));
36ff8d05
RT
207 $unsetbutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
208 $infocell .= $OUTPUT->render($unsetbutton);
209 } else if ((!$ischosentheme)) {
210 $setthemestr = get_string('usetheme');
c3d2fbf9
SH
211 $setthemeurl = new moodle_url('/theme/index.php',
212 array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey()));
36ff8d05
RT
213 $setthemebutton = new single_button($setthemeurl, $setthemestr, 'get');
214 $infocell .= $OUTPUT->render($setthemebutton);
215 }
b7009474 216
e5824bb9 217 $row[] = $infocell;
edda2b9a 218
e5824bb9
SH
219 $table->data[$themename] = $row;
220 $table->rowclasses[$themename] = join(' ', $rowclasses);
221 }
b7009474 222}
604cb179
RT
223echo $OUTPUT->header('themeselector');
224echo $OUTPUT->heading($heading);
225
32b0c696
GB
226$params = array('sesskey' => sesskey(), 'reset' => 1);
227if (!empty($device)) {
228 $params['device'] = $device;
229}
230echo $OUTPUT->single_button(new moodle_url('index.php', $params), get_string('themeresetcaches', 'admin'));
b7009474 231
16be8974 232echo html_writer::table($table);
b7009474 233
e625feaf 234echo $OUTPUT->footer();