MDL-13766
[moodle.git] / lib / editor / tinymce.js.php
1 <?php
3 define('NO_MOODLE_COOKIES', true);
5 require_once('../../config.php');
7 $editorlanguage = optional_param('editorlanguage', 'en_utf8', PARAM_ALPHANUMEXT);
9 $SESSION->lang = $editorlanguage;
10 $directionality = get_string('thisdirection');
12 /*
13  * This section configures the TinyMCE toolbar buttons on and off
14  * depending on the Moodle settings
15  *
16  * The changes are applied on a global basis,
17  * ..... but there is scope here to modify and restrict the config
18  * on a role basis, course basis, user basis, etc. if so desired.
19  *
20  */
21 if (empty($CFG->tinymcehidebuttons)) {
22     $CFG->tinymcehidebuttons = '';
23 }
25 $editorhidebuttons = str_replace(' ', ',', $CFG->tinymcehidebuttons);
27 $editorhidebuttons1 = $editorhidebuttons . ',visualaid,styleselect';
29 $editorhidebuttons = 'theme_advanced_disable : "'.$editorhidebuttons1.'",';
31 $editorhidebuttons = str_replace('fontsize',             'fontsizeselect',       $editorhidebuttons);
32 $editorhidebuttons = str_replace('subscript',            'sub',                  $editorhidebuttons);
33 $editorhidebuttons = str_replace('superscript',          'sup',                  $editorhidebuttons);
34 $editorhidebuttons = str_replace('insertorderedlist',    'numlist',              $editorhidebuttons);
35 $editorhidebuttons = str_replace('insertunorderedlist',  'bullist',              $editorhidebuttons);
36 $editorhidebuttons = str_replace('createanchor',         'anchor',               $editorhidebuttons);
37 $editorhidebuttons = str_replace('createlink',           'link',                 $editorhidebuttons);
38 $editorhidebuttons = str_replace('htmlmode',             'code',                 $editorhidebuttons);
39 $editorhidebuttons = str_replace('insertchar',           'charmap',              $editorhidebuttons);
40 $editorhidebuttons = str_replace('insertimage',          'image',                $editorhidebuttons);
41 $editorhidebuttons = str_replace('inserthorizontalrule', 'hr',                   $editorhidebuttons);
42 $editorhidebuttons = str_replace('formatblock',          'formatselect',         $editorhidebuttons);
43 $editorhidebuttons = str_replace('clean',                'cleanup,removeformat', $editorhidebuttons);
45 // insertsmile,
47 $pieces = explode(",", $editorhidebuttons1);
49 $spellcheck = '';
50 if (! in_array("spellcheck", $pieces)) {
51     $spellcheck = 'spellchecker,';
52 }
53 $inserttable = '';
54 if (! in_array("inserttable", $pieces)) {
55     $inserttable = 'tablecontrols,separator,';
56 }
57 $search_replace = '';
58 if (! in_array("search_replace", $pieces)) {
59     $search_replace = 'search,replace,separator,';
60 }
61 $lefttoright = '';
62 if (! in_array("lefttoright", $pieces)) {
63     $lefttoright = 'ltr,separator,';
64 }
65 $righttoleft = '';
66 if (! in_array("righttoleft", $pieces)) {
67     $righttoleft = 'rtl,separator,';
68 }
69 $cleanup = '';
70 if (! in_array("cleanup", $pieces)) {
71     $cleanup = 'cleanup,removeformat,separator,';
72 }
73 $fontselect = '';
74 if (! in_array("fontname", $pieces)) {
75     $fontselect = 'fontselect,';
76 }
77 $fontsize = '';
78 if (! in_array("fontsize", $pieces)) {
79     $fontsize = 'fontsizeselect,';
80 }
81 $forecolor = '';
82 if (! in_array("forecolor", $pieces)) {
83     $forecolor = 'forecolor,';
84 }
85 $hilitecolor = '';
86 if (! in_array("hilitecolor", $pieces)) {
87     $hilitecolor = 'backcolor,';
88 }
89 $popupeditor = '';
90 if (! in_array("popupeditor", $pieces)) {
91     $popupeditor = 'fullscreen,';
92 }
94 $editoraddbuttons3 = 'theme_advanced_buttons3_add : "'.$fontselect.$fontsize.$forecolor.$hilitecolor.'",';
95 $editoraddbuttons4 = 'theme_advanced_buttons4 : "'.$spellcheck.$search_replace.$inserttable.$lefttoright.$righttoleft.$popupeditor.$cleanup.'",';
97 /*
98  *
99  * ********************************************************************************************************
100  *
101  */
104 $temp = $_SERVER["REQUEST_URI"];
105 $temp = explode('/', $temp);
106 $root = $temp[1];
108 $configuration = <<<EOF
109 tinyMCE.init({
110     mode     : "exact",
111     elements : id,
112     theme    : "advanced",
114     plugins : "safari,spellchecker,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,pagebreak,",
115     spellchecker_languages : "+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv",
117     plugin_insertdate_dateFormat : "%Y-%m-%d",
118     plugin_insertdate_timeFormat : "%H:%M:%S",
120     content_css : "/$root/lib/editor/tinymce/examples/css/content.css",
122     theme_advanced_toolbar_location : "top",
123     theme_advanced_toolbar_align : "top",
124     theme_advanced_statusbar_location : "bottom",
126     theme_advanced_resize_horizontal : true,
127     theme_advanced_resizing : true,
128     apply_source_formatting : true,
130     $editorhidebuttons
131     $editoraddbuttons3
132     $editoraddbuttons4
134 });
135 EOF;
137 $strtime = get_string('strftimetime');
138 $strdate = get_string('strftimedaydate');
140 // $contentcss should be customizable, but default to this.
141 $contentcss = $CFG->themewww .'/'. current_theme() .'/styles.php';
143 $output = <<<EOF
144     tinyMCE.init({
145         mode: "textareas",
146         relative_urls: false,
147         editor_selector: "form-textarea-simple",
148         document_base_url: "$CFG->httpswwwroot",
149         content_css: "$contentcss",
150         theme: "simple",
151         skin: "o2k7",
152         skin_variant: "silver",
153         apply_source_formatting: true, 
154         remove_script_host: false,
155         entity_encoding: "raw",
156         language: "$editorlanguage",
157         directionality: "$directionality",
158         plugins: "spellchecker,emoticons,paste,standardmenu,directionality,contextmenu"
159     });
160     tinyMCE.init({
161         mode: "textareas",
162         relative_urls: false,
163         editor_selector: "form-textarea-advanced",
164         document_base_url: "$CFG->httpswwwroot",
165         content_css: "$contentcss",
166         theme: "advanced",
167         skin: "o2k7",
168         skin_variant: "silver",
169         apply_source_formatting: true, 
170         remove_script_host: false,
171         entity_encoding: "raw",
172         language: "$editorlanguage",
173         directionality: "$directionality",
174         plugins: "safari,spellchecker,table,style,layer,advhr,advimage,advlink,emoticons,inlinepopups,media,searchreplace,paste,standardmenu,directionality,fullscreen,moodlenolink,dragmath,nonbreaking,contextmenu,insertdatetime,save,iespell,preview,print,noneditable,visualchars,xhtmlxtras,template,pagebreak",
175         plugin_insertdate_dateFormat : "$strdate",
176         plugin_insertdate_timeFormat : "$strtime",
177         theme_advanced_font_sizes: "1,2,3,4,5,6,7",
178         theme_advanced_layout_manager: "SimpleLayout",
179         theme_advanced_toolbar_align : "left",
180         theme_advanced_buttons1: "fontselect,fontsizeselect,formatselect,styleselect",
181         theme_advanced_buttons1_add: "|,undo,redo,|,search,replace,spellchecker,|,fullscreen",
182         theme_advanced_buttons2: "bold,italic,underline,strikethrough,sub,sup,|,justifyleft,justifycenter,justifyright,justifyfull,|,cite,abbr,acronym",
183         theme_advanced_buttons2_add: "|,selectall,cleanup,removeformat,pastetext,pasteword,|,forecolor,backcolor,|,ltr,rtl",
184         theme_advanced_buttons3: "bullist,numlist,outdent,indent,|,link,unlink,moodlenolink,anchor,|,insertdate,inserttime,|,emoticons,image,media,dragmath,advhr,nonbreaking,charmap",
185         theme_advanced_buttons3_add: "|,table,insertlayer,styleprops,visualchars,|,code,preview",
186         theme_advanced_fonts: "Trebuchet=Trebuchet MS,Verdana,Arial,Helvetica,sans-serif;Arial=arial,helvetica,sans-serif;Courier New=courier new,courier,monospace;Georgia=georgia,times new roman,times,serif;Tahoma=tahoma,arial,helvetica,sans-serif;Times New Roman=times new roman,times,serif;Verdana=verdana,arial,helvetica,sans-serif;Impact=impact;Wingdings=wingdings", 
187         theme_advanced_resize_horizontal: true,
188         theme_advanced_resizing: true,
189         theme_advanced_toolbar_location : "top",
190         theme_advanced_statusbar_location : "bottom",
191         file_browser_callback : "moodlefilemanager",
193 EOF;
194 // the xhtml ruleset must be the last one - no comma at the end of the file
195 $output .= file_get_contents('tinymce/xhtml_ruleset.txt');
196 $output .= <<<EOF
197     });
199     function mce_toggleEditor(id) {
200         tinyMCE.execCommand('mceToggleEditor',false,id);
201     }
203     function mce_saveOnSubmit(id) {
204         var prevOnSubmit = document.getElementById(id).form.onsubmit;
205         document.getElementById(id).form.onsubmit = function() { 
206             tinyMCE.triggerSave(); 
207             var ret = true;
208             if (prevOnSubmit != undefined) {
209               if (prevOnSubmit()) {
210                 ret = true;
211                 prevOnSubmit = null;
212               } else {
213                 ret = false;
214               }
215             }
216             return ret;
217         };
218     }
219     function moodlefilemanager(field_name, url, type, win) {
220         //alert(id2suffix[tinyMCE.selectedInstance.editorId]);
221         var suffix = id2suffix[tinyMCE.selectedInstance.editorId];
222         document.body.className += ' yui-skin-sam';
223         var picker = document.createElement('DIV');
224         picker.className = "file-picker";
225         picker.id = 'file-picker-'+suffix;
226         document.body.appendChild(picker);
227         var el = win.document.getElementById(field_name);
228         eval('openpicker_'+suffix+'({"env":"editor","target":el})');
229     }
230 EOF;
232 $lifetime = '86400';
233 @header('Content-type: text/javascript; charset=utf-8');
234 @header('Content-length: '.strlen($output));
235 @header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
236 @header('Cache-control: max-age='.$lifetime);
237 @header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .'GMT');
238 @header('Pragma: ');
240 echo $output;
242 ?>