Merge branch 'MDL-37962_beep' of git://github.com/andyjdavis/moodle
[moodle.git] / theme / index.php
blobc51665f63a85e568c375c682f8716f794c4bc3aa
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(dirname(__FILE__) . '/../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);
32 admin_externalpage_setup('themeselector');
34 if (!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');
43 unset($SESSION->theme);
45 if ($reset and confirm_sesskey()) {
46 theme_reset_all_caches();
48 } else if ($choose && $device && !$unsettheme && confirm_sesskey()) {
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.
52 $themename = get_device_cfg_var_name($device);
53 set_config($themename, $theme->name);
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);
65 // Get the core renderer for the new theme.
66 $output = $confirmpage->get_renderer('core');
68 echo $output->header();
69 echo $output->heading(get_string('themesaved'));
70 echo $output->box_start();
71 echo format_text(get_string('choosereadme', 'theme_'.$theme->name), FORMAT_MOODLE);
72 echo $output->box_end();
73 echo $output->continue_button($CFG->wwwroot . '/theme/index.php');
74 echo $output->footer();
75 exit;
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 = '';
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.
85 $table = new html_table();
86 $table->data = array();
87 $heading = '';
88 if (!empty($CFG->enabledevicedetection) && empty($device)) {
89 $heading = get_string('selectdevice', 'admin');
90 // Display a list of devices that a user can select a theme for.
92 $strthemenotselected = get_string('themenoselected', 'admin');
93 $strthemeselect = get_string('themeselect', 'admin');
95 // Display the device selection screen
96 $table->id = 'admindeviceselector';
97 $table->head = array(get_string('devicetype', 'admin'), get_string('currenttheme', 'admin'), get_string('info'));
99 $devices = get_device_type_list();
100 foreach ($devices as $thedevice) {
102 $headingthemename = ''; // To output the picked theme name when needed
103 $themename = get_selected_theme_for_device_type($thedevice);
104 if (!$themename && $thedevice == 'default') {
105 $themename = theme_config::DEFAULT_THEME;
108 $screenshotcell = $strthemenotselected;
109 $unsetthemebutton = '';
110 if ($themename) {
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($thedevice));
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));
122 // Show the name of the picked theme
123 $headingthemename = $OUTPUT->heading($strthemename, 3);
125 // If not default device then show option to unset theme.
126 if ($thedevice != 'default') {
127 $unsetthemestr = get_string('unsettheme', 'admin');
128 $unsetthemeurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey(), 'unsettheme' => true));
129 $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
130 $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
134 $deviceurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey()));
135 $select = new single_button($deviceurl, $strthemeselect, 'get');
137 $table->data[] = array(
138 $OUTPUT->heading(ucfirst($thedevice), 3),
139 $screenshotcell,
140 $headingthemename . $OUTPUT->render($select) . $unsetthemebutton
143 } else {
144 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
145 // of themes to select.
146 $heading = get_string('selecttheme', 'admin', $device);
147 if (empty($device)) {
148 // if $CFG->enabledevicedetection is off this will return 'default'
149 $device = get_device_type();
152 $table->id = 'adminthemeselector';
153 $table->head = array(get_string('theme'), get_string('info'));
155 $themes = core_component::get_plugin_list('theme');
157 foreach ($themes as $themename => $themedir) {
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;
166 if ($themename !== $theme->name) {
167 //obsoleted or broken theme, just skip for now
168 continue;
170 if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
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;
175 $strthemename = get_string('pluginname', 'theme_'.$themename);
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();
182 // Set up bools whether this theme is chosen either main or legacy
183 $ischosentheme = ($themename == get_selected_theme_for_device_type($device));
185 if ($ischosentheme) {
186 // Is the chosen main theme
187 $rowclasses[] = 'selectedtheme';
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);
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);
211 $row[] = $infocell;
213 $table->data[$themename] = $row;
214 $table->rowclasses[$themename] = join(' ', $rowclasses);
217 echo $OUTPUT->header('themeselector');
218 echo $OUTPUT->heading($heading);
220 $params = array('sesskey' => sesskey(), 'reset' => 1);
221 if (!empty($device)) {
222 $params['device'] = $device;
224 echo $OUTPUT->single_button(new moodle_url('index.php', $params), get_string('themeresetcaches', 'admin'));
226 echo html_writer::table($table);
228 echo $OUTPUT->footer();