Initial commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class AdCatModel extends Model{
|
||||
//自动验证
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
protected $_validate = array(
|
||||
array('name','require','请填写广告名称!',1),
|
||||
array('type','require','请填写广告类型!',1),
|
||||
array('thumb','check_2','请上传广告图片!',1,"callback"),
|
||||
array('adcode','check_3','请填写广告代码!',1,"callback"),
|
||||
);
|
||||
public function check_2($val){
|
||||
if(I("type")=="单张图片"&&!$val){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function check_3($val){
|
||||
if(I("type")=="广告代码"&&!$val){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class AdCatModel extends Model{
|
||||
//自动验证
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
protected $_validate = array(
|
||||
array('title','require','请填写名称!',1),
|
||||
array('thumb','require','请上传广告图片!',1),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,532 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class CatFieldModel extends Model{
|
||||
//自动验证
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
protected $_validate = array(
|
||||
array('formtype', 'require', '字段类型不能为空!'),
|
||||
array('field', 'require', '字段名称必须填写!'),
|
||||
array('field', 'isFieldUnique', '该字段名称已经存在!', 0, 'callback', 1),
|
||||
array('name', 'require', '字段别名必须填写!'),
|
||||
array('field', '/^[a-z_0-9]+$/i', '字段名只支持英文或数字!', 0, 'regex', 3),
|
||||
);
|
||||
/**
|
||||
* 验证字段名是否已经存在
|
||||
* @param type $fieldName
|
||||
* @return boolean false已经存在,true不存在
|
||||
*/
|
||||
public function isFieldUnique($fieldName) {
|
||||
if (empty($fieldName)) {
|
||||
return true;
|
||||
}
|
||||
if ($this->where(array('field' => $fieldName))->count()){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 根据模型ID,返回表名
|
||||
* @param type $table_id
|
||||
* @param type $table_id
|
||||
* @return string
|
||||
*/
|
||||
protected function getTbName($issystem = 1) {
|
||||
$table_name="cat";
|
||||
//完整表名获取 判断主表 还是副表
|
||||
$table_name = $issystem ? $table_name : $table_name . "_data";
|
||||
return $table_name;
|
||||
}
|
||||
//增加
|
||||
public function data_add(){
|
||||
$data=$this->create();
|
||||
//数据表id
|
||||
$table_id = $data['table_id'];
|
||||
$fieldtype = $data['fieldtype'];
|
||||
//完整表名获取 判断主表 还是副表
|
||||
$table_name = $this->getTbName($data['issystem']);
|
||||
if (!$this->table_exists($table_name)) {
|
||||
$this->error = '数据表不存在!';
|
||||
return false;
|
||||
}
|
||||
//检查字段是否存在
|
||||
if ($this->field_exists($table_name, $data['field'])) {
|
||||
$this->error = '该字段已经存在!';
|
||||
return false;
|
||||
}
|
||||
|
||||
//增加字段
|
||||
$field = array(
|
||||
'tablename' => C("DB_PREFIX") . $table_name,
|
||||
'fieldname' => $data['field'],
|
||||
'fieldlen' => $data['fieldlen'],
|
||||
'defaultvalue' => $data['defaultvalue'],
|
||||
);
|
||||
if ($this->addFieldSql($fieldtype, $field)) {
|
||||
//更新htmlcode
|
||||
$data['htmlcode']=$this->createFieldHtmlCode($data);
|
||||
//
|
||||
$fieldid = $this->add($data);
|
||||
if ($fieldid) {
|
||||
return $fieldid;
|
||||
} else {
|
||||
$this->error = '字段信息入库失败!';
|
||||
//回滚
|
||||
$this->execute("ALTER TABLE `{$field['tablename']}` DROP `{$field['fieldname']}`");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//修改
|
||||
public function data_editor(){
|
||||
$data=$this->create();
|
||||
if (!$data['field_id']) {
|
||||
$this->error = '缺少字段id!';
|
||||
return false;
|
||||
} else {
|
||||
$field_id = $field_id ? $field_id : (int) $data['field_id'];
|
||||
}
|
||||
//重置htmlcode
|
||||
$data['htmlcode']=htmlspecialchars_decode($data['htmlcode']);
|
||||
$data['htmlcode']=$this->createFieldHtmlCode($data);
|
||||
//原字段信息
|
||||
$info = $this->where(array("field_id" => $field_id))->find();
|
||||
if (empty($info)){
|
||||
$this->error = '该字段不存在!';
|
||||
return false;
|
||||
}
|
||||
//字段主表副表不能修改
|
||||
unset($data['issystem']);
|
||||
//字段类型
|
||||
if (empty($data['formtype'])) {
|
||||
$data['formtype'] = $info['formtype'];
|
||||
}
|
||||
//模型id
|
||||
$field_type = $data['fieldtype'];
|
||||
//完整表名获取 判断主表 还是副表
|
||||
$tablename = $this->getTbName($info['issystem']);
|
||||
if (!$this->table_exists($tablename)) {
|
||||
$this->error = '数据表不存在!';
|
||||
return false;
|
||||
}
|
||||
|
||||
if (false !== $this->save($data)) {
|
||||
//如果字段名变更
|
||||
if ($data['field'] && $info['field']) {
|
||||
//检查字段是否存在,只有当字段名改变才检测
|
||||
if ($data['field'] != $info['field'] && $this->field_exists($tablename, $data['field'])) {
|
||||
$this->error = '该字段已经存在!';
|
||||
//回滚
|
||||
$this->where(array("field_id" => $field_id))->save($info);
|
||||
return false;
|
||||
}
|
||||
$field = array(
|
||||
'tablename' => C("DB_PREFIX") . $tablename,
|
||||
'newfilename' => $data['field'],
|
||||
'oldfilename' => $info['field'],
|
||||
'fieldlen' => $data['fieldlen'],
|
||||
'defaultvalue' => $data['defaultvalue'],
|
||||
);
|
||||
if (false === $this->editFieldSql($field_type, $field)) {
|
||||
$this->error = '数据库字段结构更改失败!';
|
||||
//回滚
|
||||
$this->where(array("field_id" => $field_id))->save($info);
|
||||
return false;
|
||||
}
|
||||
//并更新catmodeltemp
|
||||
$model=M("model")->where(array("cat_is_enter"=>array("like","%".$data['field']."%")))->select();
|
||||
$ModelModel=new \Admin\Model\ModelModel();
|
||||
foreach($model as $v){
|
||||
$ModelModel->updateCatModelTemp($v["modelid"]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->error = '数据库更新失败!';
|
||||
return false;
|
||||
}
|
||||
//
|
||||
|
||||
//
|
||||
return $data;
|
||||
}
|
||||
//删除字段
|
||||
public function data_delete($field_id,$systemfield) {
|
||||
//原字段信息
|
||||
$info = $this->where(array("field_id" => $field_id))->find();
|
||||
if (empty($info)) {
|
||||
$this->error = '该字段不存在!';
|
||||
return false;
|
||||
}
|
||||
//模型id
|
||||
$table_id = $info['table_id'];
|
||||
//完整表名获取 判断主表 还是副表
|
||||
$tablename = $this->getTbName($info['issystem']);
|
||||
if (!$this->table_exists($tablename)) {
|
||||
$this->error = '数据表不存在!';
|
||||
return false;
|
||||
}
|
||||
//判断是否允许删除
|
||||
if (in_array($info['field'],$systemfield)){
|
||||
$this->error = '系统字段不允许被删除!';
|
||||
return false;
|
||||
}
|
||||
if ($this->deleteFieldSql($info['field'], C("DB_PREFIX") . $tablename)) {
|
||||
$this->where(array("field_id" => $field_id, "table_id" => $table_id))->delete();
|
||||
//根据table_id查询model表,并更新modeltemp
|
||||
$model=M("model")->where(array("table_id"=>$table_id))->select();
|
||||
$ModelModel=new \Admin\Model\ModelModel();
|
||||
foreach($model as $v){
|
||||
$ModelModel->updateModelTemp($v["modelid"]);
|
||||
}
|
||||
} else {
|
||||
$this->error = '数据库表字段删除失败!';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
/**
|
||||
* 根据字段类型,删除对应的字段到相应表里面
|
||||
* @param type $filename 字段名称
|
||||
* @param type $tablename 完整表名
|
||||
*/
|
||||
protected function deleteFieldSql($filename, $tablename) {
|
||||
//不带表前缀的表名
|
||||
$noprefixTablename = str_replace(C("DB_PREFIX"), '', $tablename);
|
||||
if (empty($tablename) || empty($filename)) {
|
||||
$this->error = '表名或者字段名不能为空!';
|
||||
return false;
|
||||
}
|
||||
|
||||
if (false === $this->table_exists($noprefixTablename)) {
|
||||
$this->error = '该表不存在!';
|
||||
return false;
|
||||
}
|
||||
$sql = "ALTER TABLE `{$tablename}` DROP `{$filename}`;";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段删除失败!';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 根据字段类型,增加对应的字段到相应表里面
|
||||
* @param type $field_type 字段类型
|
||||
* @param type $field 相关配置
|
||||
* $field = array(
|
||||
* 'tablename' 表名(完整表名)
|
||||
* 'fieldname' 字段名
|
||||
* 'maxlength' 最大长度
|
||||
* 'minlength' 最小值
|
||||
* 'defaultvalue' 默认值
|
||||
* 'minnumber' 是否正整数 和整数 1为正整数,-1是为整数
|
||||
* 'decimaldigits' 小数位数
|
||||
* )
|
||||
*/
|
||||
protected function addFieldSql($field_type, $field) {
|
||||
//表名
|
||||
$tablename = $field['tablename'];
|
||||
//字段名
|
||||
$fieldname = $field['fieldname'];
|
||||
//长度
|
||||
$fieldlen = $field['fieldlen'];
|
||||
switch ($field_type) {
|
||||
case "varchar":
|
||||
if (!$fieldlen) {
|
||||
$fieldlen = 255;
|
||||
}
|
||||
$fieldlen = min($fieldlen, 255);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` VARCHAR( {$fieldlen} ) NULL DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "tinyint":
|
||||
if (!$fieldlen) {
|
||||
$fieldlen = 3;
|
||||
}
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` TINYINT( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "smallint":
|
||||
$minnumber = intval($minnumber);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` SMALLINT( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "mediumint":
|
||||
$minnumber = intval($minnumber);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` INT ( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "int":
|
||||
$minnumber = intval($minnumber);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` INT ( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "mediumtext":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` MEDIUMTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "text":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` TEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "date":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` DATE DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "datetime":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "timestamp":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "double":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` DOUBLE NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "float":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` FLOAT( {$fieldlen} ) NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "bigint":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` BIGINT( {$fieldlen} ) NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "longtext":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` LONGTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "char":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` CHAR( {$fieldlen} ) NOT NULL DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 执行数据库表结构更改
|
||||
* @param type $field_type 字段类型
|
||||
* @param type $field 相关配置
|
||||
* $field = array(
|
||||
* 'tablename' 表名(完整表名)
|
||||
* 'newfilename' 新字段名
|
||||
* 'oldfilename' 原字段名
|
||||
* 'maxlength' 最大长度
|
||||
* 'minlength' 最小值
|
||||
* 'defaultvalue' 默认值
|
||||
* 'minnumber' 是否正整数 和整数 1为正整数,-1是为整数
|
||||
* 'decimaldigits' 小数位数
|
||||
* )
|
||||
*/
|
||||
protected function editFieldSql($field_type, $field) {
|
||||
//表名
|
||||
$tablename = $field['tablename'];
|
||||
//原字段名
|
||||
$oldfilename = $field['oldfilename'];
|
||||
//新字段名
|
||||
$newfilename = $field['newfilename'] ? $field['newfilename'] : $oldfilename;
|
||||
//长度
|
||||
$fieldlen = $field['fieldlen'];
|
||||
if (empty($tablename) || empty($newfilename)) {
|
||||
$this->error = '表名或者字段名不能为空!';
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($field_type) {
|
||||
case 'varchar':
|
||||
//最大值
|
||||
if (!$fieldlen) {
|
||||
$fieldlen = 255;
|
||||
}
|
||||
$fieldlen = min($fieldlen, 255);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` VARCHAR( {$fieldlen} ) NOT NULL DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'tinyint':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` TINYINT( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'smallint':
|
||||
$minnumber = intval($minnumber);
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` SMALLINT ( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT '{$defaultvalue}' DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'mediumint':
|
||||
$minnumber = intval($minnumber);
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` MEDIUMINT ( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT '{$defaultvalue}' DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'int':
|
||||
$minnumber = intval($minnumber);
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` INT ( {$fieldlen} ) NOT NULL DEFAULT '{$defaultvalue}' DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'mediumtext':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` MEDIUMTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'text':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` TEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'date':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` DATE DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'datetime':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'timestamp':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "double":
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` DOUBLE NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "float":
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` FLOAT(" . $minnumber . ") NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "bigint":
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` BIGINT NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "longtext":
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` LONGTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "char":
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` CHAR(" . $minnumber . ") NOT NULL DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$this->error = "字段类型" . $field_type . "不存在相应信息!";
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
* 创建字段html代码
|
||||
*/
|
||||
public function createFieldHtmlCode($data){
|
||||
$TableModel=new \Admin\Model\TableModel();
|
||||
if($data['field_id']){
|
||||
$info=$this->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']){
|
||||
$htmlcode=$data['htmlcode'];
|
||||
}else{
|
||||
$htmlcode=$TableModel->FieldHtmlCodeReplace($data);
|
||||
}
|
||||
}else{
|
||||
$htmlcode=$TableModel->FieldHtmlCodeReplace($data);
|
||||
}
|
||||
return $htmlcode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class CatModel extends RelationModel{
|
||||
protected $_validate,$_link,$modelinfo,$modelid,$fieldarr;
|
||||
public function __construct(){
|
||||
//模型id
|
||||
$modelid=I("modelid",0,"int");
|
||||
//这里为什么要加个判断呢,因为在刷新缓存的时候是不会传modelid,如果执行到下面就会出错了
|
||||
if($modelid){
|
||||
$this->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="<?php \r\n \$GLOBALS['cat']=".$arr_str.";";
|
||||
file_put_contents(C("IncCache_PATH")."cat.php",$arr_str);
|
||||
}
|
||||
//更新指定栏目的父级
|
||||
public function updateParent_catids($catid){
|
||||
$parent_catids=$this->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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class CraftModel extends RelationModel{
|
||||
|
||||
protected $_link = array(
|
||||
"craft_type"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'craft_type',
|
||||
'mapping_fields' => 'type_name',
|
||||
'foreign_key' => 'type_id',
|
||||
),
|
||||
"who_add"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'craft_who_add',
|
||||
),
|
||||
"who_modify"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'craft_who_modify',
|
||||
),
|
||||
|
||||
);
|
||||
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("craft_name","require","工艺名称不能为空"),
|
||||
array("craft_code","require","工艺代码不能为空"),
|
||||
array("type_id","require","工种不能为空"),
|
||||
array("craft_name","","工艺名称已存在",0,"unique"),
|
||||
array("craft_code","","工艺代码已存在",0,"unique"),
|
||||
);
|
||||
//保存信息
|
||||
public function data_save(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$member_id = UID;
|
||||
$data['craft_who_modify'] = $member_id;
|
||||
$data['craft_time_modify'] = time();
|
||||
$this->data($data)->save();
|
||||
return $data['craft_id'];
|
||||
}
|
||||
//保存信息
|
||||
public function data_add(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$member_id = UID;
|
||||
$data['craft_who_add'] = $member_id;
|
||||
$data['craft_time_add'] = time();
|
||||
$id = $this->data($data)->add();
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class CraftParamModel extends RelationModel{
|
||||
|
||||
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("param_name","require","工艺参数名称不能为空"),
|
||||
array("param_base","require","工艺参数参考值不能为空"),
|
||||
array("param_diff_up","require","工艺参数+偏差不能为空"),
|
||||
array("param_diff_down","require","工艺参数-偏差不能为空"),
|
||||
array("param_unit","require","工艺参数单位不能为空"),
|
||||
// array("param_default","require","默认值不能为空"),
|
||||
|
||||
);
|
||||
//保存信息
|
||||
public function data_save(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$this->data($data)->save();
|
||||
return $data['param_id'];
|
||||
}
|
||||
//保存信息
|
||||
public function data_add(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$id = $this->data($data)->add();
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class CraftPriceModel extends RelationModel{
|
||||
|
||||
protected $_link = array(
|
||||
// "craft_type"=>array(
|
||||
// 'mapping_type' => self::BELONGS_TO,
|
||||
// 'class_name' => 'craft_type',
|
||||
// 'mapping_fields' => 'type_name',
|
||||
// 'foreign_key' => 'type_id',
|
||||
// ),
|
||||
// "who_add"=>array(
|
||||
// 'mapping_type' => self::BELONGS_TO,
|
||||
// 'class_name' => 'user',
|
||||
// 'mapping_fields' => 'user_name',
|
||||
// 'foreign_key' => 'craft_who_add',
|
||||
// ),
|
||||
// "who_modify"=>array(
|
||||
// 'mapping_type' => self::BELONGS_TO,
|
||||
// 'class_name' => 'user',
|
||||
// 'mapping_fields' => 'user_name',
|
||||
// 'foreign_key' => 'craft_who_modify',
|
||||
// ),
|
||||
|
||||
);
|
||||
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("flow_craft_id","require","工艺id不能为空"),
|
||||
array("subtask_pass_num","require","价格不能为空"),
|
||||
array("subtask_feiliao_num","require","价格不能为空"),
|
||||
array("subtask_rangbu_num","require","价格不能为空"),
|
||||
array("subtask_jiangji_num","require","价格不能为空"),
|
||||
array("subtask_liewen_num","require","价格不能为空"),
|
||||
array("subtask_maopi_num","require","价格不能为空"),
|
||||
array("subtask_hunliao_num","require","价格不能为空"),
|
||||
array("subtask_baofei_num","require","价格不能为空"),
|
||||
array("subtask_shengxiu_num","require","价格不能为空"),
|
||||
array("subtask_tongzhi_num","require","价格不能为空"),
|
||||
array("subtask_sp_num","require","价格不能为空"),
|
||||
array("subtask_zhenquexian_num","require","价格不能为空"),
|
||||
array("subtask_quezhen_num","require","价格不能为空"),
|
||||
array("subtask_hunzhen_num","require","价格不能为空"),
|
||||
array("subtask_baofei_num","require","价格不能为空"),
|
||||
array("subtask_sanshi_num","require","价格不能为空"),
|
||||
array("subtask_qita_num","require","价格不能为空"),
|
||||
|
||||
// db_craft_price.subtask_pass_num,
|
||||
// db_craft_price.subtask_fanxiu_num,
|
||||
// db_craft_price.subtask_posun_num,
|
||||
// db_craft_price.subtask_feiliao_num,
|
||||
// db_craft_price.subtask_sanshi_num,
|
||||
// db_craft_price.subtask_rangbu_num,
|
||||
// db_craft_price.subtask_jiangji_num,
|
||||
// db_craft_price.subtask_liewen_num,
|
||||
// db_craft_price.subtask_maopi_num,
|
||||
// db_craft_price.subtask_hunliao_num,
|
||||
// db_craft_price.subtask_baofei_num,
|
||||
// db_craft_price.subtask_shengxiu_num,
|
||||
// db_craft_price.subtask_tongzhi_num,
|
||||
// db_craft_price.subtask_sp_num,
|
||||
// db_craft_price.subtask_zhenquexian_num,
|
||||
// db_craft_price.subtask_quezhen_num,
|
||||
// db_craft_price.subtask_hunzhen_num
|
||||
);
|
||||
//保存信息
|
||||
public function data_save(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
// $member_id = UID;
|
||||
// $data['craft_who_modify'] = $member_id;
|
||||
// $data['craft_time_modify'] = time();
|
||||
$this->data($data)->save();
|
||||
return $data['price_id'];
|
||||
}
|
||||
//保存信息
|
||||
public function data_add(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
// $member_id = UID;
|
||||
// $data['craft_who_add'] = $member_id;
|
||||
// $data['craft_time_add'] = time();
|
||||
$id = $this->data($data)->add();
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class CraftTemplateModel extends RelationModel{
|
||||
|
||||
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("template_name","require","参数模板名称不能为空"),
|
||||
|
||||
|
||||
);
|
||||
//保存信息
|
||||
public function data_save(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$this->data($data)->save();
|
||||
return $data['template_id'];
|
||||
}
|
||||
//保存信息
|
||||
public function data_add(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$id = $this->data($data)->add();
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class CraftTemplateParamModel extends RelationModel{
|
||||
|
||||
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("param_name","require","工艺参数名称不能为空"),
|
||||
array("param_base","require","工艺参数参考值不能为空"),
|
||||
array("param_diff_up","require","工艺参数+偏差不能为空"),
|
||||
array("param_diff_down","require","工艺参数-偏差不能为空"),
|
||||
array("param_unit","require","工艺参数单位不能为空"),
|
||||
|
||||
);
|
||||
//保存信息
|
||||
public function data_save(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$this->data($data)->save();
|
||||
return $data['param_id'];
|
||||
}
|
||||
//保存信息
|
||||
public function data_add(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$id = $this->data($data)->add();
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class CraftTypeModel extends RelationModel{
|
||||
|
||||
protected $_link = array(
|
||||
|
||||
"who_add"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'type_who_add',
|
||||
),
|
||||
"who_modify"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'type_who_modify',
|
||||
),
|
||||
|
||||
);
|
||||
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("type_name","require","工艺名称不能为空"),
|
||||
array("type_name","","工艺名称已存在",0,"unique"),
|
||||
|
||||
);
|
||||
//保存信息
|
||||
public function data_save(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$member_id = UID;
|
||||
$data['type_who_modify'] = $member_id;
|
||||
$data['type_time_modify'] = time();
|
||||
$this->data($data)->save();
|
||||
return $data['type_id'];
|
||||
}
|
||||
//保存信息
|
||||
public function data_add(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$member_id = UID;
|
||||
$data['type_who_add'] = $member_id;
|
||||
$data['type_time_add'] = time();
|
||||
$id = $this->data($data)->add();
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class FeedbackCatModel extends Model{
|
||||
const modelTempPath = 'Data/feedbackTemp/'; //模型表单模板路径
|
||||
//自动验证
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
protected $_validate = array(
|
||||
array('name','require','请填写名称!',1),
|
||||
);
|
||||
//自动完成
|
||||
protected $_auto = array (
|
||||
array('formtemp','get_formtemp',3,'callback'), //表单的form内容
|
||||
array('member_group','arr2string',3,'callback'), //表单的form内容
|
||||
);
|
||||
//自动完成-把数组转换为字符串便于存到数据里
|
||||
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["must_enter"]=$this->arr2string(I("post.must_enter"));
|
||||
if($data["catid"]){
|
||||
$this->save($data);
|
||||
$catid = $data["catid"];
|
||||
}else{
|
||||
$catid = $this->add($data);
|
||||
$data["catid"]=$catid;
|
||||
}
|
||||
if ($catid) {
|
||||
$this->updateModelTemp($catid);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
//
|
||||
return $data;
|
||||
}
|
||||
//删除模型
|
||||
public function data_delete($catid) {
|
||||
if (empty($catid)){
|
||||
$this->error = 'id不存在!';
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $this->where(array("catid" => $catid))->find();
|
||||
if (!$data) {
|
||||
$this->error = '分类不存在!';
|
||||
return false;
|
||||
}
|
||||
//检查该模型下是否有分类
|
||||
$count = M("feedback")->where(array("catid" => $catid))->count();
|
||||
if ($count) {
|
||||
$this->error = '该分类下有信息,请先删除信息!';
|
||||
return false;
|
||||
}
|
||||
//删除模型数据
|
||||
$this->where(array("catid" => $catid))->delete();
|
||||
//删除模型表单文件
|
||||
unlink(APP_PATH.self::modelTempPath.$catid.".php");
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 更新模型模板文件
|
||||
* @param type $catid 模型id
|
||||
* @return boolean
|
||||
*/
|
||||
public function updateModelTemp($catid){
|
||||
$cat=$this->find($catid);
|
||||
//将model里的is_enter is_contribute must_enter is_list is_search is_sort 依次拿出来和table_field对比,如果不存在就删除(防止因删除字段和修改字段名而造成model里的这些字段不更新导致的错误)
|
||||
$field=M("feedback_field")->field("field")->order("sort asc,field_id asc")->select();
|
||||
$new["catid"]=$catid;
|
||||
foreach($field as $v){
|
||||
//录入项
|
||||
if(strstr($cat["is_enter"], ",".$v["field"].",")){
|
||||
$new["is_enter"].=",".$v["field"];
|
||||
}
|
||||
//必填项
|
||||
if(strstr($cat["must_enter"], ",".$v["field"].",")){
|
||||
$new["must_enter"].=",".$v["field"];
|
||||
}
|
||||
}
|
||||
$new["is_enter"]=$new["is_enter"]?$new["is_enter"].",":"";
|
||||
$new["must_enter"]=$new["must_enter"]?$new["must_enter"].",":"";
|
||||
$this->save($new);
|
||||
//
|
||||
$formtemp= explode("\r\n",$cat["formtemp"]);
|
||||
$catTemp="";
|
||||
foreach($formtemp as $key=>$v){
|
||||
$f=explode("<!--field-->",$v);
|
||||
if(!strstr($new["is_enter"], ",".$f[1].",")){
|
||||
continue;
|
||||
}
|
||||
$field=M("feedback_field")->where(array("field"=>$f[1]))->find();
|
||||
if($field){
|
||||
$htmlcode=$field["htmlcode"];
|
||||
$htmlcode= str_replace("{modelFieldTemp_name}", $f[0],$htmlcode);//名称替换
|
||||
$catTemp=$catTemp."\r\n".$htmlcode;
|
||||
}
|
||||
}
|
||||
$catTemp=htmlspecialchars_decode($catTemp);
|
||||
file_put_contents(APP_PATH.self::modelTempPath.$catid.".php",$catTemp);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,557 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class FeedbackFieldModel extends Model{
|
||||
const modelFieldPath = 'Data/feedbackFieldTemp/'; //字段模板路径
|
||||
protected $table_name;
|
||||
public function __construct(){
|
||||
$this->table_name="feedback";
|
||||
//注意,这里父类析构函数一定要放到最下面,否则框架自带的方法不能使用
|
||||
parent::__construct();
|
||||
}
|
||||
//自动验证
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
protected $_validate = array(
|
||||
array('formtype', 'require', '字段类型不能为空!'),
|
||||
array('field', 'require', '字段名称必须填写!'),
|
||||
array('field', 'isFieldUnique', '该字段名称已经存在!', 0, 'callback', 1),
|
||||
array('name', 'require', '字段别名必须填写!'),
|
||||
array('field', '/^[a-z_0-9]+$/i', '字段名只支持英文或数字!', 0, 'regex', 3),
|
||||
);
|
||||
/**
|
||||
* 验证字段名是否已经存在
|
||||
* @param type $fieldName
|
||||
* @return boolean false已经存在,true不存在
|
||||
*/
|
||||
public function isFieldUnique($fieldName) {
|
||||
if (empty($fieldName)) {
|
||||
return true;
|
||||
}
|
||||
if ($this->where(array('field' => $fieldName))->count()){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//增加
|
||||
public function data_add(){
|
||||
$data=$this->create();
|
||||
$fieldtype = $data['fieldtype'];
|
||||
//检查字段是否存在
|
||||
if ($this->field_exists("feedback", $data['field'])) {
|
||||
$this->error = '该字段已经存在!';
|
||||
return false;
|
||||
}
|
||||
|
||||
//增加字段
|
||||
$field = array(
|
||||
'tablename' => C("DB_PREFIX") .$this->table_name,
|
||||
'fieldname' => $data['field'],
|
||||
'fieldlen' => $data['fieldlen'],
|
||||
'defaultvalue' => $data['defaultvalue'],
|
||||
);
|
||||
if ($this->addFieldSql($fieldtype, $field)) {
|
||||
//更新htmlcode
|
||||
|
||||
$data['htmlcode']=$this->createFieldHtmlCode($data);
|
||||
//
|
||||
$fieldid = $this->add($data);
|
||||
if (!$fieldid){
|
||||
$this->error = '字段信息入库失败!';
|
||||
//回滚
|
||||
$this->execute("ALTER TABLE `{$field['tablename']}` DROP `{$field['fieldname']}`");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
return $fieldid;
|
||||
}
|
||||
//修改模型
|
||||
public function data_editor(){
|
||||
$data=$this->create();
|
||||
if (!$data['field_id']) {
|
||||
$this->error = '缺少字段id!';
|
||||
return false;
|
||||
} else {
|
||||
$fieldid = $fieldid ? $fieldid : (int) $data['field_id'];
|
||||
}
|
||||
//重置htmlcode
|
||||
$data['htmlcode']=htmlspecialchars_decode($data['htmlcode']);
|
||||
$data['htmlcode']=$this->createFieldHtmlCode($data);
|
||||
//原字段信息
|
||||
$info = $this->where(array("field_id" => $fieldid))->find();
|
||||
if (empty($info)) {
|
||||
$this->error = '该字段不存在!';
|
||||
return false;
|
||||
}
|
||||
//字段类型
|
||||
if (empty($data['formtype'])) {
|
||||
$data['formtype'] = $info['formtype'];
|
||||
}
|
||||
$field_type = $data['fieldtype'];
|
||||
if (false !== $this->save($data)) {
|
||||
//如果字段名变更
|
||||
if ($data['field'] && $info['field']) {
|
||||
//检查字段是否存在,只有当字段名改变才检测
|
||||
if ($data['field'] != $info['field'] && $this->field_exists($this->table_name, $data['field'])) {
|
||||
$this->error = '该字段已经存在!';
|
||||
//回滚
|
||||
$this->where(array("field_id" => $fieldid))->save($info);
|
||||
return false;
|
||||
}
|
||||
$field = array(
|
||||
'tablename' => C("DB_PREFIX") . $this->table_name,
|
||||
'newfilename' => $data['field'],
|
||||
'oldfilename' => $info['field'],
|
||||
'fieldlen' => $data['fieldlen'],
|
||||
'defaultvalue' => $data['defaultvalue'],
|
||||
);
|
||||
if (false === $this->editFieldSql($field_type, $field)) {
|
||||
$this->error = '数据库字段结构更改失败!';
|
||||
//回滚
|
||||
$this->where(array("field_id" => $fieldid))->save($info);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->error = '数据库更新失败!';
|
||||
return false;
|
||||
}
|
||||
$cat=M("feedback_cat")->select();
|
||||
foreach($cat as $v){
|
||||
$this->updateModelTemp($v["catid"]);
|
||||
}
|
||||
//
|
||||
return $data;
|
||||
}
|
||||
//删除字段
|
||||
public function data_delete($fieldid) {
|
||||
//原字段信息
|
||||
$info = $this->where(array("field_id" => $fieldid))->find();
|
||||
if (empty($info)) {
|
||||
$this->error = '该字段不存在!';
|
||||
return false;
|
||||
}
|
||||
//完整表名获取 判断主表 还是副表
|
||||
$tablename = $this->table_name;
|
||||
if ($this->deleteFieldSql($info['field'], C("DB_PREFIX") . $tablename)) {
|
||||
$this->where(array("field_id" => $fieldid))->delete();
|
||||
} else {
|
||||
$this->error = '数据库表字段删除失败!';
|
||||
return false;
|
||||
}
|
||||
//modeltemp
|
||||
$cat=M("feedback_cat")->select();
|
||||
foreach($cat as $v){
|
||||
$this->updateModelTemp($v["catid"]);
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
/**
|
||||
* 重新生成模型表单文件
|
||||
* @param type $modelId 模型id
|
||||
*/
|
||||
public function updateModelTemp($catid){
|
||||
$ModelModel=new \Admin\Model\FeedbackCatModel();
|
||||
$ModelModel->updateModelTemp($catid);
|
||||
}
|
||||
/**
|
||||
* 根据字段类型,删除对应的字段到相应表里面
|
||||
* @param type $filename 字段名称
|
||||
* @param type $tablename 完整表名
|
||||
*/
|
||||
protected function deleteFieldSql($filename, $tablename) {
|
||||
//不带表前缀的表名
|
||||
$noprefixTablename = str_replace(C("DB_PREFIX"), '', $tablename);
|
||||
if (empty($tablename) || empty($filename)) {
|
||||
$this->error = '表名或者字段名不能为空!';
|
||||
return false;
|
||||
}
|
||||
|
||||
if (false === $this->table_exists($noprefixTablename)) {
|
||||
$this->error = '该表不存在!';
|
||||
return false;
|
||||
}
|
||||
$sql = "ALTER TABLE `{$tablename}` DROP `{$filename}`;";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段删除失败!';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 根据字段类型,增加对应的字段到相应表里面
|
||||
* @param type $field_type 字段类型
|
||||
* @param type $field 相关配置
|
||||
* $field = array(
|
||||
* 'tablename' 表名(完整表名)
|
||||
* 'fieldname' 字段名
|
||||
* 'maxlength' 最大长度
|
||||
* 'minlength' 最小值
|
||||
* 'defaultvalue' 默认值
|
||||
* 'minnumber' 是否正整数 和整数 1为正整数,-1是为整数
|
||||
* 'decimaldigits' 小数位数
|
||||
* )
|
||||
*/
|
||||
protected function addFieldSql($field_type, $field) {
|
||||
//表名
|
||||
$tablename = $field['tablename'];
|
||||
//字段名
|
||||
$fieldname = $field['fieldname'];
|
||||
//长度
|
||||
$fieldlen = $field['fieldlen'];
|
||||
switch ($field_type) {
|
||||
case "varchar":
|
||||
if (!$fieldlen) {
|
||||
$fieldlen = 255;
|
||||
}
|
||||
$fieldlen = min($fieldlen, 255);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` VARCHAR( {$fieldlen} ) NOT NULL DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "tinyint":
|
||||
if (!$fieldlen) {
|
||||
$fieldlen = 3;
|
||||
}
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` TINYINT( {$fieldlen} ) " . ($minnumber >= 0 ? 'UNSIGNED' : '') . " NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "smallint":
|
||||
$minnumber = intval($minnumber);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` SMALLINT " . ($minnumber >= 0 ? 'UNSIGNED' : '') . " NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "mediumint":
|
||||
$minnumber = intval($minnumber);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` INT " . ($minnumber >= 0 ? 'UNSIGNED' : '') . " NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "int":
|
||||
$minnumber = intval($minnumber);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` INT " . ($minnumber >= 0 ? 'UNSIGNED' : '') . " NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "mediumtext":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` MEDIUMTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "text":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` TEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "date":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` DATE DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "datetime":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "timestamp":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "double":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` DOUBLE NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "float":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` FLOAT NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "bigint":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` BIGINT NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "longtext":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` LONGTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "char":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` CHAR(255) NOT NULL DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 执行数据库表结构更改
|
||||
* @param type $field_type 字段类型
|
||||
* @param type $field 相关配置
|
||||
* $field = array(
|
||||
* 'tablename' 表名(完整表名)
|
||||
* 'newfilename' 新字段名
|
||||
* 'oldfilename' 原字段名
|
||||
* 'maxlength' 最大长度
|
||||
* 'minlength' 最小值
|
||||
* 'defaultvalue' 默认值
|
||||
* 'minnumber' 是否正整数 和整数 1为正整数,-1是为整数
|
||||
* 'decimaldigits' 小数位数
|
||||
* )
|
||||
*/
|
||||
protected function editFieldSql($field_type, $field) {
|
||||
//表名
|
||||
$tablename = $field['tablename'];
|
||||
//原字段名
|
||||
$oldfilename = $field['oldfilename'];
|
||||
//新字段名
|
||||
$newfilename = $field['newfilename'] ? $field['newfilename'] : $oldfilename;
|
||||
//长度
|
||||
$fieldlen = $field['fieldlen'];
|
||||
if (empty($tablename) || empty($newfilename)) {
|
||||
$this->error = '表名或者字段名不能为空!';
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($field_type) {
|
||||
case 'varchar':
|
||||
//最大值
|
||||
if (!$maxlength) {
|
||||
$maxlength = 255;
|
||||
}
|
||||
$maxlength = min($maxlength, 255);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` VARCHAR( {$maxlength} ) NOT NULL DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'tinyint':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` TINYINT " . ($minnumber >= 0 ? 'UNSIGNED' : '') . " NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'smallint':
|
||||
$minnumber = intval($minnumber);
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` SMALLINT " . ($minnumber >= 0 ? 'UNSIGNED' : '') . " NOT NULL DEFAULT '{$defaultvalue}' DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'mediumint':
|
||||
$minnumber = intval($minnumber);
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` MEDIUMINT " . ($minnumber >= 0 ? 'UNSIGNED' : '') . " NOT NULL DEFAULT '{$defaultvalue}' DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'int':
|
||||
$minnumber = intval($minnumber);
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` INT " . ($minnumber >= 0 ? 'UNSIGNED' : '') . " NOT NULL DEFAULT '{$defaultvalue}' DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'mediumtext':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` MEDIUMTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'text':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` TEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'date':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` DATE DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'datetime':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'timestamp':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "double":
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` DOUBLE NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "float":
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` FLOAT NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "bigint":
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` BIGINT NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "longtext":
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` LONGTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "char":
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` CHAR(255) NOT NULL DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$this->error = "字段类型" . $field_type . "不存在相应信息!";
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
* 创建字段html代码
|
||||
*/
|
||||
public function createFieldHtmlCode($data){
|
||||
if($data['field_id']){
|
||||
$info=M("feedback_field")->find($data['field_id']);
|
||||
//当字段类型 and 宽度 and 高度 and 默认值 未改变的时候htmlcode等于提交过来的,否则执行重新生成动作
|
||||
if($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['htmlcode'];
|
||||
}else{
|
||||
$htmlcode=$this->FieldHtmlCodeReplace($data);
|
||||
}
|
||||
}else{
|
||||
$htmlcode=$this->FieldHtmlCodeReplace($data);
|
||||
}
|
||||
return $htmlcode;
|
||||
}
|
||||
/*
|
||||
* 替换字段类型的代码
|
||||
*
|
||||
*/
|
||||
public function FieldHtmlCodeReplace($rs){
|
||||
//获取表单类型的模板文件
|
||||
$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']."px;";
|
||||
}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("{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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class FeedbackModel extends RelationModel{
|
||||
//关联模型
|
||||
protected $_link = array(
|
||||
//类别
|
||||
"feedback_cat"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'foreign_key' => 'catid',
|
||||
),
|
||||
//用户
|
||||
"member"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'mapping_fields' => "truename,member_id",
|
||||
'foreign_key' => 'member_id',
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class FlowCraftModel extends RelationModel{
|
||||
//关联模型
|
||||
Protected $_link = array(
|
||||
//【对应副表】
|
||||
"craft"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'foreign_key' => 'craft_id',
|
||||
'relation_deep' => true
|
||||
),
|
||||
"flow"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'foreign_key' => 'flow_id',
|
||||
),
|
||||
"price"=>array(
|
||||
'mapping_type' => self::HAS_ONE,
|
||||
'class_name' => 'craft_price',
|
||||
'foreign_key' => 'flow_craft_id',
|
||||
),
|
||||
);
|
||||
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("craft_id","require","工艺名称不能为空"),
|
||||
|
||||
);
|
||||
//保存信息
|
||||
public function data_save(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$this->data($data)->save();
|
||||
return $data['flow_craft_id'];
|
||||
}
|
||||
//保存信息
|
||||
public function data_add(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$craft_ids = I('craft_ids');
|
||||
$craft_arr = explode(',',$craft_ids );
|
||||
$res = array();
|
||||
foreach ($craft_arr as $k=>$v){
|
||||
$total=$this->where(array('flow_id'=>$data['flow_id']))->count();
|
||||
$data['order_id'] = ($total + 1)* 10;
|
||||
$data['craft_id'] = $v;
|
||||
$res[] = $this->add($data);
|
||||
}
|
||||
|
||||
return implode(',',$res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class FlowModel extends RelationModel{
|
||||
|
||||
|
||||
protected $_link = array(
|
||||
"who_add"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'flow_who_add',
|
||||
),
|
||||
"who_modify"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'flow_who_modify',
|
||||
),
|
||||
|
||||
);
|
||||
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("flow_name","require","工艺流程名称不能为空"),
|
||||
array("flow_code","require","工艺流程代码不能为空"),
|
||||
array("flow_name","","工艺流程名称已存在",0,"unique"),
|
||||
array("flow_code","","工艺流程代码已存在",0,"unique"),
|
||||
);
|
||||
//保存信息
|
||||
public function data_save(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$member_id = UID;
|
||||
$data['flow_who_modify'] = $member_id;
|
||||
$data['flow_time_modify'] = time();
|
||||
$this->data($data)->save();
|
||||
return $data['flow_id'];
|
||||
}
|
||||
//保存信息
|
||||
public function data_add(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
unset($data['flow_id']);
|
||||
$member_id = UID;
|
||||
$data['flow_who_add'] = $member_id;
|
||||
$data['flow_time_add'] = time();
|
||||
$id = $this->data($data)->add();
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class ImportDataModel extends Model{
|
||||
public $TmpPath,$tableName,$params,$currentSheet,$excelData,$fieldKey,$fieldName;
|
||||
//自动验证
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
protected $_validate = array(
|
||||
array('modelid','require','缺少modelid!',1),
|
||||
array('cat_type','checkCat_type','请选择栏目!',1,"callback"),
|
||||
array('excel_src','require','请上传数据包!',1),
|
||||
);
|
||||
//
|
||||
function __construct($params){
|
||||
//文件保存路径
|
||||
$this->TmpPath="./d/image/".date("Ymd")."/";
|
||||
if (! file_exists ($this->TmpPath)) {
|
||||
mkdir($this->TmpPath, 0777, true);
|
||||
}
|
||||
//
|
||||
$this->params=$params;
|
||||
$this->tableName="cms_".$GLOBALS['model'][$params["modelid"]]['table_name'];
|
||||
}
|
||||
//检查栏目结构
|
||||
public function checkCat_type(){
|
||||
$catid=I("post.catid",0,"intval");
|
||||
$cat_type=I("post.cat_type");//栏目导入方式
|
||||
if($cat_type=="selfCheck"&&!$catid){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//创建表格对像
|
||||
public function creatExcelObj(){
|
||||
Vendor('PHPExcel.PHPExcel');
|
||||
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
|
||||
$objPHPExcel = $objReader->load(".".$this->params["excel_src"]);
|
||||
$this->currentSheet = $objPHPExcel->getActiveSheet();
|
||||
}
|
||||
//把表格里的图片保存到服务器,并把具体的路径
|
||||
public function saveImg(){
|
||||
//先处理图片
|
||||
$AllImages= $this->currentSheet->getDrawingCollection();
|
||||
$ArrayTmp="";
|
||||
foreach($AllImages as $drawing){
|
||||
if($drawing instanceof \PHPExcel_Worksheet_MemoryDrawing){
|
||||
$image = $drawing->getImageResource();
|
||||
$filename=$drawing->getIndexedFilename();
|
||||
$XY=$drawing->getCoordinates();
|
||||
//把图片存起来
|
||||
imagepng($image, $this->TmpPath.$filename);
|
||||
//把图片的单元格的值设置为图片名称
|
||||
$cell = $this->currentSheet->getCell($XY);
|
||||
$cell->setValue($this->TmpPath.$filename);
|
||||
}
|
||||
}
|
||||
//重新获取一下表格数据
|
||||
$this->getExcelData();
|
||||
//删除第一行字段名
|
||||
$data=$this->excelData;
|
||||
unset($data[0]);
|
||||
$this->excelData=$data;
|
||||
}
|
||||
//把图片路径字段保存到file表里
|
||||
public function saveImgToTable($src,$pubid){
|
||||
$fileName= str_replace($this->TmpPath,"",$src);
|
||||
$value['pubid']=$pubid;
|
||||
$value['name']=$fileName;
|
||||
$value['filepath']=trim($src,".");
|
||||
$value['savepath']=date("Ymd");
|
||||
$fileName_arr=explode(".",$fileName);
|
||||
$value['ext']=$fileName_arr[1];
|
||||
$value['size']= filesize($src);
|
||||
$value['md5'] = md5_file($src);
|
||||
$value['sha1'] = sha1_file($src);
|
||||
$value['create_time'] = time();
|
||||
//判断文件是否存在
|
||||
$where['md5'] = $value['md5'];
|
||||
$where['sha1'] = $value['sha1'];
|
||||
$isFile=M("file")->where($where)->getField("id");
|
||||
if($isFile){
|
||||
$value['id'] = $isFile;
|
||||
M("file")->save($value);
|
||||
}else{
|
||||
//压缩图片
|
||||
$image=new \Think\Image();
|
||||
$image->open($src);
|
||||
$image->thumb(200,200);
|
||||
$small_path=$this->TmpPath."small_".$fileName;
|
||||
$image->save($small_path);
|
||||
$value['smallpath']=trim($small_path,".");
|
||||
M("file")->add($value);
|
||||
}
|
||||
|
||||
}
|
||||
//获取表格内的数据内容
|
||||
public function getExcelData(){
|
||||
$data=$this->currentSheet->toArray();
|
||||
$this->excelData=$data;
|
||||
}
|
||||
//分割并提取表格第一行做为字段名称
|
||||
public function getFieldName(){
|
||||
$fieldName=array();
|
||||
$fieldName["id"]="ID";
|
||||
$fieldName["catid"]="栏目名称";
|
||||
$data=$this->excelData;
|
||||
foreach($data[0] as $key=>$v){
|
||||
$field_arr=explode("==",$v);
|
||||
$fieldKey[$key]=$field_arr[1];
|
||||
$fieldName[$field_arr[1]]=$field_arr[0];
|
||||
}
|
||||
$fieldName["result_status"]="导入结果";
|
||||
unset($data[0]);
|
||||
$this->fieldKey=$fieldKey;//返回一个excel下标对应字段名的数组,例:array(0=>title,1=>keywords)
|
||||
$this->fieldName=$fieldName;//返回一个根据字段下标,对应字段中文值的数组,例:array('title'=>'标题','keywords'=>'关键字')
|
||||
$this->excelData=$data;//返回一个去掉第一行的excel data
|
||||
}
|
||||
//开始插入数据
|
||||
public function insertData(){
|
||||
$back_data=array();
|
||||
$back_data[]=$this->fieldName;
|
||||
foreach($this->excelData as $v){
|
||||
$_POST=array();
|
||||
$_POST["id"]=0;
|
||||
if($this->params["cat_type"]=="selfCheck"){
|
||||
$_POST["catid"]=$this->params["catid"];
|
||||
}
|
||||
$_POST["pubid"]=make_pubid();
|
||||
foreach($v as $key2=>$v2){
|
||||
//如果是栏目字段
|
||||
if($this->fieldKey[$key2]=="catid"){
|
||||
//判断栏目的形式,栏目名称|栏目ID
|
||||
if($this->params["cat_type"]=="catName"){//栏目名称
|
||||
$_POST["catid"]=M("cat")->where(array("name"=>$v2))->getField("catid");
|
||||
}else if($this->params["cat_type"]=="catId"){//栏目ID
|
||||
$_POST["catid"]=intval($v2);
|
||||
}
|
||||
//
|
||||
}else{
|
||||
$_POST[$this->fieldKey[$key2]]=$v2?$v2:"";
|
||||
}
|
||||
//判断是否为图片路径,如果是图片路径把图片路径存到file表里,便用在文件管理器中查看
|
||||
if(strstr($v2,$this->TmpPath)){
|
||||
$this->saveImgToTable($v2,$_POST["pubid"]);
|
||||
$_POST[$this->fieldKey[$key2]]=trim($v2,".");
|
||||
}
|
||||
}
|
||||
//查看catid是否存在
|
||||
if(!$_POST["catid"]){
|
||||
unset($_POST["pubid"]);
|
||||
$_POST["result_status"]="<span style='color:red'>导入失败-缺少catid</span>";
|
||||
$resultData=$_POST;
|
||||
}else{
|
||||
//如果遇到重复标题如何处理
|
||||
//直接新增(为了减少服务器资源这里就不做重复查询)
|
||||
if($this->params["check_title"]=="add"){
|
||||
$resultData=$this->saveAction("add");
|
||||
}else{
|
||||
//查看数据是否存在
|
||||
$id=M($this->tableName)->where(array("title"=>$_POST["title"],"catid"=>$_POST["catid"]))->getField("id");
|
||||
if($id){
|
||||
if($this->params["check_title"]=="update"){
|
||||
$_POST["id"]=$id;
|
||||
$resultData=$this->saveAction("update");
|
||||
}else{
|
||||
unset($_POST["pubid"]);
|
||||
$_POST["result_status"]="<span style='color:red'>导入失败-标题重复</span>";
|
||||
$resultData=$_POST;
|
||||
}
|
||||
}else{
|
||||
$resultData=$this->saveAction("add");
|
||||
}
|
||||
}
|
||||
}
|
||||
$back_data[]=$resultData;
|
||||
}
|
||||
return $back_data;
|
||||
}
|
||||
//保存操作
|
||||
private function saveAction($type){
|
||||
//调用模型里的数据插入方式
|
||||
if($type=="add"){
|
||||
$resultdata=D("Info")->data_add("export");
|
||||
$result_status="导入成功(新增)";
|
||||
}else{
|
||||
$resultdata=D("Info")->data_editor("export");
|
||||
$result_status="导入成功(更新)";
|
||||
}
|
||||
foreach($this->fieldName as $key=>$v3){
|
||||
$newdata[$key]=$resultdata[$key]?$resultdata[$key]:"-";
|
||||
}
|
||||
$newdata["catid"]=$GLOBALS['cat'][$newdata["catid"]][name];
|
||||
$newdata["result_status"]=$result_status;
|
||||
return $newdata;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
<?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];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class LinkCatModel extends Model{
|
||||
//自动验证
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
protected $_validate = array(
|
||||
array('name','require','请填写名称!',1),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class LinkModel extends RelationModel{
|
||||
//自动验证
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
protected $_validate = array(
|
||||
array('name','require','请填写名称!',1),
|
||||
);
|
||||
//关联模型
|
||||
protected $_link = array(
|
||||
//对应栏目表
|
||||
"link_cat"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'foreign_key' => 'catid',
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class MemberModel extends RelationModel{
|
||||
//关联模型
|
||||
Protected $_link=array(
|
||||
"member_group"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'foreign_key' => 'group_id',
|
||||
),
|
||||
);
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("username","require","用户名不能为空"),
|
||||
array("username","5,40","用户名长度需要5位以上",2,"length"),
|
||||
array("username","","用户名已存在",0,"unique"),
|
||||
array("email","require","邮箱不能为空"),
|
||||
array("email","email","邮箱格式不正确"),
|
||||
array("email","","邮箱已存在",0,"unique"),
|
||||
array("password","require","密码不能为空"),
|
||||
array("password","6,40","密码长度需要6位以上",2,"length"),
|
||||
array('repassword','password','确认密码不正确',0,'confirm'), // 验证确认密码是否和密码一致
|
||||
);
|
||||
//保存注册信息
|
||||
public function data_save($site_id){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
//
|
||||
$data[site_id]= $site_id;
|
||||
$data[password]= md6($data[password]);
|
||||
$data[status]=1;
|
||||
$data[member_id]=$this->data($data)->add();
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class PageModel extends Model{
|
||||
//自动验证
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
protected $_validate = array(
|
||||
array('name','require','请填写页面名称!',1),
|
||||
array('classpath','checkPath','请填写栏目路径!',1,"callback"),
|
||||
array('classpath','/^\/[\w|\.|\/]+\//','栏目名称不符号要求,只能使用[数字,字母,_,/]!',2),
|
||||
array('classpath','','路径已存在',2,"unique"),
|
||||
array('pagemod','require','缺少页面类型!',1),
|
||||
array('pagetext','require','请填写页面内容!',1),
|
||||
);
|
||||
public function checkPath($val){
|
||||
if(I("path_type")=="static"&&!$val){
|
||||
return false;
|
||||
}else{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//生成template文件
|
||||
public function makePageTemplateFile($page_id){
|
||||
$page_r=$this->find($page_id);
|
||||
$TemplateModel=new \Admin\Model\TemplateModel();
|
||||
$page_r["pagetext"]=$TemplateModel->replaceTemplate($page_r["pagetext"]);
|
||||
file_put_contents(C("CMS_TEMP_PATH")."page_".$page_id.".html",$page_r["pagetext"]);
|
||||
}
|
||||
//删除模板文件
|
||||
public function deletePageTemplateFile($page_id){
|
||||
unlink(C("CMS_TEMP_PATH")."page_".$page_id.".html");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class PagesModel extends Model{
|
||||
//自动验证
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
protected $_validate = array(
|
||||
array('name','require','请填写页面名称!',1),
|
||||
array('classpath','checkPath','请填写栏目路径!',1,"callback"),
|
||||
array('classpath','/^\/[\w|\/]+\/$/','栏目名称不符号要求,只能使用[数字,字母,_,/]!',2),
|
||||
array('classpath','','路径已存在',1,"unique"),
|
||||
array('template_id','require','请选择模板!',1),
|
||||
array('count_sql','require','请填写统计sql语句!',1),
|
||||
array('select_sql','require','请填写查询sql语句!',1),
|
||||
array('lencord','require','请填写每页显示!',1),
|
||||
);
|
||||
public function checkPath($val){
|
||||
if(I("path_type")=="static"&&!$val){
|
||||
return false;
|
||||
}else{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class ProductModel extends RelationModel{
|
||||
|
||||
protected $_link = array(
|
||||
"who_add"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'product_who_add',
|
||||
),
|
||||
"who_modify"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'product_who_modify',
|
||||
),
|
||||
"flow"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'flow',
|
||||
'mapping_fields' => 'flow_name',
|
||||
'foreign_key' => 'flow_id',
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("product_name","require","产品名称不能为空"),
|
||||
array("flow_id","require","工艺流程不能为空"),
|
||||
array("product_name","","产品名称已存在",0,"unique"),
|
||||
|
||||
);
|
||||
//保存信息
|
||||
public function data_save(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$member_id = UID;
|
||||
$data['product_who_modify'] = $member_id;
|
||||
$data['product_time_modify'] = time();
|
||||
$this->data($data)->save();
|
||||
return $data['product_id'];
|
||||
}
|
||||
//保存信息
|
||||
public function data_add(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$member_id = UID;
|
||||
$data['product_who_add'] = $member_id;
|
||||
$data['product_time_add'] = time();
|
||||
$id = $this->data($data)->add();
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class RoleModel extends Model{
|
||||
protected $_validate = array(
|
||||
array('name','require','角色名称必须填写!'),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class SaveRemoteImgModel{
|
||||
//editor字段保存相关操作
|
||||
public function doIt($content){
|
||||
preg_match_all ( "/\<[img|IMG].*?src=\"(.+?)\".*?>/", $content, $img_array );
|
||||
// 时间无限制
|
||||
set_time_limit ( 0 );
|
||||
foreach ( $img_array[0] as $key => $value ) {
|
||||
$matches=array(
|
||||
0=>$value,
|
||||
1=>$img_array[1][$key],
|
||||
);
|
||||
$newimgurl=$this->saveRemoteImg($matches);
|
||||
// 替换原来的图片地址
|
||||
$content = ereg_replace ( $img_array[1][$key], $newimgurl, $content );
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
//远程保存图片
|
||||
public function saveRemoteImg($matches){
|
||||
preg_match("/alt=\"(.*)\"/U", $matches[0],$result);
|
||||
$altName=$result[1]?$result[1]:time();
|
||||
//本地路径
|
||||
$this->TmpPath="./d/image/".date("Ymd")."/";
|
||||
if (! file_exists ($this->TmpPath)) {
|
||||
mkdir($this->TmpPath, 0777, true);
|
||||
}
|
||||
$result=$this->crabImage($matches[1], $this->TmpPath);
|
||||
if(!$result){
|
||||
return $matches[1];
|
||||
}
|
||||
//生成小图
|
||||
$image=new \Think\Image();
|
||||
$image->open($result[save_path]);
|
||||
$image->thumb(200,200);
|
||||
$small_path=$this->TmpPath."small_".$result[fileName];
|
||||
$image->save($small_path);
|
||||
if(!M("file")->where(array("md5"=>md5_file($result['save_path'])))->find()){
|
||||
M("file")->add(array(
|
||||
"pubid"=>$_POST["pubid"],
|
||||
"name"=>$altName,
|
||||
"ext"=>trim($result[ext],"."),
|
||||
"savename"=>$result[fileName],
|
||||
"smallpath"=>trim($small_path,"."),
|
||||
"size"=>filesize($result[save_path]),
|
||||
"md5"=>md5_file($result['save_path']),
|
||||
"sha1"=>sha1_file($result['save_path']),
|
||||
"filepath"=>trim($result[save_path],"."),
|
||||
"create_time"=>time(),
|
||||
));
|
||||
}
|
||||
return trim($result[save_path],".");
|
||||
}
|
||||
/**
|
||||
* PHP将网页上的图片攫取到本地存储
|
||||
* @param $imgUrl 图片url地址
|
||||
* @param string $saveDir 本地存储路径 默认存储在当前路径
|
||||
* @param null $fileName 图片存储到本地的文件名
|
||||
* @return mix
|
||||
*/
|
||||
function crabImage($imgUrl, $saveDir='./', $fileName=null){
|
||||
if(empty($imgUrl)){
|
||||
return false;
|
||||
}
|
||||
//获取图片信息大小
|
||||
$imgSize = getImageSize($imgUrl);
|
||||
if(!in_array($imgSize['mime'],array('image/jpg', 'image/gif', 'image/png', 'image/jpeg'),true)){
|
||||
return false;
|
||||
}
|
||||
//获取后缀名
|
||||
$_mime = explode('/', $imgSize['mime']);
|
||||
$_ext = '.'.end($_mime);
|
||||
if(empty($fileName)){ //生成唯一的文件名
|
||||
$fileName = uniqid(time(),true).$_ext;
|
||||
}
|
||||
//开始攫取
|
||||
ob_start();
|
||||
readfile($imgUrl);
|
||||
$imgInfo = ob_get_contents();
|
||||
ob_end_clean();
|
||||
if(!file_exists($saveDir)){
|
||||
mkdir($saveDir,0777,true);
|
||||
}
|
||||
$fp = fopen($saveDir.$fileName, 'a');
|
||||
$imgLen = strlen($imgInfo); //计算图片源码大小
|
||||
$_inx = 1024; //每次写入1k
|
||||
$_time = ceil($imgLen/$_inx);
|
||||
for($i=0; $i<$_time; $i++){
|
||||
fwrite($fp,substr($imgInfo, $i*$_inx, $_inx));
|
||||
}
|
||||
fclose($fp);
|
||||
return array(
|
||||
"fileName"=>$fileName,
|
||||
'ext'=>$_ext,
|
||||
'save_path'=>$saveDir.$fileName
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class ShopOrderModel extends \Shop\Model\ShopOrderModel{
|
||||
//支付宝原路退款
|
||||
public function alipayRefund($payment){
|
||||
$detail= json_decode($payment["detail"]);//这里detail字段,保存了当时的交易详情
|
||||
//
|
||||
Vendor('AlipaySdk.aop.AopClient');
|
||||
Vendor('AlipaySdk.aop.SignData');
|
||||
Vendor('AlipaySdk.aop.request.AlipayTradeRefundRequest');
|
||||
$aop = new \AopClient();
|
||||
$config_load=load_config("./Application/Shop/Conf/config.php");//这里因为需要调用shop模块下的config配置文件,所以要用到load_config函数
|
||||
$config = $config_load["alipay"];
|
||||
$aop->gatewayUrl = $config['gatewayUrl'];
|
||||
$aop->appId = $config['app_id'];
|
||||
$aop->rsaPrivateKey = $config['merchant_private_key'];
|
||||
$aop->alipayrsaPublicKey=$config['alipay_public_key'];
|
||||
$aop->apiVersion = '1.0';
|
||||
$aop->signType = 'RSA2';
|
||||
$aop->postCharset="UTF-8";
|
||||
$aop->format='json';
|
||||
$request = new \AlipayTradeRefundRequest ();
|
||||
$request->setBizContent(json_encode(array(
|
||||
"out_trade_no"=>$detail->out_trade_no,
|
||||
"trade_no"=>$detail->trade_no,
|
||||
"refund_amount"=>$detail->total_amount,
|
||||
"refund_reason"=>"正常退款",
|
||||
)));
|
||||
$result = $aop->execute ( $request);
|
||||
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
|
||||
$resultCode = $result->$responseNode->code;
|
||||
if(!empty($resultCode)&&$resultCode == 10000){
|
||||
return true;
|
||||
}else {
|
||||
$this->error = $result->$responseNode->sub_msg;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,525 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class TableFieldModel extends Model{
|
||||
//自动验证
|
||||
//array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
|
||||
protected $_validate = array(
|
||||
array('table_id', 'require', '请选择数据表!'),
|
||||
array('formtype', 'require', '字段类型不能为空!'),
|
||||
array('field', 'require', '字段名称必须填写!'),
|
||||
array('field', 'isFieldUnique', '该字段名称已经存在!', 0, 'callback', 1),
|
||||
array('name', 'require', '字段别名必须填写!'),
|
||||
array('field', '/^[a-z_0-9]+$/i', '字段名只支持英文或数字!', 0, 'regex', 3),
|
||||
);
|
||||
/**
|
||||
* 验证字段名是否已经存在
|
||||
* @param type $fieldName
|
||||
* @return boolean false已经存在,true不存在
|
||||
*/
|
||||
public function isFieldUnique($fieldName) {
|
||||
if (empty($fieldName)) {
|
||||
return true;
|
||||
}
|
||||
if ($this->where(array('table_id' =>I("table_id"), 'field' => $fieldName))->count()){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 根据模型ID,返回表名
|
||||
* @param type $table_id
|
||||
* @param type $table_id
|
||||
* @return string
|
||||
*/
|
||||
protected function getTbName($table_id, $issystem = 1) {
|
||||
//表名获取
|
||||
$table_name = M("table")->where(array("table_id"=>$table_id))->getField("table_name");
|
||||
$table_name="cms_".$table_name;
|
||||
//完整表名获取 判断主表 还是副表
|
||||
$table_name = $issystem ? $table_name : $table_name . "_data";
|
||||
return $table_name;
|
||||
}
|
||||
//增加
|
||||
public function data_add(){
|
||||
$data=$this->create();
|
||||
//数据表id
|
||||
$table_id = $data['table_id'];
|
||||
$fieldtype = $data['fieldtype'];
|
||||
//完整表名获取 判断主表 还是副表
|
||||
$table_name = $this->getTbName($table_id, $data['issystem']);
|
||||
if (!$this->table_exists($table_name)) {
|
||||
$this->error = '数据表不存在!';
|
||||
return false;
|
||||
}
|
||||
//检查字段是否存在
|
||||
if ($this->field_exists($table_name, $data['field'])) {
|
||||
$this->error = '该字段已经存在!';
|
||||
return false;
|
||||
}
|
||||
|
||||
//增加字段
|
||||
$field = array(
|
||||
'tablename' => C("DB_PREFIX") . $table_name,
|
||||
'fieldname' => $data['field'],
|
||||
'fieldlen' => $data['fieldlen'],
|
||||
'defaultvalue' => $data['defaultvalue'],
|
||||
);
|
||||
if ($this->addFieldSql($fieldtype, $field)) {
|
||||
//更新htmlcode
|
||||
$TableModel=new \Admin\Model\TableModel();
|
||||
$data['htmlcode']=$TableModel->createFieldHtmlCode($data);
|
||||
$data['memberhtmlcode']=$TableModel->createMemberFieldHtmlCode($data);
|
||||
//
|
||||
$fieldid = $this->add($data);
|
||||
if ($fieldid) {
|
||||
return $fieldid;
|
||||
} else {
|
||||
$this->error = '字段信息入库失败!';
|
||||
//回滚
|
||||
$this->execute("ALTER TABLE `{$field['tablename']}` DROP `{$field['fieldname']}`");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//修改
|
||||
public function data_editor(){
|
||||
$data=$this->create();
|
||||
if (!$data['field_id']) {
|
||||
$this->error = '缺少字段id!';
|
||||
return false;
|
||||
} else {
|
||||
$field_id = $field_id ? $field_id : (int) $data['field_id'];
|
||||
}
|
||||
//重置htmlcode
|
||||
$TableModel=new \Admin\Model\TableModel();
|
||||
$data['htmlcode']=htmlspecialchars_decode($data['htmlcode']);
|
||||
$data['htmlcode']=$TableModel->createFieldHtmlCode($data);
|
||||
//投稿的
|
||||
$data['memberhtmlcode']=htmlspecialchars_decode($data['memberhtmlcode']);
|
||||
$data['memberhtmlcode']=$TableModel->createMemberFieldHtmlCode($data);
|
||||
//原字段信息
|
||||
$info = $this->where(array("field_id" => $field_id))->find();
|
||||
if (empty($info)){
|
||||
$this->error = '该字段不存在!';
|
||||
return false;
|
||||
}
|
||||
//字段主表副表不能修改
|
||||
unset($data['issystem']);
|
||||
//字段类型
|
||||
if (empty($data['formtype'])) {
|
||||
$data['formtype'] = $info['formtype'];
|
||||
}
|
||||
//模型id
|
||||
$table_id = $info['table_id'];
|
||||
$field_type = $data['fieldtype'];
|
||||
//完整表名获取 判断主表 还是副表
|
||||
$tablename = $this->getTbName($table_id, $info['issystem']);
|
||||
if (!$this->table_exists($tablename)) {
|
||||
$this->error = '数据表不存在!';
|
||||
return false;
|
||||
}
|
||||
|
||||
if (false !== $this->save($data)) {
|
||||
//如果字段名变更
|
||||
if ($data['field'] && $info['field']) {
|
||||
//检查字段是否存在,只有当字段名改变才检测
|
||||
if ($data['field'] != $info['field'] && $this->field_exists($tablename, $data['field'])) {
|
||||
$this->error = '该字段已经存在!';
|
||||
//回滚
|
||||
$this->where(array("field_id" => $field_id))->save($info);
|
||||
return false;
|
||||
}
|
||||
$field = array(
|
||||
'tablename' => C("DB_PREFIX") . $tablename,
|
||||
'newfilename' => $data['field'],
|
||||
'oldfilename' => $info['field'],
|
||||
'fieldlen' => $data['fieldlen'],
|
||||
'defaultvalue' => $data['defaultvalue'],
|
||||
);
|
||||
if (false === $this->editFieldSql($field_type, $field)) {
|
||||
$this->error = '数据库字段结构更改失败!';
|
||||
//回滚
|
||||
$this->where(array("field_id" => $field_id))->save($info);
|
||||
return false;
|
||||
}
|
||||
//根据table_id查询model表,并更新modeltemp
|
||||
$model=M("model")->where(array("table_id"=>$table_id))->select();
|
||||
$ModelModel=new \Admin\Model\ModelModel();
|
||||
foreach($model as $v){
|
||||
$ModelModel->updateModelTemp($v["modelid"]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->error = '数据库更新失败!';
|
||||
return false;
|
||||
}
|
||||
//
|
||||
|
||||
//
|
||||
return $data;
|
||||
}
|
||||
//删除字段
|
||||
public function data_delete($field_id,$systemfield) {
|
||||
//原字段信息
|
||||
$info = $this->where(array("field_id" => $field_id))->find();
|
||||
if (empty($info)) {
|
||||
$this->error = '该字段不存在!';
|
||||
return false;
|
||||
}
|
||||
//模型id
|
||||
$table_id = $info['table_id'];
|
||||
//完整表名获取 判断主表 还是副表
|
||||
$tablename = $this->getTbName($table_id, $info['issystem']);
|
||||
if (!$this->table_exists($tablename)) {
|
||||
$this->error = '数据表不存在!';
|
||||
return false;
|
||||
}
|
||||
//判断是否允许删除
|
||||
if (in_array($info['field'],$systemfield)){
|
||||
$this->error = '系统字段不允许被删除!';
|
||||
return false;
|
||||
}
|
||||
if ($this->deleteFieldSql($info['field'], C("DB_PREFIX") . $tablename)) {
|
||||
$this->where(array("field_id" => $field_id, "table_id" => $table_id))->delete();
|
||||
//根据table_id查询model表,并更新modeltemp
|
||||
$model=M("model")->where(array("table_id"=>$table_id))->select();
|
||||
$ModelModel=new \Admin\Model\ModelModel();
|
||||
foreach($model as $v){
|
||||
$ModelModel->updateModelTemp($v["modelid"]);
|
||||
}
|
||||
} else {
|
||||
$this->error = '数据库表字段删除失败!';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
/**
|
||||
* 根据字段类型,删除对应的字段到相应表里面
|
||||
* @param type $filename 字段名称
|
||||
* @param type $tablename 完整表名
|
||||
*/
|
||||
protected function deleteFieldSql($filename, $tablename) {
|
||||
//不带表前缀的表名
|
||||
$noprefixTablename = str_replace(C("DB_PREFIX"), '', $tablename);
|
||||
if (empty($tablename) || empty($filename)) {
|
||||
$this->error = '表名或者字段名不能为空!';
|
||||
return false;
|
||||
}
|
||||
|
||||
if (false === $this->table_exists($noprefixTablename)) {
|
||||
$this->error = '该表不存在!';
|
||||
return false;
|
||||
}
|
||||
$sql = "ALTER TABLE `{$tablename}` DROP `{$filename}`;";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段删除失败!';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 根据字段类型,增加对应的字段到相应表里面
|
||||
* @param type $field_type 字段类型
|
||||
* @param type $field 相关配置
|
||||
* $field = array(
|
||||
* 'tablename' 表名(完整表名)
|
||||
* 'fieldname' 字段名
|
||||
* 'maxlength' 最大长度
|
||||
* 'minlength' 最小值
|
||||
* 'defaultvalue' 默认值
|
||||
* 'minnumber' 是否正整数 和整数 1为正整数,-1是为整数
|
||||
* 'decimaldigits' 小数位数
|
||||
* )
|
||||
*/
|
||||
protected function addFieldSql($field_type, $field) {
|
||||
//表名
|
||||
$tablename = $field['tablename'];
|
||||
//字段名
|
||||
$fieldname = $field['fieldname'];
|
||||
//长度
|
||||
$fieldlen = $field['fieldlen'];
|
||||
switch ($field_type) {
|
||||
case "varchar":
|
||||
if (!$fieldlen) {
|
||||
$fieldlen = 255;
|
||||
}
|
||||
$fieldlen = min($fieldlen, 255);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` VARCHAR( {$fieldlen} ) DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "tinyint":
|
||||
if (!$fieldlen) {
|
||||
$fieldlen = 3;
|
||||
}
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` TINYINT( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "smallint":
|
||||
$minnumber = intval($minnumber);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` SMALLINT( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "mediumint":
|
||||
$minnumber = intval($minnumber);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` INT ( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "int":
|
||||
$minnumber = intval($minnumber);
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` INT ( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "mediumtext":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` MEDIUMTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "text":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` TEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "date":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` DATE DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "datetime":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "timestamp":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "double":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` DOUBLE NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "float":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` FLOAT( {$fieldlen} ) NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "bigint":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` BIGINT( {$fieldlen} ) NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "longtext":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` LONGTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '数据库字段添加失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "char":
|
||||
$sql = "ALTER TABLE `{$tablename}` ADD `{$fieldname}` CHAR( {$fieldlen} ) DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 执行数据库表结构更改
|
||||
* @param type $field_type 字段类型
|
||||
* @param type $field 相关配置
|
||||
* $field = array(
|
||||
* 'tablename' 表名(完整表名)
|
||||
* 'newfilename' 新字段名
|
||||
* 'oldfilename' 原字段名
|
||||
* 'maxlength' 最大长度
|
||||
* 'minlength' 最小值
|
||||
* 'defaultvalue' 默认值
|
||||
* 'minnumber' 是否正整数 和整数 1为正整数,-1是为整数
|
||||
* 'decimaldigits' 小数位数
|
||||
* )
|
||||
*/
|
||||
protected function editFieldSql($field_type, $field) {
|
||||
//表名
|
||||
$tablename = $field['tablename'];
|
||||
//原字段名
|
||||
$oldfilename = $field['oldfilename'];
|
||||
//新字段名
|
||||
$newfilename = $field['newfilename'] ? $field['newfilename'] : $oldfilename;
|
||||
//长度
|
||||
$fieldlen = $field['fieldlen'];
|
||||
if (empty($tablename) || empty($newfilename)) {
|
||||
$this->error = '表名或者字段名不能为空!';
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($field_type) {
|
||||
case 'varchar':
|
||||
//最大值
|
||||
if (!$fieldlen) {
|
||||
$fieldlen = 255;
|
||||
}
|
||||
$fieldlen = min($fieldlen, 255);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` VARCHAR( {$fieldlen} ) DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'tinyint':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` TINYINT( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'smallint':
|
||||
$minnumber = intval($minnumber);
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` SMALLINT ( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT '{$defaultvalue}' DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'mediumint':
|
||||
$minnumber = intval($minnumber);
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` MEDIUMINT ( {$fieldlen} ) UNSIGNED NOT NULL DEFAULT '{$defaultvalue}' DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'int':
|
||||
$minnumber = intval($minnumber);
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` INT ( {$fieldlen} ) NOT NULL DEFAULT '{$defaultvalue}' DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'mediumtext':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` MEDIUMTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'text':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` TEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'date':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` DATE DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'datetime':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'timestamp':
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "double":
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` DOUBLE NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "float":
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` FLOAT(" . $minnumber . ") NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "bigint":
|
||||
$defaultvalue = intval($defaultvalue);
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` BIGINT NOT NULL DEFAULT 0";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "longtext":
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` LONGTEXT";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case "char":
|
||||
$sql = "ALTER TABLE `{$tablename}` CHANGE `{$oldfilename}` `{$newfilename}` CHAR(" . $fieldlen . ") DEFAULT ''";
|
||||
if (false === $this->execute($sql)) {
|
||||
$this->error = '字段结构更改失败!';
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$this->error = "字段类型" . $field_type . "不存在相应信息!";
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class TaskModel extends RelationModel{
|
||||
|
||||
protected $_link = array(
|
||||
"who_add"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'task_who_add',
|
||||
),
|
||||
"who_modify"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'task_who_modify',
|
||||
),
|
||||
"product"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'product',
|
||||
'mapping_fields' => 'product_type',
|
||||
'foreign_key' => 'product_id',
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("task_name","require","任务单号不能为空"),
|
||||
array("task_pici","require","任务进料批次不能为空"),
|
||||
array("task_inner_order","require","任务内部单号不能为空"),
|
||||
array("product_id","require","产品型号不能为空"),
|
||||
array("task_name","","任务单号已存在",0,"unique"),
|
||||
|
||||
);
|
||||
//保存信息
|
||||
public function data_save(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$member_id = UID;
|
||||
$data['task_who_modify'] = $member_id;
|
||||
$data['task_time_modify'] = time();
|
||||
$this->data($data)->save();
|
||||
return $data['task_id'];
|
||||
}
|
||||
//保存信息
|
||||
public function data_add(){
|
||||
//创建数据
|
||||
$data=$this->create();
|
||||
$member_id = UID;
|
||||
$data['task_who_add'] = $member_id;
|
||||
$data['task_time_add'] = time();
|
||||
$data['status'] = 0;
|
||||
$id = $this->data($data)->add();
|
||||
|
||||
$res = $this->find($id);
|
||||
$product = D('product')->find($res['product_id']);
|
||||
$flow_craft = D('flow_craft')->where(array('flow_id'=>$product['flow_id']))->relation(true)->order('order_id')->select();
|
||||
foreach ($flow_craft as $k=>$v){
|
||||
$thisData = array();
|
||||
$thisData['product_desc'] = $product['product_desc'];
|
||||
$thisData['task_time'] = $res['task_time_add'];
|
||||
$thisData['task_name'] = $res['task_name'];
|
||||
$thisData['task_inner_order'] = $res['task_inner_order'];
|
||||
$thisData['task_pici'] = $res['task_pici'];
|
||||
$thisData['task_id'] = $id;
|
||||
$thisData['order_id'] = $k;
|
||||
$thisData['is_complete'] = 0;
|
||||
$thisData['is_final'] = 0;
|
||||
$thisData['status'] = 0;
|
||||
if($k == count($flow_craft)-1){
|
||||
$thisData['is_final'] = 1;
|
||||
}
|
||||
$thisData['pre_num'] = 0;
|
||||
if($k == 0){
|
||||
$thisData['pre_num'] = $res['task_product_num'];
|
||||
}
|
||||
$thisData['pass_num'] = 0;
|
||||
$thisData['product_id'] = $product['product_id'];
|
||||
$thisData['product_name'] = $product['product_name'];
|
||||
$thisData['flow_craft_id'] = $v['flow_craft_id'];
|
||||
$thisData['type_name'] = $v['craft']['craft_type']['type_name'];
|
||||
$thisData['type_id'] = $v['craft']['type_id'];
|
||||
$thisData['craft_name'] = $v['craft']['craft_name'];
|
||||
$thisData['craft_id'] = $v['craft']['craft_id'];
|
||||
D('task_flow')->add($thisData);
|
||||
}
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class TaskSubModel extends RelationModel{
|
||||
|
||||
protected $_link = array(
|
||||
"operator"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'subtask_operator_id',
|
||||
),
|
||||
"qc"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'user',
|
||||
'mapping_fields' => 'user_name',
|
||||
'foreign_key' => 'subtask_qc_id',
|
||||
),
|
||||
"task_flow"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'task_flow',
|
||||
'foreign_key' => 'task_flow_id',
|
||||
),
|
||||
"task"=>array(
|
||||
'mapping_type' => self::BELONGS_TO,
|
||||
'class_name' => 'task',
|
||||
'foreign_key' => 'task_id',
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
<?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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class UpdateModel{
|
||||
//生成首页
|
||||
public function homeHtml(){
|
||||
$_GET=array(
|
||||
"isSystemMakeHtml"=>1,
|
||||
);
|
||||
$obj=new \Home\Controller\IndexController();
|
||||
$content=$obj->index();
|
||||
$htmlfile="./".C("setting.indexFileName");
|
||||
self::saveFile($htmlfile,$content);
|
||||
}
|
||||
//生成栏目页
|
||||
public function listHtml($catid,$p,$classpath){
|
||||
$_GET=array(
|
||||
"catid"=>$catid,
|
||||
"p"=>$p,
|
||||
"isSystemMakeHtml"=>1,
|
||||
);
|
||||
$obj=new \Home\Controller\ListController();
|
||||
$content=$obj->index();
|
||||
$filename=$p>1?("index_".$p.".html"):"index.html";
|
||||
$htmlfile=".".$classpath.$filename;
|
||||
self::saveFile($htmlfile,$content);
|
||||
}
|
||||
//生成自定义页
|
||||
public function pageHtml($page_id,$classpath){
|
||||
//如果通过在Admin模块生成的,一定要用url方式获取,否则在模板里里如果调用Home模块下的函数的话,在后台生成会报错
|
||||
if(MODULE_NAME=="Admin"){
|
||||
$url = 'http://'.$_SERVER['HTTP_HOST'].U('Home/Page/index',array("page_id"=>$page_id));
|
||||
$content = @file_get_contents($url);
|
||||
}else{
|
||||
$_GET=array(
|
||||
"page_id"=>$page_id,
|
||||
"isSystemMakeHtml"=>1,
|
||||
);
|
||||
$obj=new \Home\Controller\PageController();
|
||||
$content=$obj->index();
|
||||
}
|
||||
|
||||
$htmlfile=".".$classpath;
|
||||
self::saveFile($htmlfile,$content);
|
||||
}
|
||||
//生成自定义列表页
|
||||
public function pagesHtml($pages_id,$p,$classpath){
|
||||
$_GET=array(
|
||||
"pages_id"=>$pages_id,
|
||||
"p"=>$p,
|
||||
"isSystemMakeHtml"=>1,
|
||||
);
|
||||
$obj=new \Home\Controller\PagesController();
|
||||
$content=$obj->index();
|
||||
$filename=$p>1?("index_".$p.".html"):"index.html";
|
||||
$htmlfile=".".$classpath.$filename;
|
||||
self::saveFile($htmlfile,$content);
|
||||
}
|
||||
//生成内容页
|
||||
public function detailHtml($catid,$id,$classpath){
|
||||
if(MODULE_NAME=="Admin"){
|
||||
$url = 'http://'.$_SERVER['HTTP_HOST'].U('Home/View/index',array("catid"=>$catid,"id"=>$id));
|
||||
$content = @file_get_contents($url);
|
||||
}else{
|
||||
$_GET=array(
|
||||
"catid"=>$catid,
|
||||
"id"=>$id,
|
||||
"isSystemMakeHtml"=>1,
|
||||
);
|
||||
$obj=new \Home\Controller\ViewController();
|
||||
$content=$obj->index();
|
||||
}
|
||||
|
||||
$filename=$id.".html";
|
||||
$htmlfile=".".$classpath.$filename;
|
||||
self::saveFile($htmlfile,$content);
|
||||
}
|
||||
//获取指定文件夹下文件数量
|
||||
public function fileNum($path){
|
||||
$flag = \FilesystemIterator::KEY_AS_FILENAME;
|
||||
$glob = new \FilesystemIterator($path, $flag);
|
||||
$i=0;
|
||||
foreach ($glob as $name => $file) {
|
||||
//
|
||||
$i++;
|
||||
}
|
||||
return $i;
|
||||
}
|
||||
//写入文件
|
||||
public function saveFile($filePath,$content){
|
||||
\Think\Storage::put($filePath,$content,'html');
|
||||
$fp = fopen($filePath,"w");
|
||||
fwrite($fp,$content);
|
||||
fclose($fp);
|
||||
}
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class UserLogModel extends Model{
|
||||
//写入日志
|
||||
public function add($type,$table_name,$primary_key,$remark=""){
|
||||
$data=array();
|
||||
$data["userid"]=session("user_id");
|
||||
$data["username"]=session("user_name");
|
||||
$data["addtime"]=time();
|
||||
$data["nodestr"]=admin_nav();
|
||||
$data["type"]=$type;
|
||||
$data["url"]=$this->GetCurUrl();
|
||||
$data["ip"]= get_client_ip();
|
||||
$data["table_name"]=$table_name;
|
||||
//这里会自动判断$primary_key是array还是int形式
|
||||
if(is_array($primary_key)){
|
||||
$data["primary_key"]=implode(",", $primary_key);
|
||||
}else{
|
||||
$data["primary_key"]=$primary_key;
|
||||
}
|
||||
$data["remark"]=$remark;
|
||||
parent::add($data);
|
||||
}
|
||||
//获取当前访问的url
|
||||
public function GetCurUrl() {
|
||||
$url = 'http://';
|
||||
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
|
||||
$url = 'https://';
|
||||
}
|
||||
|
||||
// 判断端口
|
||||
if($_SERVER['SERVER_PORT'] != '80') {
|
||||
$url .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . ':' . $_SERVER['REQUEST_URI'];
|
||||
} else {
|
||||
$url .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['REQUEST_URI'];
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model\RelationModel;
|
||||
class UserModel extends RelationModel{
|
||||
//关联模型
|
||||
Protected $_link=array(
|
||||
"role"=>array(
|
||||
'mapping_type'=>self::MANY_TO_MANY,
|
||||
'foreign_key'=>"user_id",
|
||||
'relation_key'=>"role_id",
|
||||
'relation_table'=>"db_role_user",
|
||||
),
|
||||
);
|
||||
//验证
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("user_name","require","用户名不能为空"),
|
||||
array("user_name","","用户名已存在",0,"unique"),
|
||||
array("user_password","require","密码不能为空"),
|
||||
array("user_password","6,40","密码长度需要6位以上",2,"length"),
|
||||
array('repassword','user_password','确认密码不正确',0,'confirm'), // 验证确认密码是否和密码一致
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class WxCrawlerModel extends Model{
|
||||
/** @var 代理 */
|
||||
protected $agent = array(
|
||||
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
|
||||
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
|
||||
"Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
|
||||
"Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
|
||||
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
|
||||
"Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
|
||||
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
|
||||
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
|
||||
"Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
|
||||
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
|
||||
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
|
||||
"Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
|
||||
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
|
||||
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
|
||||
);
|
||||
public $host = '';
|
||||
public $header = '';
|
||||
public $referer = '';
|
||||
public $antiLeech = '';
|
||||
public function __construct($host='', $referer='', $proxy=false)
|
||||
{
|
||||
/** @var 初始化curl信息 */
|
||||
$this->header = $this->agent[rand(0,count($this->agent) - 1)];
|
||||
$this->referer = empty($referer)?'http://weixin.sogou.com/' : $referer;
|
||||
$this->host = empty($host)?'weixin.sogou.com' : $host;
|
||||
/** @var 处理微信图片的防盗链 */
|
||||
$this->antiLeech = '/Public/Home/GetWeChatImg.php?url=';
|
||||
$this->antiLeech = 'http://'.$_SERVER['SERVER_NAME'].'/Public/Home/GetWeChatImg.php?url=';
|
||||
}
|
||||
/**
|
||||
* 爬取内容
|
||||
* @author bignerd
|
||||
* @since 2016-08-16T10:13:58+0800
|
||||
* @param $url
|
||||
*/
|
||||
public function _get($url)
|
||||
{
|
||||
// $ch=curl_init($url);
|
||||
// $options = [
|
||||
// CURLOPT_USERAGENT => $this->agent,
|
||||
// CURLOPT_REFERER => $this->referer,
|
||||
// ];
|
||||
// curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
|
||||
// curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
|
||||
// curl_setopt($ch,CURLOPT_TIMEOUT,60);
|
||||
// $output=curl_exec($ch);
|
||||
// return $output;
|
||||
$html = file_get_contents($url);
|
||||
return $html;
|
||||
}
|
||||
public function crawByUrl($url)
|
||||
{
|
||||
$content = $this->_get($url);
|
||||
$basicInfo = $this->articleBasicInfo($content);
|
||||
list($content_html, $content_text) = $this->contentHandle($content);
|
||||
$result= array_merge($basicInfo,array('content_html' => $content_html,'content_text' => $content_text));
|
||||
$result[cover]=$result[cover]?$this->antiLeech.$result[cover]:"";
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
* 处理微信文章源码,提取文章主体,处理图片链接
|
||||
* @author bignerd
|
||||
* @since 2016-08-16T15:59:27+0800
|
||||
* @param $content 抓取的微信文章源码
|
||||
* @return [带图html文本,无图html文本]
|
||||
*/
|
||||
public function contentHandle($content)
|
||||
{
|
||||
$content_html_pattern = '/<div class="rich_media_content " id="js_content" style="visibility: hidden;">(.*?)<\/div>/s';
|
||||
preg_match_all($content_html_pattern, $content, $html_matchs);
|
||||
$content_html = $html_matchs[1][0];
|
||||
/** @var 带图片html文本 */
|
||||
$content_html = preg_replace_callback('/data-src="(.*?)"/', function($matches){
|
||||
//如果包含qq视频,则正常返回
|
||||
if(strstr($matches[1],"v.qq.com")){
|
||||
return 'src='.$matches[1];
|
||||
}else{
|
||||
return 'src='.$this->antiLeech.urlencode($matches[1]);
|
||||
}
|
||||
|
||||
}, $content_html);
|
||||
/** @var 无图html文本 */
|
||||
$content_text = preg_replace('/<img.*?>/s','',$content_html);
|
||||
return array($content_html,$content_text);
|
||||
}
|
||||
/**
|
||||
* 获取文章的基本信息
|
||||
* @author bignerd
|
||||
* @since 2016-08-16T17:16:32+0800
|
||||
* @param $content 文章详情源码
|
||||
* @return $basicInfo
|
||||
*/
|
||||
public function articleBasicInfo($content)
|
||||
{
|
||||
//待获取item
|
||||
$item = array(
|
||||
'ct' => 'date',//发布时间
|
||||
'msg_title' => 'title',//标题
|
||||
'msg_desc' => 'digest',//描述
|
||||
'msg_link' => 'content_url',//文章链接
|
||||
'msg_cdn_url' => 'cover',//封面图片链接
|
||||
'nickname' => 'wechatname',//公众号名称
|
||||
);
|
||||
$basicInfo = array(
|
||||
'author' => '',
|
||||
'copyright_stat' => '',
|
||||
);
|
||||
foreach ($item as $k => $v) {
|
||||
$pattern = '/ var '.$k.' = "(.*?)";/s';
|
||||
preg_match_all($pattern,$content,$matches);
|
||||
if(array_key_exists(1, $matches) && !empty($matches[1][0])){
|
||||
$basicInfo[$v] = $this->htmlTransform($matches[1][0]);
|
||||
}else{
|
||||
$basicInfo[$v] = '';
|
||||
}
|
||||
}
|
||||
/** 获取作者 */
|
||||
preg_match('/<em class="rich_media_meta rich_media_meta_text">(.*?)<\/em>/s', $content, $matchAuthor);
|
||||
if(!empty($matchAuthor[1])) $basicInfo['author'] = $matchAuthor[1];
|
||||
/** 文章类型 */
|
||||
preg_match('/<span id="copyright_logo" class="rich_media_meta meta_original_tag">(.*?)<\/span>/s', $content, $matchType);
|
||||
if(!empty($matchType[1])) $basicInfo['copyright_stat'] = $matchType[1];
|
||||
return $basicInfo;
|
||||
}
|
||||
/**
|
||||
* 特殊字符转换
|
||||
* @author bignerd
|
||||
* @since 2016-08-16T17:30:52+0800
|
||||
* @param $string
|
||||
* @return $string
|
||||
*/
|
||||
public function htmlTransform($string)
|
||||
{
|
||||
$string = str_replace('"','"',$string);
|
||||
$string = str_replace('&','&',$string);
|
||||
$string = str_replace('amp;','',$string);
|
||||
$string = str_replace('<','<',$string);
|
||||
$string = str_replace('>','>',$string);
|
||||
$string = str_replace(' ',' ',$string);
|
||||
$string = str_replace("\\", '',$string);
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
Reference in New Issue
Block a user