AdminCityCategoryController.php 4.8 KB
<?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('删除失败');
        }
    }
}