<?php
// +----------------------------------------------------------------------
// | YZCMS <XueHen CMS>
// +----------------------------------------------------------------------
// | Copyright (c) http://www.xuehen.org
// +----------------------------------------------------------------------
// | Author XueHen <490860758@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

class FormController extends BaseController {
	
	function edit(){
		$id = I('id');
		if(!$id){
			$this->error('无效的参数');
			exit;
		}
		$db = M('form');
		$re = $db->where(array('id'=>$id))->find();
		
		if($re){
			$this->assign('content',$re);
			$this->display();
		}else{
			$this->error('无效的参数');
		}
	}
	
	function editok(){
		if(IS_POST){
			if($_POST['name'] == ''){
				$this->error('字段不能为空');
				exit;
			}
		}else{
			$this->error('无效的访问');
			exit;
		}
		$db = M('form');
		//验证URL
		$re = $db->where(array('name'=>I('name'),'id'=>array('neq',I('id')),'_logic'=>'AND'))->find();
		if($re){
			$this->error('字段不能重复');
			exit;
		}
		//修改
		$db->create();
		$rid = $db->save();
		if($rid){
			$this->success('修改成功！','index');
		}else{
			$this->error('修改失败！');
		}	
	}
	function index(){
		$db = M('form');
		$this->form = $db->order('id asc')->select();
		$this->display();
	}
	
	function addok(){
		if(IS_POST){
			if($_POST['name'] == ''){
				$this->error('名称不能为空');
				exit;
			}
		}else{
			$this->error('无效的访问');
			exit;
		}
		$db = M('form');
		//验证URL
		$re = $db->where(array('name'=>I('name')))->find();
		if($re){
			$this->error('字段不能重复');
			exit;
		}
		
		$db->create();
		$rid = $db->add();
		if($rid){
			$this->success('添加成功！','index');
		}else{
			$this->error('添加失败！');
		}
		
	}
	
	function delete(){
		$id = I('id');
		if(!$id){
			$this->error('无效的参数');
			exit;
		}
		$db = M('form');
		$rid = $db->where('id = '.I('id'))->delete();
		if($rid){
			$this->success('删除成功！','index');
		}else{
			$this->error('删除失败!');
		}
		
	}
	
	
}