<?php

// 分类管理
class singlepageMod extends commonMod
{

    // 管理预设
    public function default_manage()
    {
        if (empty($_POST['psot_do'])) {
        $condition['mark']=$this->modelsetting('singlepage','mark');
        $info = $this->model->table('model')->where($condition)->find();
        $this->assign('info', unserialize($info['config']));
        $this->display();
        return;

        }

        $data = array();
        $data['default_class_tpl']=in($_POST['default_class_tpl']);
        $data['default_picoff']=intval($_POST['default_picoff']);

        $condition['mark']=$this->modelsetting('singlepage','mark');
        $moddata['config']=serialize($data);
        $this->model->table('model')->data($moddata)->where($condition)->update();

        echo json_encode(array("msg" => '模型预设设置成功！', "result" => '1'));
        
    }


    // 添加单页面
    public function add()
    {
        $this->auth(7); //权限认证
        if (empty($_POST['psot_do'])) {
            $mid = intval($_GET[0]); //模型ID获取
            
            // 列出模板文件
            $lang_open=$this->config['LANG_OPEN'];
            if(!$lang_open){
            $tpl_dir = module('plus')->dirhtml(__ROOTDIR__ . "/" . $this->getconfig('TPL_TEMPLATE_PATH'));
            }else{
            $tpl_dir = module('plus')->dirhtml(__ROOTDIR__ . "/" . $this->getconfig('TPL_TEMPLATE_PATH').__LANG__); 
            }

            $this->assign('mid', $mid); //模型ID
            $this->assign('cat', $this->getCat()); //获取格式化后的分类树
            $this->assign('tpl_dir', $tpl_dir); //模版文件获取
            $this->assign('model_info', unserialize($this->modelsetting('singlepage','config'))); //获取模型预设
            $this->display();
            return;
        }
        
        // 获取数据
        $data = array();
        $data['pid'] = intval($_POST['pid']); //上一级分类id
        $data['name'] = in($_POST['name']); //单页名称
        $data['seoname'] = in($_POST['seoname']); //seo分类名称
        $data['keywords'] = in($_POST['keywords']); //关键词，目的分类列表页面SEO优化
        $data['description'] = in($_POST['description']); //关键词描述，目的分类列表页面SEO优化
        $data['pic'] = in($_POST['pic']); //栏目形象图
        $data['hide'] = in($_POST['hide']); //菜单显示
        $data['way'] = 1; //菜单显示
        $data['sorter'] = in($_POST['sorter']); //菜单排序
        $data['tpl_list'] = in($_POST['tpl_list']); //单页模板
        $data['lang'] = $this->getlangid(); //语言id
        //隐藏信息
        $data['mid'] = in($_POST['mid']); //模型ID
		
		if (empty($_POST['pinyin'])) {
        $data['pinyin']=module('plus')->pinyin($data['name']); //为空自动转换
        }else
        {
        $data['pinyin']=module('plus')->ifpinyin($_POST['pinyin']); //过滤
        }

        // 验证数据
        if (empty($data['name'])) {
            $this->error('栏目名称不能为空！');
            return;
        }
        if (empty($data['mid'])) {
            $this->error('模型ID错误！');
            return;
        }
        // 数据库操作
        $id = $this->model->table('channel')->data($data)->insert(); //插入数据
        $data2['cid'] = $id;
        if(!empty($_POST['picoff'])){
        $data2['content'] = html_in(module('plus')->index($_POST['content'])); //内容
        }else{
        $data2['content'] = html_in($_POST['content']); //内容
        }
        $cid=$this->model->table('channel_data')->data($data2)->insert(); 
        $this->plus_hook('channel','add',$cid); //插件钩子     
        $this->class_success('页面添加成功！',$cid);


    }
    // 编辑单页面
    public function edit()
    {
        $this->auth(8); //权限认证
        if (empty($_POST['psot_do'])) {
            $id = intval($_GET[0]); //获取栏目ID
            if (empty($id)) {
                $this->error('参数传递错误！');
                return;
            }
            $condition['id'] = $id;
            $info = $this->model->table('channel')->where($condition)->find(); //获取当前分类信息
            $condition2['cid'] = $id;
            $info_content = $this->model->table('channel_data')->where($condition2)->find(); //获取单页内容
            $content = $info_content['content'];
            if (empty($info)) {
               $this->error('该页面不存在或被删除！');
                return;

            }
            // 列出模板文件
            $lang_open=$this->config['LANG_OPEN'];
            if(!$lang_open){
            $tpl_dir = module('plus')->dirhtml(__ROOTDIR__ . "/" . $this->getconfig('TPL_TEMPLATE_PATH'));
            }else{
            $tpl_dir = module('plus')->dirhtml(__ROOTDIR__ . "/" . $this->getconfig('TPL_TEMPLATE_PATH').__LANG__); 
            }
            
            $this->assign('info', $info);
            $this->assign('content', $content);
            $this->assign('cat', $this->getCat()); //获取格式化后的分类树
            $this->assign('tpl_dir', $tpl_dir); //模版文件获取1
            $this->display();
            // 关闭模板文件
            return;
        }
        
        // 获取数据
        $data = array();
        $data['pid'] = intval($_POST['pid']); //上一级分类id
        $data['name'] = in($_POST['name']); //单页名称
        $data['seoname'] = in($_POST['seoname']); //seo分类名称
        $data['keywords'] = in($_POST['keywords']); //关键词，目的分类列表页面SEO优化
        $data['description'] = in($_POST['description']); //关键词描述，目的分类列表页面SEO优化
        $data['pic'] = in($_POST['pic']); //栏目形象图
        $data['hide'] = in($_POST['hide']); //菜单显示
        $data['way'] = 1; //菜单显示
        $data['sorter'] = in($_POST['sorter']); //菜单排序
        $data['tpl_list'] = in($_POST['tpl_list']); //单页模板
        //隐藏信息
        $data['id'] = in($_POST['id']); //ID

        // 验证数据
        if (empty($data['name'])) {
            $this->error('栏目名称不能为空！');
            return;
        }
        if ($data['pid'] == $data['id']) {
            $this->error('不可以将当前栏目设置为上一级栏目！');
            return;
        }
        // 不能将自己的上一级分类，移动到自己的子栏目中
        $cat = $this->getCat($data['id']); //获取$data['id']的所有下级栏目
        if (!empty($cat)) {
            foreach ($cat as $vo) {
                if ($data['pid'] == $vo['id']) {
                    $this->error('不可以将上一级栏目移动到子栏目！');
                    return;
                }
            }
        }

        if (empty($_POST['pinyin'])) {
        $data['pinyin']=module('plus')->pinyin($data['name']); //为空自动转换
        }else
        {
        $data['pinyin']=module('plus')->ifpinyin($_POST['pinyin'],$data['id']); //过滤
        }
        
        // 数据库操作
        $condition = array();
        $condition['id'] = $data['id'];
        $this->model->table('channel')->data($data)->where($condition)->update();
        if(!empty($_POST['picoff'])){
        $data2['content'] = html_in(module('plus')->index($_POST['content'])); //内容
        }else{
        $data2['content'] = html_in($_POST['content']); //内容
        }
        $condition2['cid'] = $data['id'];
        $page_count=$this->model->table('channel_data')->where($condition2)->count(); 
        if($page_count==0){
        $data2['cid']=$data['id'];
        $this->model->table('channel_data')->data($data2)->insert(); //如果没有内容就插入
        }else{
        $this->model->table('channel_data')->data($data2)->where($condition2)->update();
        }
        $this->plus_hook('channel','edit',$data); //插件钩子
        $this->class_success('页面编辑成功！',$data['id']);


    }

    // 删除页面
    public function del()
    {
        
        $id = intval($_GET[0]); //获取传递信息
        if (empty($id)) {
            $this->error('参数传递错误！');
            return;
        }
        $this->auth(9); //权限认证
        // 检测子栏目是否存在
        $condition = array();
        $condition['pid'] = $id;
        if ($this->model->table('channel')->where($condition)->count()) {
            echo json_encode(array("msg" => '请先删除该栏目下面的子栏目！', "result" => '0'));
            return;
        }
        unset($condition); //将上一次查询条件清空
        $condition['id'] = $id;
        $this->model->table('channel')->where($condition)->delete(); //删除页面
        unset($condition); //将上一次查询条件清空
        $condition['cid'] = $id;
        $this->model->table('channel_data')->where($condition)->delete(); //删除栏目

        $this->plus_hook('channel','del',$id); //插件钩子
         echo json_encode(array("msg" => '栏目删除成功！', "result" => '1'));
    }

    // 获取分类树，$id，分类id,$id=0，获取所有分类结构树
    public function getCat($id = 0)
    {
        $langid=$this->getlangid();
        if(!empty($langid)){
            $condition['lang']=$langid;
        }
        
        $data = $this->model->field('id,pid,name')->table('channel')->where($condition)->select();
        $cat = new Category(array('id', 'pid', 'name', 'cname')); //初始化无限分类
        return $cat->getTree($data, $id); //获取分类数据树结构
    }

}

?>