用户
 找回密码
 入住 CI 中国社区
搜索
查看: 9007|回复: 9
收起左侧

[讨论/交流] 很简单的集成ckeditor和ckfinder,调用非常简单

[复制链接]
发表于 2012-8-29 17:02:22 | 显示全部楼层 |阅读模式
之前找了些集成ckeditor资料,自己总结了一份,发上来共享下。
先说调用,只需在需要的控制器内使用$this->load->helper('ckeditor');
然后就可以在view中使用<?php echo ckeditor('textareaname',array(0,1,2,3),'初始化显示文字');?>;
就是这么简单。。。。
下面说下都需要哪些操作
1、下载官方的ckeditor和ckfinder文件,可以把里面没用的东西删除,进行瘦身,这个网上搜下很多的,把下载好的目录放到项目中,位置自己选定。
2、将两个目录中各自的类文件拷贝到application/library/目录下并重新命名,修改成CI支持的类文件名称。

3、在CI的application/config/config.php文件末尾添加ckeditor和ckfinder的配置信息,代码如下:
PHP复制代码
 
/*
 * ckeditor和ckfinder的配置选项
 * ckeditorPath是ckeditor目录的位置
 * ckfinderPath是ckfinder目录的位置
 * editor_config为ckeditor的标题配置文件
 */

$config['ckeditorPath'] = 'public/ckeditor/';
$config['ckfinderPath'] = 'public/ckfinder/';
$config['editor_config'] = array(
            'language'=> 'zh-cn',           //默认语言
            'skin'    => 'kama',            //皮肤方案kama(默认) office2003 v2 三种皮肤
            'resize_enable' => 'false'      //是否可拖动改变尺寸
);
$config['toolbar_Full'] = array(
    array('Source','-','Save','NewPage','Preview','-','Templates'),     //源码,保存,新建,预览,模版
    array('TextColor','BGColor'),
    array('Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'),
    array('Styles','Format','Font','FontSize'),
    array('Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'),
    array('Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'),
    array('Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'),
    array('Bold','Italic','Underline','Strike','-','Subscript','Superscript'),
    array('NumberedList','BulletedList','-','Outdent','Indent','Blockquote'),
    array('JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'),
    array('Link','Unlink','Anchor'),
);
复制代码

4、在application/helpers/目录下创建editor_helper.php文件,代码如下:
PHP复制代码
 
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

if( ! function_exists('ckeditor')){
    /*
     *  name是创建的textarea的name名字
     *  type是工具栏调用功能,如果为空的话调用全部
     *  content是编辑器默认显示的内容,默认为空
     */

    function ckeditor($name='content',$type = array(0,3,2,1),$content = ''){
        $CI =& get_instance();  
        $config = $CI->config->config;      //取得配置文件中的配置信息
        if(!empty($config['ckeditorPath'])){
            $ckeditorPath = base_url().$config['ckeditorPath'];     //ckeditor路径
        }
        if(!empty($config['ckfinderPath'])){
            $ckfinderPath = base_url().$config['ckfinderPath'];     //ckfinder路径
        }
        if(!empty($config['editor_config'])){
            $editor_config = $config['editor_config'];              //配置文件
        }else{
            $editor_config = '';
        }
        if(!empty($config['toolbar_Full'])){                        //头部功能建设置
            if(is_array($type) && count($type) > 0){
                 foreach($type as $k => $v){
                    $editor_config['toolbar_Full'][] = $config['toolbar_Full'][$v];
                 }
            }else{
                $editor_config['toolbar_Full'] = '';
                $editor_config['toolbar'] = 'Full';
            }
        }else{
            $editor_config['toolbar'] = 'Full';
        }
        $CI->load->library('ckeditor');                     //导入application/library/ckeditor.php文件
        $CI->load->library('ckfinder');                     //导入application/library/ckfinder.php文件
        $CI->ckfinder->BasePath = $ckfinderPath;            //初始化ckfinder路径
        $CI->ckeditor->returnOutput = true;                 //设置返回输出
        $CI->ckeditor->basePath = $ckeditorPath;            //初始化ckeditor路径
       
        $CI->ckfinder->SetupCKEditor($CI->ckeditor,$ckfinderPath);  //ckfinder中自带集成ckeditor函数
        $ckeditor = $CI->ckeditor->editor($name,$content,$editor_config);   //生成ckeditor编辑器,带上传文件功能
        return $ckeditor;
    }
}
?>
 
 
复制代码

5、修改ckfinder的上传文件路径,这里他本身引用的好像是决定路径。。刚在论坛里看到有个朋友已经解决了这个问题,这里就引用他的方法。
修改ckfinder目录下的config.php文件,找到$baseUrl这行代码,将其注释掉,并更换成代码:
PHP复制代码
 
$right_ulr = explode('/',$_SERVER['PHP_SELF']);
$len = count($right_ulr);
for($i=0;$i<6;$i++)
    unset($right_ulr[count($right_ulr)-1]);
//var_dump($right_ulr);
$baseUrl = implode('/', $right_ulr).'/public/upload/';
 
复制代码

6、ckfinder上传文件后采用的是原文件名,修改方法是在网上找的,
打开/ckfinder/core /connector/php/php5/CommandHandler/FileUpload.php 找到
$sUnsafeFileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding(CKFinder_Connector_Utils_Misc::mbBasename($uploadedFile['name']));

后面加上
$sExtension = CKFinder_Connector_Utils_FileSystem::getExtension($sUnsafeFileName);
$sUnsafeFileName=date('YmdHis').'.'.$sExtension;
7、上传权限问题,找到/ckfinder/config.php文件,第一个函数CheckAuthentication()是用来判断上传文件权限的,自己根据业务逻辑进行权限判断,这个函数返回true,就表示能够上传,返回false则不能上传文件。测试时可将其直接返回true。
PHP复制代码
 
function CheckAuthentication(){
                 return ture;
}
 
复制代码



注:cifinder的商业使用是需要购买版权的,详细请查看ckfinder官网

本帖被以下淘专辑推荐:

 楼主| 发表于 2012-8-29 17:15:04 | 显示全部楼层
本帖最后由 liya22315 于 2012-9-20 18:55 编辑

调用的方法和参数刚忘写了,这里给补上
控制器中
PHP复制代码
 
function index(){
$this->load->helper('ckeditor');
$this->load->view('index');
}
 
复制代码

view视图中使用
PHP复制代码
 
<?php echo ckeditor('textareaname',array(0,1,2,3),'初始化显示文字');?>
 
复制代码

第一个参数是生成的textarea的name名称,用于提交时获取其值。
第二个参数是配置文件中toolbar_Full这个数组里面的子类数组键值。
第三个是初始化的时候显示的内容,不过初始化后焦点获取到那了 值还显示,这个还没弄清楚怎么让初始化的值消失。。。欢迎拍砖
发表于 2012-8-29 22:51:05 | 显示全部楼层
要给钱的谁用啊。。。
 楼主| 发表于 2012-8-30 09:00:19 | 显示全部楼层
跟屁虫 发表于 2012-8-29 22:51
要给钱的谁用啊。。。

。。。。不给钱也能用,只是少了一些功能而已。。。 他们官网上有说明
发表于 2012-9-18 09:23:58 | 显示全部楼层
帮LZ勘误1
PHP复制代码
 function index(){
$this->load->helper('ckeditor');
$this->load->view('index');
}
 
复制代码

2、4、在application/helpers/目录下创建editor_helper.php文件,代码如下:
如果是$this->load->helper('ckeditor'),创建的是ckeditor_helper.php文件

PHP复制代码
 
 $right_ulr = explode('/',$_SERVER['PHP_SELF']);
$len = count($right_ulr);
for($i=0;$i<6;$i++)
    unset($right_ulr[count($right_ulr)-1]);
//var_dump($right_ulr);
$baseUrl = implode('/', $right_ulr).'/public/upload/';
 
复制代码


请问LZ,这步不就是为了让$baseUrl的路劲一直在根目录下的/public/upload,为什么不直接定义$baseUrl = '/public/upload/';
 楼主| 发表于 2012-9-18 20:29:22 | 显示全部楼层
★♂翼☆ 发表于 2012-9-18 09:23
帮LZ勘误1

2、4、在application/helpers/目录下创建editor_helper.php文件,代码如下:

ckfinder默认的上传路径是相对于根目录的绝对路径,并不是网站的根目录。要是直接指定$baseUrl='/public/upload/';这样的话在windows下会跑到你存放盘符下面,如c:\public\upload,在linux下面也会直接跑到根目录下的/public/upload。上面的设置是存放到网站的根目录的。
发表于 2012-9-20 10:46:48 | 显示全部楼层
liya22315 发表于 2012-9-18 20:29
ckfinder默认的上传路径是相对于根目录的绝对路径,并不是网站的根目录。要是直接指定$baseUrl='/public/ ...

for($i=0;$i<6;$i++)
循环六遍就能保证是在相对于根目录的绝对路劲?
 楼主| 发表于 2012-9-20 15:46:57 | 显示全部楼层
本帖最后由 liya22315 于 2012-9-20 19:08 编辑
★♂翼☆ 发表于 2012-9-20 10:46
for($i=0;$i

http://codeigniter.org.cn/forums ... viewthread&tid=8735
引自这里的地板回复面。。。。 你看下。。
二:config.php中的上传路径
一般而言,我们的程序都是本地测试后上传到服务器,本地的上传路径是已知的,而服务器可就不一定知道了,而且即使知道,一旦程序的路径有所变动,你的CKfinder还要重新配置,所以,一般而言,我都是用以下代码替代config.php中
$baseUrl那一行。

//var_dump($_SERVER['PHP_SELF']);
$right_ulr = explode('/',$_SERVER['PHP_SELF']);
$len = count($right_ulr);
for($i=0;$i<6;$i++)
    unset($right_ulr[count($right_ulr)-1]);
//var_dump($right_ulr);
$baseUrl = implode('/', $right_ulr).'/upload/pic/';

解释下代码
在for语句中$i<6中的6是指
ckfinder/core/connector/php中的connector.php
离网站根目录的目录数量,这个得依据你的ckfinder在网站逻辑根目录的路径决定
循环1:去除connector.php
循环 2:去除php
循环 3:去除connector
循环 4:去除core
循环 5:去除ckfinder
循环 6:去除ckfinder所属的plugins目录(这里看你自己的网站目录咯)

 楼主| 发表于 2012-9-20 19:09:18 | 显示全部楼层
@策策啃鸡腿
发表于 2014-5-27 11:22:40 | 显示全部楼层
显示编辑器,填写值,怎么获取呢?

本版积分规则