<?php

class plus_fragmentMod extends commonMod
{

    // 碎片管理
    public function index()
    {
        $list = $this->model->table('plus_fragment')->select(); //查询碎片表所有数据
        $this->assign('list', $list);
        $this->display();
    }
    // 碎片添加
    public function add()
    {
        if (empty($_POST['psot_do'])) {
            $this->display();
            return;
        }
        
        $data = array();
        $data['title'] = in($_POST['title']);
        $data['sign'] = in($_POST['sign']);
        $data['content'] = html_in($_POST['content']);

        if (empty($data['title'])) {
            $this->error('名称不能为空！');
            return;
        }

        $condition['sign'] = $data['sign'];
        $info = $this->model->table('plus_fragment')->where($condition)->find();
        if ($info['sign'] == $data['sign']) {
            $this->error('碎片标识不能重复！');
            return;
        }

        // 数据库操作
        $this->model->table('plus_fragment')->data($data)->insert();
        $this->success('碎片添加成功！');
    }
    // 碎片修改
    public function edit()
    {
        if (empty($_POST['psot_do'])) {
            $id = intval($_GET[0]); //获取ID
            if (empty($id)) {
                $this->error('参数传递错误！');
                return;
            }
            $condition['id'] = $id;
            $info = $this->model->table('plus_fragment')->where($condition)->find(); //获取当前页面信息
            if (empty($info)) {
                $this->error('该碎片不存在！');
                return;

            }

            $this->assign('info', $info);
            $this->display();
            return;
        }
        
        $data = array();
        $data['title'] = in($_POST['title']);
        $data['content'] = html_in($_POST['content']);

        if (empty($data['title'])) {
			$this->error('名称不能为空！');
            return;
        }
        // 数据库操作
        $condition2['id'] = intval($_POST['id']);
        $this->model->table('plus_fragment')->data($data)->where($condition2)->update();
        $this->success('碎片修改成功！');
    }

    // 删除碎片
    public function del()
    {
        $id = intval($_GET[0]);
        if (empty($id)) {
            $this->jserror('ID传输错误！');
            return;
        }
        
        $condition['id'] = $id;
        $this->model->table('plus_fragment')->where($condition)->delete();
        $this->jssuccess('碎片删除成功！');


    }

}

?>