c3c68d875fd6bb2c070e77af15000e9c4fd781f2
[moodle.git] / lib / editor / tinymce / lib.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * TinyMCE text editor integration.
20  *
21  * @package    editor
22  * @subpackage tinymce
23  * @copyright  2009 Petr Skoda (http://skodak.org)
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
29 class tinymce_texteditor extends texteditor {
30     /** @var string active version - directory name */
31     public $version = '3.4.6';
33     public function supported_by_browser() {
34         if (check_browser_version('MSIE', 6)) {
35             return true;
36         }
37         if (check_browser_version('Gecko', 20030516)) {
38             return true;
39         }
40         if (check_browser_version('Safari', 412)) {
41             return true;
42         }
43         if (check_browser_version('Chrome', 6)) {
44             return true;
45         }
46         if (check_browser_version('Opera', 9)) {
47             return true;
48         }
49         if (check_browser_version('Safari iOS', 534)) {
50             return true;
51         }
53         return false;
54     }
56     public function get_supported_formats() {
57         return array(FORMAT_HTML => FORMAT_HTML);
58     }
60     public function get_preferred_format() {
61         return FORMAT_HTML;
62     }
64     public function supports_repositories() {
65         return true;
66     }
68     public function head_setup() {
69     }
71     public function use_editor($elementid, array $options=null, $fpoptions=null) {
72         global $PAGE;
73         if (debugging('', DEBUG_DEVELOPER)) {
74             $PAGE->requires->js('/lib/editor/tinymce/tiny_mce/'.$this->version.'/tiny_mce_src.js');
75         } else {
76             $PAGE->requires->js('/lib/editor/tinymce/tiny_mce/'.$this->version.'/tiny_mce.js');
77         }
78         $PAGE->requires->js_init_call('M.editor_tinymce.init_editor', array($elementid, $this->get_init_params($elementid, $options)), true);
79         if ($fpoptions) {
80             $PAGE->requires->js_init_call('M.editor_tinymce.init_filepicker', array($elementid, $fpoptions), true);
81         }
82     }
84     protected function get_init_params($elementid, array $options=null) {
85         global $CFG, $PAGE, $OUTPUT;
87         //TODO: we need to implement user preferences that affect the editor setup too
89         $directionality = get_string('thisdirection', 'langconfig');
90         $strtime        = get_string('strftimetime');
91         $strdate        = get_string('strftimedaydate');
92         $lang           = current_language();
93         $contentcss     = $PAGE->theme->editor_css_url()->out(false);
95         $context = empty($options['context']) ? get_context_instance(CONTEXT_SYSTEM) : $options['context'];
97         $xmedia = 'moodlemedia,'; // HQ thinks it should be always on, so it is no matter if it will actually work or not
98         /*
99         if (!empty($options['legacy'])) {
100             $xmedia = 'moodlemedia,';
101         } else {
102             if (!empty($options['noclean']) or !empty($options['trusted'])) {
103             }
104         }*/
106         $filters = filter_get_active_in_context($context);
107         if (array_key_exists('filter/tex', $filters)) {
108             $xdragmath = 'dragmath,';
109         } else {
110             $xdragmath = '';
111         }
112         if (array_key_exists('filter/emoticon', $filters)) {
113             $xemoticon = 'moodleemoticon,';
114         } else {
115             $xemoticon = '';
116         }
118         $params = array(
119                     'mode' => "exact",
120                     'elements' => $elementid,
121                     'relative_urls' => false,
122                     'document_base_url' => $CFG->httpswwwroot,
123                     'content_css' => $contentcss,
124                     'language' => $lang,
125                     'directionality' => $directionality,
126                     'plugin_insertdate_dateFormat ' => $strdate,
127                     'plugin_insertdate_timeFormat ' => $strtime,
128                     'theme' => "advanced",
129                     'skin' => "o2k7",
130                     'skin_variant' => "silver",
131                     'apply_source_formatting' => true,
132                     'remove_script_host' => false,
133                     'entity_encoding' => "raw",
134                     'plugins' => "{$xmedia}advimage,safari,table,style,layer,advhr,advlink,emotions,inlinepopups,searchreplace,paste,directionality,fullscreen,moodlenolink,{$xemoticon}{$xdragmath}nonbreaking,contextmenu,insertdatetime,save,iespell,preview,print,noneditable,visualchars,xhtmlxtras,template,pagebreak,spellchecker",
135                     'theme_advanced_font_sizes' => "1,2,3,4,5,6,7",
136                     'theme_advanced_layout_manager' => "SimpleLayout",
137                     'theme_advanced_toolbar_align' => "left",
138                     'theme_advanced_buttons1' => "fontselect,fontsizeselect,formatselect",
139                     'theme_advanced_buttons1_add' => "|,undo,redo,|,search,replace,|,fullscreen",
140                     'theme_advanced_buttons2' => "bold,italic,underline,strikethrough,sub,sup,|,justifyleft,justifycenter,justifyright",
141                     'theme_advanced_buttons2_add' => "|,cleanup,removeformat,pastetext,pasteword,|,forecolor,backcolor,|,ltr,rtl",
142                     'theme_advanced_buttons3' => "bullist,numlist,outdent,indent,|,link,unlink,moodlenolink,|,image,{$xemoticon}{$xmedia}{$xdragmath}nonbreaking,charmap",
143                     'theme_advanced_buttons3_add' => "table,|,code,spellchecker",
144                     '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",
145                     'theme_advanced_resize_horizontal' => true,
146                     'theme_advanced_resizing' => true,
147                     'theme_advanced_resizing_min_height' => 30,
148                     'theme_advanced_toolbar_location' => "top",
149                     'theme_advanced_statusbar_location' => "bottom",
150                     'spellchecker_rpc_url' => $CFG->wwwroot."/lib/editor/tinymce/tiny_mce/$this->version/plugins/spellchecker/rpc.php"
151                   );
153         if ($xemoticon) {
154             $manager = get_emoticon_manager();
155             $emoticons = $manager->get_emoticons();
156             $imgs = array();
157             // see the TinyMCE plugin moodleemoticon for how the emoticon index is (ab)used :-S
158             $index = 0;
159             foreach ($emoticons as $emoticon) {
160                 $imgs[$emoticon->text] = $OUTPUT->render(
161                     $manager->prepare_renderable_emoticon($emoticon, array('class' => 'emoticon emoticon-index-'.$index++)));
162             }
163             $params['moodleemoticon_emoticons'] = json_encode($imgs);
164         }
166         if (empty($CFG->xmlstrictheaders) and (!empty($options['legacy']) or !empty($options['noclean']) or !empty($options['trusted']))) {
167             // now deal somehow with non-standard tags, people scream when we do not make moodle code xtml strict,
168             // but they scream even more when we strip all tags that are not strict :-(
169             $params['valid_elements'] = 'script[src|type],*[*]'; // for some reason the *[*] does not inlcude javascript src attribute MDL-25836
170             $params['invalid_elements'] = '';
171         }
173         if (empty($options['legacy'])) {
174             if (isset($options['maxfiles']) and $options['maxfiles'] != 0) {
175                 $params['file_browser_callback'] = "M.editor_tinymce.filepicker";
176             }
177         }
178         //Add onblur event for client side text validation
179         if (!empty($options['required'])) {
180             $params['init_instance_callback'] = 'M.editor_tinymce.onblur_event';
181         }
182         return $params;
183     }