MDL-25394 Improved support for mobile and device themes
[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
78946b9b
PS
25$choose = optional_param('choose', '', PARAM_SAFEDIR);
26$reset = optional_param('reset', 0, PARAM_BOOL);
37959dd4 27$device = optional_param('device', '', PARAM_TEXT);
b7009474 28
29admin_externalpage_setup('themeselector');
30
31unset($SESSION->theme);
32
78946b9b
PS
33if ($reset and confirm_sesskey()) {
34 theme_reset_all_caches();
35
37959dd4 36} else if ($choose && $device && confirm_sesskey()) {
b7009474 37
37959dd4
AF
38 $chosentheme = $choose;
39 $heading = get_string('themesaved');
40
ee8df661 41 $theme = theme_config::load($chosentheme);
37959dd4
AF
42 $themename = get_device_cfg_var_name($device);
43
44 set_config($themename, $theme->name);
8ba55c01 45
752fdfbd
SH
46 // Create a new page for the display of the themes readme.
47 // This ensures that the readme page is shown using the new theme.
48 $confirmpage = new moodle_page();
49 $confirmpage->set_context($PAGE->context);
50 $confirmpage->set_url($PAGE->url);
51 $confirmpage->set_pagelayout($PAGE->pagelayout);
52 $confirmpage->set_pagetype($PAGE->pagetype);
53 $confirmpage->set_title($PAGE->title);
54 $confirmpage->set_heading($PAGE->heading);
55
56 // Get the core renderer for the new theme.
57 $output = $confirmpage->get_renderer('core');
58
59 echo $output->header();
60 echo $output->heading($heading);
61 echo $output->box_start();
37959dd4 62 echo format_text(get_string('choosereadme', 'theme_'.$theme->name), FORMAT_MOODLE);
752fdfbd
SH
63 echo $output->box_end();
64 echo $output->continue_button($CFG->wwwroot . '/' . $CFG->admin . '/index.php');
65 echo $output->footer();
b7009474 66 exit;
67}
68
37959dd4
AF
69// Otherwise, show either a list of devices, or is enabledevicedetection set to no or a
70// device is specified show a list of themes.
71
61ef8f9f 72echo $OUTPUT->header('themeselector');
cdc62220 73echo $OUTPUT->heading(get_string('themes'));
b7009474 74
37959dd4
AF
75echo $OUTPUT->single_button(new moodle_url('index.php', array('sesskey'=>sesskey(), 'reset'=>1)), get_string('themeresetcaches', 'admin'));
76
77if ($CFG->enabledevicedetection && empty($device)) {
78 $table = new html_table();
79 $table->id = 'devicethemeselector';
80 $table->head = array(get_string('devicetype', 'admin'), get_string('theme'), get_string('info'));
81
82 $devices = get_device_type_list();
83
84 foreach ($devices as $device) {
85 $row = array();
86 $row[] = $device;
87
88 $themename = get_selected_theme_for_device_type($device);
89
90 if (!$themename && $device == 'default') {
91 $themename = theme_config::DEFAULT_THEME;
92 }
93
94 if ($themename) {
95 $strthemename = get_string('pluginname', 'theme_'.$themename);
96 // link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes, not the current one
97 $screenshotpath = new moodle_url('/theme/image.php', array('theme'=>$themename, 'image'=>'screenshot', 'component'=>'theme'));
98 // Contents of the first screenshot/preview cell.
99 $row[] = html_writer::empty_tag('img', array('src'=>$screenshotpath, 'alt'=>$strthemename));
100 } else {
101 $row[] = get_string('themenoselected', 'admin');
102 }
103
104 $select = new single_button(new moodle_url('/theme/index.php', array('device' => $device, 'sesskey' => sesskey())), get_string('themeselect', 'admin'), 'get');
105
106 $row[] = $OUTPUT->render($select);
107
108 $table->data[$device] = $row;
109 }
110
111 echo html_writer::table($table);
112 echo $OUTPUT->footer();
113
114 exit;
115}
116
117//if $CFG->enabledevicedetection is set to no, then this will return the default device type.
118if (empty($device)) {
119 $device = get_device_type();
120}
78946b9b 121
fd09397e 122$table = new html_table();
b7009474 123$table->id = 'adminthemeselector';
124$table->head = array(get_string('theme'), get_string('info'));
125
126$themes = get_plugin_list('theme');
78946b9b 127
b7009474 128foreach ($themes as $themename => $themedir) {
b7009474 129 // Load the theme config.
130 try {
131 $theme = theme_config::load($themename);
78946b9b 132 } catch (Exception $e) {
b7009474 133 // Bad theme, just skip it for now.
134 continue;
60a9a6ea 135 }
37959dd4 136
78946b9b
PS
137 if ($themename !== $theme->name) {
138 //obsoleted or broken theme, just skip for now
139 continue;
140 }
4d56ee95
SH
141 if (!$CFG->themedesignermode && $theme->hidefromselector) {
142 // The theme doesn't want to be shown in the theme selector and as theme
143 // designer mode is switched off we will respect that decision.
144 continue;
145 }
edda2b9a 146 $strthemename = get_string('pluginname', 'theme_'.$themename);
74df4357 147
b7009474 148 // Build the table row, and also a list of items to go in the second cell.
149 $row = array();
150 $infoitems = array();
ee8df661 151 $rowclasses = array();
b7009474 152
ee8df661 153 // Set up bools whether this theme is chosen either main or legacy
37959dd4 154 $ischosentheme = ($themename == get_selected_theme_for_device_type($device));
b7009474 155
ee8df661
SH
156 if ($ischosentheme) {
157 // Is the chosen main theme
158 $rowclasses[] = 'selectedtheme';
159 }
0d399dc5 160
ee8df661 161 // link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes, not the current one
37959dd4 162 $screenshotpath = new moodle_url('/theme/image.php', array('theme'=>$themename, 'image'=>'screenshot', 'component'=>'theme'));
b7009474 163 // Contents of the first screenshot/preview cell.
edda2b9a 164 $row[] = html_writer::empty_tag('img', array('src'=>$screenshotpath, 'alt'=>$strthemename));
b7009474 165 // Contents of the second cell.
edda2b9a 166 $infocell = $OUTPUT->heading($strthemename, 3);
ebb19aba 167
ee8df661 168 // Button to choose this as the main theme
37959dd4 169 $maintheme = new single_button(new moodle_url('/theme/index.php', array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey())), get_string('usetheme'), 'get');
ee8df661
SH
170 $maintheme->disabled = $ischosentheme;
171 $infocell .= $OUTPUT->render($maintheme);
b7009474 172
ee8df661 173 $row[] = $infocell;
edda2b9a 174
b7009474 175 $table->data[$themename] = $row;
ee8df661 176 $table->rowclasses[$themename] = join(' ', $rowclasses);
b7009474 177}
178
16be8974 179echo html_writer::table($table);
b7009474 180
ee8df661 181echo $OUTPUT->footer();