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

245 lines
10 KiB
PHP
Raw Permalink 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\RelationModel;
class InfoModel extends RelationModel{
protected $tableName,$_validate,$_link,$modelinfo,$modelid,$fieldarr;
public function __construct(){
$catid=I("catid",0,"int");
$this->catid=$catid;
$modelid=I("modelid",0,"int");
if($modelid){
$modelid=$modelid;
}else{
$modelid=$GLOBALS['cat'][$catid]["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_enter"];
$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($type){
//注意,这里不要用$this->create()方法,因为这种方法在正式环境上面会调用缓存,调用\Runtime\Data\_fields 下面的info里字段并不是动态指定的表因此要写自己的方法
$data = $this->create($_POST);
$fields=$this->db->getFields("db_".$this->tableName);;
foreach($fields as $f){
$fileNameArr[]=$f[name];
}
foreach($data as $key=>$d){
if(!in_array($key,$fileNameArr)){
unset($data[$key]);
}
}
if($data[id]){
$data[editortime]=time();
}else{
$data[addtime]=time();
$data[modelid]=$this->modelinfo[modelid];
$data[userid]=$_SESSION['user_id'];
$data[username]=$_SESSION['user_name'];
}
$data["catid"]=intval($data["catid"]);
$data["viewtemp"]=intval($data["viewtemp"]);
foreach ($this->fieldarr as $v){
//检测是不是副表字段如果是副表时间数据用I方法获取因为create方法只能获取主表数据
if(!$v[issystem]){
$data[$v[field]]=I($v[field]);
}//
//复选框
if($v[formtype]=="checkbox"){
//为数据导入的时候,表格里填写的是:上网|听音乐|打游戏 只要两边增加|即可
if($type=="export"){
if($data[$v[field]]){
$data[$v[field]]="|".$data[$v[field]]."|";
}
}else{
$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($_POST[$v[field]."_saveFile"]){
$SaveRemoteImg=new \Admin\Model\SaveRemoteImgModel();
$data[$v[field]]=$SaveRemoteImg->doIt($data[$v[field]]);
}
//如果开启了提取第一张图做为缩略图并且缩略图为空的话
if($_POST[$v[field]."_getFirstImg"]&&!$data["thumb"]){
$data["thumb"]=$this->getFirstImg($data[$v[field]]);
}
//提取简介
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]]=$_POST[$v[field]];//注意副表的字段一定要用post方法获取
unset($data[$v[field]]);
}
}
return $data;
}
//增加{$type为增加类型默认为直接添加需要做相关验证当为export的时候在数据完成的时候做特殊处理}
public function data_add($type){
$data=$this->data_create($type);
//是否开启自动审核
$data['checked']=$this->modelinfo['autochecked'];
//
$data['id']=$this->relation($this->tableName."_data")->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"]);
}
$this->updateInfoNum($this->catid);
return $data;
}else{
$this->error = '缺少字段id';
return false;
}
}
//修改
public function data_editor(){
$data=$this->data_create();
$this->relation($this->tableName."_data")->save($data);
$this->updateInfoNum($this->catid);
//
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);
}
$this->updateInfoNum($this->catid);
return true;
}
//更新栏目信息数量
public function updateInfoNum($catid){
$count=$this->where(array("catid"=>$catid,"checked"=>1))->count();
M("cat")->save(array(
"catid"=>$catid,
"infonum"=>$count,
));
}
//提取内容第一张图做为缩略图
private function getFirstImg($str){
preg_match("/\<[img|IMG].*?src=\"(.+?)\".*?>/",$str,$result);
return $result[1];
}
}