$v){ $str.=$field_name[$key]."".$field[$key]."\r\n"; } return trim($str); } //保存模型 增加和修改都调用此方法 public function data_save(){ $data=$this->create(); $data["is_enter"]=$this->arr2string(I("post.is_enter")); $data["must_enter"]=$this->arr2string(I("post.must_enter")); if($data["catid"]){ $this->save($data); $catid = $data["catid"]; }else{ $catid = $this->add($data); $data["catid"]=$catid; } if ($catid) { $this->updateModelTemp($catid); } else { return false; } // return $data; } //删除模型 public function data_delete($catid) { if (empty($catid)){ $this->error = 'id不存在!'; return false; } $data = $this->where(array("catid" => $catid))->find(); if (!$data) { $this->error = '分类不存在!'; return false; } //检查该模型下是否有分类 $count = M("feedback")->where(array("catid" => $catid))->count(); if ($count) { $this->error = '该分类下有信息,请先删除信息!'; return false; } //删除模型数据 $this->where(array("catid" => $catid))->delete(); //删除模型表单文件 unlink(APP_PATH.self::modelTempPath.$catid.".php"); return true; } /** * 更新模型模板文件 * @param type $catid 模型id * @return boolean */ public function updateModelTemp($catid){ $cat=$this->find($catid); //将model里的is_enter is_contribute must_enter is_list is_search is_sort 依次拿出来和table_field对比,如果不存在就删除(防止因删除字段和修改字段名而造成model里的这些字段不更新导致的错误) $field=M("feedback_field")->field("field")->order("sort asc,field_id asc")->select(); $new["catid"]=$catid; foreach($field as $v){ //录入项 if(strstr($cat["is_enter"], ",".$v["field"].",")){ $new["is_enter"].=",".$v["field"]; } //必填项 if(strstr($cat["must_enter"], ",".$v["field"].",")){ $new["must_enter"].=",".$v["field"]; } } $new["is_enter"]=$new["is_enter"]?$new["is_enter"].",":""; $new["must_enter"]=$new["must_enter"]?$new["must_enter"].",":""; $this->save($new); // $formtemp= explode("\r\n",$cat["formtemp"]); $catTemp=""; foreach($formtemp as $key=>$v){ $f=explode("",$v); if(!strstr($new["is_enter"], ",".$f[1].",")){ continue; } $field=M("feedback_field")->where(array("field"=>$f[1]))->find(); if($field){ $htmlcode=$field["htmlcode"]; $htmlcode= str_replace("{modelFieldTemp_name}", $f[0],$htmlcode);//名称替换 $catTemp=$catTemp."\r\n".$htmlcode; } } $catTemp=htmlspecialchars_decode($catTemp); file_put_contents(APP_PATH.self::modelTempPath.$catid.".php",$catTemp); return true; } }