doleditor.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /* Copyright (C) 2006-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/class/doleditor.class.php
  20. * \brief Class to manage a WYSIWYG editor
  21. */
  22. /**
  23. * Class to manage a WYSIWYG editor.
  24. * Usage: $doleditor=new DolEditor('body',$message,320,'toolbar_mailing');
  25. * $doleditor->Create();
  26. */
  27. class DolEditor
  28. {
  29. var $tool; // Store the selected tool
  30. // If using fckeditor
  31. var $editor;
  32. // If not using fckeditor
  33. var $content;
  34. var $htmlname;
  35. var $toolbarname;
  36. var $toolbarstartexpanded;
  37. var $rows;
  38. var $cols;
  39. var $height;
  40. var $width;
  41. var $readonly;
  42. /**
  43. * Create an object to build an HTML area to edit a large string content
  44. *
  45. * @param string $htmlname HTML name of WYSIWIG field
  46. * @param string $content Content of WYSIWIG field
  47. * @param int $width Width in pixel of edit area (auto by default)
  48. * @param int $height Height in pixel of edit area (200px by default)
  49. * @param string $toolbarname Name of bar set to use ('Full', 'dolibarr_notes[_encoded]', 'dolibarr_details[_encoded]'=the less featured, 'dolibarr_mailings[_encoded]', 'dolibarr_readonly')
  50. * @param string $toolbarlocation Where bar is stored :
  51. * 'In' each window has its own toolbar
  52. * 'Out:name' share toolbar into the div called 'name'
  53. * @param boolean $toolbarstartexpanded Bar is visible or not at start
  54. * @param int $uselocalbrowser Enabled to add links to local object with local browser. If false, only external images can be added in content.
  55. * @param int $okforextendededitor True=Allow usage of extended editor tool (like fckeditor)
  56. * @param int $rows Size of rows for textarea tool
  57. * @param string $cols Size of cols for textarea tool (textarea number of cols '70' or percent 'x%')
  58. * @param int $readonly 0=Read/Edit, 1=Read only
  59. */
  60. function __construct($htmlname,$content,$width='',$height=200,$toolbarname='Basic',$toolbarlocation='In',$toolbarstartexpanded=false,$uselocalbrowser=true,$okforextendededitor=true,$rows=0,$cols=0,$readonly=0)
  61. {
  62. global $conf,$langs;
  63. dol_syslog(get_class($this)."::DolEditor htmlname=".$htmlname." width=".$width." height=".$height." toolbarname=".$toolbarname);
  64. if (! $rows) $rows=round($height/20);
  65. if (! $cols) $cols=($width?round($width/6):80);
  66. $shorttoolbarname=preg_replace('/_encoded$/','',$toolbarname);
  67. // Name of extended editor to use (FCKEDITOR_EDITORNAME can be 'ckeditor' or 'fckeditor')
  68. $defaulteditor='ckeditor';
  69. $this->tool=empty($conf->global->FCKEDITOR_EDITORNAME)?$defaulteditor:$conf->global->FCKEDITOR_EDITORNAME;
  70. $this->uselocalbrowser=$uselocalbrowser;
  71. $this->readonly=$readonly;
  72. // Check if extended editor is ok. If not we force textarea
  73. if (empty($conf->fckeditor->enabled) || ! $okforextendededitor) $this->tool = 'textarea';
  74. if ($conf->dol_use_jmobile) $this->tool = 'textarea'; // TODO ckeditor ko with jmobile
  75. // Define content and some properties
  76. if ($this->tool == 'ckeditor')
  77. {
  78. $content=dol_htmlentitiesbr($content); // If content is not HTML, we convert to HTML.
  79. }
  80. if ($this->tool == 'fckeditor')
  81. {
  82. require_once DOL_DOCUMENT_ROOT.'/includes/fckeditor/fckeditor.php';
  83. $content=dol_htmlentitiesbr($content); // If content is not HTML, we convert to HTML.
  84. $this->editor = new FCKeditor($htmlname);
  85. $this->editor->BasePath = DOL_URL_ROOT.'/includes/fckeditor/' ;
  86. $this->editor->Value = $content;
  87. $this->editor->Height = $height;
  88. if (! empty($width)) $this->editor->Width = $width;
  89. $this->editor->ToolbarSet = $shorttoolbarname; // Profile of this toolbar set is deinfed into theme/mytheme/ckeditor/config.js
  90. $this->editor->Config['AutoDetectLanguage'] = 'true';
  91. $this->editor->Config['ToolbarLocation'] = $toolbarlocation ? $toolbarlocation : 'In';
  92. $this->editor->Config['ToolbarStartExpanded'] = $toolbarstartexpanded;
  93. // Rem: Le forcage de ces 2 parametres ne semble pas fonctionner.
  94. // Dolibarr utilise toujours liens avec modulepart='fckeditor' quelque soit modulepart.
  95. // Ou se trouve donc cette valeur /viewimage.php?modulepart=fckeditor&file=' ?
  96. $modulepart='fckeditor';
  97. $this->editor->Config['UserFilesPath'] = '/viewimage.php?modulepart='.$modulepart.'&entity='.$conf->entity.'&file=';
  98. $this->editor->Config['UserFilesAbsolutePath'] = DOL_DATA_ROOT.'/'.$modulepart.'/' ;
  99. $this->editor->Config['LinkBrowser']=($uselocalbrowser?'true':'false');
  100. $this->editor->Config['ImageBrowser']=($uselocalbrowser?'true':'false');
  101. if (file_exists(DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/fckeditor/fckconfig.js'))
  102. {
  103. $this->editor->Config['CustomConfigurationsPath'] = DOL_URL_ROOT.'/theme/'.$conf->theme.'/fckeditor/fckconfig.js';
  104. $this->editor->Config['SkinPath'] = DOL_URL_ROOT.'/theme/'.$conf->theme.'/fckeditor/';
  105. }
  106. }
  107. // Define some properties
  108. if (in_array($this->tool,array('textarea','ckeditor')))
  109. {
  110. $this->content = $content;
  111. $this->htmlname = $htmlname;
  112. $this->toolbarname = $shorttoolbarname;
  113. $this->toolbarstartexpanded = $toolbarstartexpanded;
  114. $this->rows = max(ROWS_3,$rows);
  115. $this->cols = (preg_match('/%/',$cols)?$cols:max(40,$cols)); // If $cols is a percent, we keep it, otherwise, we take max
  116. $this->height = $height;
  117. $this->width = $width;
  118. }
  119. }
  120. /**
  121. * Output edit area inside the HTML stream.
  122. * Output depends on this->tool (fckeditor, ckeditor, textarea, ...)
  123. *
  124. * @param int $noprint 1=Return HTML string instead of printing it to output
  125. * @param string $morejs Add more js. For example: ".on( \'saveSnapshot\', function(e) { alert(\'ee\'); });"
  126. * @param boolean $disallowAnyContent Disallow to use any content. true=restrict to a predefined list of allowed elements.
  127. * @return void|string
  128. */
  129. function Create($noprint=0,$morejs='',$disallowAnyContent=true)
  130. {
  131. global $conf,$langs;
  132. $fullpage=False;
  133. if (isset($conf->global->FCKEDITOR_ALLOW_ANY_CONTENT))
  134. {
  135. $disallowAnyContent=empty($conf->global->FCKEDITOR_ALLOW_ANY_CONTENT); // Only predefined list of html tags are allowed or all
  136. }
  137. $found=0;
  138. $out='';
  139. if ($this->tool == 'fckeditor') // not used anymore
  140. {
  141. $found=1;
  142. $this->editor->Create();
  143. }
  144. if (in_array($this->tool,array('textarea','ckeditor')))
  145. {
  146. $found=1;
  147. //$out.= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'" rows="'.$this->rows.'" cols="'.$this->cols.'"'.($this->readonly?' disabled':'').' class="flat">';
  148. $out.= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'" rows="'.$this->rows.'"'.(preg_match('/%/',$this->cols)?' style="margin-top: 5px; width: '.$this->cols.'"':' cols="'.$this->cols.'"').' class="flat">';
  149. $out.= $this->content;
  150. $out.= '</textarea>';
  151. if ($this->tool == 'ckeditor' && ! empty($conf->use_javascript_ajax))
  152. {
  153. if (! defined('REQUIRE_CKEDITOR')) define('REQUIRE_CKEDITOR','1');
  154. if (! empty($conf->global->FCKEDITOR_SKIN)) {
  155. $skin = $conf->global->FCKEDITOR_SKIN;
  156. } else {
  157. $skin = 'moono-lisa'; // default with ckeditor 4.6 : moono-lisa
  158. }
  159. $htmlencode_force=preg_match('/_encoded$/',$this->toolbarname)?'true':'false';
  160. $out.= '<!-- Output ckeditor $disallowAnyContent='.$disallowAnyContent.' toolbarname='.$this->toolbarname.' -->'."\n";
  161. $out.= '<script type="text/javascript">
  162. $(document).ready(function () {
  163. /* if (CKEDITOR.loadFullCore) CKEDITOR.loadFullCore(); */
  164. /* should be editor=CKEDITOR.replace but what if serveral editors ? */
  165. CKEDITOR.replace(\''.$this->htmlname.'\',
  166. {
  167. /* property:xxx is same than CKEDITOR.config.property = xxx */
  168. customConfig : ckeditorConfig,
  169. readOnly : '.($this->readonly?'true':'false').',
  170. htmlEncodeOutput :'.$htmlencode_force.',
  171. allowedContent :'.($disallowAnyContent?'false':'true').',
  172. extraAllowedContent : \'\',
  173. fullPage : '.($fullpage?'true':'false').',
  174. toolbar: \''.$this->toolbarname.'\',
  175. toolbarStartupExpanded: '.($this->toolbarstartexpanded ? 'true' : 'false').',
  176. width: '.($this->width ? '\''.$this->width.'\'' : '\'\'').',
  177. height: '.$this->height.',
  178. skin: \''.$skin.'\',
  179. language: \''.$langs->defaultlang.'\',
  180. textDirection: \''.$langs->trans("DIRECTION").'\',
  181. on :
  182. {
  183. instanceReady : function( ev )
  184. {
  185. // Output paragraphs as <p>Text</p>.
  186. this.dataProcessor.writer.setRules( \'p\',
  187. {
  188. indent : false,
  189. breakBeforeOpen : true,
  190. breakAfterOpen : false,
  191. breakBeforeClose : false,
  192. breakAfterClose : true
  193. });
  194. }
  195. }';
  196. if ($this->uselocalbrowser)
  197. {
  198. $out.= ','."\n";
  199. // To use filemanager with old fckeditor (GPL)
  200. $out.= ' filebrowserBrowseUrl : ckeditorFilebrowserBrowseUrl,';
  201. $out.= ' filebrowserImageBrowseUrl : ckeditorFilebrowserImageBrowseUrl,';
  202. //$out.= ' filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=File\',';
  203. //$out.= ' filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=Image\',';
  204. $out.= "\n";
  205. // To use filemanager with ckfinder (Non free) and ckfinder directory is inside htdocs/includes
  206. /* $out.= ' filebrowserBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html\',
  207. filebrowserImageBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Images\',
  208. filebrowserFlashBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Flash\',
  209. filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files\',
  210. filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images\',
  211. filebrowserFlashUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash\','."\n";
  212. */
  213. $out.= ' filebrowserWindowWidth : \'900\',
  214. filebrowserWindowHeight : \'500\',
  215. filebrowserImageWindowWidth : \'900\',
  216. filebrowserImageWindowHeight : \'500\'';
  217. }
  218. $out.= ' })'.$morejs;
  219. $out.= '});
  220. </script>';
  221. }
  222. }
  223. if (empty($found))
  224. {
  225. $out.= 'Error, unknown value for tool '.$this->tool.' in DolEditor Create function.';
  226. }
  227. if ($noprint) return $out;
  228. else print $out;
  229. }
  230. }