Initial commit
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
namespace Member\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class TougaoModel extends RelationModel{
|
||||
protected $tableName,$_validate,$_link,$modelinfo,$modelid,$fieldarr;
|
||||
|
||||
public function __construct(){
|
||||
//模型id
|
||||
$modelid=I("modelid",0,"int");
|
||||
$this->modelid=$modelid;
|
||||
//模型信息
|
||||
$modelinfo=$GLOBALS['model'][$modelid];
|
||||
if(!$modelinfo){
|
||||
return false;//
|
||||
}
|
||||
$this->modelinfo=$modelinfo;
|
||||
//
|
||||
//$this->_validate[]=array("catid","require","请选择分类",1);
|
||||
//对应数据表
|
||||
$this->tableName="cms_".$modelinfo['table_name'];
|
||||
//关联模型
|
||||
$this->_link = array(
|
||||
//对应栏目表
|
||||
"cat"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'foreign_key' => 'catid',
|
||||
),
|
||||
//【对应副表】
|
||||
$this->tableName."_data"=>array(
|
||||
'mapping_type' => self::HAS_ONE,
|
||||
'foreign_key' => 'id',
|
||||
),
|
||||
);
|
||||
//录入项
|
||||
$is_enter=$this->modelinfo["is_contribute"];
|
||||
$is_enter_arr=explode(",",trim($is_enter,","));
|
||||
$this->fieldarr=M("table_field")->where(array("table_id"=>$this->modelinfo["table_id"],"field"=>array('in',$is_enter_arr)))->select();
|
||||
//必填项
|
||||
$must_enter=$this->modelinfo["must_enter"];
|
||||
$must_enter_arr=explode(",",trim($must_enter,","));
|
||||
$this->must_enter_arr=M("table_field")->where(array("table_id"=>$this->modelinfo["table_id"],"field"=>array('in',$must_enter_arr)))->select();
|
||||
foreach($this->must_enter_arr as $v){
|
||||
//自动验证=======================================================================
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
//必填项(不能为空验证)
|
||||
if(strstr($this->modelinfo["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");
|
||||
}
|
||||
//判断是不是多图上传
|
||||
elseif($v[formtype]=="morepic"){
|
||||
$error_tips="请上传".$v[name]."!";
|
||||
$this->_validate[]=array($v[field]."_smallimg",'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();
|
||||
if($data[id]){
|
||||
$data[editortime]=time();
|
||||
}else{
|
||||
$data[addtime]=time();
|
||||
$data[modelid]=$this->modelid;
|
||||
$data[userid]=$_SESSION['user_id'];
|
||||
$data[username]=$_SESSION['user_name'];
|
||||
}
|
||||
$data["catid"]=intval($data["catid"]);
|
||||
foreach ($this->fieldarr as $v){
|
||||
//检测是不是副表字段,如果是副表时间,数据用I方法获取(因为create方法只能获取主表数据)
|
||||
if(!$v[issystem]){
|
||||
$data[$v[field]]=I($v[field]);
|
||||
}//
|
||||
//复选框
|
||||
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(get_magic_quotes_gpc()){ //如果get_magic_quotes_gpc()是打开的
|
||||
$data[$v[field]]=stripslashes($data[$v[field]]);//将字符串进行处理
|
||||
}
|
||||
//如果是编辑器字段截取内容做为简介
|
||||
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]]);//禁止出现负数
|
||||
}
|
||||
//将副表的数据提取出来[这里用到了关联模型]
|
||||
if(!$v[issystem]){
|
||||
$data[$this->tableName."_data"][$v[field]]=$data[$v[field]];
|
||||
unset($data[$v[field]]);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
//增加
|
||||
public function data_add(){
|
||||
$data=$this->data_create();
|
||||
//是否开启自动审核
|
||||
$data['checked']=0;
|
||||
//
|
||||
$data['id']=$this->relation(true)->add($data);
|
||||
if($data['id']){
|
||||
//如果副表字段为空,需要插入id数据
|
||||
if(!$data[$this->tableName."_data"]){
|
||||
$data[$this->tableName."_data"][id]=$data['id'];
|
||||
M($this->tableName."_data")->add($data[$this->tableName."_data"]);
|
||||
}
|
||||
return $data;
|
||||
}else{
|
||||
$this->error = '缺少字段id!';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//修改
|
||||
public function data_editor(){
|
||||
$data=$this->data_create();
|
||||
$this->relation(true)->save($data);
|
||||
return $data;
|
||||
}
|
||||
//删除字段
|
||||
public function data_delete($ids) {
|
||||
if(!$ids){
|
||||
$this->error = '请选择信息!';
|
||||
return false;
|
||||
}
|
||||
if(is_array($ids)){
|
||||
foreach($ids as $id){
|
||||
$this->relation(true)->delete($id);
|
||||
}
|
||||
}else{
|
||||
$this->relation(true)->delete($ids);
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user