PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/include/SugarTinyMCE.php

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