<?php

/* ========================================================================
 * $Id: ueditor.php 3443 2017-05-12 12:00:18Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
class onezphp_ueditor extends onezphp{
  function __construct(){
    
  }
  function form_code($arr){
    $this->arr=$arr;
    $html='';
    #只加载一次
    if(!$this->loaded){
      $this->loaded=1;
      $html.='
<script type="text/javascript">
window.UEDITOR_HOME_URL = "'.onez()->homepage().substr($this->path,strlen(ONEZ_ROOT)).'/lib/";
</script>';
      if($arr['full']){
        $html.='<script src="'.$this->url.'/lib/ueditor.config.js"></script>';
      }else{
        $html.='<script src="'.$this->url.'/lib/ueditor.common.js"></script>';
      }
      $html.='<script src="'.$this->url.'/lib/ueditor.all.js?t='.filemtime($this->path.'/lib/ueditor.all.js').'"></script>';
    }
    $cacheKey=md5(onez()->cururl()."\t{$arr['key']}");
    $_ueditor_apiurl=$this->view('cache');
    $html.='
<script type="text/javascript">
onez.editors=onez.editors||{};
var editor_input_'.$arr['key'].' = UE.getEditor(\'input-'.$arr['key'].'\' );
onez.editor_input_'.$arr['key'].'=editor_input_'.$arr['key'].';
onez.editors[\''.$arr['key'].'\']=editor_input_'.$arr['key'].';
</script>';
    $height=$arr['height'];
    !$height && $height='600';
    $formkz=onez('bootstrap_form_kz')->init($arr);
    $formkz->input->attr('tagname','textarea')
               ->attr('class','ueditor')
               ->attr('style','height:'.$height.'px')
    ->is_end=1;
    $formkz->input->html=$arr['value'];
    
    
    $html.=$formkz->code();
    $html.=$this->auto($arr);
    return $html;
  }
  function form_save(&$value,$arr=false){
    global $G;
    !$arr && $arr=$this->arr;
    $value=$_REQUEST[$arr['key']];
    
    $login=onez('login')->init($G['this']->cname,0);
    $G['userid']=(int)$login['id'];
    if($G['userid']){
      $ueditor_cache_hashs=onez()->gp('ueditor_cache_hashs');
      if($ueditor_cache_hashs && is_array($ueditor_cache_hashs)){
        foreach($ueditor_cache_hashs as $hash){
          $G['this']->data()->open('ueditor.cache')->delete("token='$hash' and userid='$G[userid]'");
        }
      }
    }
  }
  function cache(){
    global $G;
    $A=array('version'=>'1.0');
    $login=onez('login')->init($G['this']->cname,0);
    $G['userid']=(int)$login['id'];
    $G['appid']=(int)onez()->gp('appid');
    $G['siteid']=(int)onez()->gp('siteid');
    if($G['userid']){
      $action=onez()->gp('action');
      $hash=md5(onez()->gp('hash'));
      if($action=='get'){
        $G['this']->data()->set('noappid',1);
        $G['this']->data()->open('ueditor.cache')->delete("updatetime<".(time()-86400*7));
        $G['this']->data()->set('noappid',0);
        $A['hash']=$hash;
        $cache=$G['this']->data()->open('ueditor.cache')->one("token='$hash' and userid='$G[userid]'");
        if($cache['content']){
          $A['data']=$cache['content'];
        }
      }elseif($action=='set'){
        $content=$_REQUEST['data'];
        $cache=$G['this']->data()->open('ueditor.cache')->one("token='$hash' and userid='$G[userid]'");
        if($cache){
          $cache=$G['this']->data()->open('ueditor.cache')->update(array('content'=>$content),"id='$cache[id]'");
        }else{
          $G['this']->data()->open('ueditor.cache')->insert(array(
            'userid'=>$G['userid'],
            'token'=>$hash,
            'content'=>$content,
          ));
        }
        $A['tip']='数据已于 '.date('H:i').' 保存';
      }
    }
    onez()->output($A);
  }
}