MDL-35042 blocks: Allow HTML block advanced setting to be toggled
[moodle.git] / blocks / html / edit_form.php
CommitLineData
1d13c75c 1<?php
2
4ca6cfbf
PS
3// This file is part of Moodle - http://moodle.org/
4//
1d13c75c 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.
4ca6cfbf 14//
1d13c75c 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 * Form for editing HTML block instances.
20 *
64f93798 21 * @package block_html
1d13c75c 22 * @copyright 2009 Tim Hunt
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26/**
27 * Form for editing HTML block instances.
28 *
29 * @copyright 2009 Tim Hunt
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
32class block_html_edit_form extends block_edit_form {
33 protected function specific_definition($mform) {
055cc835
AB
34 global $CFG;
35
a60bdd8c 36 // Fields for editing HTML block title and contents.
1d13c75c 37 $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
38
39 $mform->addElement('text', 'config_title', get_string('configtitle', 'block_html'));
071e68f9 40 $mform->setType('config_title', PARAM_TEXT);
1d13c75c 41
d449b246 42 $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->block->context);
94ee9ae0 43 $mform->addElement('editor', 'config_text', get_string('configcontent', 'block_html'), null, $editoroptions);
2ee40a70 44 $mform->addRule('config_text', null, 'required', null, 'client');
d449b246 45 $mform->setType('config_text', PARAM_RAW); // XSS is prevented when printing the block contents and serving files
14bcde67 46
055cc835
AB
47 if (!empty($CFG->block_html_allowcssclasses)) {
48 $mform->addElement('text', 'config_classes', get_string('configclasses', 'block_html'));
49 $mform->setType('config_classes', PARAM_TEXT);
50 $mform->addHelpButton('config_classes', 'configclasses', 'block_html');
51 }
1d13c75c 52 }
41976db3
DC
53
54 function set_data($defaults) {
b1384289
DC
55 if (!empty($this->block->config) && is_object($this->block->config)) {
56 $text = $this->block->config->text;
57 $draftid_editor = file_get_submitted_draft_itemid('config_text');
58 if (empty($text)) {
ce162914
DC
59 $currenttext = '';
60 } else {
b1384289 61 $currenttext = $text;
ce162914 62 }
b1384289
DC
63 $defaults->config_text['text'] = file_prepare_draft_area($draftid_editor, $this->block->context->id, 'block_html', 'content', 0, array('subdirs'=>true), $currenttext);
64 $defaults->config_text['itemid'] = $draftid_editor;
65 $defaults->config_text['format'] = $this->block->config->format;
66 } else {
67 $text = '';
00ab45e7 68 }
34856876
SH
69
70 if (!$this->block->user_can_edit() && !empty($this->block->config->title)) {
71 // If a title has been set but the user cannot edit it format it nicely
72 $title = $this->block->config->title;
73 $defaults->config_title = format_string($title, true, $this->page->context);
74 // Remove the title from the config so that parent::set_data doesn't set it.
75 unset($this->block->config->title);
76 }
77
b1384289
DC
78 // have to delete text here, otherwise parent::set_data will empty content
79 // of editor
80 unset($this->block->config->text);
41976db3 81 parent::set_data($defaults);
b1384289 82 // restore $text
b85b25eb
PS
83 if (!isset($this->block->config)) {
84 $this->block->config = new stdClass();
85 }
b1384289 86 $this->block->config->text = $text;
34856876
SH
87 if (isset($title)) {
88 // Reset the preserved title
89 $this->block->config->title = $title;
90 }
41976db3 91 }
94ee9ae0 92}