qc.ifish7.com/Application/Admin/Model/TableModel.class.php

299 lines
14 KiB
PHP
Raw 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;
class TableModel extends Model{
const mainTableSql = 'Data/modelSql/model_zhubiao.sql'; //模型主表SQL模板文件
const sideTablesSql = 'Data/modelSql/model_zhubiao_data.sql'; //模型副表SQL模板文件
const modelTablesInsert = 'Data/modelSql/model_insert.sql'; //可用默认模型字段
const modelFieldPath = 'Data/modelFieldTemp/'; //模型字段模板路径
const modelFieldMemberPath = 'Data/modelFieldTempMember/'; //会员模型字段模板路径
//自动验证
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
protected $_validate = array(
array('name','require','请填写模型名称!',1),
array('name', '', '该模型名称已经存在!', 0, 'unique', 1),
array('table_name','require','请填写表名!',1),
array('table_name','/^[a-z_0-9wd_]+$/i', '表名只支持英文!',0,'regex',3),
array('table_name', 'checkTablesql', '创建模型所需要的SQL文件丢失创建失败', 1, 'callback', 3),
array('table_name', 'checkTablename', '该表名是系统保留或者已经存在,不允许创建!', 0, 'callback', 1),
);
//检查SQL文件是否存在
public function checkTablesql() {
//检查主表结构sql文件是否存在
if (!is_file(APP_PATH.self::mainTableSql)) {
return false;
}
if (!is_file(APP_PATH.self::sideTablesSql)) {
return false;
}
return true;
}
//增加模型
public function data_add(){
$data=$this->create();
$data['table_name'] = strtolower($data['table_name']);
$table_id = $this->add($data);
if ($table_id) {
//创建数据表
if ($this->createModel("cms_".$data['table_name'], $table_id,$data['name'])) {
//将模型表默认的系统字段创建htmlcode
$m_data=M("table_field")->where(array("table_id"=>$table_id))->select();
foreach($m_data as $rs){
$newdata=array();
$newdata['field_id']=$rs['field_id'];
unset($rs['field_id']);//销毁fieldid 便于重新创建字段htmlcode
$newdata['htmlcode']=$this->createFieldHtmlCode($rs);
$newdata['memberhtmlcode']=$this->createMemberFieldHtmlCode($rs);
M("table_field")->save($newdata);
}
return $table_id;
} else {
//表创建失败
$this->where(array("table_id" => $table_id))->delete();
$this->error = '数据表创建失败!';
return false;
}
} else {
return false;
}
//
return $data;
}
//修改模型
public function data_editor(){
$data=$this->create();
$data['table_name'] = strtolower($data['table_name']);//强制表名小写
$table_id=$data['table_id'];
$info = $this->where(array("table_id" => $table_id))->find();
if (empty($info)){
$this->error = '数据表不存在!';
return false;
}
$this->save($data);
//修改model表中的table_name
M("model")->where(array("table_id"=>$table_id))->save(array("table_name"=>$data['table_name']));
//修改备注
//表前缀
$dbPrefix = C("DB_PREFIX")."cms_";
//主表
if (!$this->sql_execute("ALTER TABLE `{$dbPrefix}{$info['table_name']}` COMMENT='".$data['name']."';")) {
$this->error = '数据库修改表名失败!';
return false;
}
//副表
if (!$this->sql_execute("ALTER TABLE `{$dbPrefix}{$info['table_name']}_data` COMMENT='".$data['name']."-副表"."';")) {
//主表未修改成功,进行回滚
$this->sql_execute("ALTER TABLE `{$dbPrefix}{$info['table_name']}` COMMENT='".$info['name']."';");
$this->error = '数据库修改副表表名失败!';
return false;
}
//检查是否更改表名了
if ($info['table_name'] != $data['table_name'] && !empty($data['table_name'])) {
//检查新表名是否存在
if ($this->table_exists("cms_" . $data['table_name']) || $this->table_exists("cms_" . $data['table_name'] . '_data')) {
$this->error = '该表名已经存在!';
return false;
}
//表名更改
if (!$this->sql_execute("RENAME TABLE `{$dbPrefix}{$info['table_name']}` TO `{$dbPrefix}{$data['table_name']}` ;")) {
$this->error = '数据库修改表名失败!';
return false;
}
//修改副表
if (!$this->sql_execute("RENAME TABLE `{$dbPrefix}{$info['table_name']}_data` TO `{$dbPrefix}{$data['table_name']}_data` ;")) {
//主表未修改成功,进行回滚
$this->sql_execute("RENAME TABLE `{$dbPrefix}{$data['table_name']}` TO `{$dbPrefix}{$info['table_name']}` ;");
$this->error = '数据库修改副表表名失败!';
return false;
}
}
//
return $data;
}
//删除模型
public function data_delete($table_id) {
if (empty($table_id)){
$this->error = 'id不存在';
return false;
}
$data = $this->where(array("table_id" => $table_id))->find();
if (!$data) {
$this->error = '数据表不存在!';
return false;
}
//检查该模型下是否有分类
$model = M("model")->where(array("table_id" => $table_id))->find();
if ($model) {
$this->error = '该数据表下有模型,请先删除模型!';
return false;
}
//检查新表名是否存在(如果存在需要验证该表里是否有数据)
if ($this->table_exists("cms_" . $data['table_name'])) {
$count=M("cms_".$data['table_name'])->count();
if($count>0){
$this->error = '该模型下有数据,请先删除数据!';
return false;
}
}
//表名
$table_name = $data['table_name'];
//删除模型数据
$this->where(array("table_id" => $table_id))->delete();
//删除所有和这个模型相关的字段
D("TableField")->where(array("table_id" => $table_id))->delete();
//删除主表
$this->deleteTable("cms_".$table_name);
//删除副表
$this->deleteTable("cms_".$table_name . "_data");
return true;
}
/**
* 创建内容模型
* @param type $tableName 模型主表名称(不包含表前缀)
* @param type $modelId 模型id
* @return boolean
*/
protected function createModel($tableName,$table_id,$name){
if (empty($tableName) || $table_id < 1) {
return false;
}
//表前缀
$dbPrefix = C("DB_PREFIX");
//读取模型主表SQL模板
$mainTableSqll = file_get_contents(APP_PATH.self::mainTableSql);
//副表
$sideTablesSql = file_get_contents(APP_PATH.self::sideTablesSql);
//字段数据
$modelTablesInsert = file_get_contents(APP_PATH.self::modelTablesInsert);
//表备注
$zhubiao_note=$name;
$fubiao_note=$name."-副表";
//表前缀表名模型id替换
$sqlSplit = str_replace(array('@cms@', '@zhubiao@', '@table_id@', '@zhubiao_note@', '@fubiao_note@'), array($dbPrefix, $tableName, $table_id, $zhubiao_note, $fubiao_note), $mainTableSqll . "\n" . $sideTablesSql . "\n" . $modelTablesInsert);
$this->execute($sqlSplit);
return $table_id;
}
/*
* 创建字段html代码
*/
public function createFieldHtmlCode($data){
if($data['field_id']){
$info=M("table_field")->find($data['field_id']);
//当字段类型 and 宽度 and 高度 and 默认值 未改变的时候htmlcode等于提交过来的否则执行重新生成动作
if(
$data['htmlcode']
&&$info['field']==$data['field']
&&$info['name']==$data['name']
&&$info['formtype']==$data['formtype']
&&$info['formwidth']==$data['formwidth']
&&$info['formheight']==$data['formheight']
&&$info['defaultval']==$data['defaultval']
&&$info['tips']==$data['tips']
&&$info['imgwidth']==$data['imgwidth']
){
$htmlcode=$data['htmlcode'];
}else{
$htmlcode=$this->FieldHtmlCodeReplace($data);
}
}else{
$htmlcode=$this->FieldHtmlCodeReplace($data);
}
return $htmlcode;
}
/*
* 创建会员投入字段html代码
*/
public function createMemberFieldHtmlCode($data){
if($data['field_id']){
$info=M("table_field")->find($data['field_id']);
//当字段类型 and 宽度 and 高度 and 默认值 未改变的时候htmlcode等于提交过来的否则执行重新生成动作
if(
$data['memberhtmlcode']
&&$info['field']==$data['field']
&&$info['name']==$data['name']
&&$info['formtype']==$data['formtype']
&&$info['formwidth']==$data['formwidth']
&&$info['formheight']==$data['formheight']
&&$info['defaultval']==$data['defaultval']
&&$info['tips']==$data['tips']
){
$htmlcode=$data['memberhtmlcode'];
}else{
$htmlcode=$this->FieldHtmlCodeReplace($data,1);
}
}else{
$htmlcode=$this->FieldHtmlCodeReplace($data,1);
}
return $htmlcode;
}
/*
* 替换字段类型的代码
*
*/
public function FieldHtmlCodeReplace($rs,$isMember=0){
//获取表单类型的模板文件
if($isMember){
$field_temp= file_get_contents(APP_PATH.self::modelFieldMemberPath.$rs['formtype'].".php");
}else{
$field_temp= file_get_contents(APP_PATH.self::modelFieldPath.$rs['formtype'].".php");
}
//内容替换
$field_temp= str_replace("{modelFieldTemp_field}",$rs['field'],$field_temp);
$field_temp= str_replace("{tips}",$rs['tips'],$field_temp);
$field_temp= str_replace("{defaultval}",$rs['defaultval'],$field_temp);
//宽度
if($rs['formwidth']){
$style_formwidth="width:".$rs['formwidth']."%;";
}else{
$style_formwidth="";
}
$field_temp= str_replace("{style_formwidth}",$style_formwidth,$field_temp);
//高度
if($rs['formheight']){
$style_formheight="height:".$rs['formheight']."px;";
}else{
$style_formheight="";
}
$field_temp= str_replace("{style_formheight}",$style_formheight,$field_temp);
//编辑器高度 宽度替换因为编辑器只调用数值不需要css样式
$field_temp= str_replace("{imgwidth}",$rs['imgwidth'],$field_temp);
$field_temp= str_replace("{imgheight}",$rs['imgheight'],$field_temp);
$field_temp= str_replace("{formwidth}",$rs['formwidth']."%",$field_temp);
$field_temp= str_replace("{formheight}",$rs['formheight'],$field_temp);
//图片上传高度宽度替换
//checkbox radio select选项替换
$checkbox_option="";
if($rs['formtype']=="checkbox"||$rs['formtype']=="radio"||$rs['formtype']=="select"){
if($rs['defaultval']){
$defaultval=explode("\r\n",$rs['defaultval']);
foreach($defaultval as $v){
$dufault_arr=explode(":",$v);
$option_arr=explode("==",$dufault_arr[0]);
$select_checked=$dufault_arr[1]=="default"?'<?=$r?"":"selected"?>':"";//select 默认选择中的
$checked_default=$dufault_arr[1]=="default"?'<?=$r?"":"checked"?>':"";//checked radio 默认选择中的
$option_name=trim($option_arr[0]);//字段名称
$option_value=trim($option_arr[1]==""?$option_arr[0]:$option_arr[1]);//字段值 为空时=名称
if($rs['formtype']=="checkbox"){
$checkbox_option.='<label class="label-radio"><input type="checkbox" name="'.$rs['field'].'[]" value="'.$option_value.'" class="modelform_title" <?=strstr($r[\''.$rs['field'].'\'],\'|'.$option_value.'|\')?"checked":""?> '.$checked_default.'/>'.$option_name.'</label>';
}
if($rs['formtype']=="radio"){
$checkbox_option.='<label class="label-radio"><input type="radio" name="'.$rs['field'].'" value="'.$option_value.'" class="modelform_title" <?=$r[\''.$rs['field'].'\']==\''.$option_value.'\'?"checked":""?> '.$checked_default.'/>'.$option_name.'</label>';
}
if($rs['formtype']=="select"){
$checkbox_option.='<option value="'.$option_value.'" <?=$r[\''.$rs['field'].'\']==\''.$option_value.'\'?"selected":""?> '.$select_checked.'>'.$option_name.'</option>';
}
}
}
}
$field_temp= str_replace("{select_option}",$checkbox_option,$field_temp);
return $field_temp;
}
}