]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarTinyMCE.php
Release 6.1.4
[Github/sugarcrm.git] / include / SugarTinyMCE.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 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 $buttonConfigs = array(
53                         'default' => array(
54                                                 'buttonConfig' => "code,help,separator,bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,
55                                                                 justifyfull,separator,forecolor,backcolor,separator,styleselect,formatselect,fontselect,fontsizeselect,", 
56                             'buttonConfig2' => "cut,copy,paste,pastetext,pasteword,selectall,separator,search,replace,separator,bullist,numlist,separator,outdent,
57                                                                 indent,separator,ltr,rtl,separator,undo,redo,separator, link,unlink,anchor,image,separator,sub,sup,separator,charmap,
58                                                                 visualaid", 
59                             'buttonConfig3' => "tablecontrols,separator,advhr,hr,removeformat,separator,insertdate,inserttime,separator,preview"),
60                 'email_compose' => array(
61                                                 'buttonConfig' => "code,help,separator,bold,italic,underline,strikethrough,separator,bullist,numlist,separator,justifyleft,justifycenter,justifyright,
62                                                                 justifyfull,separator,forecolor,backcolor,separator,styleselect,formatselect,fontselect,fontsizeselect,", 
63                             'buttonConfig2' => "", 
64                             'buttonConfig3' => ""),
65                 'email_compose_light' => array(
66                                                 'buttonConfig' => "code,help,separator,bold,italic,underline,strikethrough,separator,bullist,numlist,separator,justifyleft,justifycenter,justifyright,
67                                                                 justifyfull,separator,forecolor,backcolor,separator,styleselect,formatselect,fontselect,fontsizeselect,", 
68                             'buttonConfig2' => "", 
69                             'buttonConfig3' => ""),
70         );
71         
72         var $pluginsConfig = array(
73             'email_compose_light' => 'insertdatetime,paste,directionality,safari'         
74         );
75         
76         var $defaultConfig = array(
77             'convert_urls' => false,
78             'height' => 300,
79                 'width' => '100%',
80                 'theme' => 'advanced',
81                 'theme_advanced_toolbar_align' => "left",
82                 'theme_advanced_toolbar_location'       => "top",
83                 'theme_advanced_buttons1'       => "",
84                 'theme_advanced_buttons2'       => "",
85                 'theme_advanced_buttons3'       => "",
86                 'strict_loading_mode'   => true,
87                 'mode'  => 'exact',
88                 'language' => 'en',
89             'plugins' => 'advhr,insertdatetime,table,preview,paste,searchreplace,directionality',
90                 'elements'      => '',
91         'extended_valid_elements' => 'style,hr[class|width|size|noshade],@[class|style]',
92         'content_css' => 'include/javascript/tiny_mce/themes/advanced/skins/default/content.css',
93
94         );
95         
96         
97         /**
98          * Sole constructor
99          */
100         function SugarTinyMCE() {
101             
102                 $this->overloadButtonConfigs();
103         }
104         
105         /**
106          * Returns the Javascript necessary to initialize a TinyMCE instance for a given <textarea> or <div>
107          * @param string target Comma delimited list of DOM ID's, <textarea id='someTarget'>
108          * @return string 
109          */
110         function getInstance($targets = "") {
111                 global $json;
112                 
113                 if(empty($json)) {
114                         $json = getJSONobj();
115                 }
116                 
117                 $config = $this->defaultConfig;
118                 $config['elements'] = $targets;
119                 $config['theme_advanced_buttons1'] = $this->buttonConfigs['default']['buttonConfig']; 
120                 $config['theme_advanced_buttons2'] = $this->buttonConfigs['default']['buttonConfig2']; 
121                 $config['theme_advanced_buttons3'] = $this->buttonConfigs['default']['buttonConfig3']; 
122                 $jsConfig = $json->encode($config);
123                 
124                 $instantiateCall = '';
125                 if (!empty($targets)) {
126                         $exTargets = explode(",", $targets);
127                         foreach($exTargets as $instance) {
128                                 //$instantiateCall .= "tinyMCE.execCommand('mceAddControl', false, document.getElementById('{$instance}'));\n";
129                         } 
130                 }
131                 $path = getJSPath('include/javascript/tiny_mce/tiny_mce.js');
132                 $ret =<<<eoq
133 <script type="text/javascript" language="Javascript" src="$path"></script>
134
135 <script type="text/javascript" language="Javascript">
136 <!--
137 if (!SUGAR.util.isTouchScreen()) {
138     tinyMCE.init({$jsConfig});
139         {$instantiateCall}      
140 }
141 else {
142 eoq;
143 $exTargets = explode(",", $targets);
144 foreach($exTargets as $instance) { 
145 $ret .=<<<eoq
146     document.getElementById('$instance').style.width = '100%';
147     document.getElementById('$instance').style.height = '100px';
148 eoq;
149 }
150 $ret .=<<<eoq
151 }
152 -->
153 </script>
154
155 eoq;
156                 return $ret;
157         }
158         
159     function getConfig($type = 'default') {
160         global $json;
161         
162         if(empty($json)) {
163             $json = getJSONobj();
164         }
165         
166         $config = $this->defaultConfig;
167         //include tinymce lang file
168         $lang = substr($GLOBALS['current_language'], 0, 2);
169         if(file_exists('include/javascript/tiny_mce/langs/'.$lang.'.js'))
170                         $config['language'] = $lang;        
171                 $config['theme_advanced_buttons1'] = $this->buttonConfigs[$type]['buttonConfig']; 
172         $config['theme_advanced_buttons2'] = $this->buttonConfigs[$type]['buttonConfig2'];
173         $config['theme_advanced_buttons3'] = $this->buttonConfigs[$type]['buttonConfig3'];
174         
175         if(isset($this->pluginsConfig[$type]))
176             $config['plugins'] = $this->pluginsConfig[$type];
177             
178         $jsConfig = $json->encode($config);
179         return "var tinyConfig = ".$jsConfig.";";
180         
181     }
182     
183     /**
184      * This function takes in html code that has been produced (and somewhat mauled) by TinyMCE
185      * and returns a cleaned copy of it.
186      * 
187      * @param $html
188      * @return $html with all the tinyMCE specific html removed
189      */
190     function cleanEncodedMCEHtml($html) {
191         $html = str_replace("mce:script", "script", $html);
192         $html = str_replace("mce_src=", "src=", $html);
193         $html = str_replace("mce_href=", "href=", $html);
194         return $html;
195     }
196     
197     /**
198      * Reload the default button configs by allowing admins to specify
199      * which tinyMCE buttons will be displayed in a seperate config file.
200      *
201      */
202     private function overloadButtonConfigs() 
203     {
204         if( file_exists( $this->customConfigFile ) )    
205         {
206             require_once($this->customConfigFile);
207             
208             if(!isset($buttonConfigs))
209                 return;
210             
211             foreach ($buttonConfigs as $k => $v)
212             {
213                 if( isset($this->buttonConfigs[$k]) )
214                     $this->buttonConfigs[$k] = $v;
215             }
216         }
217     }
218     
219 } // end class def