审查视图

app/portal/controller/AdminCityCategoryController.php 4.8 KB
景龙 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use app\portal\model\CityCategoryModel;
use think\Db;
use app\admin\model\ThemeModel;


class AdminCityCategoryController extends AdminBaseController
{
    //分类列表页
    public function index()
    {
        $portalCategoryModel = new CityCategoryModel();
        $parentList = $portalCategoryModel->where(['delete_time'=>0,'pid'=>0])->select()->toArray();
        $list = $portalCategoryModel->where(['delete_time'=>0])->select()->toArray();
        $arr = [];
        foreach($parentList as &$item) {
            $arr[] = $item;
            foreach ($list as $list_value) {
                if ($list_value['pid'] == $item['id']) {
                    $list_value['name'] = '&nbsp;&nbsp;&nbsp;├─' . $list_value['name'];
                    $arr[] = $list_value;
                }
            }
        }
        $this->assign('list', $arr);
        return $this->fetch();
    }

    //新建分类页展示
    public function add()
    {
        $cityCategoryModel = new CityCategoryModel();
        $parentList      = $cityCategoryModel->where(['delete_time'=>0,'pid'=>0])->select()->toArray();

        $this->assign('parentList', $parentList);
        return $this->fetch();
    }

    //新建分类页提交
    public function addPost()
    {
        $portalCategoryModel = new CityCategoryModel();

        $data = $this->request->param();

        $result = $this->validate($data, 'AdminCityCategory');

        if ($result !== true) {
            $this->error($result);
        }
        $data['create_time'] = time();
        $result = $portalCategoryModel->save($data);

        if ($result === false) {
            $this->error('添加失败!');
        }

        $this->success('添加成功!', url('AdminCityCategory/index'));
    }

    //编辑分类页展示
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
            $cityCategoryModel = new CityCategoryModel();
            $parentList = $cityCategoryModel->where(['delete_time'=>0,'pid'=>0])->select()->toArray();
            $category_name = $cityCategoryModel->where(['delete_time'=>0,'id'=>$id])->find()->toArray();
            $this->assign('parentList', $parentList);
            $this->assign('category_name', $category_name);
            return $this->fetch();
        }else{
            $this->error('操作错误!');
        }
    }

    /**
     * 编辑文章分类提交
     * @adminMenu(
     *     'name'   => '编辑文章分类提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文章分类提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data = $this->request->param();
        $cityCategoryModel = new CityCategoryModel();
        $result = $this->validate($data, 'AdminCityCategory');

        if ($result !== true) {
            $this->error($result);
        }
        $data['update_time'] = time();
        $result = $cityCategoryModel->where(['id'=>$data['id']])->update($data);

        if ($result === false) {
            $this->error('保存失败!');
        }

        $this->success('保存成功!', url('AdminCityCategory/index'));
    }

    //删除分类
    public function delete()
    {
        $cityCategoryModel = new CityCategoryModel();
        $id = $this->request->param('id');
        //获取删除的内容
        $findCategory = $cityCategoryModel->where('id', $id)->find();

        if (empty($findCategory)) {
            $this->error('分类不存在!');
        }
        //判断此分类有无子分类(不算被删除的子分类)
        $categoryChildrenCount = $cityCategoryModel->where(['pid' => $id, 'delete_time' => 0])->count();

        if ($categoryChildrenCount > 0) {
            $this->error('此分类有子类无法删除!');
        }
        $result = $cityCategoryModel
            ->where('id', $id)
            ->update(['delete_time' => time()]);
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
}