MDL-76059 forms: graceful early exit for invalid button elements.
[moodle.git] / admin / settings.php
blob30c61121b23474adc579baa1e35b7e22f08b5d82
1 <?php
3 require_once('../config.php');
4 require_once($CFG->libdir.'/adminlib.php');
6 $section = required_param('section', PARAM_SAFEDIR);
7 $return = optional_param('return','', PARAM_ALPHA);
8 $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
10 /// no guest autologin
11 require_login(0, false);
12 $PAGE->set_context(context_system::instance());
13 $PAGE->set_url('/admin/settings.php', array('section' => $section));
14 $PAGE->set_pagetype('admin-setting-' . $section);
15 $PAGE->set_pagelayout('admin');
16 $PAGE->navigation->clear_cache();
17 navigation_node::require_admin_tree();
19 $adminroot = admin_get_root(); // need all settings
20 $settingspage = $adminroot->locate($section, true);
22 if (empty($settingspage) or !($settingspage instanceof admin_settingpage)) {
23 if (moodle_needs_upgrading()) {
24 redirect(new moodle_url('/admin/index.php'));
25 } else {
26 throw new \moodle_exception('sectionerror', 'admin', "$CFG->wwwroot/$CFG->admin/");
28 die;
31 if (!($settingspage->check_access())) {
32 throw new \moodle_exception('accessdenied', 'admin');
33 die;
36 // If the context in the admin_settingpage object is explicitly defined and it is not system, reset the current
37 // page context and use that one instead. This ensures that the proper navigation is displayed and highlighted.
38 if ($settingspage->context && !$settingspage->context instanceof \context_system) {
39 $PAGE->set_context($settingspage->context);
42 $hassiteconfig = has_capability('moodle/site:config', context_system::instance());
43 // Display the admin search input element in the page header if the user has the capability to change the site
44 // configuration and the current page context is system.
45 if ($hassiteconfig && $PAGE->context instanceof \context_system) {
46 $PAGE->add_header_action($OUTPUT->render_from_template('core_admin/header_search_input', [
47 'action' => new moodle_url('/admin/search.php'),
48 ]));
51 /// WRITING SUBMITTED DATA (IF ANY) -------------------------------------------------------------------------------
53 $statusmsg = '';
54 $errormsg = '';
56 // Form is submitted with changed settings. Do not want to execute when modifying a block.
57 if ($data = data_submitted() and confirm_sesskey() and isset($data->action) and $data->action == 'save-settings') {
59 $count = admin_write_settings($data);
60 // Regardless of whether any setting change was written (a positive count), check validation errors for those that didn't.
61 if (empty($adminroot->errors)) {
62 // No errors. Did we change any setting? If so, then redirect with success.
63 if ($count) {
64 redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
66 // We didn't change a setting.
67 switch ($return) {
68 case 'site': redirect("$CFG->wwwroot/");
69 case 'admin': redirect("$CFG->wwwroot/$CFG->admin/");
71 redirect($PAGE->url);
72 } else {
73 $errormsg = get_string('errorwithsettings', 'admin');
74 $firsterror = reset($adminroot->errors);
76 $settingspage = $adminroot->locate($section, true);
79 if ($PAGE->user_allowed_editing() && $adminediting != -1) {
80 $USER->editing = $adminediting;
83 /// print header stuff ------------------------------------------------------------
84 if (empty($SITE->fullname)) {
85 $PAGE->set_title($settingspage->visiblename);
86 $PAGE->set_heading($settingspage->visiblename);
88 echo $OUTPUT->header();
89 echo $OUTPUT->box(get_string('configintrosite', 'admin'));
91 if ($errormsg !== '') {
92 echo $OUTPUT->notification($errormsg);
94 } else if ($statusmsg !== '') {
95 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
98 // ---------------------------------------------------------------------------------------------------------------
100 $pageparams = $PAGE->url->params();
101 $context = [
102 'actionurl' => $PAGE->url->out(false),
103 'params' => array_map(function($param) use ($pageparams) {
104 return [
105 'name' => $param,
106 'value' => $pageparams[$param]
108 }, array_keys($pageparams)),
109 'sesskey' => sesskey(),
110 'return' => $return,
111 'title' => null,
112 'settings' => $settingspage->output_html(),
113 'showsave' => true
116 echo $OUTPUT->render_from_template('core_admin/settings', $context);
118 } else {
119 if ($PAGE->user_allowed_editing() && !$PAGE->theme->haseditswitch) {
120 $url = clone($PAGE->url);
121 if ($PAGE->user_is_editing()) {
122 $caption = get_string('blockseditoff');
123 $url->param('adminedit', 'off');
124 } else {
125 $caption = get_string('blocksediton');
126 $url->param('adminedit', 'on');
128 $buttons = $OUTPUT->single_button($url, $caption, 'get');
129 $PAGE->set_button($buttons);
132 $visiblepathtosection = array_reverse($settingspage->visiblepath);
134 $PAGE->set_title("$SITE->shortname: " . implode(": ",$visiblepathtosection));
135 $PAGE->set_heading($SITE->fullname);
136 echo $OUTPUT->header();
138 if ($errormsg !== '') {
139 echo $OUTPUT->notification($errormsg);
141 } else if ($statusmsg !== '') {
142 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
145 // ---------------------------------------------------------------------------------------------------------------
147 $pageparams = $PAGE->url->params();
148 $context = [
149 'actionurl' => $PAGE->url->out(false),
150 'params' => array_map(function($param) use ($pageparams) {
151 return [
152 'name' => $param,
153 'value' => $pageparams[$param]
155 }, array_keys($pageparams)),
156 'sesskey' => sesskey(),
157 'return' => $return,
158 'title' => $settingspage->visiblename,
159 'settings' => $settingspage->output_html(),
160 'showsave' => $settingspage->show_save()
163 echo $OUTPUT->render_from_template('core_admin/settings', $context);
166 // Add the form change checker.
167 $PAGE->requires->js_call_amd('core_form/changechecker', 'watchFormById', ['adminsettings']);
169 if ($settingspage->has_dependencies()) {
170 $opts = [
171 'dependencies' => $settingspage->get_dependencies_for_javascript()
173 $PAGE->requires->js_call_amd('core/showhidesettings', 'init', [$opts]);
176 echo $OUTPUT->footer();