Welcome Guest, Not a member yet? Register   Sign In
Installing / Integrate FCKEditor as form_helper extension.
#1

[eluser]flyer[/eluser]
Hi,

Thought someone might find this useful. Some of the other FCKEditor/CodeIgniter integrations seem to want to add it as a library or otherwise. The way I see it, its basically a fancy text area, and should be treated as such. Hence integration into the form helper.

It has 3 basic steps:

Step 1.

Download and unzip the FCKEditor application into a directory of your choice.
I prefer either the /system/plugins/fckeditor/ or /system/application/plugins/fckeditor/ or my choice - external/fckeditor/


Step 2.

Add following FCKEditor settings to the system/application/config/config.php file:

Code:
/*
|--------------------------------------------------------------------------
| FCKEditor Basepath
|--------------------------------------------------------------------------
|
| The path from your site's root in which the fckeditor folder is. Note
| this is from the site's root, not the file system root. Also note the
| required slashes at start and finish.
|
|    e.g. /fckeditor/ or /system/plugins/fckeditor/  etc...
|
*/
$config['fckeditor_basepath']    = "/system/plugins/fckeditor/";

/*
|--------------------------------------------------------------------------
| FCKEditor Toolbar Set Default
|--------------------------------------------------------------------------
|
| The default Toolbar set to be used for FCKEditor across your site. Leave
| as empty string or comment out if your happy enough with the standard
| default.
|
*/
$config['fckeditor_toolbarset_default'] = 'Default';

Step 3.

Finally the new helper class. The following code should be placed in a file named:
/system/application/helpers/form_helper.php
Note this will override /system/helpers/form_helper.php but all functions are imported from here anyway, so no clash or reduced functionality should occur.

Code:
<!--****** FILE /system/application/helpers/form_helper.php code ********-->

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

include_once( BASEPATH . '/helpers/form_helper'.EXT);

function form_fckeditor($data = '', $value = '', $extra = '')
{
    $CI =& get_instance();

    $fckeditor_basepath = $CI->config->item('fckeditor_basepath');
    
    require_once( $_SERVER["DOCUMENT_ROOT"] . $fckeditor_basepath. 'fckeditor.php' );
    
    $instanceName = ( is_array($data) && isset($data['name'])  ) ? $data['name'] : $data;
    $fckeditor = new FCKeditor($instanceName);
    
    if( $fckeditor->IsCompatible() )
    {
        $fckeditor->Value = html_entity_decode($value);
        $fckeditor->BasePath = $fckeditor_basepath;
        if( $fckeditor_toolbarset = $CI->config->item('fckeditor_toolbarset_default'))
                $fckeditor->ToolbarSet = $fckeditor_toolbarset;
        
        if( is_array($data) )
        {
            if( isset($data['value']) )
                $fckeditor->Value = html_entity_decode($data['value']);
            if( isset($data['basepath']) )
                $fckeditor->BasePath = $data['basepath'];
            if( isset($data['toolbarset']) )
                $fckeditor->ToolbarSet = $data['toolbarset'];
            if( isset($data['width']) )
                $fckeditor->Width = $data['width'];
            if( isset($data['height']) )
                $fckeditor->Height = $data['height'];
        }
        
        
        return $fckeditor->CreateHtml();
    }
    else
    {
        return form_textarea( $data, $value, $extra );
    }
}

?>

form_fckeditor() Usage

This function is identical in nearly all respects to the form_textarea() function above except that it generates a FCKEditor editor instead of a textarea and it takes some additional $data fields. So please read the Form Helper section of the Code Igniter User Guide!

However, the FCKEditor takes $data fields- 'name' 'basepath', 'toolbarset', 'width', 'height' which are passed on to the FCKEditor.php class from the FCKEditor application download. These $data fields control the settings for the editor, rather than any that you would normally pass to form_textarea() e.g. 'rows' or 'cols'.

Code:
***** USAGE ******

// e.g. 1
echo form_fckeditor('textareaName', $value );


// e.g. 2
$data = array(
              'name'        => 'textareaName2',
              'id'          => 'textareaName2',
              'toolbarset'  => 'Advanced',
              'basepath'    => '/fckeditor/',
              'width'       => '100%',
              'height'      => '200'
    );

echo form_fckeditor( $data );

It is important to know the regular $data fields that you would normal supply to the form_textarea() should also be provided if you wish to alter the defaults. This is because if the user's browser is not compatible with FCKEditor e.g. no javascript, the form_fckeditor() falls back to form_textarea().

Also, the 3rd parameter of the function, $extra, is discarded unless the function falls back to form_textarea().

Note that this helper technique requires zero modification of the FCKEditor source files, so when it comes to updates to FCKEditor, then your not going to have to remember where you've changed. Personally, I highly value keeping plugin code as independent as possible.

Any other changes to customize FCKEditor can be done exactly as per the FCKEditor documentation on its website.

Let me know what u think. Hopefully fairly easy to see what I've done!


Eddie

PS. Please don't ask about customizing FCKEditor. All customisation other than that above occurs within the FCKEditor source directory and better left for the FCKEditor support site Smile
#2

[eluser]phpwebdev[/eluser]
Thanks work fine
#3

[eluser]Unknown[/eluser]
flyer, this works fine for Firefox, but for some reason it breaks IE... FCK loads up and works just fine, but it breaks all my CSS..! (it is unbelievable, I know, but it happens!)

any idea why?



SOLVED:
I also copied this line:

<!--****** FILE /system/application/helpers/form_helper.php code ********-->

...on the top of the form_helper...

can you believe that IE broke down when it came around that line...? I now deleted it and everything works fine.
#4

[eluser]Vang[/eluser]
It doesn't work for me.
I'm using fck 2.4.3 and CI 1.5.4

I get a CI style 404 error in the iframe that is supposed to hold FCK
#5

[eluser]flyer[/eluser]
If its a 404 (Document not found) it sounds like you could have your paths set up wrong somewhere, so somewhere along the line a file can't be found. Check all the paths, then check the FCK site to see what path setups you have todo there. If the iframe is being created (which is done by FCK's files) seems to suggest it could be a settings file somewhere in the FCK sources. Other than that, could be some routing issue with CI. Good luck.
#6

[eluser]Vang[/eluser]
I got it working after all. The problem was that i had fck in /application/plugins/fck. I should allow the /application/ directory in .htaccess which is not a good practice after all. The sollution was to put fck in the same folder as my css and javascript files and allow it through .htaccess
#7

[eluser]flyer[/eluser]
Updated my initial post's code. There was a problem with the basepath not being set if data was given as an array if $data['basepath'] was not provided. Should work fine now.
#8

[eluser]nirbhab[/eluser]
thank you dear, it works perfectly fine. i just simply copied the codes and it worked very well.
#9

[eluser]EugeneS[/eluser]
Please change in initial post these rows:

Code:
if( isset($data['value']) )
                $fckeditor->Value = html_entity_decode($data['value']);
            if( isset($data['basepath']) )
                $fckeditor->Basepath = $data['basepath'];
#10

[eluser]flyer[/eluser]
done, I copy and paste far too much! Wink

ps. just a thought, not entirely sure whether the html_entity_decode function needs to be applied to $value or $data['value'] in this function either. What does anyone think?




Theme © iAndrew 2016 - Forum software by © MyBB