侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

YII视图整合kindeditor扩展的方法

框架(架构)  /  管理员 发布于 7年前   181

本文实例讲述了YII视图整合kindeditor扩展的方法。分享给大家供大家参考,具体如下:

比较喜欢用kindeditor,YII上的版本比较旧,所以自己重新整了个扩展
先在protected\extensions下创建KEditor文件夹用来放文件,keSource里放kindeditor的源文件,然后建三个类KEditor、KEditorManage和KEditorUpload,KEditor是扩展的主文件,KEditorManage是用来浏览服务器文件的,KEditorUpload是用来示例接收上传文件的,

KEditor代码

params->uploadPath)){      return Yii::getPathOfAlias('webroot').str_replace(    '/',DIRECTORY_SEPARATOR,    Yii::app()->params->    uploadPath);    }    return Yii::app()->getAssetmanager()        ->getPublishedPath($dir).DIRECTORY_SEPARATOR.'upload';  }  public static function getUploadUrl(){    $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'keSource';    if(isset(Yii::app()->params->uploadPath)){      return Yii::app()->baseUrl.Yii::app()->params->uploadPath;    }    return Yii::app()->getAssetManager()->publish($dir).'/upload';  }  public function init(){    if($this->name===null)      throw new CException(Yii::t('zii','The id property cannot be empty.'));    $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'keSource';    $this->baseUrl=Yii::app()->getAssetManager()->publish($dir);    $cs=Yii::app()->getClientScript();    $cs->registerCssFile($this->baseUrl.'/themes/default/default.css');    if(YII_DEBUG) $cs->registerScriptFile($this->baseUrl.'/kindeditor.js');    else $cs->registerScriptFile($this->baseUrl.'/kindeditor-min.js');  }  public function run(){    $cs=Yii::app()->getClientScript();    $textAreaOptions=$this->gettextareaOptions();    $textAreaOptions['name']=CHtml::resolveName($this->model,$this->name);    $this->id=$textAreaOptions['id']=CHtml::getIdByName($textAreaOptions['name']);    echo CHtml::activeTextArea($this->model,$this->name,$textAreaOptions);    $properties_string = CJavaScript::encode($this->getKeProperties());    $js=<<id = K.create('#$this->id',$properties_string  );});EOF;    $cs->registerScript('KE'.$this->name,$js,CClientScript::POS_HEAD);  }  public function gettextareaOptions(){    //允许获取的属性    $allowParams=array('rows','cols','style');    //准备返回的属性数组    $params=array();    foreach($allowParams as $key){      if(isset($this->textareaOptions[$key]))        $params[$key]=$this->textareaOptions[$key];    }    $params['name']=$params['id']=$this->name;    return $params;  }  public function getKeProperties(){    $properties_key=array(      'width',      'height',      'minWidth',      'minHeight',      'items',      'noDisableItems',      'filterMode',      'htmlTags',      'wellFormatMode',      'resizeType',      'themeType',      'langType',      'designMode',      'fullscreenMode',      'basePath',      'themesPath',      'pluginsPath',      'langPath',      'minChangeSize',      'urlType',      'newlineTag',      'pasteType',      'dialogAlignType',      'shadowMode',      'useContextmenu',      'syncType',      'indentChar',      'cssPath',      'cssData',      'bodyClass',      'colorTable',      'afterCreate',      'afterChange',      'afterTab',      'afterFocus',      'afterBlur',      'afterUpload',      'uploadJson',      'fileManagerJson',      'allowPreviewEmoticons',      'allowImageUpload',      'allowFlashUpload',      'allowMediaUpload',      'allowFileUpload',      'allowFileManager',      'fontSizeTable',      'imageTabIndex',      'formatUploadUrl',      'fullscreenShortcut',      'extraFileUploadParams',    );    //准备返回的属性数组    $params=array();    foreach($properties_key as $key){      if(isset($this->properties[$key]))        $params[$key]=$this->properties[$key];    }    return $params;  }}

KEditorManage代码

isDot()) continue;      if($file->isDir()){        $file_list[$i]['is_dir'] = true; //是否文件夹        $file_list[$i]['has_file'] = (count(scandir($file->getPath())) > 2); //文件夹是否包含文件        $file_list[$i]['filesize'] = 0; //文件大小        $file_list[$i]['is_photo'] = false; //是否图片        $file_list[$i]['filetype'] = ''; //文件类别,用扩展名判断      }else{        $file_list[$i]['is_dir'] = false;        $file_list[$i]['has_file'] = false;        $file_list[$i]['filesize'] = $file->getSize();        $file_list[$i]['dir_path'] = '';        $file_ext = $file->getExtension();        $file_list[$i]['is_photo'] = in_array($file_ext, $ext_arr);        $file_list[$i]['filetype'] = $file_ext;      }      $file_list[$i]['filename'] = $file->getFilename(); //文件名,包含扩展名      $file_list[$i]['datetime'] = date('Y-m-d H:i:s', $file->getMTime());      $i++;    }    usort($file_list, array($this,'cmp_func'));    $result = array();    //相对于根目录的上一级目录    $result['moveup_dir_path'] = $moveup_dir_path;    //相对于根目录的当前目录    $result['current_dir_path'] = $current_dir_path;    //当前目录的URL    $result['current_url'] = $current_url;    //文件数    $result['total_count'] = count($file_list);    //文件列表数组    $result['file_list'] = $file_list;    //输出JSON字符串    header('Content-type: application/json; ');    echo CJSON::encode($result);    exit;  }  //排序  public function cmp_func($a, $b) {    global $order;    if ($a['is_dir'] && !$b['is_dir']) {      return -1;    } else if (!$a['is_dir'] && $b['is_dir']) {      return 1;    } else {      if ($order == 'size') {        if ($a['filesize'] > $b['filesize']) {          return 1;        } else if ($a['filesize'] < $b['filesize']) {          return -1;        } else {          return 0;        }      } else if ($order == 'type') {        return strcmp($a['filetype'], $b['filetype']);      } else {        return strcmp($a['filename'], $b['filename']);      }    }  }}?>

KEditorUpload代码

 array('gif', 'jpg', 'jpeg', 'png', 'bmp'),      'flash' => array('swf', 'flv'),      'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),      'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),    );    if(empty($ext_arr[$dir])){      echo CJSON::encode(array('error'=>1,'message'=>'目录名不正确。'));      exit;    }    $originalurl='';    $filename='';    $date=date('Ymd');    $id=0;    $max_size=2097152; //2MBs    $upload_image=CUploadedFile::getInstanceByName('imgFile');    Yii::import('ext.KEditor.KEditor');    $upload_dir=KEditor::getUploadPath().'/'.$dir;    if(!file_exists($upload_dir)) mkdir($upload_dir);    $upload_dir=$upload_dir.'/'.$date;    if(!file_exists($upload_dir)) mkdir($upload_dir);    $upload_url=KEditor::getUploadUrl().'/'.$dir.'/'.$date;    if(is_object($upload_image) && get_class($upload_image)==='CUploadedFile'){      if($upload_image->size > $max_size){        echo CJSON::encode(array('error'=>1,'message'=>'上传文件大小超过限制。'));        exit;      }      //新文件名      $filename=date("YmdHis").'_'.rand(10000, 99999);      $ext=$upload_image->extensionName;      if(in_array($ext, $ext_arr[$dir]) === false){        echo CJSON::encode(array('error'=>1,'message'=>"上传文件扩展名是不允许的扩展名。\n只允许".implode(',',$ext_arr[$dir]).'格式。'));        exit;      }      $uploadfile=$upload_dir.'/'.$filename.'.'.$ext;      $originalurl=$upload_url.'/'.$filename.'.'.$ext;      $upload_image->saveAs($uploadfile);      echo CJSON::encode(array('error'=>0,'url'=>$originalurl));    }else{      echo CJSON::encode(array('error'=>1,'message'=>'未知错误'));    }  }}

配置config/main.php文件,设置上传文件存放位置

'params'=>array(    // this is used in contact page    'adminEmail'=>'[email protected]',    'uploadPath'=>'/upload', //添加这句,upload为存放文件文件夹的名字,自己定义,这里是放在根目录的upload文件夹

设置接收文件和浏览服务器文件的action

public function actions(){  return array(    //在actions下的return array添加下面两句,没有actions的话自己添加    'upload'=>array('class'=>'application.extensions.KEditor.KEditorUpload'),    'manageJson'=>array('class'=>'application.extensions.KEditor.KEditorManage'),  );}

在视图里面使用

widget('ext.KEditor.KEditor',array(  'model'=>$model, //传入form model  'name'=>'content', //设置name  'properties'=>array(    //设置接收文件上传的action    'uploadJson'=>'/admin/default/upload',    //设置浏览服务器文件的action,这两个就是上面配置在/admin/default的    'fileManagerJson'=>'/admin/default/manageJson',    'newlineTag'=>'br',    'allowFileManager'=>true,    //传值前加js:来标记这些是js代码    'afterCreate'=>"js:function() {        K('#ChapterForm_all_len').val(this.count());        K('#ChapterForm_word_len').val(this.count('text'));      }",    'afterChange'=>"js:function() {        K('#ChapterForm_all_len').val(this.count());        K('#ChapterForm_word_len').val(this.count('text'));      }",  ),  'textareaOptions'=>array(    'style'=>'width:98%;height:400px;',  )));?>

textareaOptions用来设置textarea的大小和样式,仅支持rows、cols和style
properties的各项跟js设置kindeditor的是一样的,上面的设置与下面用js设置的是一致,kindeditor原来有的项都可以设置

var editor1 = K.create('#editor_modelname_name', {  uploadJson : "/admin/default/upload",  fileManagerJson : "/admin/default/manageJson",  newlineTag : "br",  allowFileManager : true,  afterCreate : function() {    K('#ChapterForm_all_len').html(this.count());    K('#ChapterForm_word_len').html(this.count('text'));  },  afterChange : function() {    K('#ChapterForm_all_len').html(this.count());    K('#ChapterForm_word_len').html(this.count('text'));  }});

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • Yii框架视图、视图布局、视图数据块操作示例
  • Yii框架的布局文件实例分析
  • PHP的Yii框架中View视图的使用进阶
  • PHP的Yii框架中创建视图和渲染视图的方法详解
  • Yii控制器中操作视图js的方法
  • Yii视图操作之自定义分页实现方法
  • Yii2框架视图(View)操作及Layout的使用方法分析
  • Yii视图CGridView实现操作按钮定义地址示例
  • Yii视图CGridView列表用法实例分析
  • YII框架学习笔记之命名空间、操作响应与视图操作示例
  • Yii框架布局文件的动态切换操作示例


  • 上一条:
    Yii实现的多级联动下拉菜单
    下一条:
    Yii+upload实现AJAX上传图片的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客