MDL-16597, added some style to make filemanger prettier
[moodle.git] / blocks / html / block_html.php
CommitLineData
4ca6cfbf 1<?php
86db09ef 2
3class block_html extends block_base {
4
5 function init() {
8c5bba16 6 $this->title = get_string('pluginname', 'block_html');
433c242f 7 $this->version = 2007101509;
86db09ef 8 }
9
b959599b 10 function applicable_formats() {
11 return array('all' => true);
12 }
13
86db09ef 14 function specialization() {
f36acb35 15 $this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_html'));
86db09ef 16 }
17
18 function instance_allow_multiple() {
19 return true;
20 }
21
22 function get_content() {
86db09ef 23 if ($this->content !== NULL) {
24 return $this->content;
25 }
26
3179b000 27 if ($this->content_is_trusted()) {
e92c286c 28 // fancy html allowed only on course, category and system blocks.
e8a7edd7 29 $filteropt = new stdClass;
30 $filteropt->noclean = true;
31 } else {
32 $filteropt = null;
33 }
e345909c 34
86db09ef 35 $this->content = new stdClass;
86db09ef 36 $this->content->footer = '';
94ee9ae0 37 if (isset($this->config->text)) {
41976db3
DC
38 // rewrite url
39 $this->config->text['text'] = file_rewrite_pluginfile_urls($this->config->text['text'], 'pluginfile.php', $this->context->id, 'block_html', $this->instance->id);
94ee9ae0
MD
40 $this->content->text = format_text($this->config->text['text'], $this->config->text['format'], $filteropt);
41 } else {
42 $this->content->text = '';
43 }
86db09ef 44
e345909c 45 unset($filteropt); // memory footprint
46
86db09ef 47 return $this->content;
48 }
43457dc8 49
41976db3
DC
50
51 /**
52 * Serialize and store config data
53 */
54 function instance_config_save($data, $nolongerused = false) {
55 global $DB;
56
57 // Move embedded files into a proper filearea and adjust HTML links to match
58 $data->text['text'] = file_save_draft_area_files($data->text['itemid'], $this->context->id, 'block_html', $this->instance->id, array('subdirs'=>true), $data->text['text']);
59
60 parent::instance_config_save($data, $nolongerused);
61 }
62
63
3179b000 64 function content_is_trusted() {
65 return in_array($this->page->context->contextlevel, array(CONTEXT_COURSE, CONTEXT_COURSECAT, CONTEXT_SYSTEM));
66 }
67
5cfefc9b 68 /**
69 * Will be called before an instance of this block is backed up, so that any links in
70 * any links in any HTML fields on config can be encoded.
71 * @return string
72 */
73 function get_backup_encoded_config() {
83fc4004 74 /// Prevent clone for non configured block instance. Delegate to parent as fallback.
75 if (empty($this->config)) {
76 return parent::get_backup_encoded_config();
77 }
5cfefc9b 78 $data = clone($this->config);
79 $data->text = backup_encode_absolute_links($data->text);
80 return base64_encode(serialize($data));
43457dc8 81 }
82
5cfefc9b 83 /**
84 * This function makes all the necessary calls to {@link restore_decode_content_links_worker()}
4ca6cfbf
PS
85 * function in order to decode contents of this block from the backup
86 * format to destination site/course in order to mantain inter-activities
87 * working in the backup/restore process.
88 *
5cfefc9b 89 * This is called from {@link restore_decode_content_links()} function in the restore process.
90 *
91 * NOTE: There is no block instance when this method is called.
92 *
93 * @param object $restore Standard restore object
94 * @return boolean
95 **/
96 function decode_content_links_caller($restore) {
f28f2d90 97 global $CFG, $DB;
5cfefc9b 98
f28f2d90 99 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 100 $restored_blocks = implode(',', array_keys($restored_blocks));
101 $sql = "SELECT bi.*
f28f2d90 102 FROM {block_instance} bi
103 JOIN {block} b ON b.id = bi.blockid
4ca6cfbf 104 WHERE b.name = 'html' AND bi.id IN ($restored_blocks)";
5cfefc9b 105
f28f2d90 106 if ($instances = $DB->get_records_sql($sql)) {
5cfefc9b 107 foreach ($instances as $instance) {
108 $blockobject = block_instance('html', $instance);
109 $blockobject->config->text = restore_decode_absolute_links($blockobject->config->text);
110 $blockobject->config->text = restore_decode_content_links_worker($blockobject->config->text, $restore);
e92c286c 111 $blockobject->instance_config_commit();
5cfefc9b 112 }
113 }
114 }
115
116 return true;
43457dc8 117 }
41976db3
DC
118 function send_file($context, $filearea, $itemid, $filepath, $filename) {
119 $fs = get_file_storage();
120 $fullpath = $context->id.'block_html'.$itemid.$filepath.$filename;
121
122 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
123 send_file_not_found();
124 }
125
126 session_get_instance()->write_close();
127 send_stored_file($file, 60*60, 0, false);
128 }
86db09ef 129}
4ca6cfbf 130