Skip to content

Instantly share code, notes, and snippets.

@slywalker
Created April 26, 2009 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slywalker/101976 to your computer and use it in GitHub Desktop.
Save slywalker/101976 to your computer and use it in GitHub Desktop.
<?php
App::import('Vendor', 'Post.FCKeditor', array('file' => 'fckeditor/fckeditor.php'));
class FckHelper extends AppHelper {
var $helpers = array('Form', 'Javascript', 'Session');
function textarea($fieldName, $options = array())
{
$defaultOptions = array(
'value' => '',
'width' => '100%',
'height' => '300',
'toolbar' => 'Cake',
);
$options = array_merge($defaultOptions, $options);
$fieldName = explode('.', $fieldName);
$editor_name = "data";
if (count($fieldName) > 1) {
foreach ($fieldName as $key) {
$editor_name .= "[{$key}]";
}
} else {
$model = $this->Form->params['models'][0];
$editor_name .= "[{$model}][{$fieldName[0]}]";
}
$oFCKeditor = new FCKeditor($editor_name) ;
$oFCKeditor->BasePath = $this->webroot('/js/fckeditor/') ;
$oFCKeditor->Value = $options['value'];
$oFCKeditor->Width = $options['width'];
$oFCKeditor->Height = $options['height'];
$oFCKeditor->ToolbarSet = $options['toolbar'];
$userId = $this->Session->read('Auth.AccountUser.id');
$userDir = WWW_ROOT.'files/'.Security::hash($userId);
if (!is_dir($userDir)) {
mkdir($userDir);
chmod($userDir, 0777);
}
$path = $userDir.'/';
$connector = $this->url('/js/fckeditor/editor/filemanager/connectors/php/connector.php', true);
$url = $oFCKeditor->BasePath
. 'editor/filemanager/browser/default/browser.html';
$data = array(
'Connector' => $connector,
'ServerPath' => $path,
'Type' => 'Image',
);
$query = http_build_query($data);
$oFCKeditor->Config['LinkBrowserURL'] = $url.'?'.$query;
$data = am($data, array('Type' => 'Image'));
$query = http_build_query($data);
$oFCKeditor->Config['ImageBrowserURL'] = $url.'?'.$query;
$data = am($data, array('Type' => 'Flash'));
$query = http_build_query($data);
$oFCKeditor->Config['FlashBrowserURL'] = $url.'?'.$query;
$url = $oFCKeditor->BasePath
. 'editor/filemanager/upload/php/upload.php';
$data = array(
'ServerPath' => $path,
'Type' => 'Image',
);
$query = http_build_query($data);
$oFCKeditor->Config['LinkUploadURL'] = $url.'?'.$query;
$data = am($data, array('Type' => 'Image'));
$query = http_build_query($data);
$oFCKeditor->Config['ImageUploadURL'] = $url.'?'.$query;;
$data = am($data, array('Type' => 'Flash'));
$query = http_build_query($data);
$oFCKeditor->Config['FlashUploadURL'] = $url.'?'.$query;
return $oFCKeditor->CreateHtml();
}
function input($fieldName, $options = array())
{
$options = array_merge($options, array('type' => 'textarea'));
$out = $this->Form->input($fieldName, $options);
$out = preg_replace('/<textarea[^>]*>[^<]*<\/textarea>/', $this->textarea($fieldName, $options), $out);
return $out;
}
function code()
{
$basePath = $this->webroot('/js/fckeditor/');
$userId = $this->Session->read('Auth.AccountUser.id');
$userDir = WWW_ROOT.'files/'.Security::hash($userId);
if (!is_dir($userDir)) {
mkdir($userDir);
chmod($userDir, 0777);
}
$path = $userDir.'/';
$connector = $this->url('/js/fckeditor/editor/filemanager/connectors/php/connector.php', true);
$this->Javascript->link('fckeditor/fckeditor.js', false);
$this->Javascript->codeBlock("
function Toggle()
{
// Try to get the FCKeditor instance, if available.
var oEditor ;
if ( typeof( FCKeditorAPI ) != 'undefined' )
oEditor = FCKeditorAPI.GetInstance( 'DataFCKeditor' ) ;
// Get the _Textarea and _FCKeditor DIVs.
var eTextareaDiv = document.getElementById( 'Textarea' ) ;
var eFCKeditorDiv = document.getElementById( 'FCKeditor' ) ;
// If the _Textarea DIV is visible, switch to FCKeditor.
if ( eTextareaDiv.style.display != 'none' )
{
// If it is the first time, create the editor.
if ( !oEditor )
{
CreateEditor() ;
}
else
{
// Set the current text in the textarea to the editor.
oEditor.SetData( document.getElementById('DataTextarea').value.replace(/\\n/g, \"<br />\") ) ;
}
// Switch the DIVs display.
eTextareaDiv.style.display = 'none' ;
eFCKeditorDiv.style.display = '' ;
// This is a hack for Gecko 1.0.x ... it stops editing when the editor is hidden.
if ( oEditor && !document.all )
{
if ( oEditor.EditMode == FCK_EDITMODE_WYSIWYG )
oEditor.MakeEditable() ;
}
}
else
{
// Set the textarea value to the editor value.
document.getElementById('DataTextarea').value = oEditor.GetXHTML().replace(/<br \/>/g, \"\\n\") ;
// Switch the DIVs display.
eTextareaDiv.style.display = '' ;
eFCKeditorDiv.style.display = 'none' ;
}
}
function CreateEditor()
{
// Copy the value of the current textarea, to the textarea that will be used by the editor.
document.getElementById('DataFCKeditor').value = document.getElementById('DataTextarea').value.replace(/\\n/g, \"<br />\") ;
// Automatically calculates the editor base path based on the _samples directory.
// This is usefull only for these samples. A real application should use something like this:
// oFCKeditor.BasePath = '/fckeditor/' ; // '/fckeditor/' is the default value.
var sBasePath = document.location.href.substring(0,document.location.href.lastIndexOf('_samples')) ;
// Create an instance of FCKeditor (using the target textarea as the name).
var oFCKeditor = new FCKeditor( 'DataFCKeditor' ) ;
oFCKeditor.BasePath = '{$basePath}';
oFCKeditor.Width = '100%';
oFCKeditor.Height = '300';
oFCKeditor.ToolbarSet = 'Cake';
path = '{$path}';
connector = '{$connector}';
url = oFCKeditor.BasePath + 'editor/filemanager/browser/default/browser.html';
oFCKeditor.Config['LinkBrowserURL'] = url + '?Connector=' + connector + '&ServerPath=' + path + '&Type=Image';
oFCKeditor.Config['ImageBrowserURL'] = url + '?Connector=' + connector + '&ServerPath=' + path + '&Type=Image';
oFCKeditor.Config['FlashBrowserURL'] = url + '?Connector=' + connector + '&ServerPath=' + path + '&Type=Flash';
url = oFCKeditor.BasePath + 'editor/filemanager/upload/php/upload.php';
oFCKeditor.Config['LinkUploadURL'] = url + '?ServerPath=' + path + '&Type=Image';
oFCKeditor.Config['ImageUploadURL'] = url + '?ServerPath=' + path + '&Type=Image';
oFCKeditor.Config['FlashUploadURL'] = url + '?ServerPath=' + path + '&Type=Flash';
oFCKeditor.ReplaceTextarea() ;
}
// The FCKeditor_OnComplete function is a special function called everytime an
// editor instance is completely loaded and available for API interactions.
function FCKeditor_OnComplete( editorInstance )
{
// Enable the switch button. It is disabled at startup, waiting the editor to be loaded.
document.getElementById('BtnSwitchTextarea').disabled = false ;
}
function PrepareSave()
{
// If the textarea isn't visible update the content from the editor.
if ( document.getElementById( 'Textarea' ).style.display == 'none' )
{
var oEditor = FCKeditorAPI.GetInstance( 'DataFCKeditor' ) ;
document.getElementById( 'DataTextarea' ).value = oEditor.GetXHTML().replace(/<br \/>/g, \"\\n\") ;
}
}
", array('inline' => false));
return;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment