MDL-35042 blocks: Allow HTML block advanced setting to be toggled
[moodle.git] / blocks / html / block_html.php
CommitLineData
4ca6cfbf 1<?php
86db09ef 2
64f93798
PS
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 * Form for editing HTML block instances.
20 *
21 * @package block_html
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
86db09ef 26class block_html extends block_base {
27
28 function init() {
8c5bba16 29 $this->title = get_string('pluginname', 'block_html');
86db09ef 30 }
31
055cc835
AB
32 function has_config() {
33 return true;
34 }
35
b959599b 36 function applicable_formats() {
37 return array('all' => true);
38 }
39
86db09ef 40 function specialization() {
f36acb35 41 $this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_html'));
86db09ef 42 }
43
44 function instance_allow_multiple() {
45 return true;
46 }
47
48 function get_content() {
99d19c13
PS
49 global $CFG;
50
51 require_once($CFG->libdir . '/filelib.php');
52
86db09ef 53 if ($this->content !== NULL) {
54 return $this->content;
55 }
56
367a75fa
SH
57 $filteropt = new stdClass;
58 $filteropt->overflowdiv = true;
3179b000 59 if ($this->content_is_trusted()) {
e92c286c 60 // fancy html allowed only on course, category and system blocks.
e8a7edd7 61 $filteropt->noclean = true;
e8a7edd7 62 }
e345909c 63
86db09ef 64 $this->content = new stdClass;
86db09ef 65 $this->content->footer = '';
94ee9ae0 66 if (isset($this->config->text)) {
41976db3 67 // rewrite url
ce162914 68 $this->config->text = file_rewrite_pluginfile_urls($this->config->text, 'pluginfile.php', $this->context->id, 'block_html', 'content', NULL);
bd462476
SH
69 // Default to FORMAT_HTML which is what will have been used before the
70 // editor was properly implemented for the block.
71 $format = FORMAT_HTML;
72 // Check to see if the format has been properly set on the config
73 if (isset($this->config->format)) {
74 $format = $this->config->format;
75 }
76 $this->content->text = format_text($this->config->text, $format, $filteropt);
94ee9ae0
MD
77 } else {
78 $this->content->text = '';
79 }
86db09ef 80
e345909c 81 unset($filteropt); // memory footprint
82
86db09ef 83 return $this->content;
84 }
43457dc8 85
41976db3
DC
86
87 /**
88 * Serialize and store config data
89 */
90 function instance_config_save($data, $nolongerused = false) {
91 global $DB;
92
ce162914 93 $config = clone($data);
41976db3 94 // Move embedded files into a proper filearea and adjust HTML links to match
ce162914
DC
95 $config->text = file_save_draft_area_files($data->text['itemid'], $this->context->id, 'block_html', 'content', 0, array('subdirs'=>true), $data->text['text']);
96 $config->format = $data->text['format'];
41976db3 97
ce162914 98 parent::instance_config_save($config, $nolongerused);
41976db3
DC
99 }
100
af140288
DC
101 function instance_delete() {
102 global $DB;
103 $fs = get_file_storage();
64f93798 104 $fs->delete_area_files($this->context->id, 'block_html');
af140288
DC
105 return true;
106 }
41976db3 107
3179b000 108 function content_is_trusted() {
d449b246
PS
109 global $SCRIPT;
110
758bfdf6
PS
111 if (!$context = get_context_instance_by_id($this->instance->parentcontextid)) {
112 return false;
113 }
d449b246
PS
114 //find out if this block is on the profile page
115 if ($context->contextlevel == CONTEXT_USER) {
116 if ($SCRIPT === '/my/index.php') {
117 // this is exception - page is completely private, nobody else may see content there
118 // that is why we allow JS here
119 return true;
120 } else {
121 // no JS on public personal pages, it would be a big security issue
122 return false;
123 }
124 }
125
126 return true;
3179b000 127 }
efe41e35 128
baf9c832
JF
129 /**
130 * The block should only be dockable when the title of the block is not empty
131 * and when parent allows docking.
132 *
133 * @return bool
134 */
135 public function instance_can_be_docked() {
136 return (!empty($this->config->title) && parent::instance_can_be_docked());
137 }
14bcde67
AB
138
139 /*
140 * Add custom html attributes to aid with theming and styling
141 *
142 * @return array
143 */
144 function html_attributes() {
055cc835
AB
145 global $CFG;
146
14bcde67
AB
147 $attributes = parent::html_attributes();
148
055cc835
AB
149 if (!empty($CFG->block_html_allowcssclasses)) {
150 if (!empty($this->config->classes)) {
151 $attributes['class'] .= ' '.$this->config->classes;
152 }
14bcde67
AB
153 }
154
155 return $attributes;
156 }
86db09ef 157}