31d42092d3d218859e2ff207d0e68dd55e3acb70
[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.3.9.2';
33     public function supported_by_browser() {
34         if (check_browser_version('MSIE', 5.5)) {
35             return true;
36         } else if (check_browser_version('Gecko', 20030516)) {
37             return true;
38         } else if (check_browser_version('Safari iOS')) {
39             return false;
40         } else if (check_browser_version('Safari', 3)) {
41             return true;
42         } else if (check_browser_version('Opera', 9)) {
43             return true;
44         }
46         return false;
47     }
49     public function get_supported_formats() {
50         return array(FORMAT_HTML => FORMAT_HTML);
51     }
53     public function get_preferred_format() {
54         return FORMAT_HTML;
55     }
57     public function supports_repositories() {
58         return true;
59     }
61     public function head_setup() {
62     }
64     public function use_editor($elementid, array $options=null, $fpoptions=null) {
65         global $PAGE;
66         $PAGE->requires->js('/lib/editor/tinymce/tiny_mce/'.$this->version.'/tiny_mce.js');
67         $PAGE->requires->js_init_call('M.editor_tinymce.init_editor', array($elementid, $this->get_init_params($elementid, $options)), true);
68         if ($fpoptions) {
69             $PAGE->requires->js_init_call('M.editor_tinymce.init_filepicker', array($elementid, $fpoptions), true);
70         }
71     }
73     protected function get_init_params($elementid, array $options=null) {
74         global $CFG, $PAGE;
76         //TODO: we need to implement user preferences that affect the editor setup too
78         $directionality = get_string('thisdirection', 'langconfig');
79         $strtime        = get_string('strftimetime');
80         $strdate        = get_string('strftimedaydate');
81         $lang           = current_language();
82         $contentcss     = $PAGE->theme->editor_css_url()->out(false);
84         $context = empty($options['context']) ? get_context_instance(CONTEXT_SYSTEM) : $options['context'];
86         $xmedia = 'moodlemedia,'; // HQ thinks it should be always on, so it is no matter if it will actually work or not
87         /*
88         if (!empty($options['legacy'])) {
89             $xmedia = 'moodlemedia,';
90         } else {
91             if (!empty($options['noclean']) or !empty($options['trusted'])) {
92             }
93         }*/
95         $filters = filter_get_active_in_context($context);
96         if (array_key_exists('filter/tex', $filters)) {
97             $xdragmath = 'dragmath,';
98         } else {
99             $xdragmath = '';
100         }
102         $params = array(
103                     'mode' => "exact",
104                     'elements' => $elementid,
105                     'relative_urls' => false,
106                     'document_base_url' => $CFG->httpswwwroot,
107                     'content_css' => $contentcss,
108                     'language' => $lang,
109                     'directionality' => $directionality,
110                     'plugin_insertdate_dateFormat ' => $strdate,
111                     'plugin_insertdate_timeFormat ' => $strtime,
112                     'theme' => "advanced",
113                     'skin' => "o2k7",
114                     'skin_variant' => "silver",
115                     'apply_source_formatting' => true,
116                     'remove_script_host' => false,
117                     'entity_encoding' => "raw",
118                     'plugins' => "{$xmedia}advimage,safari,table,style,layer,advhr,advlink,emotions,inlinepopups,searchreplace,paste,directionality,fullscreen,moodlenolink,{$xdragmath}nonbreaking,contextmenu,insertdatetime,save,iespell,preview,print,noneditable,visualchars,xhtmlxtras,template,pagebreak,spellchecker",
119                     'theme_advanced_font_sizes' => "1,2,3,4,5,6,7",
120                     'theme_advanced_layout_manager' => "SimpleLayout",
121                     'theme_advanced_toolbar_align' => "left",
122                     'theme_advanced_buttons1' => "fontselect,fontsizeselect,formatselect",
123                     'theme_advanced_buttons1_add' => "|,undo,redo,|,search,replace,|,fullscreen",
124                     'theme_advanced_buttons2' => "bold,italic,underline,strikethrough,sub,sup,|,justifyleft,justifycenter,justifyright",
125                     'theme_advanced_buttons2_add' => "|,cleanup,removeformat,pastetext,pasteword,|,forecolor,backcolor,|,ltr,rtl",
126                     'theme_advanced_buttons3' => "bullist,numlist,outdent,indent,|,link,unlink,moodlenolink,|,image,{$xmedia}{$xdragmath}nonbreaking,charmap",
127                     'theme_advanced_buttons3_add' => "table,|,code,spellchecker",
128                     '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",
129                     'theme_advanced_resize_horizontal' => true,
130                     'theme_advanced_resizing' => true,
131                     'theme_advanced_toolbar_location' => "top",
132                     'theme_advanced_statusbar_location' => "bottom",
133                     'spellchecker_rpc_url' => $CFG->wwwroot."/lib/editor/tinymce/tiny_mce/$this->version/plugins/spellchecker/rpc.php"
134                   );
135         if (empty($CFG->xmlstrictheaders) and (!empty($options['legacy']) or !empty($options['noclean']) or !empty($options['trusted']))) {
136             // now deal somehow with non-standard tags, people scream when we do not make moodle code xtml strict,
137             // but they scream even more when we strip all tags that are not strict :-(
138             $params['valid_elements'] = '*[*]';
139             $params['invalid_elements'] = '';
140         }
142         if (empty($options['legacy'])) {
143             if (isset($options['maxfiles']) and $options['maxfiles'] != 0) {
144                 $params['file_browser_callback'] = "M.editor_tinymce.filepicker";
145             }
146         }
148         return $params;
149     }