webservice MDL-21908 admin: use renderer into token pages + a bit of code cleaning
[moodle.git] / lib / form / editor.php
CommitLineData
ae4a98a1 1<?php
2
3global $CFG;
cefe1408 4
5require_once('HTML/QuickForm/element.php');
ae4a98a1 6require_once($CFG->dirroot.'/lib/filelib.php');
ad362539 7require_once($CFG->dirroot.'/repository/lib.php');
cefe1408 8
cefe1408 9//TODO:
10// * locking
11// * freezing
12// * ajax format conversion
cefe1408 13
14class MoodleQuickForm_editor extends HTML_QuickForm_element {
12f11f23 15 public $_helpbutton = '';
775af58f
DC
16
17 public $_type = 'editor';
69429650 18 protected $_options = array('subdirs'=>0, 'maxbytes'=>0, 'maxfiles'=>0, 'changeformat'=>0,
7de5a29a 19 'context'=>null, 'noclean'=>0, 'trusttext'=>0);
cefe1408 20 protected $_values = array('text'=>null, 'format'=>null, 'itemid'=>null);
21
34d39b79 22 function MoodleQuickForm_editor($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
99eaca9d 23 global $CFG, $PAGE;
cefe1408 24
25 $options = (array)$options;
26 foreach ($options as $name=>$value) {
27 if (array_key_exists($name, $this->_options)) {
28 $this->_options[$name] = $value;
0b94f320 29 }
30 }
cefe1408 31 if (!empty($options['maxbytes'])) {
32 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
0b94f320 33 }
69429650 34 if (!$this->_options['context']) {
35 $this->_options['context'] = get_context_instance(CONTEXT_SYSTEM);
36 }
7de5a29a 37 $this->_options['trusted'] = trusttext_trusted($this->_options['context']);
cefe1408 38 parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
ff5fe311 39
ff5fe311 40 editors_head_setup();
0b94f320 41 }
cefe1408 42
43 function setName($name) {
44 $this->updateAttributes(array('name'=>$name));
45 }
46
47 function getName() {
48 return $this->getAttribute('name');
49 }
50
51 function setValue($values) {
52 $values = (array)$values;
53 foreach ($values as $name=>$value) {
54 if (array_key_exists($name, $this->_values)) {
55 $this->_values[$name] = $value;
0b94f320 56 }
57 }
0b94f320 58 }
59
cefe1408 60 function getValue() {
61 return $this->getAttribute('value');
62 }
63
64 function getMaxbytes() {
65 return $this->_options['maxbytes'];
66 }
67
68 function setMaxbytes($maxbytes) {
69 global $CFG;
70 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
71 }
72
ac025043 73 function getMaxfiles() {
74 return $this->_options['maxfiles'];
75 }
76
77 function setMaxfiles($num) {
78 $this->_options['maxfiles'] = $num;
79 }
80
cefe1408 81 function getSubdirs() {
82 return $this->_options['subdirs'];
83 }
84
85 function setSubdirs($allow) {
86 $this->_options['subdirs'] = $allow;
87 }
88
89 function setHelpButton($_helpbuttonargs, $function='_helpbutton') {
90 if (!is_array($_helpbuttonargs)) {
91 $_helpbuttonargs = array($_helpbuttonargs);
92 } else {
93 $_helpbuttonargs = $_helpbuttonargs;
94 }
95 //we do this to to return html instead of printing it
96 //without having to specify it in every call to make a button.
97 if ('_helpbutton' == $function){
98 $defaultargs = array('', '', 'moodle', true, false, '', true);
99 $_helpbuttonargs = $_helpbuttonargs + $defaultargs ;
100 }
101 $this->_helpbutton=call_user_func_array($function, $_helpbuttonargs);
102 }
103
104 function getHelpButton() {
105 return $this->_helpbutton;
106 }
107
108 function getElementTemplateType() {
109 if ($this->_flagFrozen){
110 return 'nodisplay';
111 } else {
112 return 'default';
113 }
114 }
115
116 function toHtml() {
1420162a 117 global $CFG, $COURSE, $PAGE;
99eaca9d 118 require_once($CFG->dirroot.'/repository/lib.php');
cefe1408 119
0b94f320 120 if ($this->_flagFrozen) {
121 return $this->getFrozenHtml();
cefe1408 122 }
123
4c508047
PS
124 $ctx = $this->_options['context'];
125
cefe1408 126 $id = $this->_attributes['id'];
127 $elname = $this->_attributes['name'];
128
129 $subdirs = $this->_options['subdirs'];
130 $maxbytes = $this->_options['maxbytes'];
ac025043 131 $maxfiles = $this->_options['maxfiles'];
cefe1408 132 $changeformat = $this->_options['changeformat']; // TO DO: implement as ajax calls
133
134 $text = $this->_values['text'];
135 $format = $this->_values['format'];
7cdee748 136 $draftitemid = $this->_values['itemid'];
cefe1408 137
138 // security - never ever allow guest/not logged in user to upload anything
139 if (isguestuser() or !isloggedin()) {
ac025043 140 $maxfiles = 0;
cefe1408 141 }
142
143 $str = $this->_getTabs();
144 $str .= '<div>';
145
20e5da7d 146 $editor = editors_get_preferred_editor($format);
5ca3c838 147 $strformats = format_text_menu();
148 $formats = $editor->get_supported_formats();
149 foreach ($formats as $fid) {
150 $formats[$fid] = $strformats[$fid];
cefe1408 151 }
152
4c508047 153 // get filepicker info
875f4e7b
DC
154 //
155 $fpoptions = array();
4c508047
PS
156 if ($maxfiles != 0 ) {
157 if (empty($draftitemid)) {
158 // no existing area info provided - let's use fresh new draft area
159 require_once("$CFG->libdir/filelib.php");
160 $this->setValue(array('itemid'=>file_get_unused_draft_itemid()));
161 $draftitemid = $this->_values['itemid'];
162 }
163
164 $args = new stdclass;
165 // need these three to filter repositories list
875f4e7b 166 $args->accepted_types = array('image');
4c508047
PS
167 $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
168 $args->context = $ctx;
169 $args->env = 'filepicker';
170
875f4e7b
DC
171 $image_options = initialise_filepicker($args);
172
033aad78 173 $args->accepted_types = array('video', 'audio');
775af58f 174 $media_options = initialise_filepicker($args);
875f4e7b 175
be85f7ab
DC
176 $image_options->context = $ctx;
177 $media_options->context = $ctx;
875f4e7b
DC
178 $image_options->client_id = uniqid();
179 $media_options->client_id = uniqid();
180 $image_options->maxbytes = $this->_options['maxbytes'];
181 $media_options->maxbytes = $this->_options['maxbytes'];
182 $image_options->maxfiles = 1;
183 $media_options->maxfiles = 1;
184 $image_options->env = 'editor';
185 $media_options->env = 'editor';
186 $image_options->itemid = $draftitemid;
187 $media_options->itemid = $draftitemid;
188 $fpoptions['image'] = $image_options;
189 $fpoptions['media'] = $media_options;
4c508047
PS
190 }
191
5ca3c838 192 /// print text area - TODO: add on-the-fly switching, size configuration, etc.
4c508047 193 $editor->use_editor($id, $this->_options, $fpoptions);
7cdee748 194
69429650 195 $str .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="15" cols="80">';
7cdee748 196 $str .= s($text);
197 $str .= '</textarea></div>';
198
cefe1408 199 $str .= '<div>';
5ca3c838 200 $str .= '<select name="'.$elname.'[format]">';
201 foreach ($formats as $key=>$desc) {
202 $selected = ($format == $key) ? 'selected="selected"' : '';
203 $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
cefe1408 204 }
5ca3c838 205 $str .= '</select>';
cefe1408 206 $str .= '</div>';
207
137bbbc6
DC
208 // during moodle installation, user area doesn't exist
209 // so we need to disable filepicker here.
210 if (!during_initial_install() && empty($CFG->adminsetuppending)) {
211 // 0 means no files, -1 unlimited
212 if ($maxfiles != 0 ) {
213 $str .= '<div><input type="hidden" name="'.$elname.'[itemid]" value="'.$draftitemid.'" /></div>';
214 $str .= '<div id="'.$id.'_filemanager">';
7f39d66a
DC
215 $editorurl = new moodle_url("$CFG->wwwroot/repository/filepicker.php", array(
216 'action'=>'browse',
217 'env'=>'editor',
218 'itemid'=>$draftitemid,
219 'subdirs'=>$subdirs,
220 'maxbytes'=>$maxbytes,
221 'ctx_id'=>$ctx->id,
222 'course'=>$PAGE->course->id
223 ));
224 $str .= html_writer::link($editorurl->out(false), get_string('manageeditorfiles'), array('target'=>'_blank'));
137bbbc6
DC
225 //$str .= '<object type="text/html" data="'.$editorurl.'" height="160" width="600" style="border:1px solid #000">Error</object>';
226 $str .= '</div>';
227 }
0b94f320 228 }
cefe1408 229
137bbbc6 230
cefe1408 231 $str .= '</div>';
232
233 return $str;
234 }
235
0b94f320 236}