weekly release 2.3dev
[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() {
8367651e 61 return $this->_values;
cefe1408 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
ab978b38
RT
89 /**
90 * Returns editor format
91 *
92 * @return int.
93 */
94 function getFormat() {
95 return $this->_values['format'];
96 }
97
cbe20ec3
RT
98 /**
99 * Checks if editor used is tinymce and is required field
100 *
101 * @return true if required field.
102 */
ab978b38
RT
103 function isRequired() {
104 return (isset($this->_options['required']) && $this->_options['required']);
cbe20ec3
RT
105 }
106
cefe1408 107 function setHelpButton($_helpbuttonargs, $function='_helpbutton') {
108 if (!is_array($_helpbuttonargs)) {
109 $_helpbuttonargs = array($_helpbuttonargs);
110 } else {
111 $_helpbuttonargs = $_helpbuttonargs;
112 }
113 //we do this to to return html instead of printing it
114 //without having to specify it in every call to make a button.
115 if ('_helpbutton' == $function){
116 $defaultargs = array('', '', 'moodle', true, false, '', true);
117 $_helpbuttonargs = $_helpbuttonargs + $defaultargs ;
118 }
119 $this->_helpbutton=call_user_func_array($function, $_helpbuttonargs);
120 }
121
122 function getHelpButton() {
123 return $this->_helpbutton;
124 }
125
126 function getElementTemplateType() {
127 if ($this->_flagFrozen){
128 return 'nodisplay';
129 } else {
130 return 'default';
131 }
132 }
133
134 function toHtml() {
057815f3 135 global $CFG, $PAGE;
99eaca9d 136 require_once($CFG->dirroot.'/repository/lib.php');
cefe1408 137
0b94f320 138 if ($this->_flagFrozen) {
139 return $this->getFrozenHtml();
cefe1408 140 }
141
4c508047
PS
142 $ctx = $this->_options['context'];
143
cefe1408 144 $id = $this->_attributes['id'];
145 $elname = $this->_attributes['name'];
146
147 $subdirs = $this->_options['subdirs'];
148 $maxbytes = $this->_options['maxbytes'];
ac025043 149 $maxfiles = $this->_options['maxfiles'];
cefe1408 150 $changeformat = $this->_options['changeformat']; // TO DO: implement as ajax calls
151
152 $text = $this->_values['text'];
153 $format = $this->_values['format'];
7cdee748 154 $draftitemid = $this->_values['itemid'];
cefe1408 155
156 // security - never ever allow guest/not logged in user to upload anything
157 if (isguestuser() or !isloggedin()) {
ac025043 158 $maxfiles = 0;
cefe1408 159 }
160
161 $str = $this->_getTabs();
162 $str .= '<div>';
163
20e5da7d 164 $editor = editors_get_preferred_editor($format);
5ca3c838 165 $strformats = format_text_menu();
166 $formats = $editor->get_supported_formats();
167 foreach ($formats as $fid) {
168 $formats[$fid] = $strformats[$fid];
cefe1408 169 }
170
4c508047 171 // get filepicker info
875f4e7b
DC
172 //
173 $fpoptions = array();
4c508047
PS
174 if ($maxfiles != 0 ) {
175 if (empty($draftitemid)) {
176 // no existing area info provided - let's use fresh new draft area
177 require_once("$CFG->libdir/filelib.php");
178 $this->setValue(array('itemid'=>file_get_unused_draft_itemid()));
179 $draftitemid = $this->_values['itemid'];
180 }
181
6bdfef5d 182 $args = new stdClass();
4c508047 183 // need these three to filter repositories list
875f4e7b 184 $args->accepted_types = array('image');
4c508047
PS
185 $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
186 $args->context = $ctx;
187 $args->env = 'filepicker';
a4d91438 188 // advimage plugin
875f4e7b 189 $image_options = initialise_filepicker($args);
766514a0
DC
190 $image_options->context = $ctx;
191 $image_options->client_id = uniqid();
192 $image_options->maxbytes = $this->_options['maxbytes'];
193 $image_options->env = 'editor';
194 $image_options->itemid = $draftitemid;
875f4e7b 195
a4d91438 196 // moodlemedia plugin
033aad78 197 $args->accepted_types = array('video', 'audio');
775af58f 198 $media_options = initialise_filepicker($args);
be85f7ab 199 $media_options->context = $ctx;
875f4e7b 200 $media_options->client_id = uniqid();
875f4e7b 201 $media_options->maxbytes = $this->_options['maxbytes'];
875f4e7b 202 $media_options->env = 'editor';
875f4e7b 203 $media_options->itemid = $draftitemid;
766514a0 204
a4d91438 205 // advlink plugin
766514a0
DC
206 $args->accepted_types = '*';
207 $link_options = initialise_filepicker($args);
208 $link_options->context = $ctx;
209 $link_options->client_id = uniqid();
210 $link_options->maxbytes = $this->_options['maxbytes'];
211 $link_options->env = 'editor';
212 $link_options->itemid = $draftitemid;
213
875f4e7b
DC
214 $fpoptions['image'] = $image_options;
215 $fpoptions['media'] = $media_options;
766514a0 216 $fpoptions['link'] = $link_options;
4c508047
PS
217 }
218
cbe20ec3
RT
219 //If editor is required and tinymce, then set required_tinymce option to initalize tinymce validation.
220 if (($editor instanceof tinymce_texteditor) && !is_null($this->getAttribute('onchange'))) {
ab978b38 221 $this->_options['required'] = true;
cbe20ec3
RT
222 }
223
5ca3c838 224 /// print text area - TODO: add on-the-fly switching, size configuration, etc.
4c508047 225 $editor->use_editor($id, $this->_options, $fpoptions);
7cdee748 226
cb2c1963
PS
227 $rows = empty($this->_attributes['rows']) ? 15 : $this->_attributes['rows'];
228 $cols = empty($this->_attributes['cols']) ? 80 : $this->_attributes['cols'];
229
cbe20ec3 230 //Apply editor validation if required field
56728202
AO
231 $editorrules = '';
232 if (!is_null($this->getAttribute('onblur')) && !is_null($this->getAttribute('onchange'))) {
856484f9 233 $editorrules = ' onblur="'.htmlspecialchars($this->getAttribute('onblur')).'" onchange="'.htmlspecialchars($this->getAttribute('onchange')).'"';
56728202
AO
234 }
235 $str .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="'.$rows.'" cols="'.$cols.'"'.$editorrules.'>';
7cdee748 236 $str .= s($text);
237 $str .= '</textarea></div>';
238
cefe1408 239 $str .= '<div>';
5a1a7112
CC
240 if (count($formats)>1) {
241 $str.= html_writer::select($formats, $elname.'[format]', $format, false);
242 } else {
243 $str.= html_writer::empty_tag('input',
244 array('name'=>$elname.'[format]', 'type'=> 'hidden', 'value' => array_pop(array_keys($formats))));
cefe1408 245 }
246 $str .= '</div>';
247
137bbbc6
DC
248 // during moodle installation, user area doesn't exist
249 // so we need to disable filepicker here.
250 if (!during_initial_install() && empty($CFG->adminsetuppending)) {
251 // 0 means no files, -1 unlimited
252 if ($maxfiles != 0 ) {
563d0417
DC
253 $str .= '<input type="hidden" name="'.$elname.'[itemid]" value="'.$draftitemid.'" />';
254
255 // used by non js editor only
256 $editorurl = new moodle_url("$CFG->wwwroot/repository/draftfiles_manager.php", array(
7f39d66a
DC
257 'action'=>'browse',
258 'env'=>'editor',
259 'itemid'=>$draftitemid,
260 'subdirs'=>$subdirs,
261 'maxbytes'=>$maxbytes,
563d0417 262 'maxfiles'=>$maxfiles,
7f39d66a 263 'ctx_id'=>$ctx->id,
71267723
DC
264 'course'=>$PAGE->course->id,
265 'sesskey'=>sesskey(),
7f39d66a 266 ));
563d0417 267 $str .= '<noscript>';
6ef1402e 268 $str .= "<div><object type='text/html' data='$editorurl' height='160' width='600' style='border:1px solid #000'></object></div>";
563d0417 269 $str .= '</noscript>';
137bbbc6 270 }
0b94f320 271 }
cefe1408 272
137bbbc6 273
cefe1408 274 $str .= '</div>';
275
276 return $str;
277 }
278
8367651e
RW
279 /**
280 * What to display when element is frozen.
281 *
282 * @return empty string
283 */
284 function getFrozenHtml() {
285
286 return '';
287 }
0b94f320 288}