RegAction.class.php 7.38 KB
<?php
class RegAction extends CommonAction {
	public $tab;
	public $notDel;
	public $jsArr;
	public function __construct() {
		parent::__construct();
		$this->tab='Regfields';
		$this->notDel=C('FIELDNOTDEL');
		$this->jsArr=C('JSCOMPONENT');//js组件数组
	}
	public function index(){
		$System=D($this->tab);
		if($this->isPost()){
			$_POST['name']=array2single($_POST['name'],true);
			foreach($_POST['name'] as $k=>$v){
				$arr_id=explode('_',$k);
				// $data['rownum']=I('rownum_'.$arr_id[1]);
				// $data['colnum']=I('colnum_'.$arr_id[1]);
				$data['sort']=I('sort_'.$arr_id[1]);
				$lastid=$System->where('id='.$arr_id[1])->save($data);
				//$f=$this->addColumn($v,I('type_'.$arr_id[1]),I('description_'.$arr_id[1]));	// wa_apply表添加不存在的字段
			}
			$this->success('字段信息更改成功',U('Reg/index'));
		}else{
			// $where['isshow']=array('eq',1);
			$list=$System->order('sort asc,id asc')->select();
			$this->assign('lists',$list);
			$this->assign('notDel',$this->notDel);
			$this->display();	
		}
	}
	/**
	 * 添加报名应填报信息
	 */
	public function add()
	{
		if($this->isPost()){
			$Config=D($this->tab);
			if($Config->create())
			{
				$field=$Config->name;	//字段名
				$type=$Config->type;	//字段类型
				$description=$Config->description.':'.$Config->note;	//字段注释
				$lastid=$Config->add();
				if($lastid)
				{
					$f=$this->addColumn($field,$type,$description);// wa_apply表添加不存在的字段
					$this->success('字段添加成功',U('Reg/index'));
				}
				else
					$this->error('字段添加失败');
			}
			else
			{
				$this->error($Config->getError());
			}
			exit;
		}
		$this->assign('jsarr',$this->jsArr);
		$this->display();
	}
	/**
	 * 修改学生申请应填写字段信息
	 */
	public function edit(){
		$id=I('id');
		if(!$id) exit('<script>alert("参数有误!");history.back();</script>');

		$Config=D($this->tab);
		if($this->isPost()){
			if($Config->create())
			{
				$field=$Config->name;	//字段名
				$type=$Config->type;	//字段类型
				$description=$Config->description.':'.$Config->note;	//字段注释
				$lastid=$Config->save();
				if($lastid)
				{
					$f=$this->editColumn($field,$type,$description);// wa_apply表添加不存在的字段
					$this->success('字段修改成功',U('Reg/index'));
				}
				else
					$this->error('字段修改失败');
			}
			else
			{
				$this->error($Config->getError());
			}
			exit;
		}
		$where['id']=array('eq',$id);
		$fdata=$Config->where($where)->find();
		$this->assign('fdata',$fdata);
		$this->assign('jsarr',$this->jsArr);
		$this->display();
	}
	public function delete(){
		$m=M($this->tab);
		if(!$id=(int)I('id')) exit('<script>alert("参数有误!");history.back();</script>');
		$where['id']=array('eq',$id);
		$name=$m->where($where)->getField('name');
		if(in_array($name,$this->notDel)){
			$this->error('该信息不可删除!');
			exit;
		}
		if($name){
			$r=$m->where($where)->delete();	//删除字段记录
			if($r){
				$this->deleteColumn($name);	//删除wa_apply表中字段
				$this->success('删除成功!',U('index'));
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('信息不存在!');
		}
	}
	/**
	 * 添加wa_apply表字段
	 * @param fieldName   字段名
	 * @param type 字段类型
	 * @param description 字段注释
	 */
	private function addColumn($fieldName,$type,$description){
		//修改申请wa_apply表结构
		import('ORG.Util.MysqlManage');
		$mg=new MysqlManage();
		$f=null;
		if(!$mg->checkField('wa_apply',$fieldName)){	//判断字段是否存在
			$info['name']=$fieldName;
			$info=array_merge($info,$this->getExinfo($type,$description));	//获取字段信息 type length isNull default comment
			$f=$mg->addField('wa_apply',$info);
		}
		unset($mg);
		return $f;
	}
	/**
	 * 修改wa_apply表字段
	 * @param fieldName   字段名
	 * @param type 字段类型
	 * @param description 字段注释
	 */
	private function editColumn($fieldName,$type,$description){
		//修改申请wa_apply表结构
		import('ORG.Util.MysqlManage');
		$mg=new MysqlManage();
		$f=null;
		$info['name']=$fieldName;
		$info=array_merge($info,$this->getExinfo($type,$description));	//获取字段信息 type length isNull default comment
		if($mg->checkField('wa_apply',$fieldName)){	//判断字段是否存在
			$f=$mg->editField('wa_apply',$info);	//存在修改
		}else{
			$f=$mg->addField('wa_apply',$info);	//不存在添加
		}
		unset($mg);
		return $f;
	}
	/**
	 * 删除wa_apply表字段
	 * @param fieldName   字段名
	 * @return bool
	 */
	private function deleteColumn($fieldName){
		import('ORG.Util.MysqlManage');
		$mg=new MysqlManage();
		$f=true;
		if($mg->checkField('wa_apply',$fieldName)){	//判断字段是否存在
			$f=$mg->dropField('wa_apply',$fieldName);	//删除字段
		}
		unset($mg);
		return $f;
	}
	/**
	 * 获取字段信息 type length isNull default comment
	 * @param type 字段类型
	 * @param description 注释
	 */
	private function getExinfo($type,$description){
		switch($type){
			case 'string':
				$info['type']='varchar';
				$info['length']='300';
				$info['isNull']=2;	//是否为空 1为空,2不为空
                $info['default']='""';
                $info['comment']=$description;
				break;
			case 'bstring':
				$info['type']='text';
				$info['length']='';
				$info['isNull']=1;
                $info['default']='';
                $info['comment']=$description;
				break;
			case 'bool':
				$info['type']='tinyint';
				$info['length']='1';
				$info['isNull']=2;
                $info['default']=0;
                $info['comment']=$description;
				break;
			case 'number':
				$info['type']='int';
				$info['length']='11';
				$info['isNull']=2;
                $info['default']=0;
                $info['comment']=$description;
				break;
			case 't_extarea':
				$info['type']='text';
				$info['length']='';
				$info['isNull']=1;
                $info['default']='';
                $info['comment']=$description;
				break;
			case 's_elect':
				$info['type']='varchar';
				$info['length']='120';
				$info['isNull']=2;
                $info['default']='""';
                $info['comment']=$description;
				break;
			case 'c_heckbox':
				$info['type']='varchar';
				$info['length']='120';
				$info['isNull']=2;
                $info['default']='""';
                $info['comment']=$description;
				break;
			case 'r_adio':
				$info['type']='varchar';
				$info['length']='60';
				$info['isNull']=2;
                $info['default']='""';
                $info['comment']=$description;
				break;
		}
		return $info;
	}
	/**
	 * 字段状态修改
	 */
	public function Shenhe(){
		$Article=M($this->tab);
		$is=$Article->where('id='.$this->_param('id'))->getField('isshow');
		if($is==1){
			$isnew=0;
		}else{
			$isnew=1;
		}
		$aa=$Article->where('id='.$this->_param('id'))->setField('isshow',$isnew);
		if($aa){	//更新成功
			echo 1;
		}else{
			echo 0;
		}
	}
	public function look(){
		$id=I('id');
		if(!intval($id)) exit('<script>alert("参数有误!");history.back();</script>');
		$m=M($this->tab);
		$where['id']=array('eq',$id);
		$data=$m->where($where)->find();
		$this->assign('data',$data);
		$this->display();
	}
}