on demand release 2.5beta+
[moodle.git] / theme / index.php
CommitLineData
b7009474 1<?php
2
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.
14//
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/>.
17
18/**
19 * This page prvides the Administration -> ... -> Theme selector UI.
20 */
21
22require_once(dirname(__FILE__) . '/../config.php');
23require_once($CFG->libdir . '/adminlib.php');
24
aff24313 25$choose = optional_param('choose', '', PARAM_PLUGIN);
78946b9b 26$reset = optional_param('reset', 0, PARAM_BOOL);
37959dd4 27$device = optional_param('device', '', PARAM_TEXT);
bff67f49 28$unsettheme = optional_param('unsettheme', 0, PARAM_BOOL);
b7009474 29
30admin_externalpage_setup('themeselector');
31
e5824bb9
SH
32if (!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');
38 }
39}
40
b7009474 41unset($SESSION->theme);
42
78946b9b
PS
43if ($reset and confirm_sesskey()) {
44 theme_reset_all_caches();
45
bff67f49 46} else if ($choose && $device && !$unsettheme && confirm_sesskey()) {
e5824bb9
SH
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.
37959dd4 50 $themename = get_device_cfg_var_name($device);
37959dd4 51 set_config($themename, $theme->name);
8ba55c01 52
752fdfbd
SH
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);
62
63 // Get the core renderer for the new theme.
64 $output = $confirmpage->get_renderer('core');
65
66 echo $output->header();
e5824bb9 67 echo $output->heading(get_string('themesaved'));
752fdfbd 68 echo $output->box_start();
37959dd4 69 echo format_text(get_string('choosereadme', 'theme_'.$theme->name), FORMAT_MOODLE);
752fdfbd 70 echo $output->box_end();
0e01784c 71 echo $output->continue_button($CFG->wwwroot . '/theme/index.php');
752fdfbd 72 echo $output->footer();
b7009474 73 exit;
bff67f49
RT
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 = '';
b7009474 78}
79
37959dd4
AF
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.
82
fd09397e 83$table = new html_table();
e5824bb9 84$table->data = array();
604cb179 85$heading = '';
7bff5441 86if (!empty($CFG->enabledevicedetection) && empty($device)) {
604cb179 87 $heading = get_string('selectdevice', 'admin');
e5824bb9
SH
88 // Display a list of devices that a user can select a theme for.
89
90 $strthemenotselected = get_string('themenoselected', 'admin');
91 $strthemeselect = get_string('themeselect', 'admin');
92
93 // Display the device selection screen
94 $table->id = 'admindeviceselector';
604cb179 95 $table->head = array(get_string('devicetype', 'admin'), get_string('currenttheme', 'admin'), get_string('info'));
37959dd4
AF
96
97 $devices = get_device_type_list();
37959dd4 98 foreach ($devices as $device) {
37959dd4 99
43c741b9 100 $headingthemename = ''; // To output the picked theme name when needed
37959dd4 101 $themename = get_selected_theme_for_device_type($device);
37959dd4
AF
102 if (!$themename && $device == 'default') {
103 $themename = theme_config::DEFAULT_THEME;
104 }
105
e5824bb9 106 $screenshotcell = $strthemenotselected;
bff67f49 107 $unsetthemebutton = '';
37959dd4 108 if ($themename) {
e5824bb9
SH
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));
43c741b9
EL
120 // Show the name of the picked theme
121 $headingthemename = $OUTPUT->heading($strthemename, 3);
e5824bb9 122 }
bff67f49
RT
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);
129 }
37959dd4
AF
130 }
131
e5824bb9
SH
132 $deviceurl = new moodle_url('/theme/index.php', array('device' => $device, 'sesskey' => sesskey()));
133 $select = new single_button($deviceurl, $strthemeselect, 'get');
37959dd4 134
e5824bb9 135 $table->data[] = array(
c069c017 136 $OUTPUT->heading(ucfirst($device), 3),
e5824bb9 137 $screenshotcell,
bff67f49 138 $headingthemename . $OUTPUT->render($select) . $unsetthemebutton
e5824bb9 139 );
78946b9b 140 }
e5824bb9
SH
141} else {
142 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
143 // of themes to select.
604cb179 144 $heading = get_string('selecttheme', 'admin', $device);
e5824bb9
SH
145 if (empty($device)) {
146 // if $CFG->enabledevicedetection is off this will return 'default'
147 $device = get_device_type();
ee8df661 148 }
b7009474 149
e5824bb9
SH
150 $table->id = 'adminthemeselector';
151 $table->head = array(get_string('theme'), get_string('info'));
37959dd4 152
e5824bb9 153 $themes = get_plugin_list('theme');
78946b9b 154
e5824bb9 155 foreach ($themes as $themename => $themedir) {
37959dd4 156
e5824bb9
SH
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;
163 }
164 if ($themename !== $theme->name) {
165 //obsoleted or broken theme, just skip for now
166 continue;
167 }
e625feaf 168 if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
e5824bb9
SH
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;
172 }
173 $strthemename = get_string('pluginname', 'theme_'.$themename);
74df4357 174
e5824bb9
SH
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();
b7009474 179
e5824bb9
SH
180 // Set up bools whether this theme is chosen either main or legacy
181 $ischosentheme = ($themename == get_selected_theme_for_device_type($device));
b7009474 182
e5824bb9
SH
183 if ($ischosentheme) {
184 // Is the chosen main theme
185 $rowclasses[] = 'selectedtheme';
186 }
0d399dc5 187
e5824bb9
SH
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);
ebb19aba 194
36ff8d05
RT
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);
207 }
b7009474 208
e5824bb9 209 $row[] = $infocell;
edda2b9a 210
e5824bb9
SH
211 $table->data[$themename] = $row;
212 $table->rowclasses[$themename] = join(' ', $rowclasses);
213 }
b7009474 214}
604cb179
RT
215echo $OUTPUT->header('themeselector');
216echo $OUTPUT->heading($heading);
217
df46b9b4 218echo $OUTPUT->single_button(new moodle_url('index.php', array('sesskey' => sesskey(), 'reset' => 1, 'device' => $device)), get_string('themeresetcaches', 'admin'));
b7009474 219
16be8974 220echo html_writer::table($table);
b7009474 221
e625feaf 222echo $OUTPUT->footer();