PageRenderTime 64ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 1ms

/theme/index.php

https://github.com/CLAMP-IT/moodle
PHP | 255 lines | 166 code | 37 blank | 52 comment | 49 complexity | 3e5cd6dae2297baeceb9497a8835e7b1 MD5 | raw file
  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.
  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. * This page provides the Administration -> ... -> Theme selector UI.
  18. *
  19. * @package core
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. require_once(__DIR__ . '/../config.php');
  23. require_once($CFG->libdir . '/adminlib.php');
  24. $choose = optional_param('choose', '', PARAM_PLUGIN);
  25. $reset = optional_param('reset', 0, PARAM_BOOL);
  26. $device = optional_param('device', '', PARAM_TEXT);
  27. $unsettheme = optional_param('unsettheme', 0, PARAM_BOOL);
  28. $confirmation = optional_param('confirmation', 0, PARAM_BOOL);
  29. admin_externalpage_setup('themeselector');
  30. if (!empty($device)) {
  31. // Make sure the device requested is valid.
  32. $devices = core_useragent::get_device_type_list();
  33. if (!in_array($device, $devices)) {
  34. // The provided device isn't a valid device throw an error.
  35. print_error('invaliddevicetype');
  36. }
  37. }
  38. unset($SESSION->theme);
  39. if ($reset and confirm_sesskey()) {
  40. theme_reset_all_caches();
  41. } else if ($choose && $confirmation) {
  42. $theme = theme_config::load($choose);
  43. echo $OUTPUT->header();
  44. echo $OUTPUT->heading(get_string('themesaved'));
  45. echo $OUTPUT->box_start();
  46. echo format_text(get_string('choosereadme', 'theme_'.$theme->name), FORMAT_MOODLE);
  47. echo $OUTPUT->box_end();
  48. echo $OUTPUT->continue_button($CFG->wwwroot . '/theme/index.php');
  49. echo $OUTPUT->footer();
  50. exit;
  51. } else if ($choose && $device && !theme_is_device_locked($device) && !$unsettheme && confirm_sesskey()) {
  52. // Load the theme to make sure it is valid.
  53. $theme = theme_config::load($choose);
  54. // Get the config argument for the chosen device.
  55. $themename = core_useragent::get_device_type_cfg_var_name($device);
  56. set_config($themename, $theme->name);
  57. $urlconfirm = new moodle_url('/theme/index.php', array('confirmation' => 1, 'choose' => $choose));
  58. redirect($urlconfirm);
  59. } else if ($device && !theme_is_device_locked($device) && $unsettheme && confirm_sesskey() && ($device != 'default')) {
  60. // Unset the theme and continue.
  61. unset_config(core_useragent::get_device_type_cfg_var_name($device));
  62. $device = '';
  63. }
  64. // Otherwise, show either a list of devices, or is enabledevicedetection set to no or a
  65. // device is specified show a list of themes.
  66. $table = new html_table();
  67. $table->data = array();
  68. $heading = '';
  69. if (!empty($CFG->enabledevicedetection) && empty($device)) {
  70. $heading = get_string('selectdevice', 'admin');
  71. // Display a list of devices that a user can select a theme for.
  72. $strthemenotselected = get_string('themenoselected', 'admin');
  73. $strthemeselect = get_string('themeselect', 'admin');
  74. // Display the device selection screen.
  75. $table->id = 'admindeviceselector';
  76. $table->head = array(get_string('devicetype', 'admin'), get_string('currenttheme', 'admin'), get_string('info'));
  77. $devices = core_useragent::get_device_type_list();
  78. foreach ($devices as $thedevice) {
  79. $headingthemename = ''; // To output the picked theme name when needed.
  80. $themename = core_useragent::get_device_type_theme($thedevice);
  81. if (!$themename && $thedevice == 'default') {
  82. $themename = theme_config::DEFAULT_THEME;
  83. }
  84. $themelocked = theme_is_device_locked($thedevice);
  85. $screenshotcell = $strthemenotselected;
  86. $unsetthemebutton = '';
  87. if ($themename) {
  88. // Check the theme exists.
  89. $themename = clean_param($themename, PARAM_THEME);
  90. if (empty($themename)) {
  91. // Likely the theme has been deleted.
  92. unset_config(core_useragent::get_device_type_cfg_var_name($thedevice));
  93. } else {
  94. $strthemename = get_string('pluginname', 'theme_'.$themename);
  95. // Link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes,
  96. // not the current one.
  97. $screenshoturl = new moodle_url('/theme/image.php',
  98. array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
  99. // Contents of the screenshot/preview cell.
  100. $screenshotcell = html_writer::empty_tag('img', array('class' => 'img-fluid',
  101. 'src' => $screenshoturl, 'alt' => $strthemename));
  102. // Show the name of the picked theme.
  103. $headingthemename = $OUTPUT->heading($strthemename, 3);
  104. }
  105. // If not default device then show option to unset theme.
  106. if ($thedevice != 'default' && !$themelocked) {
  107. $unsetthemestr = get_string('unsettheme', 'admin');
  108. $unsetthemeurl = new moodle_url('/theme/index.php',
  109. array('device' => $thedevice, 'sesskey' => sesskey(), 'unsettheme' => true));
  110. $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
  111. $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
  112. }
  113. }
  114. $deviceurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey()));
  115. $select = '';
  116. if (!$themelocked) {
  117. $select = $OUTPUT->render(new single_button($deviceurl, $strthemeselect, 'get'));
  118. }
  119. $lockwarning = '';
  120. if ($themelocked) {
  121. $lockwarning = html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
  122. }
  123. $table->data[] = array(
  124. $OUTPUT->heading(ucfirst($thedevice), 3),
  125. $screenshotcell,
  126. $headingthemename . $lockwarning . $select . $unsetthemebutton
  127. );
  128. }
  129. } else {
  130. // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
  131. // of themes to select.
  132. $heading = get_string('selecttheme', 'admin', $device);
  133. if (empty($device)) {
  134. // If $CFG->enabledevicedetection is off this will return 'default'.
  135. $device = core_useragent::get_device_type();
  136. }
  137. $themelocked = theme_is_device_locked($device);
  138. $table->id = 'adminthemeselector';
  139. $table->head = array(get_string('theme'), get_string('info'));
  140. $themes = array();
  141. if ($themelocked) {
  142. $heading = get_string('currenttheme', 'admin');
  143. $themename = theme_get_locked_theme_for_device($device);
  144. $themedirectory = core_component::get_plugin_directory('theme', $themename);
  145. $themes[$themename] = $themedirectory;
  146. } else {
  147. $themes = core_component::get_plugin_list('theme');
  148. }
  149. foreach ($themes as $themename => $themedir) {
  150. // Load the theme config.
  151. try {
  152. $theme = theme_config::load($themename);
  153. } catch (Exception $e) {
  154. // Bad theme, just skip it for now.
  155. continue;
  156. }
  157. if ($themename !== $theme->name) {
  158. // Obsoleted or broken theme, just skip for now.
  159. continue;
  160. }
  161. if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
  162. // The theme doesn't want to be shown in the theme selector and as theme
  163. // designer mode is switched off we will respect that decision.
  164. continue;
  165. }
  166. $strthemename = get_string('pluginname', 'theme_'.$themename);
  167. // Build the table row, and also a list of items to go in the second cell.
  168. $row = array();
  169. $infoitems = array();
  170. $rowclasses = array();
  171. // Set up bools whether this theme is chosen either main or legacy.
  172. $ischosentheme = ($themename == core_useragent::get_device_type_theme($device));
  173. if ($ischosentheme) {
  174. // Is the chosen main theme.
  175. $rowclasses[] = 'selectedtheme';
  176. }
  177. // Link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes,
  178. // not the current one.
  179. $screenshotpath = new moodle_url('/theme/image.php',
  180. array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
  181. // Contents of the first screenshot/preview cell.
  182. $row[] = html_writer::empty_tag('img', array('class' => 'img-fluid',
  183. 'src' => $screenshotpath, 'alt' => $strthemename));
  184. // Contents of the second cell.
  185. $infocell = $OUTPUT->heading($strthemename, 3);
  186. if ($themelocked) {
  187. $infocell .= html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
  188. }
  189. // Button to choose this as the main theme or unset this theme for devices other then default.
  190. if (!$themelocked) {
  191. if (($ischosentheme) && ($device != 'default')) {
  192. $unsetthemestr = get_string('unsettheme', 'admin');
  193. $unsetthemeurl = new moodle_url('/theme/index.php',
  194. array('device' => $device, 'unsettheme' => true, 'sesskey' => sesskey()));
  195. $unsetbutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
  196. $infocell .= $OUTPUT->render($unsetbutton);
  197. } else if ((!$ischosentheme)) {
  198. $setthemestr = get_string('usetheme');
  199. $setthemeurl = new moodle_url('/theme/index.php',
  200. array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey()));
  201. $setthemebutton = new single_button($setthemeurl, $setthemestr, 'get');
  202. $infocell .= $OUTPUT->render($setthemebutton);
  203. }
  204. }
  205. $row[] = $infocell;
  206. $table->data[$themename] = $row;
  207. $table->rowclasses[$themename] = join(' ', $rowclasses);
  208. }
  209. }
  210. echo $OUTPUT->header('themeselector');
  211. echo $OUTPUT->heading($heading);
  212. $params = array('sesskey' => sesskey(), 'reset' => 1);
  213. if (!empty($device)) {
  214. $params['device'] = $device;
  215. }
  216. echo $OUTPUT->single_button(new moodle_url('index.php', $params), get_string('themeresetcaches', 'admin'));
  217. echo html_writer::table($table);
  218. echo $OUTPUT->footer();