Files
qc.ifish7.com/Application/Admin/Model/TemplateModel.class.php
T
2021-04-18 18:51:51 +08:00

184 lines
7.6 KiB
PHP

<?php
namespace Admin\Model;
use Think\Model;
class TemplateModel extends Model{
//自动验证
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
protected $_validate = array(
array('name','require','请填写模板名称!',1),
array('type','require','缺少模板类型!',1),
array('myvar','checkmyvar','请填写变量名!',1,"callback"),
array('myvar','','变量名已经存在!',1,'unique'), // 在新增的时候验证name字段是否唯一
array('content','require','请填写模板代码!',1),
);
//当添加模板变量的时候必须填写myvar
public function checkmyvar(){
$type=I("type");
$myvar=I("myvar");
if($type=="public"&&!$myvar){
return false;
}else{
return true;
}
}
/*
* 模板替换
*/
public function replaceTemplate($content){
//转码
$content=htmlspecialchars_decode($content);
//公共模板替换
$content= preg_replace("/\[!--public.(.+?)--\]/","<include file='".C("CMS_TEMP_PATH")."\\1.html'/>",$content);
//变量替换
$content= preg_replace("/\[!--(.+?)--\]/","{\$\\1}",$content);
//万能标签替换
$content= $this->cmsinfo_replace($content);
return $content;
}
/*
万能标签替换
* */
public function cmsinfo_replace($content){
preg_match_all("/<cmsinfo(.+?)\"\s*>/",$content,$myvar);
if(count($myvar)){
foreach($myvar[0] as $v){
$cmsinfo=$v;
//
$str="<?php \r\n";//要替换后的str
preg_match_all('/\[(\w+)\]\s*=\s*"(.*?)"/',$cmsinfo,$m1);//匹配[字段] 带有中括号的字段名表示该字段是动态判断的,即存在就增加该条件,不存在删除该条件
preg_match_all('/(\w+)\s*=\s*"(.*?)"/',$cmsinfo,$m2);//匹配正常字段
$parameter1= array_combine($m1[1],$m1[2]);
$parameter2= array_combine($m2[1],$m2[2]);
$parameter=array_merge((array)$parameter1,(array)$parameter2);//这里要强制置换一下array,否则当其中一个为null的时候返回为空,出处:https://blog.csdn.net/htmlgood/article/details/49557075
$parameter= array_filter($parameter);//过滤空数组
//sql方式
if($parameter["sql"]){
$str.='
$Model = new \Think\Model;
$result=$Model->query("'.$parameter["sql"].'");
';
}
//指定表方式
else if($parameter["table"]||$parameter["catid"]){
if(!$parameter["table"]){
//如果没有指定table,则从catid里查询表名
$catid_arr=explode(",",$parameter["catid"]);
$cat=M("cat")->where(array("catid"=>$catid_arr[0]))->find();
$modelid=$cat["modelid"];
$table_name=$cat["table_name"];
$parameter["table"]="cms_".$table_name;
}
if($parameter["catid"]){
//如果是int形返回当前栏目和子栏目catid
if(is_numeric($parameter["catid"])){
$son=getSonCat($parameter["catid"]);
if(count($son)>1){
$parameter["catid"]=array("IN",$son);
}else{
$parameter["catid"]=$son[0];
}
}
//字符串形式的话,返回array("IN","1,2,3")
else{
$catid_num=explode(",",$parameter["catid"]);
$parameter["catid"]=array("IN",$parameter["catid"]);
}
}
//
$where=$parameter;
unset($where["sql"]);
unset($where["table"]);
unset($where["limit"]);
unset($where["order"]);
unset($where["field"]);
unset($where["group"]);
unset($where["having"]);
unset($where["join"]);
unset($where["union"]);
unset($where["distinct"]);
$where = var_export($where,true);
$where= preg_replace("/'array\((.*)\)',/","array(\\1),", $where);//去掉数组外的单引号
$where= preg_replace("/=> '\\$(.+)',/U","=> $\\1,", $where);//如果数组里包含变更的话,去掉单引号
$where= stripslashes($where);//去掉转义字符
$str.='$mwhere='.$where.';';
preg_match_all('/ \[(\w+)\]=/',$cmsinfo,$autofield);
foreach($autofield[1] as $vv){
$str.='
if(!$mwhere[\''.$vv.'\']){
unset($mwhere[\''.$vv.'\']);
}
';
}
$str.='$result=M("'.$parameter["table"].'")';
//相关条件字段判断
if($where){
$str.='->where($mwhere)';
}
if($parameter["limit"]){
$str.='->limit('.$parameter["limit"].')';
}
if($parameter["order"]){
$str.='->order("'.$parameter["order"].'")';
}
if($parameter["field"]){
$str.='->field("'.$parameter["field"].'")';
}
if($parameter["group"]){
$str.='->group("'.$parameter["group"].'")';
}
if($parameter["having"]){
$str.='->having("'.$parameter["having"].'")';
}
if($parameter["join"]){
$str.='->join("'.$parameter["join"].'")';
}
if($parameter["union"]){
$str.='->union("'.$parameter["union"].'")';
}
if($parameter["distinct"]){
$str.='->distinct("'.$parameter["distinct"].'")';
}
$str.='->select();';
}
//PHP代码
$str.='
$no=1;
foreach($result as $key=>$v){
if($v[id]&&$v[catid]){
$v[titleurl]=titleurl($v);
}
if($v[catid]){
$cat=M("cat")->find($v[catid]);
$v[caturl]=caturl($cat[catid]);
}
?>';
//
$content=str_replace($cmsinfo,$str,$content);
}
}
$content=str_replace("</cmsinfo>",'<?php $no++; }?>',$content);
return $content;
}
//查询公共模板
public function getTempTemplate($myvar){
$template=$this->where(array("myvar"=>$myvar))->getField("content");
$template=htmlspecialchars_decode($template);
return $template;
}
//生成template文件
public function makeTemplateFile($template_id){
$template_r=$this->find($template_id);
$template_r["content"]=$this->replaceTemplate($template_r["content"]);
//如果模板类型是公共模板的话文件名以myvar的值命名
if($template_r["type"]=="public"){
file_put_contents(C("CMS_TEMP_PATH").$template_r["myvar"].".html",$template_r["content"]);
}else{
file_put_contents(C("CMS_TEMP_PATH").$template_id.".html",$template_r["content"]);
}
}
//删除模板文件
public function deleteTemplateFile($template_id){
unlink(C("CMS_TEMP_PATH").$template_id.".html");
}
}