审查视图

application/admin/controller/Category.php 5.0 KB
Karson authored
1 2 3 4 5 6 7 8 9 10 11
<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\Category as CategoryModel;
use fast\Tree;

/**
 * 分类管理
 *
12
 * @icon   fa fa-list
13
 * @remark 用于统一管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加
Karson authored
14 15 16 17
 */
class Category extends Backend
{
18 19 20
    /**
     * @var \app\common\model\Category
     */
Karson authored
21 22 23 24 25 26 27
    protected $model = null;
    protected $categorylist = [];
    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
28
        $this->model = model('app\common\model\Category');
Karson authored
29 30 31 32 33

        $tree = Tree::instance();
        $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
        $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
        $categorydata = [0 => ['type' => 'all', 'name' => __('None')]];
34
        foreach ($this->categorylist as $k => $v) {
Karson authored
35 36
            $categorydata[$v['id']] = $v;
        }
37
        $typeList = CategoryModel::getTypeList();
Karson authored
38
        $this->view->assign("flagList", $this->model->getFlagList());
39
        $this->view->assign("typeList", $typeList);
Karson authored
40
        $this->view->assign("parentList", $categorydata);
41
        $this->assignconfig('typeList', $typeList);
Karson authored
42 43 44 45 46 47 48
    }

    /**
     * 查看
     */
    public function index()
    {
49 50
        //设置过滤方法
        $this->request->filter(['strip_tags']);
51
        if ($this->request->isAjax()) {
Karson authored
52
            $search = $this->request->request("search");
53 54
            $type = $this->request->request("type");
Karson authored
55 56
            //构造父类select列表选项数据
            $list = [];
57
58 59 60 61
            foreach ($this->categorylist as $k => $v) {
                if ($search) {
                    if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
                        if ($type == "all" || $type == null) {
62
                            $list = $this->categorylist;
63
                        } else {
64 65
                            $list[] = $v;
                        }
Karson authored
66
                    }
67 68 69 70 71 72
                } else {
                    if ($type == "all" || $type == null) {
                        $list = $this->categorylist;
                    } elseif ($v['type'] == $type) {
                        $list[] = $v;
                    }
Karson authored
73
                }
74
            }
75
Karson authored
76 77 78 79 80 81 82 83 84
            $total = count($list);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
85 86 87 88 89 90 91 92 93 94 95
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
        }
        return parent::add();
    }

    /**
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
111
            $this->token();
112 113 114 115 116
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if ($params['pid'] != $row['pid']) {
117
                    $childrenIds = Tree::instance()->init(collection(\app\common\model\Category::select())->toArray())->getChildrenIds($row['id'], true);
118
                    if (in_array($params['pid'], $childrenIds)) {
119
                        $this->error(__('Can not change the parent to child or itself'));
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                    }
                }

                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    /**
Karson authored
150
     * Selectpage搜索
151
     *
Karson authored
152 153 154 155 156 157 158
     * @internal
     */
    public function selectpage()
    {
        return parent::selectpage();
    }
}