<?php
namespace Admin\Controller;
use Think\Controller;

class ArticleController extends BasicController
{
    /*
   * 单页列表
  */
    public function pageList(){
        $model=M('about');
        $list=$model->where(array('is_del'=>'0'))->order('sort asc')->select();
        $this->assign('title','单页列表');
        $this->assign('roleid', session('role'));
        $this->assign('list',$list);
        $this->display();
    }
    
    /*
   * 添加单页
   */
    public  function addPage(){
        $model=M('about');
        if(IS_POST){
            $title = I('post.title');//标题
            $content =  I('post.content');//文章内容
            $name=I('post.name');//英文名称
            if($title==''){
                echo 1;die;
            }
            if($content==''){
                echo 2;die;
            }
            if($name==''){
                echo 3;die;
            }
            $msg=$model->add(array('title'=>$title,'content'=>$content,'name'=>$name));
            if($msg){
                echo 'ok';die;
            }else{
                echo 'error';die;
            }
        }
        $this->assign('title','添加单页');
        $this->display();
    }

    /*
    * 编辑单页
    */
    public function editPage(){
        $model=M('about');
        $id=I('get.id');

        if(IS_POST){
            $title = I('post.title');//标题
            $content =  I('post.content');//文章内容
            $name=I('post.name');//英文名称

            if($title==''){
                echo 1;die;
            }
            if($content==''){
                echo 2;die;
            }
            if($name==''){
                echo 3;die;
            }
            $msg=$model->where(array('id'=>$id))->save(array('title'=>$title,'content'=>$content,'name'=>$name));
            echo 'ok';die;

        }
        $info=$model->where(array('id'=>$id))->find();
        $this->assign('title','编辑单页');
        $this->assign('info',$info);
        $this->assign('action_name','pageList');
        $this->assign('id',$id);
        $this->display();
    }
    /*
     * 删除单页
     */
    public function delPage(){
        $model=M('about');
        $id=I('get.id');
        $data['is_del'] = 1;
        $model->where(array('id' => $id))->save($data);
        echo 'ok';
    }


    //百度编辑器上传图片
    public function Upload()
    {
        $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(WEB_PATH . "/Public/Ueditor/php/config.json")), true);
        $action = $_GET['action'];

        switch ($action) {
            case 'config':
                $result = json_encode($CONFIG);
                break;

            /* 上传图片 */
            case 'uploadimage':
                /* 上传涂鸦 */
            case 'uploadscrawl':
                /* 上传视频 */
            case 'uploadvideo':
                /* 上传文件 */
            case 'uploadfile':
                $upload = new \Think\Upload();
                $upload->maxSize = 3145728;
                $upload->rootPath = './Upload/';
                $upload->savePath = 'article/'; // 设置附件上传（子）目录
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
                $dir = '/Upload/article';
                if (!file_exists($dir))//文件夹不存在，先生成文件夹
                {
                    mkdir($dir);
                }
                $info = $upload->upload();
                if (!$info) {
                    $result = json_encode(array(
                        'state' => $upload->getError(),
                    ));
                } else {
                    $url = __ROOT__ . "/Upload/" . $info["upfile"]["savepath"] . $info["upfile"]['savename'];
                    $result = json_encode(array(
                        'url' => $url,
                        'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES),
                        'original' => $info["upfile"]['name'],
                        'state' => 'SUCCESS'
                    ));
                }
                break;
            default:
                $result = json_encode(array(
                    'state' => '请求地址出错'
                ));
                break;
        }
        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                echo json_encode(array(
                    'state' => 'callback参数不合法'
                ));
            }
        } else {
            echo $result;
        }
    }

    //上传图片
    public function uploadImage()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Upload/'; // 设置附件上传根目录
        $upload->savePath = 'image/'; // 设置附件上传（子）目录
        $dir = '/Upload/image';
        if (!file_exists($dir))//文件夹不存在，先生成文件夹
        {
            mkdir($dir);
        }
        // 上传文件
        $info = $upload->upload();
        $path = "/Upload/" . $info['thumb']['savepath'] . $info['thumb']['savename'];
        if ($info) {
            echo $path;

        } else {
            echo "-1";//上传失败
        }
    }






}