136 lines
5.4 KiB
PHP
136 lines
5.4 KiB
PHP
<?php
|
||
namespace Home\Model;
|
||
use Think\Model\RelationModel;
|
||
class FeedbackModel extends RelationModel{
|
||
protected $_validate,$fieldarr;
|
||
//关联模型
|
||
protected $_link = array(
|
||
//类别
|
||
"feedback_cat"=>array(
|
||
'mapping_type' => self::BELONGS_TO,
|
||
'foreign_key' => 'catid',
|
||
),
|
||
//用户
|
||
"member"=>array(
|
||
'mapping_type' => self::BELONGS_TO,
|
||
'mapping_fields' => "truename,member_id",
|
||
'foreign_key' => 'member_id',
|
||
)
|
||
);
|
||
public function __construct(){
|
||
//模型id
|
||
$catid=I("catid",0,"int");
|
||
$this->cat=M("feedback_cat")->find($catid);
|
||
if(!$this->cat){
|
||
$this->error="分类不存在";
|
||
return;
|
||
}
|
||
//录入项
|
||
$is_enter=$this->cat["is_enter"];
|
||
$is_enter_arr=explode(",",trim($is_enter,","));
|
||
$this->fieldarr=M("feedback_field")->where(array("field"=>array('in',$is_enter_arr)))->order("sort asc,field_id asc")->select();
|
||
//必填项
|
||
$must_enter=$this->cat["must_enter"];
|
||
$must_enter_arr=explode(",",trim($must_enter,","));
|
||
$this->must_enter_arr=M("feedback_field")->where(array("field"=>array('in',$must_enter_arr)))->order("sort asc,field_id asc")->select();
|
||
foreach($this->must_enter_arr as $v){
|
||
//自动验证=======================================================================
|
||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||
//必填项(不能为空验证)
|
||
if(strstr($this->cat["is_enter"],",".$v[field].",")){
|
||
$error_tips=$v[name]."必须填写!";
|
||
//判断是不是checkbox,如果是执行函数验证
|
||
if($v[formtype]=="checkbox"){
|
||
$error_tips=$v[name]."必须勾选!";
|
||
$this->_validate[]=array($v[field],'validate_checkbox',$error_tips,1,"callback");
|
||
}
|
||
else{
|
||
$this->_validate[]=array($v[field],'require',$error_tips,1);
|
||
}
|
||
}
|
||
//正则验证
|
||
if($v[pattern]){
|
||
$p_error_tips=$v[errortips]?$v[errortips]:$v[name]."验证不通过!";
|
||
$this->_validate[]=array($v[field],$v[pattern],$p_error_tips,1,"regex");
|
||
}
|
||
//函数验证
|
||
if($v[savefun]){
|
||
$p_error_tips=$v[errortips]?$v[errortips]:$v[name]."验证不通过!";
|
||
$this->_validate[]=array($v[field],$v[savefun],$p_error_tips,1,"function");
|
||
}
|
||
//值维一验证
|
||
if($v[isunique]){
|
||
$p_error_tips=$v[name]."对应的记录已存在!";
|
||
$this->_validate[]=array($v[field],"",$p_error_tips,0,"unique");
|
||
}
|
||
}
|
||
//注意,这里父类析构函数一定要放到最下面,否则框架自带的方法不能使用
|
||
parent::__construct();
|
||
}
|
||
//自动验证的时候 验证checkbox
|
||
protected function validate_checkbox($arr){
|
||
if(count($arr)){
|
||
return true;
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
//数据完成
|
||
protected function data_create(){
|
||
$data=$this->create();
|
||
$data[addtime]=time();
|
||
$data[member_id]=intval(session("member_id"));
|
||
$data[ip]= get_client_ip();
|
||
foreach ($this->fieldarr as $v){
|
||
//复选框
|
||
if($v[formtype]=="checkbox"){
|
||
$data[$v[field]]=implode("|",$data[$v[field]]);
|
||
if($data[$v[field]]){
|
||
$data[$v[field]]="|".$data[$v[field]]."|";
|
||
}
|
||
}
|
||
//多图上传
|
||
if($v[formtype]=="morepic"){
|
||
$morepic_str="";
|
||
$smallimg=$_POST[$v[field]."_smallimg"];
|
||
$bigimg=$_POST[$v[field]."_bigimg"];
|
||
$imgname=$_POST[$v[field]."_imgname"];
|
||
foreach($smallimg as $key=>$val){
|
||
$morepic_str.=$smallimg[$key]."||".$bigimg[$key]."||".$imgname[$key]."\r\n";
|
||
}
|
||
$morepic_str=trim($morepic_str,"\r\n");
|
||
$data[$v[field]]=$morepic_str;
|
||
}
|
||
//如果是编辑器字段截取内容做为简介
|
||
if($v[formtype]=="editor"){
|
||
if(!$data[description]){
|
||
$data[description]= mb_substr(strip_tags(htmlspecialchars_decode($data[$v[field]])),0,C("display.description_len"),'utf-8');
|
||
}
|
||
}
|
||
//日期
|
||
if($v[formtype]=="date"){
|
||
$data[$v[field]]=$data[$v[field]]?strtotime($data[$v[field]]):"";
|
||
}
|
||
//未定义的数据设为空【否则mysql会报 cannot be null错误】
|
||
$data[$v[field]]=isset($data[$v[field]])?$data[$v[field]]:"";
|
||
//判断是否为 int smallint tinyint bigint
|
||
if(in_array($v[fieldtype],array("tinyint","smallint","int","bigint"))){
|
||
$data[$v[field]]=abs($data[$v[field]]);//禁止出现负数
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
//增加
|
||
public function data_add(){
|
||
$data=$this->data_create();
|
||
$data['id']=$this->add($data);
|
||
if($data['id']){
|
||
return $data;
|
||
}else{
|
||
$this->error = '缺少字段id!';
|
||
return false;
|
||
}
|
||
}
|
||
}
|