weekly release 2.6dev
[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)) {
9ead041c
DW
35 // Make sure the device requested is valid
36 $devices = get_device_type_list();
e5824bb9 37 if (!in_array($device, $devices)) {
9ead041c 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.
9ead041c 52 $themename = get_device_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')) {
9ead041c
DW
77 //Unset the theme and continue.
78 unset_config(get_device_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
9ead041c 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
9ead041c 99 $devices = get_device_type_list();
32b0c696 100 foreach ($devices as $thedevice) {
37959dd4 101
9ead041c
DW
102 $headingthemename = ''; // To output the picked theme name when needed
103 $themename = get_selected_theme_for_device_type($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) {
9ead041c 111 // Check the theme exists
e5824bb9
SH
112 $themename = clean_param($themename, PARAM_THEME);
113 if (empty($themename)) {
9ead041c
DW
114 // Likely the theme has been deleted
115 unset_config(get_device_cfg_var_name($thedevice));
e5824bb9
SH
116 } else {
117 $strthemename = get_string('pluginname', 'theme_'.$themename);
9ead041c
DW
118 // link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes, not the current one
119 $screenshoturl = new moodle_url('/theme/image.php', array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
e5824bb9
SH
120 // Contents of the screenshot/preview cell.
121 $screenshotcell = html_writer::empty_tag('img', array('src' => $screenshoturl, 'alt' => $strthemename));
9ead041c 122 // Show the name of the picked theme
43c741b9 123 $headingthemename = $OUTPUT->heading($strthemename, 3);
e5824bb9 124 }
bff67f49 125 // If not default device then show option to unset theme.
32b0c696 126 if ($thedevice != 'default') {
bff67f49 127 $unsetthemestr = get_string('unsettheme', 'admin');
9ead041c 128 $unsetthemeurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey(), 'unsettheme' => true));
bff67f49
RT
129 $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
130 $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
131 }
37959dd4
AF
132 }
133
32b0c696 134 $deviceurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey()));
e5824bb9 135 $select = new single_button($deviceurl, $strthemeselect, 'get');
37959dd4 136
e5824bb9 137 $table->data[] = array(
32b0c696 138 $OUTPUT->heading(ucfirst($thedevice), 3),
e5824bb9 139 $screenshotcell,
bff67f49 140 $headingthemename . $OUTPUT->render($select) . $unsetthemebutton
e5824bb9 141 );
78946b9b 142 }
e5824bb9
SH
143} else {
144 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
145 // of themes to select.
604cb179 146 $heading = get_string('selecttheme', 'admin', $device);
e5824bb9 147 if (empty($device)) {
9ead041c
DW
148 // if $CFG->enabledevicedetection is off this will return 'default'
149 $device = get_device_type();
ee8df661 150 }
b7009474 151
e5824bb9
SH
152 $table->id = 'adminthemeselector';
153 $table->head = array(get_string('theme'), get_string('info'));
37959dd4 154
bd3b3bba 155 $themes = core_component::get_plugin_list('theme');
78946b9b 156
e5824bb9 157 foreach ($themes as $themename => $themedir) {
37959dd4 158
e5824bb9
SH
159 // Load the theme config.
160 try {
161 $theme = theme_config::load($themename);
162 } catch (Exception $e) {
163 // Bad theme, just skip it for now.
164 continue;
165 }
166 if ($themename !== $theme->name) {
9ead041c 167 //obsoleted or broken theme, just skip for now
e5824bb9
SH
168 continue;
169 }
e625feaf 170 if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
e5824bb9
SH
171 // The theme doesn't want to be shown in the theme selector and as theme
172 // designer mode is switched off we will respect that decision.
173 continue;
174 }
175 $strthemename = get_string('pluginname', 'theme_'.$themename);
74df4357 176
e5824bb9
SH
177 // Build the table row, and also a list of items to go in the second cell.
178 $row = array();
179 $infoitems = array();
180 $rowclasses = array();
b7009474 181
9ead041c
DW
182 // Set up bools whether this theme is chosen either main or legacy
183 $ischosentheme = ($themename == get_selected_theme_for_device_type($device));
b7009474 184
e5824bb9 185 if ($ischosentheme) {
9ead041c 186 // Is the chosen main theme
e5824bb9
SH
187 $rowclasses[] = 'selectedtheme';
188 }
0d399dc5 189
9ead041c
DW
190 // link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes, not the current one
191 $screenshotpath = new moodle_url('/theme/image.php', array('theme'=>$themename, 'image'=>'screenshot', 'component'=>'theme'));
e5824bb9
SH
192 // Contents of the first screenshot/preview cell.
193 $row[] = html_writer::empty_tag('img', array('src'=>$screenshotpath, 'alt'=>$strthemename));
194 // Contents of the second cell.
195 $infocell = $OUTPUT->heading($strthemename, 3);
ebb19aba 196
9ead041c
DW
197 // Button to choose this as the main theme or unset this theme for
198 // devices other then default
36ff8d05
RT
199 if (($ischosentheme) && ($device != 'default')) {
200 $unsetthemestr = get_string('unsettheme', 'admin');
9ead041c 201 $unsetthemeurl = new moodle_url('/theme/index.php', array('device' => $device, 'unsettheme' => true, 'sesskey' => sesskey()));
36ff8d05
RT
202 $unsetbutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
203 $infocell .= $OUTPUT->render($unsetbutton);
204 } else if ((!$ischosentheme)) {
205 $setthemestr = get_string('usetheme');
9ead041c 206 $setthemeurl = new moodle_url('/theme/index.php', array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey()));
36ff8d05
RT
207 $setthemebutton = new single_button($setthemeurl, $setthemestr, 'get');
208 $infocell .= $OUTPUT->render($setthemebutton);
209 }
b7009474 210
e5824bb9 211 $row[] = $infocell;
edda2b9a 212
e5824bb9
SH
213 $table->data[$themename] = $row;
214 $table->rowclasses[$themename] = join(' ', $rowclasses);
215 }
b7009474 216}
604cb179
RT
217echo $OUTPUT->header('themeselector');
218echo $OUTPUT->heading($heading);
219
32b0c696
GB
220$params = array('sesskey' => sesskey(), 'reset' => 1);
221if (!empty($device)) {
222 $params['device'] = $device;
223}
224echo $OUTPUT->single_button(new moodle_url('index.php', $params), get_string('themeresetcaches', 'admin'));
b7009474 225
16be8974 226echo html_writer::table($table);
b7009474 227
e625feaf 228echo $OUTPUT->footer();