doleditor.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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->browser->phone) $this->tool = 'textarea';
  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.'&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. * @return void|string
  127. */
  128. function Create($noprint=0,$morejs='')
  129. {
  130. global $conf,$langs;
  131. $fullpage=False;
  132. $disallowAnyContent=empty($conf->global->FCKEDITOR_ALLOW_ANY_CONTENT); // Only predefined list of html tags are allowed
  133. $found=0;
  134. $out='';
  135. if ($this->tool == 'fckeditor')
  136. {
  137. $found=1;
  138. $this->editor->Create();
  139. }
  140. if (in_array($this->tool,array('textarea','ckeditor')))
  141. {
  142. $found=1;
  143. //$out.= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'" rows="'.$this->rows.'" cols="'.$this->cols.'"'.($this->readonly?' disabled':'').' class="flat">';
  144. $out.= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'" rows="'.$this->rows.'"'.(preg_match('/%/',$this->cols)?' style="margin-top: 2px; width: '.$this->cols.'"':' cols="'.$this->cols.'"').' class="flat">';
  145. $out.= $this->content;
  146. $out.= '</textarea>';
  147. if ($this->tool == 'ckeditor')
  148. {
  149. if (! defined('REQUIRE_CKEDITOR')) define('REQUIRE_CKEDITOR','1');
  150. if (! empty($conf->global->FCKEDITOR_SKIN)) {
  151. $skin = $conf->global->FCKEDITOR_SKIN;
  152. } else {
  153. $skin = 'moono'; // default with ckeditor 4 : moono
  154. }
  155. $htmlencode_force=preg_match('/_encoded$/',$this->toolbarname)?'true':'false';
  156. $out.= '<script type="text/javascript">
  157. $(document).ready(function () {
  158. /* if (CKEDITOR.loadFullCore) CKEDITOR.loadFullCore(); */
  159. /* should be editor=CKEDITOR.replace but what if serveral editors ? */
  160. CKEDITOR.replace(\''.$this->htmlname.'\',
  161. {
  162. /* property:xxx is same than CKEDITOR.config.property = xxx */
  163. customConfig : ckeditorConfig,
  164. readOnly : '.($this->readonly?'true':'false').',
  165. htmlEncodeOutput :'.$htmlencode_force.',
  166. allowedContent :'.($disallowAnyContent?'false':'true').',
  167. fullPage : '.($fullpage?'true':'false').',
  168. toolbar: \''.$this->toolbarname.'\',
  169. toolbarStartupExpanded: '.($this->toolbarstartexpanded ? 'true' : 'false').',
  170. width: '.($this->width ? '\''.$this->width.'\'' : '\'\'').',
  171. height: '.$this->height.',
  172. skin: \''.$skin.'\',
  173. language: \''.$langs->defaultlang.'\',
  174. textDirection: \''.$langs->trans("DIRECTION").'\',
  175. on :
  176. {
  177. instanceReady : function( ev )
  178. {
  179. // Output paragraphs as <p>Text</p>.
  180. this.dataProcessor.writer.setRules( \'p\',
  181. {
  182. indent : false,
  183. breakBeforeOpen : true,
  184. breakAfterOpen : false,
  185. breakBeforeClose : false,
  186. breakAfterClose : true
  187. });
  188. }
  189. }';
  190. if ($this->uselocalbrowser)
  191. {
  192. $out.= ','."\n";
  193. // To use filemanager with old fckeditor (GPL)
  194. $out.= ' filebrowserBrowseUrl : ckeditorFilebrowserBrowseUrl,';
  195. $out.= ' filebrowserImageBrowseUrl : ckeditorFilebrowserImageBrowseUrl,';
  196. //$out.= ' filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=File\',';
  197. //$out.= ' filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=Image\',';
  198. $out.= "\n";
  199. // To use filemanager with ckfinder (Non free) and ckfinder directory is inside htdocs/includes
  200. /* $out.= ' filebrowserBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html\',
  201. filebrowserImageBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Images\',
  202. filebrowserFlashBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Flash\',
  203. filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files\',
  204. filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images\',
  205. filebrowserFlashUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash\','."\n";
  206. */
  207. $out.= ' filebrowserWindowWidth : \'900\',
  208. filebrowserWindowHeight : \'500\',
  209. filebrowserImageWindowWidth : \'900\',
  210. filebrowserImageWindowHeight : \'500\'';
  211. }
  212. $out.= ' })'.$morejs;
  213. $out.= '});
  214. </script>';
  215. }
  216. }
  217. if (empty($found))
  218. {
  219. $out.= 'Error, unknown value for tool '.$this->tool.' in DolEditor Create function.';
  220. }
  221. if ($noprint) return $out;
  222. else print $out;
  223. }
  224. }