dolibarr  7.0.0-beta
doleditor.class.php
Go to the documentation of this file.
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 
29 class DolEditor
30 {
31  var $tool; // Store the selected tool
32 
33  // If using fckeditor
34  var $editor;
35 
36  // If not using fckeditor
37  var $content;
38  var $htmlname;
39  var $toolbarname;
40  var $toolbarstartexpanded;
41  var $rows;
42  var $cols;
43  var $height;
44  var $width;
45  var $readonly;
46 
47 
67  function __construct($htmlname, $content, $width='', $height=200, $toolbarname='Basic', $toolbarlocation='In', $toolbarstartexpanded=false, $uselocalbrowser=true, $okforextendededitor=true, $rows=0, $cols=0, $readonly=0)
68  {
69  global $conf,$langs;
70 
71  dol_syslog(get_class($this)."::DolEditor htmlname=".$htmlname." width=".$width." height=".$height." toolbarname=".$toolbarname);
72 
73  if (! $rows) $rows=round($height/20);
74  if (! $cols) $cols=($width?round($width/6):80);
75  $shorttoolbarname=preg_replace('/_encoded$/','',$toolbarname);
76 
77  // Name of extended editor to use (FCKEDITOR_EDITORNAME can be 'ckeditor' or 'fckeditor')
78  $defaulteditor='ckeditor';
79  $this->tool=empty($conf->global->FCKEDITOR_EDITORNAME)?$defaulteditor:$conf->global->FCKEDITOR_EDITORNAME;
80  $this->uselocalbrowser=$uselocalbrowser;
81  $this->readonly=$readonly;
82 
83  // Check if extended editor is ok. If not we force textarea
84  if ((empty($conf->fckeditor->enabled) && $okforextendededitor != 'ace') || empty($okforextendededitor)) $this->tool = 'textarea';
85  if ($okforextendededitor === 'ace') $this->tool='ace';
86  //if ($conf->dol_use_jmobile) $this->tool = 'textarea'; // ckeditor and ace seems ok with mobile
87 
88  // Define content and some properties
89  if ($this->tool == 'ckeditor')
90  {
91  $content=dol_htmlentitiesbr($content); // If content is not HTML, we convert to HTML.
92  }
93  if ($this->tool == 'fckeditor')
94  {
95  require_once DOL_DOCUMENT_ROOT.'/includes/fckeditor/fckeditor.php';
96 
97  $content=dol_htmlentitiesbr($content); // If content is not HTML, we convert to HTML.
98 
99  $this->editor = new FCKeditor($htmlname);
100  $this->editor->BasePath = DOL_URL_ROOT.'/includes/fckeditor/' ;
101  $this->editor->Value = $content;
102  $this->editor->Height = $height;
103  if (! empty($width)) $this->editor->Width = $width;
104  $this->editor->ToolbarSet = $shorttoolbarname; // Profile of this toolbar set is deinfed into theme/mytheme/ckeditor/config.js
105  $this->editor->Config['AutoDetectLanguage'] = 'true'; // Language of user (browser)
106  $this->editor->Config['ToolbarLocation'] = $toolbarlocation ? $toolbarlocation : 'In';
107  $this->editor->Config['ToolbarStartExpanded'] = $toolbarstartexpanded;
108 
109  // Rem: Le forcage de ces 2 parametres ne semble pas fonctionner.
110  // Dolibarr utilise toujours liens avec modulepart='fckeditor' quelque soit modulepart.
111  // Ou se trouve donc cette valeur /viewimage.php?modulepart=fckeditor&file=' ?
112  $modulepart='fckeditor';
113  $this->editor->Config['UserFilesPath'] = '/viewimage.php?modulepart='.$modulepart.'&entity='.$conf->entity.'&file=';
114  $this->editor->Config['UserFilesAbsolutePath'] = DOL_DATA_ROOT.'/'.$modulepart.'/' ;
115 
116  $this->editor->Config['LinkBrowser']=($uselocalbrowser?'true':'false');
117  $this->editor->Config['ImageBrowser']=($uselocalbrowser?'true':'false');
118 
119  if (file_exists(DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/fckeditor/fckconfig.js'))
120  {
121  $this->editor->Config['CustomConfigurationsPath'] = DOL_URL_ROOT.'/theme/'.$conf->theme.'/fckeditor/fckconfig.js';
122  $this->editor->Config['SkinPath'] = DOL_URL_ROOT.'/theme/'.$conf->theme.'/fckeditor/';
123  }
124  }
125 
126  // Define some properties
127  if (in_array($this->tool,array('textarea','ckeditor','ace')))
128  {
129  $this->content = $content;
130  $this->htmlname = $htmlname;
131  $this->toolbarname = $shorttoolbarname;
132  $this->toolbarstartexpanded = $toolbarstartexpanded;
133  $this->rows = max(ROWS_3,$rows);
134  $this->cols = (preg_match('/%/',$cols)?$cols:max(40,$cols)); // If $cols is a percent, we keep it, otherwise, we take max
135  $this->height = $height;
136  $this->width = $width;
137  }
138 
139  }
140 
152  function Create($noprint=0, $morejs='', $disallowAnyContent=true, $titlecontent='', $option='')
153  {
154  global $conf,$langs;
155 
156  $fullpage=False;
157  if (isset($conf->global->FCKEDITOR_ALLOW_ANY_CONTENT))
158  {
159  $disallowAnyContent=empty($conf->global->FCKEDITOR_ALLOW_ANY_CONTENT); // Only predefined list of html tags are allowed or all
160  }
161 
162  $found=0;
163  $out='';
164 
165  if ($this->tool == 'fckeditor') // not used anymore
166  {
167  $found=1;
168  $this->editor->Create();
169  }
170  if (in_array($this->tool,array('textarea','ckeditor')))
171  {
172  $found=1;
173  //$out.= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'" '.($this->readonly?' disabled':'').' rows="'.$this->rows.'"'.(preg_match('/%/',$this->cols)?' style="margin-top: 5px; width: '.$this->cols.'"':' cols="'.$this->cols.'"').' class="flat">';
174  // TODO We do not put the disabled tag because on a read form, it change style with grey.
175  $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">';
176  $out.= $this->content;
177  $out.= '</textarea>';
178 
179  if ($this->tool == 'ckeditor' && ! empty($conf->use_javascript_ajax))
180  {
181  if (! defined('REQUIRE_CKEDITOR')) define('REQUIRE_CKEDITOR','1');
182 
183  if (! empty($conf->global->FCKEDITOR_SKIN)) {
184  $skin = $conf->global->FCKEDITOR_SKIN;
185  } else {
186  $skin = 'moono-lisa'; // default with ckeditor 4.6 : moono-lisa
187  }
188 
189  $htmlencode_force=preg_match('/_encoded$/',$this->toolbarname)?'true':'false';
190 
191  $out.= '<!-- Output ckeditor $disallowAnyContent='.$disallowAnyContent.' toolbarname='.$this->toolbarname.' -->'."\n";
192  $out.= '<script type="text/javascript">
193  $(document).ready(function () {
194  /* if (CKEDITOR.loadFullCore) CKEDITOR.loadFullCore(); */
195  /* should be editor=CKEDITOR.replace but what if serveral editors ? */
196  CKEDITOR.replace(\''.$this->htmlname.'\',
197  {
198  /* property:xxx is same than CKEDITOR.config.property = xxx */
199  customConfig : ckeditorConfig,
200  readOnly : '.($this->readonly?'true':'false').',
201  htmlEncodeOutput :'.$htmlencode_force.',
202  allowedContent :'.($disallowAnyContent?'false':'true').',
203  extraAllowedContent : \'\',
204  fullPage : '.($fullpage?'true':'false').',
205  toolbar: \''.$this->toolbarname.'\',
206  toolbarStartupExpanded: '.($this->toolbarstartexpanded ? 'true' : 'false').',
207  width: '.($this->width ? '\''.$this->width.'\'' : '\'\'').',
208  height: '.$this->height.',
209  skin: \''.$skin.'\',
210  language: \''.$langs->defaultlang.'\',
211  textDirection: \''.$langs->trans("DIRECTION").'\',
212  on :
213  {
214  instanceReady : function( ev )
215  {
216  // Output paragraphs as <p>Text</p>.
217  this.dataProcessor.writer.setRules( \'p\',
218  {
219  indent : false,
220  breakBeforeOpen : true,
221  breakAfterOpen : false,
222  breakBeforeClose : false,
223  breakAfterClose : true
224  });
225  }
226  }';
227  if ($this->uselocalbrowser)
228  {
229  $out.= ','."\n";
230  // To use filemanager with old fckeditor (GPL)
231  $out.= ' filebrowserBrowseUrl : ckeditorFilebrowserBrowseUrl,';
232  $out.= ' filebrowserImageBrowseUrl : ckeditorFilebrowserImageBrowseUrl,';
233  //$out.= ' filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=File\',';
234  //$out.= ' filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=Image\',';
235  $out.= "\n";
236  // To use filemanager with ckfinder (Non free) and ckfinder directory is inside htdocs/includes
237  /* $out.= ' filebrowserBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html\',
238  filebrowserImageBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Images\',
239  filebrowserFlashBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Flash\',
240  filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files\',
241  filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images\',
242  filebrowserFlashUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash\','."\n";
243  */
244  $out.= ' filebrowserWindowWidth : \'900\',
245  filebrowserWindowHeight : \'500\',
246  filebrowserImageWindowWidth : \'900\',
247  filebrowserImageWindowHeight : \'500\'';
248  }
249  $out.= ' })'.$morejs;
250  $out.= '});'."\n";
251  $out.= '</script>'."\n";
252  }
253  }
254 
255  // Output editor ACE
256  // Warning: ace.js and ext-statusbar.js must be loaded by the parent page.
257  if (preg_match('/^ace/', $this->tool))
258  {
259  $found=1;
260  $format=$option;
261 
262  $out.= "\n".'<!-- Output Ace editor -->'."\n";
263 
264  if ($titlecontent)
265  {
266  $out.= '<div class="aceeditorstatusbar" id="statusBar'.$this->htmlname.'">'.$titlecontent;
267  $out.= ' &nbsp; - &nbsp; <a id="morelines" href="#" class="right morelines'.$this->htmlname.'">'.dol_escape_htmltag($langs->trans("ShowMoreLines")).'</a> &nbsp; &nbsp; ';
268  $out.= '</div>';
269  $out.= '<script type="text/javascript" language="javascript">'."\n";
270  $out.= 'jQuery(document).ready(function() {'."\n";
271  $out.= ' var aceEditor = window.ace.edit("'.$this->htmlname.'aceeditorid");
272  var StatusBar = window.ace.require("ace/ext/statusbar").StatusBar; // Init status bar. Need lib ext-statusbar
273  var statusBar = new StatusBar(aceEditor, document.getElementById("statusBar'.$this->htmlname.'")); // Init status bar. Need lib ext-statusbar
274  var oldNbOfLines = 0
275  jQuery(".morelines'.$this->htmlname.'").click(function() {
276  var aceEditorClicked = window.ace.edit("'.$this->htmlname.'aceeditorid");
277  currentline = aceEditorClicked.getOption("maxLines");
278  if (oldNbOfLines == 0)
279  {
280  oldNbOfLines = currentline;
281  }
282  console.log("We click on more lines, oldNbOfLines is "+oldNbOfLines+", we have currently "+currentline);
283  if (currentline < 500)
284  {
285  aceEditorClicked.setOptions({ maxLines: 500 });
286  }
287  else
288  {
289  aceEditorClicked.setOptions({ maxLines: oldNbOfLines });
290  }
291  });
292  })';
293  $out.= '</script>'."\n";
294  }
295 
296  $out.= '<pre id="'.$this->htmlname.'aceeditorid" style="'.($this->width?'width: '.$this->width.'px; ':'');
297  $out.= ($this->height?' height: '.$this->height.'px; ':'');
298  //$out.=" min-height: 100px;";
299  $out.= '">';
300  $out.= htmlspecialchars($this->content);
301  $out.= '</pre>';
302  $out.= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'" style="width:0px; height: 0px; display: none;">';
303  $out.= htmlspecialchars($this->content);
304  $out.= '</textarea>';
305 
306  $out.= '<script type="text/javascript" language="javascript">'."\n";
307  $out.= 'var aceEditor = window.ace.edit("'.$this->htmlname.'aceeditorid");
308 
309  aceEditor.session.setMode("ace/mode/'.$format.'");
310  aceEditor.setOptions({
311  enableBasicAutocompletion: true, // the editor completes the statement when you hit Ctrl + Space. Need lib ext-language_tools.js
312  enableLiveAutocompletion: false, // the editor completes the statement while you are typing. Need lib ext-language_tools.js
313  showPrintMargin: false, // hides the vertical limiting strip
314  minLines: 10,
315  maxLines: '.(empty($this->height)?'34':(round($this->height/10))).',
316  fontSize: "110%" // ensures that the editor fits in the environment
317  });
318 
319  // defines the style of the editor
320  aceEditor.setTheme("ace/theme/chrome");
321  // hides line numbers (widens the area occupied by error and warning messages)
322  //aceEditor.renderer.setOption("showLineNumbers", false);
323  // ensures proper autocomplete, validation and highlighting of JavaScript code
324  //aceEditor.getSession().setMode("ace/mode/javascript_expression");
325  '."\n";
326 
327  $out.= 'jQuery(document).ready(function() {
328  jQuery(".buttonforacesave").click(function() {
329  console.log("We click on savefile button for component '.$this->htmlname.'");
330  var aceEditor = window.ace.edit("'.$this->htmlname.'aceeditorid")
331  console.log(aceEditor.getSession().getValue());
332  jQuery("#'.$this->htmlname.'").val(aceEditor.getSession().getValue());
333  /*if (jQuery("#'.$this->htmlname.'").html().length > 0) return true;
334  else return false;*/
335  });
336  })';
337  $out.= '</script>'."\n";
338  }
339 
340  if (empty($found))
341  {
342  $out.= 'Error, unknown value for tool '.$this->tool.' in DolEditor Create function.';
343  }
344 
345  if ($noprint) return $out;
346  else print $out;
347  }
348 
349 }
350 
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Create($noprint=0, $morejs='', $disallowAnyContent=true, $titlecontent='', $option='')
Output edit area inside the HTML stream.
__construct($htmlname, $content, $width='', $height=200, $toolbarname='Basic', $toolbarlocation='In', $toolbarstartexpanded=false, $uselocalbrowser=true, $okforextendededitor=true, $rows=0, $cols=0, $readonly=0)
Create an object to build an HTML area to edit a large string content.
print
Draft customers invoices.
Definition: index.php:91
dol_htmlentitiesbr($stringtoencode, $nl2brmode=0, $pagecodefrom='UTF-8', $removelasteolbr=1)
This function is called to encode a string into a HTML string but differs from htmlentities because a...
Class to manage a WYSIWYG editor.