HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/dd.cwoyt.com/Application/Admin/Controller/CategoryController.class.php
<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 赛脑 <2233759493@qq.com> <http://www.sn.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台分类管理控制器
 * @author 赛脑 <2233759493@qq.com>
 */

class CategoryController extends AdminController {

    static protected $allow = array( 'tree');
    /**
     * 左侧导航节点定义
     * @author 赛脑 <2233759493@qq.com>
     */
    static protected $nodes = array(

        /* 导航栏目设置 */
        array( 'title' => '分类管理', 'url' => 'Category/index', 'group' => '导航栏目设置',
            'operator'=>array(
                array('title'=>'编辑','url'=>'Category/edit','tip'=>'编辑和保存栏目分类'),
            	array('title'=>'新增','url'=>'Category/add','tip'=>'新增栏目分类'),
            	array('title'=>'删除','url'=>'Category/remove','tip'=>'删除栏目分类'),
            	array('title'=>'移动','url'=>'Category/move','tip'=>'移动栏目分类'),
            	array('title'=>'合并','url'=>'Category/merge','tip'=>'合并栏目分类'),
            ),
        ),
    );

    /**
     * 分类管理列表
     * @author 赛脑 <2233759493@qq.com>
     */
    public function index(){
        $tree = D('Category')->getTree(0,'id,name,title,sort,pid,allow_publish');
        $this->assign('tree', $tree);
        C('_SYS_GET_CATEGORY_TREE_', true); //标记系统获取分类树模板
        $this->meta_title = '分类管理';
        $this->display();
    }

    /**
     * 显示分类树,仅支持内部调
     * @param  array $tree 分类树
     * @author 赛脑 <2233759493@qq.com>
     */
    public function tree($tree = null){
        C('_SYS_GET_CATEGORY_TREE_') || $this->_empty();
        $this->assign('tree', $tree);
        $this->display('tree');
    }

    /* 编辑分类 */
    public function edit($id = null, $pid = 0){
        $Category = D('Category');

        if(IS_POST){ //提交表单
            if(false !== $Category->update()){
                $this->success('编辑成功!', U('index'));
            } else {
                $error = $Category->getError();
                $this->error(empty($error) ? '未知错误!' : $error);
            }
        } else {
            $cate = '';
            if($pid){
                /* 获取上级分类信息 */
                $cate = $Category->info($pid, 'id,name,title,status');
                if(!($cate && 1 == $cate['status'])){
                    $this->error('指定的上级分类不存在或被禁用!');
                }
            }

            /* 获取分类信息 */
            $info = $id ? $Category->info($id) : '';

            $this->assign('info',       $info);
            $this->assign('category',   $cate);
            $this->meta_title = '编辑分类';
            $this->display();
        }
    }

    /* 新增分类 */
    public function add($pid = 0){
        $Category = D('Category');

        if(IS_POST){ //提交表单
            if(false !== $Category->update()){
                $this->success('新增成功!', U('index'));
            } else {
                $error = $Category->getError();
                $this->error(empty($error) ? '未知错误!' : $error);
            }
        } else {
            $cate = array();
            if($pid){
                /* 获取上级分类信息 */
                $cate = $Category->info($pid, 'id,name,title,status');
                if(!($cate && 1 == $cate['status'])){
                    $this->error('指定的上级分类不存在或被禁用!');
                }
            }

            /* 获取分类信息 */
            $this->assign('category', $cate);
            $this->meta_title = '新增分类';
            $this->display('edit');
        }
    }

    /**
     * 删除一个分类
     * @author huajie <2233759493@qq.com>
     */
    public function remove(){
        $cate_id = I('id');
        if(empty($cate_id)){
            $this->error('参数错误!');
        }

        //判断该分类下有没有子分类,有则不允许删除
        $child = M('Category')->where(array('pid'=>$cate_id))->field('id')->select();
        if(!empty($child)){
            $this->error('请先删除该分类下的子分类');
        }

        //判断该分类下有没有内容
        $document_list = M('Document')->where(array('category_id'=>$cate_id))->field('id')->select();
        if(!empty($document_list)){
            $this->error('请先删除该分类下的文章(包含回收站)');
        }

        //删除该分类信息
        $res = M('Category')->delete($cate_id);
        if($res !== false){
        	//记录行为
        	action_log('update_category', 'category', $cate_id, UID);
            $this->success('删除分类成功!');
        }else{
            $this->error('删除分类失败!');
        }
    }

    /**
     * 操作分类初始化
     * @param string $type
     * @author huajie <2233759493@qq.com>
     */
    public function operate($type = 'move'){
    	//检查操作参数
    	if(strcmp($type, 'move') == 0){
    		$operate = '移动';
    	}elseif(strcmp($type, 'merge') == 0){
    		$operate = '合并';
    	}else{
    		$this->error('参数错误!');
    	}
    	$from = intval(I('get.from'));
    	empty($from) && $this->error('参数错误!');

    	//获取分类
    	$map = array('status'=>1, 'id'=>array('neq', $from));
		$list = M('Category')->where($map)->field('id,title')->select();

    	$this->assign('type'	, $type);
    	$this->assign('operate'	, $operate);
    	$this->assign('from'	, $from);
    	$this->assign('list'	, $list);
    	$this->meta_title = $operate.'分类';
    	$this->display();
    }

    /**
     * 移动分类
     * @author huajie <2233759493@qq.com>
     */
    public function move(){
    	$to = I('post.to');
    	$from = I('post.from');
    	$res = M('Category')->where(array('id'=>$from))->setField('pid', $to);
    	if($res !== false){
    		$this->success('分类移动成功!', U('index'));
    	}else{
    		$this->error('分类移动失败!');
    	}
    }

    /**
     * 合并分类
     * @author huajie <2233759493@qq.com>
     */
    public function merge(){
		$to = I('post.to');
    	$from = I('post.from');
    	$Model = M('Category');

    	//检查分类绑定的模型
    	$from_models = explode(',', $Model->getFieldById($from, 'model'));
    	$to_models = explode(',', $Model->getFieldById($to, 'model'));
    	foreach ($from_models as $value){
    		if(!in_array($value, $to_models)){
    			$this->error('请给目标分类绑定' . get_document_model($value, 'title') . '模型');
    		}
    	}

    	//检查分类选择的文档类型
    	$from_types = explode(',', $Model->getFieldById($from, 'type'));
    	$to_types = explode(',', $Model->getFieldById($to, 'type'));
    	foreach ($from_types as $value){
    		if(!in_array($value, $to_types)){
    			$types = C('DOCUMENT_MODEL_TYPE');
    			$this->error('请给目标分类绑定文档类型:' . $types[$value]);
    		}
    	}

    	//合并文档
    	$res = M('Document')->where(array('category_id'=>$from))->setField('category_id', $to);

    	if($res){
    		//删除被合并的分类
    		$Model->delete($from);
    		$this->success('合并分类成功!', U('index'));
    	}else{
    		$this->error('合并分类失败!');
    	}

    }
}