85 private $events = array();
89 private $globalEvents = array();
131 public function editor($name, $value =
"",
$config = array(), $events = array())
134 foreach ($this->textareaAttributes as $key => $val) {
135 $attr.=
" " . $key .
'="' . str_replace(
'"',
'"', $val) .
'"';
137 $out =
"<textarea name=\"" . $name .
"\"" . $attr .
">" . htmlspecialchars($value) .
"</textarea>\n";
138 if (!$this->initialized) {
139 $out .= $this->init();
142 $_config = $this->configSettings(
$config, $events);
144 $js = $this->returnGlobalEvents();
145 if (!empty($_config))
146 $js .=
"CKEDITOR.replace('".$name.
"', ".$this->jsEncode($_config).
");";
148 $js .=
"CKEDITOR.replace('".$name.
"');";
150 $out .= $this->script($js);
152 if (!$this->returnOutput) {
176 if (!$this->initialized) {
177 $out .= $this->init();
180 $_config = $this->configSettings(
$config, $events);
182 $js = $this->returnGlobalEvents();
183 if (!empty($_config)) {
184 $js .=
"CKEDITOR.replace('".$id.
"', ".$this->jsEncode($_config).
");";
187 $js .=
"CKEDITOR.replace('".$id.
"');";
189 $out .= $this->script($js);
191 if (!$this->returnOutput) {
219 if (!$this->initialized) {
220 $out .= $this->init();
223 $_config = $this->configSettings();
225 $js = $this->returnGlobalEvents();
226 if (empty($_config)) {
227 if (empty($className)) {
228 $js .=
"CKEDITOR.replaceAll();";
231 $js .=
"CKEDITOR.replaceAll('".$className.
"');";
235 $classDetection =
"";
236 $js .=
"CKEDITOR.replaceAll( function(textarea, config) {\n";
237 if (!empty($className)) {
238 $js .=
" var classRegex = new RegExp('(?:^| )' + '". $className .
"' + '(?:$| )');\n";
239 $js .=
" if (!classRegex.test(textarea.className))\n";
240 $js .=
" return false;\n";
242 $js .=
" CKEDITOR.tools.extend(config, ". $this->jsEncode($_config) .
", true);";
247 $out .= $this->script($js);
249 if (!$this->returnOutput) {
273 if (!isset($this->events[$event])) {
274 $this->events[$event] = array();
277 if (!in_array($javascriptCode, $this->events[$event])) {
278 $this->events[$event][] = $javascriptCode;
290 if (!empty($event)) {
291 $this->events[$event] = array();
294 $this->events = array();
313 if (!isset($this->globalEvents[$event])) {
314 $this->globalEvents[$event] = array();
317 if (!in_array($javascriptCode, $this->globalEvents[$event])) {
318 $this->globalEvents[$event][] = $javascriptCode;
330 if (!empty($event)) {
331 $this->globalEvents[$event] = array();
334 $this->globalEvents = array();
343 private function script($js)
345 $out =
"<script type=\"text/javascript\">";
346 $out .=
"//<![CDATA[\n";
349 $out .=
"</script>\n";
360 private function configSettings(
$config = array(), $events = array())
363 $_events = $this->events;
366 $_config = array_merge($_config,
$config);
369 if (is_array($events) && !empty($events)) {
370 foreach ($events as $eventName => $code) {
371 if (!isset($_events[$eventName])) {
372 $_events[$eventName] = array();
374 if (!in_array($code, $_events[$eventName])) {
375 $_events[$eventName][] = $code;
380 if (!empty($_events)) {
381 foreach($_events as $eventName => $handlers) {
382 if (empty($handlers)) {
385 else if (count($handlers) == 1) {
386 $_config[
'on'][$eventName] =
'@@'.$handlers[0];
389 $_config[
'on'][$eventName] =
'@@function (ev){';
390 foreach ($handlers as $handler => $code) {
391 $_config[
'on'][$eventName] .=
'('.$code.
')(ev);';
393 $_config[
'on'][$eventName] .=
'}';
404 private function returnGlobalEvents()
406 static $returnedEvents;
409 if (!isset($returnedEvents)) {
410 $returnedEvents = array();
413 if (!empty($this->globalEvents)) {
414 foreach ($this->globalEvents as $eventName => $handlers) {
415 foreach ($handlers as $handler => $code) {
416 if (!isset($returnedEvents[$eventName])) {
417 $returnedEvents[$eventName] = array();
420 if (!in_array($code, $returnedEvents[$eventName])) {
421 $out .= ($code ?
"\n" :
"") .
"CKEDITOR.on('". $eventName .
"', $code);";
422 $returnedEvents[$eventName][] = $code;
434 private function init()
436 static $initComplete;
439 if (!empty($initComplete)) {
443 if ($this->initialized) {
444 $initComplete =
true;
449 $ckeditorPath = $this->ckeditorPath();
456 if (strpos($ckeditorPath,
'..') !== 0) {
457 $out .= $this->script(
"window.CKEDITOR_BASEPATH='". $ckeditorPath .
"';");
460 $out .=
"<script type=\"text/javascript\" src=\"" . $ckeditorPath .
'ckeditor.js' . $args .
"\"></script>\n";
463 if ($this->
timestamp != self::timestamp) {
464 $extraCode .= ($extraCode ?
"\n" :
"") .
"CKEDITOR.timestamp = '". $this->
timestamp .
"';";
467 $out .= $this->script($extraCode);
470 $initComplete = $this->initialized =
true;
478 private function ckeditorPath()
480 if (!empty($this->basePath)) {
489 if (isset($_SERVER[
'SCRIPT_FILENAME'])) {
490 $realPath = dirname($_SERVER[
'SCRIPT_FILENAME']);
496 $realPath = realpath(
'./' ) ;
504 $selfPath = dirname($_SERVER[
'PHP_SELF']);
505 $file = str_replace(
"\\",
"/", __FILE__);
507 if (!$selfPath || !$realPath || !$file) {
511 $documentRoot = substr($realPath, 0, strlen($realPath) - strlen($selfPath));
512 $fileUrl = substr($file, strlen($documentRoot));
513 $ckeditorUrl = str_replace(
"ckeditor_php5.php",
"", $fileUrl);
524 private function jsEncode($val)
530 return $val ?
'true' :
'false';
535 if (is_float($val)) {
536 return str_replace(
',',
'.', $val);
538 if (is_array($val) || is_object($val)) {
539 if (is_array($val) && (array_keys($val) === range(0,count($val)-1))) {
540 return '[' . implode(
',', array_map(array($this,
'jsEncode'), $val)) .
']';
543 foreach ($val as $k => $v){
544 $temp[] = $this->jsEncode(
"{$k}") .
':' . $this->jsEncode($v);
546 return '{' . implode(
',', $temp) .
'}';
549 if (strpos($val,
'@@') === 0)
550 return substr($val, 2);
551 if (strtoupper(substr($val, 0, 9)) ==
'CKEDITOR.')
554 return '"' . str_replace(array(
"\\",
"/",
"\n",
"\t",
"\r",
"\x08",
"\x0c",
'"'), array(
'\\\\',
'\\/',
'\\n',
'\\t',
'\\r',
'\\b',
'\\f',
'\"'), $val) .
'"';
const version
The version of CKEditor.
clearGlobalEventHandlers($event=null)
Clear registered global event handlers.
$textareaAttributes
An array with textarea attributes.
$returnOutput
Boolean variable indicating whether created code should be printed out or returned by a function...
__construct($basePath=null)
Main Constructor.
CKEditor class that can be used to create editor instances in PHP pages on server side...
const timestamp
A constant string unique for each release of CKEditor.
replaceAll($className=null)
Replace all
$initialized
A boolean variable indicating whether CKEditor has been initialized.
addGlobalEventHandler($event, $javascriptCode)
Adds global event listener.
$timestamp
A string indicating the creation date of CKEditor.
$config
An array that holds the global CKEditor configuration.
$basePath
URL to the CKEditor installation directory (absolute or relative to document root).
editor($name, $value="", $config=array(), $events=array())
Creates a CKEditor instance.
addEventHandler($event, $javascriptCode)
Adds event listener.
replace($id, $config=array(), $events=array())
Replaces a
clearEventHandlers($event=null)
Clear registered event handlers.