MDL-22952 - Fixed bidirectional page problems (right to left).
[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
b959599b 32 function applicable_formats() {
33 return array('all' => true);
34 }
35
86db09ef 36 function specialization() {
f36acb35 37 $this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_html'));
86db09ef 38 }
39
40 function instance_allow_multiple() {
41 return true;
42 }
43
44 function get_content() {
86db09ef 45 if ($this->content !== NULL) {
46 return $this->content;
47 }
48
3179b000 49 if ($this->content_is_trusted()) {
e92c286c 50 // fancy html allowed only on course, category and system blocks.
e8a7edd7 51 $filteropt = new stdClass;
52 $filteropt->noclean = true;
53 } else {
54 $filteropt = null;
55 }
e345909c 56
86db09ef 57 $this->content = new stdClass;
86db09ef 58 $this->content->footer = '';
94ee9ae0 59 if (isset($this->config->text)) {
41976db3 60 // rewrite url
64f93798 61 $this->config->text['text'] = file_rewrite_pluginfile_urls($this->config->text['text'], 'pluginfile.php', $this->context->id, 'block_html', 'content', NULL);
94ee9ae0
MD
62 $this->content->text = format_text($this->config->text['text'], $this->config->text['format'], $filteropt);
63 } else {
64 $this->content->text = '';
65 }
86db09ef 66
e345909c 67 unset($filteropt); // memory footprint
68
86db09ef 69 return $this->content;
70 }
43457dc8 71
41976db3
DC
72
73 /**
74 * Serialize and store config data
75 */
76 function instance_config_save($data, $nolongerused = false) {
77 global $DB;
78
79 // Move embedded files into a proper filearea and adjust HTML links to match
64f93798 80 $data->text['text'] = file_save_draft_area_files($data->text['itemid'], $this->context->id, 'block_html', 'content', 0, array('subdirs'=>true), $data->text['text']);
41976db3
DC
81
82 parent::instance_config_save($data, $nolongerused);
83 }
84
af140288
DC
85 function instance_delete() {
86 global $DB;
87 $fs = get_file_storage();
64f93798 88 $fs->delete_area_files($this->context->id, 'block_html');
af140288
DC
89 return true;
90 }
41976db3 91
3179b000 92 function content_is_trusted() {
93 return in_array($this->page->context->contextlevel, array(CONTEXT_COURSE, CONTEXT_COURSECAT, CONTEXT_SYSTEM));
94 }
95
5cfefc9b 96 /**
97 * Will be called before an instance of this block is backed up, so that any links in
98 * any links in any HTML fields on config can be encoded.
99 * @return string
100 */
101 function get_backup_encoded_config() {
83fc4004 102 /// Prevent clone for non configured block instance. Delegate to parent as fallback.
103 if (empty($this->config)) {
104 return parent::get_backup_encoded_config();
105 }
5cfefc9b 106 $data = clone($this->config);
107 $data->text = backup_encode_absolute_links($data->text);
108 return base64_encode(serialize($data));
43457dc8 109 }
110
5cfefc9b 111 /**
112 * This function makes all the necessary calls to {@link restore_decode_content_links_worker()}
4ca6cfbf
PS
113 * function in order to decode contents of this block from the backup
114 * format to destination site/course in order to mantain inter-activities
115 * working in the backup/restore process.
116 *
5cfefc9b 117 * This is called from {@link restore_decode_content_links()} function in the restore process.
118 *
119 * NOTE: There is no block instance when this method is called.
120 *
121 * @param object $restore Standard restore object
122 * @return boolean
123 **/
124 function decode_content_links_caller($restore) {
f28f2d90 125 global $CFG, $DB;
5cfefc9b 126
f28f2d90 127 if ($restored_blocks = $DB->get_records_select("backup_ids", "table_name = 'block_instance' AND backup_code = ? AND new_id > 0", array($restore->backup_unique_code), "", "new_id")) {
5cfefc9b 128 $restored_blocks = implode(',', array_keys($restored_blocks));
129 $sql = "SELECT bi.*
f28f2d90 130 FROM {block_instance} bi
131 JOIN {block} b ON b.id = bi.blockid
4ca6cfbf 132 WHERE b.name = 'html' AND bi.id IN ($restored_blocks)";
5cfefc9b 133
f28f2d90 134 if ($instances = $DB->get_records_sql($sql)) {
5cfefc9b 135 foreach ($instances as $instance) {
136 $blockobject = block_instance('html', $instance);
137 $blockobject->config->text = restore_decode_absolute_links($blockobject->config->text);
138 $blockobject->config->text = restore_decode_content_links_worker($blockobject->config->text, $restore);
e92c286c 139 $blockobject->instance_config_commit();
5cfefc9b 140 }
141 }
142 }
143
144 return true;
43457dc8 145 }
86db09ef 146}