国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP開發(fā) How to integrate kindeditor extension with YII view

How to integrate kindeditor extension with YII view

Dec 30, 2016 pm 04:33 PM

本文實例講述了YII視圖整合kindeditor擴(kuò)展的方法。分享給大家供大家參考,具體如下:

比較喜歡用kindeditor,YII上的版本比較舊,所以自己重新整了個擴(kuò)展
先在protected\extensions下創(chuàng)建KEditor文件夾用來放文件,keSource里放kindeditor的源文件,然后建三個類KEditor、KEditorManage和KEditorUpload,KEditor是擴(kuò)展的主文件,KEditorManage是用來瀏覽服務(wù)器文件的,KEditorUpload是用來示例接收上傳文件的,

KEditor代碼

<?php
class KEditor extends CWidget{
  /*
   * TEXTAREA輸入框的屬性,保證js調(diào)用KE失敗時,文本框的樣式。
   */
  public $textareaOptions=array();
  /*
   * 編輯器屬性集。
   */
  public $properties=array();
  /*
   * TEXTAREA輸入框的name,必須設(shè)置。
   * 數(shù)據(jù)類型:String
   */
  public $name;
  /*
   * TEXTAREA的id,可為空
   */
  public $id;
  public $model;
  public $baseUrl;
  public static function getUploadPath(){
    $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.&#39;keSource&#39;;
    if(isset(Yii::app()->params->uploadPath)){
      return Yii::getPathOfAlias(&#39;webroot&#39;).str_replace(
                &#39;/&#39;,DIRECTORY_SEPARATOR,
                Yii::app()->params->
                uploadPath);
    }
    return Yii::app()->getAssetmanager()
        ->getPublishedPath($dir).DIRECTORY_SEPARATOR.&#39;upload&#39;;
  }
  public static function getUploadUrl(){
    $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.&#39;keSource&#39;;
    if(isset(Yii::app()->params->uploadPath)){
      return Yii::app()->baseUrl.Yii::app()->params->uploadPath;
    }
    return Yii::app()->getAssetManager()->publish($dir).&#39;/upload&#39;;
  }
  public function init(){
    if($this->name===null)
      throw new CException(Yii::t(&#39;zii&#39;,&#39;The id property cannot be empty.&#39;));
    $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.&#39;keSource&#39;;
    $this->baseUrl=Yii::app()->getAssetManager()->publish($dir);
    $cs=Yii::app()->getClientScript();
    $cs->registerCssFile($this->baseUrl.&#39;/themes/default/default.css&#39;);
    if(YII_DEBUG) $cs->registerScriptFile($this->baseUrl.&#39;/kindeditor.js&#39;);
    else $cs->registerScriptFile($this->baseUrl.&#39;/kindeditor-min.js&#39;);
  }
  public function run(){
    $cs=Yii::app()->getClientScript();
    $textAreaOptions=$this->gettextareaOptions();
    $textAreaOptions[&#39;name&#39;]=CHtml::resolveName($this->model,$this->name);
    $this->id=$textAreaOptions[&#39;id&#39;]=CHtml::getIdByName($textAreaOptions[&#39;name&#39;]);
    echo CHtml::activeTextArea($this->model,$this->name,$textAreaOptions);
    $properties_string = CJavaScript::encode($this->getKeProperties());
    $js=<<<EOF
KindEditor.ready(function(K) {
  var editor_$this->id = K.create(&#39;#$this->id&#39;,
$properties_string
  );
});
EOF;
    $cs->registerScript(&#39;KE&#39;.$this->name,$js,CClientScript::POS_HEAD);
  }
  public function gettextareaOptions(){
    //允許獲取的屬性
    $allowParams=array(&#39;rows&#39;,&#39;cols&#39;,&#39;style&#39;);
    //準(zhǔn)備返回的屬性數(shù)組
    $params=array();
    foreach($allowParams as $key){
      if(isset($this->textareaOptions[$key]))
        $params[$key]=$this->textareaOptions[$key];
    }
    $params[&#39;name&#39;]=$params[&#39;id&#39;]=$this->name;
    return $params;
  }
  public function getKeProperties(){
    $properties_key=array(
      &#39;width&#39;,
      &#39;height&#39;,
      &#39;minWidth&#39;,
      &#39;minHeight&#39;,
      &#39;items&#39;,
      &#39;noDisableItems&#39;,
      &#39;filterMode&#39;,
      &#39;htmlTags&#39;,
      &#39;wellFormatMode&#39;,
      &#39;resizeType&#39;,
      &#39;themeType&#39;,
      &#39;langType&#39;,
      &#39;designMode&#39;,
      &#39;fullscreenMode&#39;,
      &#39;basePath&#39;,
      &#39;themesPath&#39;,
      &#39;pluginsPath&#39;,
      &#39;langPath&#39;,
      &#39;minChangeSize&#39;,
      &#39;urlType&#39;,
      &#39;newlineTag&#39;,
      &#39;pasteType&#39;,
      &#39;dialogAlignType&#39;,
      &#39;shadowMode&#39;,
      &#39;useContextmenu&#39;,
      &#39;syncType&#39;,
      &#39;indentChar&#39;,
      &#39;cssPath&#39;,
      &#39;cssData&#39;,
      &#39;bodyClass&#39;,
      &#39;colorTable&#39;,
      &#39;afterCreate&#39;,
      &#39;afterChange&#39;,
      &#39;afterTab&#39;,
      &#39;afterFocus&#39;,
      &#39;afterBlur&#39;,
      &#39;afterUpload&#39;,
      &#39;uploadJson&#39;,
      &#39;fileManagerJson&#39;,
      &#39;allowPreviewEmoticons&#39;,
      &#39;allowImageUpload&#39;,
      &#39;allowFlashUpload&#39;,
      &#39;allowMediaUpload&#39;,
      &#39;allowFileUpload&#39;,
      &#39;allowFileManager&#39;,
      &#39;fontSizeTable&#39;,
      &#39;imageTabIndex&#39;,
      &#39;formatUploadUrl&#39;,
      &#39;fullscreenShortcut&#39;,
      &#39;extraFileUploadParams&#39;,
    );
    //準(zhǔn)備返回的屬性數(shù)組
    $params=array();
    foreach($properties_key as $key){
      if(isset($this->properties[$key]))
        $params[$key]=$this->properties[$key];
    }
    return $params;
  }
}

KEditorManage代碼

<?php
class KEditorManage extends CAction{
  public function run(){
    Yii::import(&#39;ext.KEditor.KEditor&#39;);
    $root_path=KEditor::getUploadPath().&#39;/&#39;;
    $root_url=KEditor::getUploadUrl().&#39;/&#39;;
    //圖片擴(kuò)展名
    $ext_arr = array(&#39;gif&#39;, &#39;jpg&#39;, &#39;jpeg&#39;, &#39;png&#39;, &#39;bmp&#39;);
    //目錄名
    $dir_name = empty($_GET[&#39;dir&#39;]) ? &#39;&#39; : trim($_GET[&#39;dir&#39;]);
    if (!in_array($dir_name, array(&#39;&#39;, &#39;image&#39;, &#39;flash&#39;, &#39;media&#39;, &#39;file&#39;))) {
      echo "Invalid Directory name.";
      exit;
    }
    if ($dir_name !== &#39;&#39;) {
      $root_path .= $dir_name . "/";
      $root_url .= $dir_name . "/";
      if (!file_exists($root_path)) {
        mkdir($root_path);
      }
    }
    //根據(jù)path參數(shù),設(shè)置各路徑和URL
    if (empty($_GET[&#39;path&#39;])) {
      $current_path = realpath($root_path) . &#39;/&#39;;
      $current_url = $root_url;
      $current_dir_path = &#39;&#39;;
      $moveup_dir_path = &#39;&#39;;
    } else {
      $current_path = realpath($root_path) . &#39;/&#39; . $_GET[&#39;path&#39;];
      $current_url = $root_url . $_GET[&#39;path&#39;];
      $current_dir_path = $_GET[&#39;path&#39;];
      $moveup_dir_path = preg_replace(&#39;/(.*?)[^\/]+\/$/&#39;, &#39;$1&#39;, $current_dir_path);
    }
    echo realpath($root_path);
    //排序形式,name or size or type
    $order = empty($_GET[&#39;order&#39;]) ? &#39;name&#39; : strtolower($_GET[&#39;order&#39;]);
    //不允許使用..移動到上一級目錄
    if (preg_match(&#39;/\.\./&#39;, $current_path)) {
      echo &#39;Access is not allowed.&#39;;
      exit;
    }
    //最后一個字符不是/
    if (!preg_match(&#39;/\/$/&#39;, $current_path)) {
      echo &#39;Parameter is not valid.&#39;;
      exit;
    }
    //目錄不存在或不是目錄
    if (!file_exists($current_path) || !is_dir($current_path)) {
      echo &#39;Directory does not exist.&#39;;
      exit;
    }
    //遍歷目錄取得文件信息
    $file_list = array();
    $handle = new DirectoryIterator($current_path);
    $i=0;
    foreach($handle as $file){
      if($file->isDot()) continue;
      if($file->isDir()){
        $file_list[$i][&#39;is_dir&#39;] = true; //是否文件夾
        $file_list[$i][&#39;has_file&#39;] = (count(scandir($file->getPath())) > 2); //文件夾是否包含文件
        $file_list[$i][&#39;filesize&#39;] = 0; //文件大小
        $file_list[$i][&#39;is_photo&#39;] = false; //是否圖片
        $file_list[$i][&#39;filetype&#39;] = &#39;&#39;; //文件類別,用擴(kuò)展名判斷
      }else{
        $file_list[$i][&#39;is_dir&#39;] = false;
        $file_list[$i][&#39;has_file&#39;] = false;
        $file_list[$i][&#39;filesize&#39;] = $file->getSize();
        $file_list[$i][&#39;dir_path&#39;] = &#39;&#39;;
        $file_ext = $file->getExtension();
        $file_list[$i][&#39;is_photo&#39;] = in_array($file_ext, $ext_arr);
        $file_list[$i][&#39;filetype&#39;] = $file_ext;
      }
      $file_list[$i][&#39;filename&#39;] = $file->getFilename(); //文件名,包含擴(kuò)展名
      $file_list[$i][&#39;datetime&#39;] = date(&#39;Y-m-d H:i:s&#39;, $file->getMTime());
      $i++;
    }
    usort($file_list, array($this,&#39;cmp_func&#39;));
    $result = array();
    //相對于根目錄的上一級目錄
    $result[&#39;moveup_dir_path&#39;] = $moveup_dir_path;
    //相對于根目錄的當(dāng)前目錄
    $result[&#39;current_dir_path&#39;] = $current_dir_path;
    //當(dāng)前目錄的URL
    $result[&#39;current_url&#39;] = $current_url;
    //文件數(shù)
    $result[&#39;total_count&#39;] = count($file_list);
    //文件列表數(shù)組
    $result[&#39;file_list&#39;] = $file_list;
    //輸出JSON字符串
    header(&#39;Content-type: application/json; charset=UTF-8&#39;);
    echo CJSON::encode($result);
    exit;
  }
  //排序
  public function cmp_func($a, $b) {
    global $order;
    if ($a[&#39;is_dir&#39;] && !$b[&#39;is_dir&#39;]) {
      return -1;
    } else if (!$a[&#39;is_dir&#39;] && $b[&#39;is_dir&#39;]) {
      return 1;
    } else {
      if ($order == &#39;size&#39;) {
        if ($a[&#39;filesize&#39;] > $b[&#39;filesize&#39;]) {
          return 1;
        } else if ($a[&#39;filesize&#39;] < $b[&#39;filesize&#39;]) {
          return -1;
        } else {
          return 0;
        }
      } else if ($order == &#39;type&#39;) {
        return strcmp($a[&#39;filetype&#39;], $b[&#39;filetype&#39;]);
      } else {
        return strcmp($a[&#39;filename&#39;], $b[&#39;filename&#39;]);
      }
    }
  }
}
?>

KEditorUpload代碼

<?php
class KEditorUpload extends CAction{
  public function run(){
    $dir=isset($_GET[&#39;dir&#39;])?trim($_GET[&#39;dir&#39;]):&#39;file&#39;;
    $ext_arr = array(
      &#39;image&#39; => array(&#39;gif&#39;, &#39;jpg&#39;, &#39;jpeg&#39;, &#39;png&#39;, &#39;bmp&#39;),
      &#39;flash&#39; => array(&#39;swf&#39;, &#39;flv&#39;),
      &#39;media&#39; => array(&#39;swf&#39;, &#39;flv&#39;, &#39;mp3&#39;, &#39;wav&#39;, &#39;wma&#39;, &#39;wmv&#39;, &#39;mid&#39;, &#39;avi&#39;, &#39;mpg&#39;, &#39;asf&#39;, &#39;rm&#39;, &#39;rmvb&#39;),
      &#39;file&#39; => array(&#39;doc&#39;, &#39;docx&#39;, &#39;xls&#39;, &#39;xlsx&#39;, &#39;ppt&#39;, &#39;htm&#39;, &#39;html&#39;, &#39;txt&#39;, &#39;zip&#39;, &#39;rar&#39;, &#39;gz&#39;, &#39;bz2&#39;),
    );
    if(empty($ext_arr[$dir])){
      echo CJSON::encode(array(&#39;error&#39;=>1,&#39;message&#39;=>&#39;目錄名不正確。&#39;));
      exit;
    }
    $originalurl=&#39;&#39;;
    $filename=&#39;&#39;;
    $date=date(&#39;Ymd&#39;);
    $id=0;
    $max_size=2097152; //2MBs
    $upload_image=CUploadedFile::getInstanceByName(&#39;imgFile&#39;);
    Yii::import(&#39;ext.KEditor.KEditor&#39;);
    $upload_dir=KEditor::getUploadPath().&#39;/&#39;.$dir;
    if(!file_exists($upload_dir)) mkdir($upload_dir);
    $upload_dir=$upload_dir.&#39;/&#39;.$date;
    if(!file_exists($upload_dir)) mkdir($upload_dir);
    $upload_url=KEditor::getUploadUrl().&#39;/&#39;.$dir.&#39;/&#39;.$date;
    if(is_object($upload_image) && get_class($upload_image)===&#39;CUploadedFile&#39;){
      if($upload_image->size > $max_size){
        echo CJSON::encode(array(&#39;error&#39;=>1,&#39;message&#39;=>&#39;上傳文件大小超過限制。&#39;));
        exit;
      }
      //新文件名
      $filename=date("YmdHis").&#39;_&#39;.rand(10000, 99999);
      $ext=$upload_image->extensionName;
      if(in_array($ext, $ext_arr[$dir]) === false){
        echo CJSON::encode(array(&#39;error&#39;=>1,&#39;message&#39;=>"上傳文件擴(kuò)展名是不允許的擴(kuò)展名。\n只允許".implode(&#39;,&#39;,$ext_arr[$dir]).&#39;格式。&#39;));
        exit;
      }
      $uploadfile=$upload_dir.&#39;/&#39;.$filename.&#39;.&#39;.$ext;
      $originalurl=$upload_url.&#39;/&#39;.$filename.&#39;.&#39;.$ext;
      $upload_image->saveAs($uploadfile);
      echo CJSON::encode(array(&#39;error&#39;=>0,&#39;url&#39;=>$originalurl));
    }else{
      echo CJSON::encode(array(&#39;error&#39;=>1,&#39;message&#39;=>&#39;未知錯誤&#39;));
    }
  }
}

配置config/main.php文件,設(shè)置上傳文件存放位置

&#39;params&#39;=>array(
    // this is used in contact page
    &#39;adminEmail&#39;=>&#39;webmaster@example.com&#39;,
    &#39;uploadPath&#39;=>&#39;/upload&#39;, //添加這句,upload為存放文件文件夾的名字,自己定義,這里是放在根目錄的upload文件夾

設(shè)置接收文件和瀏覽服務(wù)器文件的action

public function actions()
{
  return array(
    //在actions下的return array添加下面兩句,沒有actions的話自己添加
    &#39;upload&#39;=>array(&#39;class&#39;=>&#39;application.extensions.KEditor.KEditorUpload&#39;),
    &#39;manageJson&#39;=>array(&#39;class&#39;=>&#39;application.extensions.KEditor.KEditorManage&#39;),
  );
}

在視圖里面使用

<?php $this->widget(&#39;ext.KEditor.KEditor&#39;,array(
  &#39;model&#39;=>$model, //傳入form model
  &#39;name&#39;=>&#39;content&#39;, //設(shè)置name
  &#39;properties&#39;=>array(
    //設(shè)置接收文件上傳的action
    &#39;uploadJson&#39;=>&#39;/admin/default/upload&#39;,
    //設(shè)置瀏覽服務(wù)器文件的action,這兩個就是上面配置在/admin/default的
    &#39;fileManagerJson&#39;=>&#39;/admin/default/manageJson&#39;,
    &#39;newlineTag&#39;=>&#39;br&#39;,
    &#39;allowFileManager&#39;=>true,
    //傳值前加js:來標(biāo)記這些是js代碼
    &#39;afterCreate&#39;=>"js:function() {
        K(&#39;#ChapterForm_all_len&#39;).val(this.count());
        K(&#39;#ChapterForm_word_len&#39;).val(this.count(&#39;text&#39;));
      }",
    &#39;afterChange&#39;=>"js:function() {
        K(&#39;#ChapterForm_all_len&#39;).val(this.count());
        K(&#39;#ChapterForm_word_len&#39;).val(this.count(&#39;text&#39;));
      }",
  ),
  &#39;textareaOptions&#39;=>array(
    &#39;style&#39;=>&#39;width:98%;height:400px;&#39;,
  )
));
?>

textareaOptions用來設(shè)置textarea的大小和樣式,僅支持rows、cols和style
properties的各項跟js設(shè)置kindeditor的是一樣的,上面的設(shè)置與下面用js設(shè)置的是一致,kindeditor原來有的項都可以設(shè)置

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

? ?

希望本文所述對大家基于Yii框架的PHP程序設(shè)計有所幫助。

更多YII視圖整合kindeditor擴(kuò)展的方法相關(guān)文章請關(guān)注PHP中文網(wǎng)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)