edb57055fd01f405952a951f5afa88ddbb17d6e4
[moodle.git] / blocks / html / edit_form.php
1 <?php
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/>.
18 /**
19  * Form for editing HTML block instances.
20  *
21  * @package   block_html
22  * @copyright 2009 Tim Hunt
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
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  */
32 class block_html_edit_form extends block_edit_form {
33     protected function specific_definition($mform) {
34         global $CFG;
36         // Fields for editing HTML block title and contents.
37         $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
39         $mform->addElement('text', 'config_title', get_string('configtitle', 'block_html'));
40         $mform->setType('config_title', PARAM_TEXT);
42         $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->block->context);
43         $mform->addElement('editor', 'config_text', get_string('configcontent', 'block_html'), null, $editoroptions);
44         $mform->addRule('config_text', null, 'required', null, 'client');
45         $mform->setType('config_text', PARAM_RAW); // XSS is prevented when printing the block contents and serving files
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         }
52     }
54     function set_data($defaults) {
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)) {
59                 $currenttext = '';
60             } else {
61                 $currenttext = $text;
62             }
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 = '';
68         }
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         }
78         // have to delete text here, otherwise parent::set_data will empty content
79         // of editor
80         unset($this->block->config->text);
81         parent::set_data($defaults);
82         // restore $text
83         if (!isset($this->block->config)) {
84             $this->block->config = new stdClass();
85         }
86         $this->block->config->text = $text;
87         if (isset($title)) {
88             // Reset the preserved title
89             $this->block->config->title = $title;
90         }
91     }
92 }