CooperationController.php 3.3 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\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\CooperationModel;

class CooperationController extends AdminBaseController
{
    protected $targets = ["_blank" => "新标签页打开", "_self" => "本窗口打开"];

    //首页
    public function index(){
        $CooperationModel = new CooperationModel();
        $cooperation     = $CooperationModel->order('weigh desc')->select();
        $this->assign('cooperation', $cooperation);
        return $this->fetch();
    }

    //添加页面
    public function add(){
        $this->assign('targets', $this->targets);
        return $this->fetch();
    }

    //提交保存
    public function addPost(){
        $data      = $this->request->param();
        $CooperationModel = new CooperationModel();
        $result    = $this->validate($data, 'Cooperation');
        if ($result !== true) {
            $this->error($result);
        }
        $CooperationModel->allowField(true)->save($data);

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

    //编辑页面
    public function edit(){
        $id        = $this->request->param('id', 0, 'intval');
        $CooperationModel = new CooperationModel();
        $cooperation      = $CooperationModel->get($id);
        $this->assign('targets', $this->targets);
        $this->assign('cooperation', $cooperation);
        return $this->fetch();
    }

    //编辑保存页面
    public function editPost()
    {
        $data      = $this->request->param();
        $CooperationModel = new CooperationModel();
        $result    = $this->validate($data, 'Cooperation');
        if ($result !== true) {
            $this->error($result);
        }
        $CooperationModel->allowField(true)->isUpdate(true)->save($data);

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

    //删除
    public function delete(){
        $id = $this->request->param('id', 0, 'intval');
        CooperationModel::destroy($id);
        $this->success("删除成功!", url("Cooperation/index"));
    }

    //显示与隐藏
    public function toggle()
    {
        $data      = $this->request->param();
        $CooperationModel = new CooperationModel();

        if (isset($data['ids']) && !empty($data["display"])) {
            $ids = $this->request->param('ids/a');
            $CooperationModel->where('id', 'in', $ids)->update(['status' => 1]);
            $this->success("更新成功!");
        }

        if (isset($data['ids']) && !empty($data["hide"])) {
            $ids = $this->request->param('ids/a');
            $CooperationModel->where('id', 'in', $ids)->update(['status' => 0]);
            $this->success("更新成功!");
        }

    }

}