<?php

class plus_replaceMod extends commonMod
{

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

        if (empty($data['key'])) {
            $this->error('替换内容不能为空！');
            return;
        }

        $condition['key'] = $data['key'];
        $info = $this->model->table('plus_replace')->where($condition)->find();
        if ($info['key'] == $data['key']) {
            $this->error('替换内容不能重复！');
            return;
        }
		
		if (empty($data['key'])) {
            $this->error('替换次数不能为空！');
            return;
        }

        // 数据库操作
        $this->model->table('plus_replace')->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_replace')->where($condition)->find(); //获取当前页面信息
            if (empty($info)) {
                $this->error('该替换不存在！');
                return;

            }

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


        if (empty($data['key'])) {
            $this->error('替换内容不能为空！');
            return;
        }
		
		if (empty($data['key'])) {
            $this->error('替换次数不能为空！');
            return;
        }


        // 数据库操作
        $condition2['id'] = intval($_POST['id']);
        $this->model->table('plus_replace')->data($data)->where($condition2)->update();
        $this->success('内容替换修改成功！');
    }

    // 删除替换
    public function del()
    {
        $id = intval($_GET[0]);
        if (empty($id)) {
            $this->jserror('参数传递错误！');
            return;
        }
        
        $condition['id'] = $id;
        $this->model->table('plus_replace')->where($condition)->delete();
        $this->jssuccess('内容替换删除成功！');

    }

}

?>