Skip to content

Instantly share code, notes, and snippets.

@cherifGsoul
Created June 14, 2010 08:27
Show Gist options
  • Save cherifGsoul/437452 to your computer and use it in GitHub Desktop.
Save cherifGsoul/437452 to your computer and use it in GitHub Desktop.
<?php
/**
* CKEditorHelper
* - Allow you to easily add CKEditor textareas to your application
*
*/
class CkeditorHelper extends AppHelper {
/**
* Helpers
*
* @var array $helpers
* @access public
*/
public $helpers = array('Form', 'Html', 'Js');
/**
* Ids of the generated inputs
* @var array
*/
protected $_ids = array();
/**
* Create a CKEditor input
*
* @param string $fieldName Name of the field to be generated
* @param array $options Options for the Html helper
* @param array $ckOptions Options for the CKEditor. Valid keys are:
* - buffer [default: true]
* + all other keys available at http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html
* @return string
*/
public function input($fieldName, $options = array(), $ckOptions = array()) {
$this->_insertScript();
$cssFile = $this->webroot($this->assetTimestamp(CSS_URL . Configure::read('Config.theme') . '.css'));
$configFile = $this->webroot($this->assetTimestamp(JS_URL . 'ckeditor/config.js'));
$defaults = array(
'buffer' => true,
'contentsCss' => $cssFile,
'customConfig' => $configFile,
'filebrowserBrowseUrl' => '/js/filemanager/index.html',
'filebrowserImageBrowseUrl' => '/js/filemanager/index.html?type=Images',
'filebrowserUploadUrl' => '/js/filemanager/index.html',
'filebrowserImageUploadUrl' => '/js/filemanager/index.html?type=Images');
$ckOptions = array_merge($defaults, $ckOptions);
if (!isset($options['id'])) {
$options['id'] = 'ckeditor' . intval(rand());
}
$this->_ids[] = $options['id'];
$options['type'] = 'textarea';
$ckScript = "CKEDITOR.replace('{$options['id']}', {$this->Js->object($ckOptions)});";
$out = '';
$view = ClassRegistry::getObject('view');
if (isset($view->loaded['js']) && $ckOptions['buffer'] === true) {
$view->loaded['js']->buffer($ckScript);
} else {
$out .= $this->Html->scriptBlock($ckScript);
}
$options['div'] = 'ckeditor';
return $this->Form->input($fieldName, $options) . $out;
}
/**
* Insert CKEditor script into the page
*
* @return void
*/
protected function _insertScript() {
$this->Html->script('ckeditor/ckeditor', false);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment