Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / editor / tinymce / tinymce.class.php
blobe710c365149a78efad64ff6addb7266ad1b0a729
1 <?php // $Id$
3 /**
4 * This file contains the tinymce subclass for moodle editorObject.
6 * @author Janne Mikkonen
7 * @version $Id$
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package editorObject
11 class tinymce extends editorObject {
13 /**
14 * The tinyconf variable holds custom variable keys and values pairs in array.
15 * @var array $tinyconf
17 var $tinyconf = array();
19 /**
20 * Internal tinyconfkeys is an array of valid configuration keys.
21 * @var array $tinyconfkeys
23 var $tinyconfkeys = array(
24 "mode","theme","plugins","language","ask","textarea_trigger",
25 "editor_selector","editor_deselector","elements","docs_language",
26 "debug","focus_alert","directionality","auto_reset_designmode",
27 "auto_focus","nowrap","button_tile_map","auto_resize","browsers",
28 "dialog_type","accessibility_warnings","accessibility_focus",
29 "event_elements","table_inline_editing","object_resizing","custom_shortcuts",
30 "cleanup","valid_elements","extended_valid_elements","invalid_elements",
31 "verify_css_classes","verify_html","preformatted","encoding","cleanup_on_startup",
32 "fix_content_duplication","inline_styles","convert_newlines_to_brs","force_br_newlines",
33 "force_p_newlines","entities","entity_encoding","remove_linebreaks","convert_fonts_to_spans",
34 "font_size_classes","font_size_style_values","merge_styles_invalid_parents",
35 "force_hex_style_colors","apply_source_formatting","trim_span_elements","doctype",
36 "convert_urls","relative_urls","remove_script_host","document_base_url",
37 "urlconverter_callback","insertlink_callback","insertimage_callback","setupcontent_callback",
38 "save_callback","onchange_callback","init_instance_callback","file_browser_callback",
39 "cleanup_callback","handle_event_callback","execcommand_callback","oninit","onpageload",
40 "content_css","popups_css","editor_css","width","height","visual","visual_table_class",
41 "custom_undo_redo","custom_undo_redo_levels","custom_undo_redo_keyboard_shortcuts",
42 "custom_undo_redo_restore_selection","external_link_list_url","external_image_list_url",
43 "add_form_submit_trigger","add_unload_trigger","submit_patch");
45 /**
46 * Array of valid advanced theme configuration keys.
47 * @var array $tinythemekeys
49 var $tinythemekeys = array(
50 "theme_advanced_layout_manager","theme_advanced_blockformats","theme_advanced_styles",
51 "theme_advanced_source_editor_width","theme_advanced_source_editor_height",
52 "theme_advanced_toolbar_location","theme_advanced_toolbar_align",
53 "theme_advanced_statusbar_location","theme_advanced_buttons<1-n>","theme_advanced_buttons<1-n>_add",
54 "theme_advanced_buttons<1-n>_add_before","theme_advanced_disable","theme_advanced_containers",
55 "theme_advanced_containers_default_class","theme_advanced_containers_default_align",
56 "theme_advanced_container_<container>","theme_advanced_container_<container>_class",
57 "theme_advanced_container_<container>_align","theme_advanced_custom_layout",
58 "theme_advanced_link_targets","theme_advanced_resizing","theme_advanced_resizing_use_cookie",
59 "theme_advanced_resize_horizontal","theme_advanced_path","theme_advanced_fonts");
61 /**
62 * The defaults configuration array for internal use.
63 * @var array $defaults
65 var $defaults = array();
67 /**
68 * For internal usage variable which holds the information
69 * should dialogs script be printed after configuration.
70 * @var bool $printdialogs
72 var $printdialogs = false;
74 /**
75 * PHP4 style class constructor.
77 * @param int $courseid
79 function tinymce ($courseid) {
80 parent::editorObject();
81 $this->courseid = clean_param($courseid, PARAM_INT);
83 $isteacher = isteacher($courseid);
85 $this->defaults = array(
86 "mode" => "textareas",
87 "theme" => $this->cfg->tinymcetheme,
88 "language" => $this->__get_language(),
89 "width" => "100%",
90 "plugins" => !empty($this->cfg->tinymceplugins) ?
91 $this->cfg->tinymceplugins : '',
92 "content_css" => !empty($this->cfg->tinymcecontentcss) ?
93 $this->cfg->tinymcecontentcss : '',
94 "popup_css" => !empty($this->cfg->tinymcepopupcss) ?
95 $this->cfg->tinymcepopupcss : '',
96 "editor_css" => !empty($this->cfg->tinymceeditorcss) ?
97 $this->cfg->tinymceeditorcss : '',
98 "file_browser_callback" => has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $courseid)) ? 'moodleFileBrowser' : '',
99 "convert_urls" => false,
100 "relative_urls" => false);
102 if ( $this->cfg->tinymcetheme == 'advanced' ) {
103 $this->defaults['theme_advanced_buttons1_add'] = "fontselect,fontsizeselect";
104 $this->defaults['theme_advanced_buttons2_add'] = "separator,insertdate,inserttime,preview,zoom,separator,forecolor,backcolor,liststyle";
105 $this->defaults['theme_advanced_buttons2_add_before'] = "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator";
106 $this->defaults['theme_advanced_buttons3_add_before'] = "tablecontrols,separator";
107 $this->defaults['theme_advanced_buttons3_add'] = "emotions,iespell,flash,advhr,separator,print,separator,ltr,rtl,separator,fullscreen";
108 $this->defaults['theme_advanced_toolbar_location'] = "top";
109 $this->defaults['theme_advanced_toolbar_align'] = "left";
110 $this->defaults['theme_advanced_statusbar_location'] = "bottom";
111 $this->defaults['theme_advanced_resizing'] = true;
112 $this->defaults['theme_advanced_resize_horizontal'] = true;
115 $this->printdialogs = has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $courseid)) ? true : false;
119 * PHP5 style class constructor.
121 * @param int $courseid
123 function __construct($courseid) {
124 $this->tinymce($courseid);
128 * Checks configuration key validity.
129 * @param string $key Configuration key to check.
130 * @return bool Returns true if key is valid. Otherwise false is returned.
132 function __is_valid_key($key) {
134 if ( is_array($key) ) {
135 return false;
138 if ( strstr($key, "theme_advanced_") ) { // Search in theme keys.
140 foreach ( $this->tinythemekeys as $value ) {
141 if ( strstr($key, "<1-n>") ) {
142 $value = preg_replace("/<(.*)>/", "([0-9]+)", $value);
143 } else {
144 $value = preg_replace("/<(.*)>/", "([a-z0-9]+)", $value);
147 if ( preg_match("/^(". $value .")$/i", $key) ) {
148 return true;
152 } else {
153 if ( in_array($key, $this->tinyconfkeys) ) {
154 return true;
157 return false;
162 * Sets configuration key and value pairs.
163 * Passed parameters can be key and value pair or
164 * an associative array of keys and values.
165 * @todo code example
167 function setconfig () {
169 $numargs = func_num_args();
171 if ( $numargs > 2 ) {
172 $this->error("Too many arguments!");
173 exit;
176 if ( $numargs < 1 ) {
177 $this->error("No arguments passed!");
178 exit;
181 switch ( $numargs ) {
182 case 1: // Must be an array.
184 $arg = func_get_arg(0);
185 if ( is_array($arg) ) {
186 foreach ( $arg as $key => $value ) {
187 if ( !is_string($key) ) {
188 $this->error("Array is not associative array!");
189 exit;
191 if ( !$this->__is_valid_key($key) ) {
192 $this->error("Invalid configuration key: '$key'");
195 $this->tinyconf[$key] = $value;
197 } else {
198 $this->error("Given argument is not an array!!!");
201 break;
202 case 2: // Key, Value pair.
204 $key = func_get_arg(0);
205 $value = func_get_arg(1);
207 if ( !$this->__is_valid_key($key) ) {
208 $this->error("Invalid configuration key: $key");
210 $this->tinyconf[$key] = $value;
212 break;
218 * For internal usage. Print out configuration arrays.
219 * @param string $conftype Type of configuration.
220 * @return void
222 function __printconfig ($conftype='') {
224 switch ( $conftype ) {
225 case 'merge': // New config overrides defaults if found.
226 $conf = array_merge($this->defaults,$this->tinyconf);
227 break;
228 case 'append': // Append mode leave default value if found.
229 $conf = $this->defaults;
230 $keys = array_keys($this->defaults);
231 foreach ( $this->tinyconf as $key => $value ) {
232 if ( in_array($key, $keys) ) {
233 continue;
234 } else {
235 $conf[$key] = $value;
238 break;
239 case 'default':
240 $conf = $this->defaults;
241 break;
242 default:
243 $conf = $this->tinyconf;
246 echo "\n";
247 echo '<script type="text/javascript">'."\n";
248 echo '//<![CDATA['."\n";
249 echo ' tinyMCE.init({'."\n";
251 if ( !empty($conf) ) {
252 $max = count($conf);
253 $cnt = 1;
254 foreach ( $conf as $key => $value ) {
256 if ( empty($value) ) {
257 continue;
260 if ( $cnt > 1 ) {
261 echo ',' ."\n";
264 echo "\t" . $key .' : ';
266 if ( is_bool($value) ) {
267 echo ($value) ? 'true' : 'false';
268 } else {
269 echo '"'. $value .'"';
272 $cnt++;
276 echo ' });'."\n";
278 if ( $this->printdialogs ) {
279 $this->__dialogs();
281 echo '//]]>'."\n";
282 echo '</script>'."\n";
287 * Print out code that start up the editor.
288 * @param string $conftype Configuration type to print.
290 function starteditor($conftype='default') {
291 $this->__printconfig($conftype);
295 * For backward compatibility only.
296 * @param string $name
297 * @param string $editorhidesomebuttons
299 function use_html_editor($name='', $editorhidesomebuttons='') {
300 if ( empty($this->tinyconf) ) {
301 $this->__printconfig('default');
304 if ( !empty($this->cfg->editorsrc) ) {
305 unset($this->cfg->editorsrc);
311 * Print out needed script for custom dialog which is
312 * needed to provide access to Moodle's files and folders.
313 * For internal use only.
315 function __dialogs() {
318 function moodleFileBrowser (field_name, url, type, win) {
319 Dialog("<?php p($this->cfg->wwwroot) ?>/lib/editor/htmlarea/popups/link.php?id=<?php p($this->courseid) ?>", 470, 400, function (param) {
321 if ( !param ) {
322 return false;
325 win.document.forms[0].elements[field_name].value = param;
327 },null);
330 <?php
334 * Try to generate TinyMCE compatible language string from
335 * current users language. If not successful return default
336 * language which is english.
337 * For internal use only.
339 function __get_language() {
341 $tinylangdir = $this->cfg->libdir .'/editor/tinymce/jscripts/tiny_mce/langs';
342 $currentlanguage = current_language();
343 $defaultlanguage = 'en';
345 if ( !$fp = opendir($tinylangdir) ) {
346 return $defaultlanguage;
347 exit;
350 $languages = array();
352 while ( ($file = readdir($fp)) !== false ) {
353 if ( preg_match("/\.js$/i", $file) ) {
354 array_push($languages, basename($file, '.js'));
358 if ( $fp ) {
359 closedir($fp);
362 // If language is found in array.
363 if ( in_array($currentlanguage, $languages) ) {
364 return $currentlanguage;
367 // Check if two character country code is found (eg. fi, de, sv etc.)
368 // then return that.
369 $currentlanguage = str_replace("_utf8", "", $currentlanguage);
371 if ( in_array($currentlanguage, $languages) ) {
372 return $currentlanguage;
375 return $defaultlanguage;