modelid=$modelid; //模型信息 $modelinfo=$GLOBALS['model'][$modelid]; if(!$modelinfo){ return false;// } $this->modelinfo=$modelinfo; // //关联模型 $this->_link = array( //【对应副表】 "cat_data"=>array( 'mapping_type' => self::HAS_ONE, 'foreign_key' => 'catid', ), ); //录入项 $cat_is_enter=$this->modelinfo["cat_is_enter"]; $is_enter_arr=explode(",",trim($cat_is_enter,",")); $this->fieldarr=M("cat_field")->where(array("field"=>array('in',$is_enter_arr)))->select(); //默认字段验证 $this->_validate[]=array('name','require','名称必须填写!',1); $this->_validate[]=array('modelid','require','缺少模型id!',1); $this->_validate[]=array('classpath','require','请填写栏目路径!',1); $this->_validate[]=array('classpath','/^\/[\w|\/]+\/$/','栏目名称不符号要求,只能使用[数字,字母,_,/]!',1); $this->_validate[]=array('classpath','','路径已存在',1,"unique"); $this->_validate[]=array('lencord','require','请输入每页显示条数!',1); $this->_validate[]=array('listtemp','require','请选择列表模板!',1); $this->_validate[]=array('viewtemp','require','请选择内容模板!',1); //必填项 $cat_must_enter=$this->modelinfo["cat_must_enter"]; $must_enter_arr=explode(",",trim($cat_must_enter,",")); $this->must_enter_arr=M("cat_field")->where(array("field"=>array('in',$must_enter_arr)))->select(); foreach($this->must_enter_arr as $v){ //自动验证======================================================================= //array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间]) //必填项(不能为空验证) if(strstr($this->modelinfo["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"); } //判断是不是多图上传 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(); //表名 $data["table_name"]=$this->modelinfo["table_name"]; //会员组 if($data[member_group_id]){ $data[member_group_id]=implode("|",$data[member_group_id]); $data[member_group_id]="|".$data[member_group_id]."|"; } //level if($data[pid]){ $level=$this->where(array("catid"=>$data[pid]))->getField("level"); $level=$level+1; $data['level']=$level; }else{ $data[pid]=0; $data['level']=1; } 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]=="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["cat_data"][$v[field]]=$_POST[$v[field]]; unset($data[$v[field]]); } } return $data; } //增加 public function data_add(){ $data=$this->data_create(); $data["is_last"]=1;//默认最新增加的都是终极栏目 $data["infonum"]=0;//默认当前栏目数据为0条 //如果存在parent_id,把parent_id改百非终极栏目 if($data["pid"]){ $parent_data["catid"]=$data["pid"]; $parent_data["is_last"]=0; $this->save($parent_data); } // $data['catid']=$this->relation(true)->add($data); if($data['catid']){ //更新parent_catids字段 $parent_catids=$this->updateParent_catids($data['catid']); //再依次更新每个父级的son_catids字段 $parent_catids_arr=explode("|",trim($parent_catids,"|")); foreach($parent_catids_arr as $objid){ $this->updateSon_catids($objid); } //如果副表字段为空,需要插入id数据 if(!$data["cat_data"]){ $data["cat_data"]['catid']=$data['catid']; M("cat_data")->add($data["cat_data"]); } //更新权限 $this->updateRole($data['catid']); // return $data; }else{ $this->error = '缺少字段id!'; return false; } } //修改 public function data_editor(){ $data=$this->data_create(); $past_data=$this->find($data["catid"]);//修改前的数据 $this->relation(true)->save($data); $new_data=$this->find($data["catid"]);//修改前的数据 //更新parent_catids字段,son_catids字段 $this->updateParent_catids($data['catid']); $this->updateSon_catids($data['catid']); //再把旧的parent_catids字段,son_catids字段和新的parent_catids字段,son_catids字段结果组合在一起,再逐一对每条信息更新父级字段和子级字段 $arrid_str=$past_data[parent_catids].$past_data[son_catids].$new_data[parent_catids].$new_data[parent_catids]; $arrid_str_arr=explode("|",$arrid_str); $arrid_str_arr = array_unique($arrid_str_arr); foreach($arrid_str_arr as $catid_val){ if($catid_val){ $this->updateParent_catids($catid_val); $this->updateSon_catids($catid_val); } } //更新修改前的pid if($past_data["pid"]){ $this->updateCatIslast($past_data["pid"]); } //更新修改后的pid if($data["pid"]){ $this->updateCatIslast($data["pid"]); } //更新权限 $this->updateRole($data['catid']); return $data; } //修正该栏目,看有无子栏目,并根据结果修改is_last public function updateCatIslast($catid){ $sons=$this->where(array("pid"=>$catid))->count(); $data["catid"]=$catid; if($sons){ $data["is_last"]=0; }else{ $data["is_last"]=1; } $this->save($data); } //更新栏目缓存 public function updateCache(){ $cat=$this->field("catid,modelid,classpath,lang,pubid,table_name,name,infonum,list_type,view_type,lencord,status,pid,sort,level,is_last,parent_catids,son_catids,listtemp,viewtemp,thumb,pagetitle,keywords,description,is_page,listorder,reorder")->select(); $newcat=array(); foreach($cat as $v){ //更新栏目信息数 $where=array(); $where['catid']=array('in',getSonCat($v["catid"])); $where['checked']=1; $count=M("cms_".$v[table_name])->where($where)->count(); M("cat")->save(array( "catid"=>$v["catid"], "infonum"=>$count, )); // $newcat[$v["catid"]]=$v; } $arr_str=var_export ($newcat,true); $arr_str="getParents($catid); $this->save(array( "catid"=>$catid, "parent_catids"=>$parent_catids, )); return $parent_catids; } //更新指定栏目的子极 public function updateSon_catids($catid){ $son_catids=$this->getSons($catid); $this->save(array( "catid"=>$catid, "son_catids"=>$son_catids, )); } //查找一个栏目的所有父级catid protected function getParents($catid){ $catarr=array(); for($i=1;$i<=2;$i++){ $cat=$this->field("catid,pid")->find($catid); $pid=$cat["pid"]; if($pid){ $catarr[]=$pid; $catid=$pid; $i=1; }else{ break; } } //数组颠倒 if($catarr){ $catarr=array_reverse($catarr); } $catarr= implode("|",$catarr); if($catarr){ $catarr="|".$catarr."|"; } return $catarr; } //查找一个栏目的所有子级catid protected function getSons($catid){ $catidArr=$this->forCat($catid); $catidArr= implode("|",$catidArr); if($catidArr){ $catidArr="|".$catidArr."|"; } return $catidArr; } protected function forCat($pid,$catidArr){ $where["pid"]=$pid; $cat=M("cat")->field("catid")->where($where)->order("sort asc")->select(); if(count($cat)>0){ foreach($cat as $v){ $catidArr[]=$v[catid]; $catidArr=$this->forCat($v[catid],$catidArr); } return $catidArr; }else{ return $catidArr; } } //更新权限 protected function updateRole($catid){ $role=M("role")->select(); foreach($role as $v){ $saveData=array(); $saveData["id"]=$v[id]; if($_POST["view_role_".$v[id]]){ $saveData["info_view"]=$this->addCatidStr($v["info_view"],$catid); }else{ $saveData["info_view"]=$this->removeCatidStr($v["info_view"],$catid); } if($_POST["add_role_".$v[id]]){ $saveData["info_add"]=$this->addCatidStr($v["info_add"],$catid); }else{ $saveData["info_add"]=$this->removeCatidStr($v["info_add"],$catid); } if($_POST["editor_role_".$v[id]]){ $saveData["info_editor"]=$this->addCatidStr($v["info_editor"],$catid); }else{ $saveData["info_editor"]=$this->removeCatidStr($v["info_editor"],$catid); } if($_POST["delete_role_".$v[id]]){ $saveData["info_delete"]=$this->addCatidStr($v["info_delete"],$catid); }else{ $saveData["info_delete"]=$this->removeCatidStr($v["info_delete"],$catid); } if($_POST["checked_role_".$v[id]]){ $saveData["info_checked"]=$this->addCatidStr($v["info_checked"],$catid); }else{ $saveData["info_checked"]=$this->removeCatidStr($v["info_checked"],$catid); } M("role")->save($saveData); } } //更新权限里用到的函数 protected function addCatidStr($str,$catid){ if(strstr($str,"|".$catid."|")){ return $str; }else{ return $str."|".$catid."|"; } } //更新权限里用到的函数 protected function removeCatidStr($str,$catid){ return str_replace("|".$catid."|","", $str); } }