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
SH
34if (!empty($device)) {
35 // Make sure the device requested is valid
36 $devices = 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');
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.
37959dd4 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
RT
76} else if ($device && $unsettheme && confirm_sesskey() && ($device != 'default')) {
77 //Unset the theme and continue.
78 unset_config(get_device_cfg_var_name($device));
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
95 // Display the device selection screen
96 $table->id = 'admindeviceselector';
604cb179 97 $table->head = array(get_string('devicetype', 'admin'), get_string('currenttheme', 'admin'), get_string('info'));
37959dd4
AF
98
99 $devices = get_device_type_list();
37959dd4 100 foreach ($devices as $device) {
37959dd4 101
43c741b9 102 $headingthemename = ''; // To output the picked theme name when needed
37959dd4 103 $themename = get_selected_theme_for_device_type($device);
37959dd4
AF
104 if (!$themename && $device == 'default') {
105 $themename = theme_config::DEFAULT_THEME;
106 }
107
e5824bb9 108 $screenshotcell = $strthemenotselected;
bff67f49 109 $unsetthemebutton = '';
37959dd4 110 if ($themename) {
e5824bb9
SH
111 // Check the theme exists
112 $themename = clean_param($themename, PARAM_THEME);
113 if (empty($themename)) {
114 // Likely the theme has been deleted
115 unset_config(get_device_cfg_var_name($device));
116 } else {
117 $strthemename = get_string('pluginname', 'theme_'.$themename);
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'));
120 // Contents of the screenshot/preview cell.
121 $screenshotcell = html_writer::empty_tag('img', array('src' => $screenshoturl, 'alt' => $strthemename));
43c741b9
EL
122 // Show the name of the picked theme
123 $headingthemename = $OUTPUT->heading($strthemename, 3);
e5824bb9 124 }
bff67f49
RT
125 // If not default device then show option to unset theme.
126 if ($device != 'default') {
127 $unsetthemestr = get_string('unsettheme', 'admin');
128 $unsetthemeurl = new moodle_url('/theme/index.php', array('device' => $device, 'sesskey' => sesskey(), 'unsettheme' => true));
129 $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
130 $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
131 }
37959dd4
AF
132 }
133
e5824bb9
SH
134 $deviceurl = new moodle_url('/theme/index.php', array('device' => $device, 'sesskey' => sesskey()));
135 $select = new single_button($deviceurl, $strthemeselect, 'get');
37959dd4 136
e5824bb9 137 $table->data[] = array(
c069c017 138 $OUTPUT->heading(ucfirst($device), 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
SH
147 if (empty($device)) {
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
e5824bb9 155 $themes = 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) {
167 //obsoleted or broken theme, just skip for now
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
e5824bb9
SH
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
SH
185 if ($ischosentheme) {
186 // Is the chosen main theme
187 $rowclasses[] = 'selectedtheme';
188 }
0d399dc5 189
e5824bb9
SH
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'));
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
36ff8d05
RT
197 // Button to choose this as the main theme or unset this theme for
198 // devices other then default
199 if (($ischosentheme) && ($device != 'default')) {
200 $unsetthemestr = get_string('unsettheme', 'admin');
201 $unsetthemeurl = new moodle_url('/theme/index.php', array('device' => $device, 'unsettheme' => true, 'sesskey' => sesskey()));
202 $unsetbutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
203 $infocell .= $OUTPUT->render($unsetbutton);
204 } else if ((!$ischosentheme)) {
205 $setthemestr = get_string('usetheme');
206 $setthemeurl = new moodle_url('/theme/index.php', array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey()));
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
df46b9b4 220echo $OUTPUT->single_button(new moodle_url('index.php', array('sesskey' => sesskey(), 'reset' => 1, 'device' => $device)), get_string('themeresetcaches', 'admin'));
b7009474 221
16be8974 222echo html_writer::table($table);
b7009474 223
e625feaf 224echo $OUTPUT->footer();