Merge branch 'MDL-45449-master' of https://github.com/sammarshallou/moodle
[moodle.git] / theme / index.php
blob1dbeded965ae96c4149dd7b78e8dc61adac64f96
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 = core_useragent::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 = core_useragent::get_device_type_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(core_useragent::get_device_type_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 = core_useragent::get_device_type_list();
100 foreach ($devices as $thedevice) {
102 $headingthemename = ''; // To output the picked theme name when needed.
103 $themename = core_useragent::get_device_type_theme($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(core_useragent::get_device_type_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,
119 // not the current one.
120 $screenshoturl = new moodle_url('/theme/image.php',
121 array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
122 // Contents of the screenshot/preview cell.
123 $screenshotcell = html_writer::empty_tag('img', array('src' => $screenshoturl, 'alt' => $strthemename));
124 // Show the name of the picked theme.
125 $headingthemename = $OUTPUT->heading($strthemename, 3);
127 // If not default device then show option to unset theme.
128 if ($thedevice != 'default') {
129 $unsetthemestr = get_string('unsettheme', 'admin');
130 $unsetthemeurl = new moodle_url('/theme/index.php',
131 array('device' => $thedevice, 'sesskey' => sesskey(), 'unsettheme' => true));
132 $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
133 $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
137 $deviceurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey()));
138 $select = new single_button($deviceurl, $strthemeselect, 'get');
140 $table->data[] = array(
141 $OUTPUT->heading(ucfirst($thedevice), 3),
142 $screenshotcell,
143 $headingthemename . $OUTPUT->render($select) . $unsetthemebutton
146 } else {
147 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
148 // of themes to select.
149 $heading = get_string('selecttheme', 'admin', $device);
150 if (empty($device)) {
151 // If $CFG->enabledevicedetection is off this will return 'default'.
152 $device = core_useragent::get_device_type();
155 $table->id = 'adminthemeselector';
156 $table->head = array(get_string('theme'), get_string('info'));
158 $themes = core_component::get_plugin_list('theme');
160 foreach ($themes as $themename => $themedir) {
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;
169 if ($themename !== $theme->name) {
170 // Obsoleted or broken theme, just skip for now.
171 continue;
173 if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
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;
178 $strthemename = get_string('pluginname', 'theme_'.$themename);
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();
185 // Set up bools whether this theme is chosen either main or legacy.
186 $ischosentheme = ($themename == core_useragent::get_device_type_theme($device));
188 if ($ischosentheme) {
189 // Is the chosen main theme.
190 $rowclasses[] = 'selectedtheme';
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'));
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);
202 // Button to choose this as the main theme or unset this theme for devices other then default.
203 if (($ischosentheme) && ($device != 'default')) {
204 $unsetthemestr = get_string('unsettheme', 'admin');
205 $unsetthemeurl = new moodle_url('/theme/index.php',
206 array('device' => $device, 'unsettheme' => true, 'sesskey' => sesskey()));
207 $unsetbutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
208 $infocell .= $OUTPUT->render($unsetbutton);
209 } else if ((!$ischosentheme)) {
210 $setthemestr = get_string('usetheme');
211 $setthemeurl = new moodle_url('/theme/index.php',
212 array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey()));
213 $setthemebutton = new single_button($setthemeurl, $setthemestr, 'get');
214 $infocell .= $OUTPUT->render($setthemebutton);
217 $row[] = $infocell;
219 $table->data[$themename] = $row;
220 $table->rowclasses[$themename] = join(' ', $rowclasses);
223 echo $OUTPUT->header('themeselector');
224 echo $OUTPUT->heading($heading);
226 $params = array('sesskey' => sesskey(), 'reset' => 1);
227 if (!empty($device)) {
228 $params['device'] = $device;
230 echo $OUTPUT->single_button(new moodle_url('index.php', $params), get_string('themeresetcaches', 'admin'));
232 echo html_writer::table($table);
234 echo $OUTPUT->footer();