Merge branch 'install_master' of git://github.com/amosbot/moodle
[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);
b7009474 28
29admin_externalpage_setup('themeselector');
30
e5824bb9
SH
31if (!empty($device)) {
32 // Make sure the device requested is valid
33 $devices = get_device_type_list();
34 if (!in_array($device, $devices)) {
35 // The provided device isn't a valid device throw an error
36 print_error('invaliddevicetype');
37 }
38}
39
b7009474 40unset($SESSION->theme);
41
78946b9b
PS
42if ($reset and confirm_sesskey()) {
43 theme_reset_all_caches();
44
37959dd4 45} else if ($choose && $device && confirm_sesskey()) {
aff24313 46
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;
74}
75
37959dd4
AF
76// Otherwise, show either a list of devices, or is enabledevicedetection set to no or a
77// device is specified show a list of themes.
78
61ef8f9f 79echo $OUTPUT->header('themeselector');
cdc62220 80echo $OUTPUT->heading(get_string('themes'));
b7009474 81
e5824bb9 82echo $OUTPUT->single_button(new moodle_url('index.php', array('sesskey' => sesskey(), 'reset' => 1)), get_string('themeresetcaches', 'admin'));
78946b9b 83
fd09397e 84$table = new html_table();
e5824bb9 85$table->data = array();
7bff5441 86if (!empty($CFG->enabledevicedetection) && empty($device)) {
e5824bb9
SH
87 // Display a list of devices that a user can select a theme for.
88
89 $strthemenotselected = get_string('themenoselected', 'admin');
90 $strthemeselect = get_string('themeselect', 'admin');
91
92 // Display the device selection screen
93 $table->id = 'admindeviceselector';
37959dd4
AF
94 $table->head = array(get_string('devicetype', 'admin'), get_string('theme'), get_string('info'));
95
96 $devices = get_device_type_list();
37959dd4 97 foreach ($devices as $device) {
37959dd4 98
43c741b9 99 $headingthemename = ''; // To output the picked theme name when needed
37959dd4 100 $themename = get_selected_theme_for_device_type($device);
37959dd4
AF
101 if (!$themename && $device == 'default') {
102 $themename = theme_config::DEFAULT_THEME;
103 }
104
e5824bb9 105 $screenshotcell = $strthemenotselected;
37959dd4 106 if ($themename) {
e5824bb9
SH
107 // Check the theme exists
108 $themename = clean_param($themename, PARAM_THEME);
109 if (empty($themename)) {
110 // Likely the theme has been deleted
111 unset_config(get_device_cfg_var_name($device));
112 } else {
113 $strthemename = get_string('pluginname', 'theme_'.$themename);
114 // link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes, not the current one
115 $screenshoturl = new moodle_url('/theme/image.php', array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
116 // Contents of the screenshot/preview cell.
117 $screenshotcell = html_writer::empty_tag('img', array('src' => $screenshoturl, 'alt' => $strthemename));
43c741b9
EL
118 // Show the name of the picked theme
119 $headingthemename = $OUTPUT->heading($strthemename, 3);
e5824bb9 120 }
37959dd4
AF
121 }
122
e5824bb9
SH
123 $deviceurl = new moodle_url('/theme/index.php', array('device' => $device, 'sesskey' => sesskey()));
124 $select = new single_button($deviceurl, $strthemeselect, 'get');
37959dd4 125
e5824bb9
SH
126 $table->data[] = array(
127 $device,
128 $screenshotcell,
43c741b9 129 $headingthemename . $OUTPUT->render($select)
e5824bb9 130 );
78946b9b 131 }
e5824bb9
SH
132} else {
133 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
134 // of themes to select.
b7009474 135
e5824bb9
SH
136 if (empty($device)) {
137 // if $CFG->enabledevicedetection is off this will return 'default'
138 $device = get_device_type();
ee8df661 139 }
b7009474 140
e5824bb9
SH
141 $table->id = 'adminthemeselector';
142 $table->head = array(get_string('theme'), get_string('info'));
37959dd4 143
e5824bb9 144 $themes = get_plugin_list('theme');
78946b9b 145
e5824bb9 146 foreach ($themes as $themename => $themedir) {
37959dd4 147
e5824bb9
SH
148 // Load the theme config.
149 try {
150 $theme = theme_config::load($themename);
151 } catch (Exception $e) {
152 // Bad theme, just skip it for now.
153 continue;
154 }
155 if ($themename !== $theme->name) {
156 //obsoleted or broken theme, just skip for now
157 continue;
158 }
e625feaf 159 if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
e5824bb9
SH
160 // The theme doesn't want to be shown in the theme selector and as theme
161 // designer mode is switched off we will respect that decision.
162 continue;
163 }
164 $strthemename = get_string('pluginname', 'theme_'.$themename);
74df4357 165
e5824bb9
SH
166 // Build the table row, and also a list of items to go in the second cell.
167 $row = array();
168 $infoitems = array();
169 $rowclasses = array();
b7009474 170
e5824bb9
SH
171 // Set up bools whether this theme is chosen either main or legacy
172 $ischosentheme = ($themename == get_selected_theme_for_device_type($device));
b7009474 173
e5824bb9
SH
174 if ($ischosentheme) {
175 // Is the chosen main theme
176 $rowclasses[] = 'selectedtheme';
177 }
0d399dc5 178
e5824bb9
SH
179 // link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes, not the current one
180 $screenshotpath = new moodle_url('/theme/image.php', array('theme'=>$themename, 'image'=>'screenshot', 'component'=>'theme'));
181 // Contents of the first screenshot/preview cell.
182 $row[] = html_writer::empty_tag('img', array('src'=>$screenshotpath, 'alt'=>$strthemename));
183 // Contents of the second cell.
184 $infocell = $OUTPUT->heading($strthemename, 3);
ebb19aba 185
e5824bb9
SH
186 // Button to choose this as the main theme
187 $maintheme = new single_button(new moodle_url('/theme/index.php', array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey())), get_string('usetheme'), 'get');
188 $maintheme->disabled = $ischosentheme;
189 $infocell .= $OUTPUT->render($maintheme);
b7009474 190
e5824bb9 191 $row[] = $infocell;
edda2b9a 192
e5824bb9
SH
193 $table->data[$themename] = $row;
194 $table->rowclasses[$themename] = join(' ', $rowclasses);
195 }
b7009474 196}
197
16be8974 198echo html_writer::table($table);
b7009474 199
e625feaf 200echo $OUTPUT->footer();