PHP   发布时间:2019-11-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了YII视图整合kindeditor扩展的方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

KEditor代码

php;"> 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代码

php;"> 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]['datetiR_875_11845@e'] = 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['@R_20_10586@l_count'] = count($file_list); //文件列表数组 $result['file_list'] = $file_list; //输出JSON字符串 header('Content-type: application/json; charset=UTF-8'); 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'] @H_801_13@

KEditorUpload代码

php;"> array('gif','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文件,设置上传文件存放位置

array( // this is used in contact page 'adminEmail'=>'webmaster@example.com','uploadPath'=>'/upload',//添加这句,upload为存放文件文件夹的名字,自己定义,这里是放在根目录的upload文件夹

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

array('class'=>'application.extensions.KEditor.KEditorUpload'),'manageJson'=>array('class'=>'application.extensions.KEditor.KEditoRMANage'),); }

在视图里面使用

php;"> 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原来有的项都可以设置

php;"> 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程序设计有所帮助。

大佬总结

以上是大佬教程为你收集整理的YII视图整合kindeditor扩展的方法全部内容,希望文章能够帮你解决YII视图整合kindeditor扩展的方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:整合视图