]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarTinyMCE.php
Added SpellCheck Support to TinyMCE
[Github/sugarcrm.git] / include / SugarTinyMCE.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description:
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc. All Rights
42  * Reserved. Contributor(s): ______________________________________..
43  *********************************************************************************/
44
45
46 /**
47  * PHP wrapper class for Javascript driven TinyMCE WYSIWYG HTML editor
48  */
49 class SugarTinyMCE {
50         var $jsroot = "include/javascript/tiny_mce/";
51         var $customConfigFile = 'custom/include/tinyButtonConfig.php';
52         var $customDefaultConfigFile = 'custom/include/tinyMCEDefaultConfig.php';
53         var $buttonConfigs = array(
54                         'default' => array(
55                                                 'buttonConfig' => "code,help,separator,bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,
56                                                                 justifyfull,separator,forecolor,backcolor,separator,styleselect,formatselect,fontselect,fontsizeselect,", 
57                             'buttonConfig2' => "cut,copy,paste,pastetext,pasteword,selectall,separator,search,replace,separator,bullist,numlist,separator,outdent,
58                                                                 indent,separator,ltr,rtl,separator,undo,redo,separator, link,unlink,anchor,image,separator,sub,sup,separator,charmap,
59                                                                 visualaid", 
60                             'buttonConfig3' => "tablecontrols,separator,advhr,hr,removeformat,separator,insertdate,inserttime,separator,preview,spellchecker"),
61                 'email_compose' => array(
62                                                 'buttonConfig' => "code,help,separator,bold,italic,underline,strikethrough,separator,bullist,numlist,separator,justifyleft,justifycenter,justifyright,
63                                                                 justifyfull,separator,forecolor,backcolor,separator,spellchecker,seperator,styleselect,formatselect,fontselect,fontsizeselect", 
64                             'buttonConfig2' => "", 
65                             'buttonConfig3' => ""),
66                 'email_compose_light' => array(
67                                                 'buttonConfig' => "code,help,separator,bold,italic,underline,strikethrough,separator,bullist,numlist,separator,justifyleft,justifycenter,justifyright,
68                                                                 justifyfull,separator,forecolor,backcolor,separator,spellchecker,seperator,styleselect,formatselect,fontselect,fontsizeselect", 
69                             'buttonConfig2' => "", 
70                             'buttonConfig3' => ""),
71         );
72         
73         var $pluginsConfig = array(
74             'email_compose_light' => 'insertdatetime,paste,directionality,safari,spellchecker',
75         'email_compose' => 'advhr,insertdatetime,table,preview,paste,searchreplace,directionality,fullpage,spellchecker',
76         );
77
78
79         var $defaultConfig = array(
80             'convert_urls' => false,
81         'valid_children' => '+body[style]',
82             'height' => 300,
83                 'width' => '100%',
84                 'theme' => 'advanced',
85                 'theme_advanced_toolbar_align' => "left",
86                 'theme_advanced_toolbar_location'       => "top",
87                 'theme_advanced_buttons1'       => "",
88                 'theme_advanced_buttons2'       => "",
89                 'theme_advanced_buttons3'       => "",
90                 'strict_loading_mode'   => true,
91                 'mode'  => 'exact',
92                 'language' => 'en',
93             'plugins' => 'advhr,insertdatetime,table,preview,paste,searchreplace,directionality,spellchecker',
94                 'elements'      => '',
95         'extended_valid_elements' => 'style[dir|lang|media|title|type],hr[class|width|size|noshade],@[class|style]',
96         'content_css' => 'include/javascript/tiny_mce/themes/advanced/skins/default/content.css',
97
98         );
99         
100         
101         /**
102          * Sole constructor
103          */
104         function SugarTinyMCE() {
105             
106                 $this->overloadButtonConfigs();
107                 $this->overloadDefaultConfigs();
108         }
109         
110         /**
111          * Returns the Javascript necessary to initialize a TinyMCE instance for a given <textarea> or <div>
112          * @param string target Comma delimited list of DOM ID's, <textarea id='someTarget'>
113          * @return string 
114          */
115         function getInstance($targets = "") {
116                 global $json;
117                 
118                 if(empty($json)) {
119                         $json = getJSONobj();
120                 }
121                 
122                 $config = $this->defaultConfig;
123                 //include tinymce lang file
124         $lang = substr($GLOBALS['current_language'], 0, 2);
125         if(file_exists('include/javascript/tiny_mce/langs/'.$lang.'.js'))
126         {
127                         $config['language'] = $lang;
128         }
129                 $config['directionality'] = SugarThemeRegistry::current()->directionality;
130                 $config['elements'] = $targets;
131                 $config['theme_advanced_buttons1'] = $this->buttonConfigs['default']['buttonConfig']; 
132                 $config['theme_advanced_buttons2'] = $this->buttonConfigs['default']['buttonConfig2']; 
133                 $config['theme_advanced_buttons3'] = $this->buttonConfigs['default']['buttonConfig3'];
134
135                 $jsConfig = $json->encode($config);
136                 
137                 $instantiateCall = '';
138                 if (!empty($targets)) {
139                         $exTargets = explode(",", $targets);
140                         foreach($exTargets as $instance) {
141                                 //$instantiateCall .= "tinyMCE.execCommand('mceAddControl', false, document.getElementById('{$instance}'));\n";
142                         } 
143                 }
144                 $path = getJSPath('include/javascript/tiny_mce/tiny_mce.js');
145                 $ret =<<<eoq
146 <script type="text/javascript" language="Javascript" src="$path"></script>
147
148 <script type="text/javascript" language="Javascript">
149 <!--
150 if (!SUGAR.util.isTouchScreen()) {
151     tinyMCE.init({$jsConfig});
152         {$instantiateCall}      
153 }
154 else {
155 eoq;
156 $exTargets = explode(",", $targets);
157 foreach($exTargets as $instance) { 
158 $ret .=<<<eoq
159     document.getElementById('$instance').style.width = '100%';
160     document.getElementById('$instance').style.height = '100px';
161 eoq;
162 }
163 $ret .=<<<eoq
164 }
165 -->
166 </script>
167
168 eoq;
169                 return $ret;
170         }
171         
172     function getConfig($type = 'default') {
173         global $json;
174         
175         if(empty($json)) {
176             $json = getJSONobj();
177         }
178         
179         $config = $this->defaultConfig;
180         //include tinymce lang file
181         $lang = substr($GLOBALS['current_language'], 0, 2);
182         if(file_exists('include/javascript/tiny_mce/langs/'.$lang.'.js'))
183                         $config['language'] = $lang;        
184                 $config['theme_advanced_buttons1'] = $this->buttonConfigs[$type]['buttonConfig']; 
185         $config['theme_advanced_buttons2'] = $this->buttonConfigs[$type]['buttonConfig2'];
186         $config['theme_advanced_buttons3'] = $this->buttonConfigs[$type]['buttonConfig3'];
187         
188         if(isset($this->pluginsConfig[$type]))
189             $config['plugins'] = $this->pluginsConfig[$type];
190             
191         $jsConfig = $json->encode($config);
192         return "var tinyConfig = ".$jsConfig.";";
193         
194     }
195     
196     /**
197      * This function takes in html code that has been produced (and somewhat mauled) by TinyMCE
198      * and returns a cleaned copy of it.
199      * 
200      * @param $html
201      * @return $html with all the tinyMCE specific html removed
202      */
203     function cleanEncodedMCEHtml($html) {
204         $html = str_replace("mce:script", "script", $html);
205         $html = str_replace("mce_src=", "src=", $html);
206         $html = str_replace("mce_href=", "href=", $html);
207         return $html;
208     }
209     
210     /**
211      * Reload the default button configs by allowing admins to specify
212      * which tinyMCE buttons will be displayed in a seperate config file.
213      *
214      */
215     private function overloadButtonConfigs() 
216     {
217         if( file_exists( $this->customConfigFile ) )    
218         {
219             require_once($this->customConfigFile);
220             
221             if(!isset($buttonConfigs))
222                 return;
223             
224             foreach ($buttonConfigs as $k => $v)
225             {
226                 if( isset($this->buttonConfigs[$k]) )
227                     $this->buttonConfigs[$k] = $v;
228             }
229         }
230     }
231     
232     /**
233      * Reload the default tinyMCE config, preserving our default extended 
234      * allowable tag set.
235      *
236      */
237     private function overloadDefaultConfigs() 
238     {
239         if( file_exists( $this->customDefaultConfigFile ) )    
240         {
241             require_once($this->customDefaultConfigFile);
242             
243             if(!isset($defaultConfig))
244                 return;
245             
246             foreach ($defaultConfig as $k => $v)
247             {
248                 if( isset($this->defaultConfig[$k]) ){
249                         
250                         if($k == "extended_valid_elements"){
251                                 $this->defaultConfig[$k] .= "," . $v;
252                         }
253                         else{
254                                 $this->defaultConfig[$k] = $v;
255                         }
256                 }       
257             }
258         }
259     }
260     
261 } // end class def