Initial commit

This commit is contained in:
易焱
2021-04-18 18:51:51 +08:00
commit ec705f2fa8
3240 changed files with 623103 additions and 0 deletions
@@ -0,0 +1,395 @@
<?php
/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 永无BUG
*/
namespace Admin\Model;
use Think\Model;
class ModelModel extends Model{
const modelTempPath = 'Data/modelTemp/'; //模型表单模板路径
const modelTempMemberPath = 'Data/modelTempMember/'; //会员模型表单模板路径
//自动验证
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
protected $_validate = array(
array('name','require','请填写模型名称!',1),
array('name', '', '该模型名称已经存在!', 0, 'unique', 1),
array('table_id','require','缺少table_id',1,0,self::MODEL_INSERT),
array('table_id','check_table','数据表不存在!',1,"callback",self::MODEL_INSERT),
);
//自动完成
protected $_auto = array (
array('table_name','get_table_name',self::MODEL_INSERT,'callback'), //表名
array('formtemp','get_formtemp',3,'callback'), //表单的form内容
);
//自动验证-table_id对应的信息是否存在
public function check_table($table_id){
if(M("table")->find($table_id)){
return true;
}else{
return false;
}
}
//自动完成-获取表名
public function get_table_name(){
$table_id=I("post.table_id",0,"int");
return M("table")->where(array("table_id"=>$table_id))->getField("table_name");
}
//自动完成-把数组转换为字符串便于存到数据里
public function arr2string($arr){
$str= implode(",", $arr);
if($str){
$str=",".$str.",";
}
return $str;
}
//自动完成-get_formtemp
//检查SQL文件是否存在!
public function get_formtemp() {
$field_name=I("post.field_name");
$field=I("post.field");
$str="";
foreach($field as $key=>$v){
$str.=$field_name[$key]."<!--field-->".$field[$key]."\r\n";
}
return trim($str);
}
//保存模型 增加和修改都调用此方法
public function data_save(){
$data=$this->create();
$data["is_enter"]=$this->arr2string(I("post.is_enter"));
$data["is_contribute"]=$this->arr2string(I("post.is_contribute"));
$data["must_enter"]=$this->arr2string(I("post.must_enter"));
$data["is_list"]=$this->arr2string(I("post.is_list"));
$data["is_search"]=$this->arr2string(I("post.is_search"));
$data["is_sort"]=$this->arr2string(I("post.is_sort"));
//如果在新增模型的情况下设置栏目默认字段
if(!$data["modelid"]){
$data['cat_formtemp']="栏目名称<!--field-->name
缩略图<!--field-->thumb
每页显示<!--field-->lencord
是否为单面模式<!--field-->is_page
列表模板<!--field-->listtemp
内容模板<!--field-->viewtemp
状态<!--field-->status
SEO标题<!--field-->pagetitle
关键词<!--field-->keywords
描述<!--field-->description
";
$data['cat_is_enter']=",name,thumb,lencord,is_page,listtemp,viewtemp,status,pagetitle,keywords,description,";
$data['cat_must_enter']=",name,listtemp,viewtemp,status,";
}
if($data["modelid"]){
$this->save($data);
$modelid = $data["modelid"];
}else{
$modelid = $this->add($data);
$data["modelid"]=$modelid;
}
if ($modelid) {
$this->updateModelTemp($modelid);
$this->updateModelTempMember($modelid);
$this->updateCatModelTemp($modelid);//默认增加模型会勾选栏目模型字段
//更新缓存数据
$this->updateCache();
} else {
return false;
}
//
return $data;
}
//保存栏目模型
public function saveCatModel(){
$data["modelid"]=I("post.modelid");
$data["cat_is_enter"]=$this->arr2string(I("post.cat_is_enter"));
$data["cat_must_enter"]=$this->arr2string(I("post.cat_must_enter"));
//栏目模型字段
$field_name=I("post.field_name");
$field=I("post.field");
$str="";
foreach($field as $key=>$v){
$str.=$field_name[$key]."<!--field-->".$field[$key]."\r\n";
}
$data["cat_formtemp"]=$str;
$this->save($data);
//
$modelid = $data["modelid"];
if ($modelid) {
$this->updateCatModelTemp($modelid);
//更新缓存数据
$this->updateCache();
} else {
return false;
}
//
return $data;
}
//删除模型
public function data_delete($modelid) {
if (empty($modelid)){
$this->error = 'id不存在!';
return false;
}
$data = $this->where(array("modelid" => $modelid))->find();
if (!$data) {
$this->error = '模型不存在!';
return false;
}
//检查该模型下是否有分类
$cat = M("cat")->where(array("modelid" => $modelid))->find();
if ($cat) {
$this->error = '该模型下有分类,请先删除分类!';
return false;
}
//删除模型数据
$this->where(array("modelid" => $modelid))->delete();
//更新缓存数据
$this->updateCache();
//删除模型表单文件
unlink(APP_PATH.self::modelTempPath.$modelid.".php");
unlink(APP_PATH.self::modelTempMemberPath.$modelid.".php");
return true;
}
/**
* 更新模型模板文件
* @param type $modelId 模型id
* @return boolean
*/
public function updateModelTemp($modelId){
$model=$this->find($modelId);
//将model里的is_enter is_contribute must_enter is_list is_search is_sort 依次拿出来和table_field对比,如果不存在就删除(防止因删除字段和修改字段名而造成model里的这些字段不更新导致的错误)
$field=M("table_field")->field("field")->order("sort asc,field_id asc")->where(array("table_id"=>$model["table_id"]))->select();
$new["modelid"]=$modelId;
foreach($field as $v){
//录入项
if(strstr($model["is_enter"], ",".$v["field"].",")){
$new["is_enter"].=",".$v["field"];
}
//投稿
if(strstr($model["is_contribute"], ",".$v["field"].",")){
$new["is_contribute"].=",".$v["field"];
}
//必填项
if(strstr($model["must_enter"], ",".$v["field"].",")){
$new["must_enter"].=",".$v["field"];
}
//列表展示
if(strstr($model["is_list"], ",".$v["field"].",")){
$new["is_list"].=",".$v["field"];
}
//搜索项
if(strstr($model["is_search"], ",".$v["field"].",")){
$new["is_search"].=",".$v["field"];
}
//排序项
if(strstr($model["is_sort"], ",".$v["field"].",")){
$new["is_sort"].=",".$v["field"];
}
}
$new["is_enter"]=$new["is_enter"]?$new["is_enter"].",":"";
$new["is_contribute"]=$new["is_contribute"]?$new["is_contribute"].",":"";
$new["must_enter"]=$new["must_enter"]?$new["must_enter"].",":"";
$new["is_list"]=$new["is_list"]?$new["is_list"].",":"";
$new["is_search"]=$new["is_search"]?$new["is_search"].",":"";
$new["is_sort"]=$new["is_sort"]?$new["is_sort"].",":"";
$this->save($new);
//
$formtemp= explode("\r\n",$model["formtemp"]);
$modelTemp="";
foreach($formtemp as $key=>$v){
$f=explode("<!--field-->",$v);
if(!strstr($new["is_enter"], ",".$f[1].",")){
continue;;
}
$field=M("table_field")->where(array("table_id"=>$model["table_id"],"field"=>$f[1]))->find();
if($field){
$htmlcode=$field["htmlcode"];
$htmlcode= str_replace("{modelFieldTemp_name}", $f[0],$htmlcode);//名称替换
$modelTemp=$modelTemp."\r\n".$htmlcode;
}
}
//
$modelTemp=htmlspecialchars_decode($modelTemp);
file_put_contents(APP_PATH.self::modelTempPath.$modelId.".php",$modelTemp);
return true;
}
//
//获取批量导入excel模板,用于用户在后台批量导入数据的
public function getExportExcelDemo($modelId,$cat_type){
$model=$this->find($modelId);
$formtemp= explode("\r\n",$model["formtemp"]);
foreach($formtemp as $key=>$v){
$f=explode("<!--field-->",$v);
if(!strstr($model["is_enter"], ",".$f[1].",")){
continue;;
}
$data[]=$f[0]."==".$f[1];
}
Vendor('PHPExcel.PHPExcel');
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load(APP_PATH."Data/excelDemo/export_template.xls");
$sheet=$objPHPExcel->setActiveSheetIndex(0);
//设置第一列为栏目字段
if($cat_type=="catName"){
$sheet ->setCellValue("A1","栏目名称==catid");
$sheet->getColumnDimension("A")->setAutoSize(true);
}elseif($cat_type=="catId"){
$sheet ->setCellValue("A1","栏目ID==catid");
$sheet->getColumnDimension("A")->setAutoSize(true);
}else{
}
//
foreach($data as $key=>$v){
if($cat_type!="selfCheck"){
$key2=$key+1;
}else{
$key2=$key;
}
$no=IntToChr($key2);
$sheet ->setCellValue($no."1",$v);
$sheet->getColumnDimension($no)->setAutoSize(true);
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$model["name"].'-数据导入结构.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
/**
* 更新模型模板文件(会员投稿表单)
* @param type $modelId 模型id
* @return boolean
*/
public function updateModelTempMember($modelId){
$model=$this->find($modelId);
//将model里的is_enter is_contribute must_enter is_list is_search is_sort 依次拿出来和table_field对比,如果不存在就删除(防止因删除字段和修改字段名而造成model里的这些字段不更新导致的错误)
$field=M("table_field")->field("field")->order("sort asc,field_id asc")->where(array("table_id"=>$model["table_id"]))->select();
$new["modelid"]=$modelId;
foreach($field as $v){
//录入项
if(strstr($model["is_enter"], ",".$v["field"].",")){
$new["is_enter"].=",".$v["field"];
}
//投稿
if(strstr($model["is_contribute"], ",".$v["field"].",")){
$new["is_contribute"].=",".$v["field"];
}
//必填项
if(strstr($model["must_enter"], ",".$v["field"].",")){
$new["must_enter"].=",".$v["field"];
}
//列表展示
if(strstr($model["is_list"], ",".$v["field"].",")){
$new["is_list"].=",".$v["field"];
}
//搜索项
if(strstr($model["is_search"], ",".$v["field"].",")){
$new["is_search"].=",".$v["field"];
}
//排序项
if(strstr($model["is_sort"], ",".$v["field"].",")){
$new["is_sort"].=",".$v["field"];
}
}
$new["is_enter"]=$new["is_enter"]?$new["is_enter"].",":"";
$new["is_contribute"]=$new["is_contribute"]?$new["is_contribute"].",":"";
$new["must_enter"]=$new["must_enter"]?$new["must_enter"].",":"";
$new["is_list"]=$new["is_list"]?$new["is_list"].",":"";
$new["is_search"]=$new["is_search"]?$new["is_search"].",":"";
$new["is_sort"]=$new["is_sort"]?$new["is_sort"].",":"";
$this->save($new);
//
$formtemp= explode("\r\n",$model["formtemp"]);
$modelTemp="";
foreach($formtemp as $key=>$v){
$f=explode("<!--field-->",$v);
if(!strstr($new["is_contribute"], ",".$f[1].",")){
continue;;
}
$field=M("table_field")->where(array("table_id"=>$model["table_id"],"field"=>$f[1]))->find();
if($field){
$htmlcode=$field["memberhtmlcode"];
$htmlcode= str_replace("{modelFieldTemp_name}", $f[0],$htmlcode);//名称替换
$modelTemp=$modelTemp."\r\n".$htmlcode;
}
}
$modelTemp=htmlspecialchars_decode($modelTemp);
file_put_contents(APP_PATH.self::modelTempMemberPath.$modelId.".php",$modelTemp);
return true;
}
/**
* 更新模型模板文件
* @param type $modelId 模型id
* @return boolean
*/
public function updateCatModelTemp($modelId){
$model=$this->find($modelId);
//将model里的is_enter must_enter 依次拿出来和cat_field对比,如果不存在就删除(防止因删除字段和修改字段名而造成model里的这些字段不更新导致的错误)
$field=M("cat_field")->field("field")->order("sort asc,field_id asc")->select();
$new["modelid"]=$modelId;
foreach($field as $v){
//录入项
if(strstr($model["cat_is_enter"], ",".$v["field"].",")){
$new["cat_is_enter"].=",".$v["field"];
}
//必填项
if(strstr($model["cat_must_enter"], ",".$v["field"].",")){
$new["cat_must_enter"].=",".$v["field"];
}
}
$new["cat_is_enter"]=$new["cat_is_enter"]?$new["cat_is_enter"].",":"";
$new["cat_must_enter"]=$new["cat_must_enter"]?$new["cat_must_enter"].",":"";
$this->save($new);
//
$formtemp= explode("\r\n",$model["cat_formtemp"]);
$modelTemp="";
foreach($formtemp as $key=>$v){
$f=explode("<!--field-->",$v);
if(!strstr($new["cat_is_enter"], ",".$f[1].",")){
continue;
}
$field=M("cat_field")->where(array("field"=>$f[1]))->find();
if($field){
$htmlcode=$field["htmlcode"];
$htmlcode= str_replace("{modelFieldTemp_name}", $f[0],$htmlcode);//名称替换
$modelTemp=$modelTemp."\r\n".$htmlcode;
}
}
$modelTemp=htmlspecialchars_decode($modelTemp);
file_put_contents(APP_PATH.self::modelTempPath.$modelId."_cat.php",$modelTemp);
return true;
}
//更新模型缓存
public function updateCache(){
$cat=$this->select();
$newcat=array();
foreach($cat as $v){
$newcat[$v["modelid"]]=$v;
}
$arr_str=var_export ($newcat,true);
$arr_str="<?php \r\n \$GLOBALS['model']=".$arr_str.";";
file_put_contents(C("IncCache_PATH")."model.php",$arr_str);
}
}