qa-ifish7/web/Application/Admin/Model/FeedbackCatModel.class.php

122 lines
4.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Admin\Model;
use Think\Model;
class FeedbackCatModel extends Model{
const modelTempPath = 'Data/feedbackTemp/'; //模型表单模板路径
//自动验证
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
protected $_validate = array(
array('name','require','请填写名称!',1),
);
//自动完成
protected $_auto = array (
array('formtemp','get_formtemp',3,'callback'), //表单的form内容
array('member_group','arr2string',3,'callback'), //表单的form内容
);
//自动完成-把数组转换为字符串便于存到数据里
public function arr2string($arr){
$str= implode(",", $arr);
if($str){
$str=",".$str.",";
}
return $str;
}
//自动完成-get_formtemp
//检查SQL文件是否存在
public function get_formtemp() {
$field_name=I("post.field_name");
$field=I("post.field");
$str="";
foreach($field as $key=>$v){
$str.=$field_name[$key]."<!--field-->".$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("<!--field-->",$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;
}
}