更新目录结构
This commit is contained in:
199
app/model/module/Attribute.php
Normal file
199
app/model/module/Attribute.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\model\module;
|
||||
|
||||
use think\facade\Config;
|
||||
use think\facade\Db;
|
||||
use sent\tree\Tree;
|
||||
use app\model\Model as Models;
|
||||
|
||||
/**
|
||||
* 设置模型
|
||||
*/
|
||||
class Attribute extends \think\Model {
|
||||
|
||||
protected $type = array(
|
||||
'id' => 'integer',
|
||||
);
|
||||
|
||||
/**
|
||||
* @title 新增后事件
|
||||
*/
|
||||
protected static function onAfterInsert($data){
|
||||
$data = $data->toArray();
|
||||
if ($data['model_id']) {
|
||||
$db = new \com\Datatable();
|
||||
$name = Models::where('id', $data['model_id'])->value('name');
|
||||
$data['after'] = self::where('name', '<>', $data['name'])->where('model_id', $data['model_id'])->order('sort asc, id desc')->value('name');
|
||||
return $db->columField(strtolower($name), $data)->query();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 更新后事件
|
||||
*/
|
||||
protected static function onAfterUpdate($data){
|
||||
$data = $data->toArray();
|
||||
if (isset($data['model_id']) && isset($data['name'])) {
|
||||
$tablename = Models::where('id', $data['model_id'])->value('name');
|
||||
//删除模型表中字段
|
||||
$db = new \com\Datatable();
|
||||
if ($db->CheckField($tablename, $data['name'])) {
|
||||
$data['action'] = 'CHANGE';
|
||||
}
|
||||
$data['after'] = self::where('name', '<>', $data['name'])->where('model_id', $data['model_id'])->order('sort asc, id asc')->value('name');
|
||||
$result = $db->columField(strtolower($tablename), $data)->query();
|
||||
return $result;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 删除后事件
|
||||
*/
|
||||
protected static function onAfterDelete($data){
|
||||
$data = $data->toArray();
|
||||
if ($data['model_id']) {
|
||||
$tablename = Models::where('id', $data['model_id'])->value('name');
|
||||
|
||||
//删除模型表中字段
|
||||
$db = new \com\Datatable();
|
||||
if (!$db->CheckField($tablename, $data['name'])) {
|
||||
$result = true;
|
||||
}else{
|
||||
$result = $db->delField($tablename, $data['name'])->query();
|
||||
}
|
||||
return $result;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getTypeTextAttr($value, $data) {
|
||||
$config_type_list = Config::get('config.config_type_list') ?? [];
|
||||
$type = [];
|
||||
foreach ($config_type_list as $key => $value) {
|
||||
$type[$value['key']] = $value['label'];
|
||||
}
|
||||
return isset($type[$data['type']]) ? $type[$data['type']] : '';
|
||||
}
|
||||
|
||||
protected function getOptionAttr($value, $data){
|
||||
$list = [];
|
||||
if ($data == '') {
|
||||
return $list;
|
||||
}
|
||||
if (in_array($data['type'], ['checkbox', 'radio', 'select'])) {
|
||||
$row = explode(PHP_EOL, $data['extra']);
|
||||
foreach ($row as $k => $val) {
|
||||
if (strrpos($val, ":")) {
|
||||
list($key, $label) = explode(":", $val);
|
||||
$list[] = ['key' => $key, 'label' => $label];
|
||||
}else{
|
||||
$list[] = ['key' => $k, 'label' => $val];
|
||||
}
|
||||
}
|
||||
}elseif($data['type'] == 'bool'){
|
||||
$list = [['key'=>0,'label'=>'禁用'],['key'=>1,'label'=>'启用']];
|
||||
}elseif($data['type'] == 'bind'){
|
||||
$map = [];
|
||||
$db = new \com\Datatable();
|
||||
if (strrpos($data['extra'], ":")) {
|
||||
$extra = explode(":", $data['extra']);
|
||||
if ($db->CheckField($extra[0], 'model_id')) {
|
||||
$map[] = ['model_id', '=', $data['model_id']];
|
||||
}
|
||||
$row = Db::name($extra[0])->where($map)->select()->toArray();
|
||||
if(empty($row)){
|
||||
return [];
|
||||
}
|
||||
if ($extra[1] == 'tree') {
|
||||
$row = (new Tree())->toFormatTree($row);
|
||||
foreach ($row as $val) {
|
||||
$list[] = ['key'=>$val['id'], 'label'=>$val['title_show']];
|
||||
}
|
||||
}else{
|
||||
foreach ($row as $val) {
|
||||
$list[] = ['key'=>$val['id'], 'label'=>$val['title']];
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if ($db->CheckField($data['extra'], 'model_id')) {
|
||||
$map[] = ['model_id', '=', $data['model_id']];
|
||||
}
|
||||
$row = Db::name($data['extra'])->select($map)->toArray();
|
||||
foreach ($row as $val) {
|
||||
$list[] = ['key'=>$val['id'], 'label'=>$val['title']];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
public static function getField($model, $ac = "add"){
|
||||
$list = [];
|
||||
$group = $model['attr_group'];
|
||||
|
||||
$map = [];
|
||||
$map[] = ['model_id', '=', $model['id']];
|
||||
if ($ac == 'add') {
|
||||
$map[] = ['is_show', 'IN', [1, 2]];
|
||||
}else if ($ac == 'edit') {
|
||||
$map[] = ['is_show', 'IN', [1, 3]];
|
||||
}
|
||||
|
||||
$row = self::where($map)->order('group_id asc, sort asc, id desc')
|
||||
->select()
|
||||
->append(['option'])
|
||||
->toArray();
|
||||
foreach ($row as $key => $value) {
|
||||
if (isset($group[$value['group_id']])) {
|
||||
$list[$group[$value['group_id']]['label']][] = $value;
|
||||
}else{
|
||||
$list[$value['group_id']][] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public static function getfieldList(){
|
||||
$config = \think\facade\Cache::get('system_config_data');
|
||||
$time = [['key'=>1, 'label'=>'新增'],['key'=>2, 'label'=>'编辑'],['key'=>3, 'label'=>'始终']];
|
||||
$auto_type = [['key'=>'function', 'label'=>'函数'],['key'=>'field', 'label'=>'字段'],['key'=>'string', 'label'=>'字符串']];
|
||||
$validate_type = [['key'=>'thinkphp', 'label'=>'thinkphp内置'],['key'=>'regex', 'label'=>'正则验证']];
|
||||
return [
|
||||
'基础' => [
|
||||
['name' => 'id', 'title' => 'id', 'help' => '', 'type' => 'hidden'],
|
||||
['name' => 'model_id', 'title' => 'model_id', 'help' => '', 'type' => 'hidden'],
|
||||
['name' => 'name', 'title' => '字段名', 'help' => '英文字母开头,长度不超过30', 'is_must'=> true, 'type' => 'text'],
|
||||
['name' => 'title', 'title' => '字段标题', 'help' => '请输入字段标题,用于表单显示', 'is_must'=> true, 'type' => 'text'],
|
||||
['name' => 'type', 'title' => '字段类型', 'help' => '用于表单中的展示方式', 'type' => 'select', 'option' => $config['config_type_list'], 'help' => ''],
|
||||
['name' => 'length', 'title' => '字段长度', 'help' => '字段的长度值', 'type' => 'text'],
|
||||
['name' => 'extra', 'title' => '参数', 'help' => '布尔、枚举、多选字段类型的定义数据', 'type' => 'textarea'],
|
||||
['name' => 'value', 'title' => '默认值', 'help' => '字段的默认值', 'type' => 'text'],
|
||||
['name' => 'remark', 'title' => '字段备注', 'help' => '用于表单中的提示', 'type' => 'text'],
|
||||
['name' => 'is_show', 'title' => '是否显示', 'help' => '是否显示在表单中', 'type' => 'select', 'option' => [
|
||||
['key'=>'1', 'label' => '始终显示'], ['key'=>'2', 'label' => '新增显示'], ['key'=>'3', 'label' => '编辑显示'], ['key'=>'0', 'label' => '不显示']
|
||||
], 'value' => 1],
|
||||
['name' => 'is_must', 'title' => '是否必填', 'help' => '用于自动验证', 'type' => 'select', 'option' => [['key'=>'0', 'label' => '否'], ['key'=>'1', 'label' => '是']]],
|
||||
],
|
||||
'高级' => [
|
||||
// ['name' => 'validate_type', 'title' => '验证方式', 'type' => 'select', 'option' => $validate_type, 'help' => ''],
|
||||
['name' => 'validate_rule', 'title' => '验证规则', 'help' => '使用thinkphp内置验证规则,详情:https://www.kancloud.cn/manual/thinkphp6_0/1037629', 'type' => 'text'],
|
||||
['name' => 'error_info', 'title' => '出错提示', 'type' => 'text', 'help' => ''],
|
||||
['name' => 'validate_time', 'title' => '验证时间', 'help' => '英文字母开头,长度不超过30', 'type' => 'select', 'option' => $time, 'help' => ''],
|
||||
['name' => 'auto_type', 'title' => '自动完成方式', 'help' => '英文字母开头,长度不超过30', 'type' => 'select', 'option' => $auto_type, 'help' => ''],
|
||||
['name' => 'auto_rule', 'title' => '自动完成规则', 'help' => '根据完成方式订阅相关规则', 'type' => 'text'],
|
||||
['name' => 'auto_time', 'title' => '自动完成时间', 'help' => '英文字母开头,长度不超过30', 'type' => 'select', 'option' => $time],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
40
app/model/module/Category.php
Normal file
40
app/model/module/Category.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\model\module;
|
||||
|
||||
/**
|
||||
* 设置模型
|
||||
*/
|
||||
class Category extends \think\Model{
|
||||
|
||||
protected $name = "Category";
|
||||
protected $auto = array('update_time', 'status' => 1);
|
||||
|
||||
protected $type = array(
|
||||
'icon' => 'integer',
|
||||
);
|
||||
|
||||
protected static function onAfterUpdate($model){
|
||||
$data = $model->toArray();
|
||||
}
|
||||
|
||||
public static function getCategoryTree($map = []){
|
||||
$list = self::where($map)->select();
|
||||
|
||||
if (!empty($list)) {
|
||||
$tree = new \sent\tree\Tree();
|
||||
$list = $tree->toFormatTree($list->toArray());
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function info($id, $field = true) {
|
||||
return $this->db()->where(array('id' => $id))->field($field)->find();
|
||||
}
|
||||
}
|
||||
33
app/model/module/Document.php
Normal file
33
app/model/module/Document.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\model\module;
|
||||
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 文档模型
|
||||
*/
|
||||
class Document {
|
||||
|
||||
public static function getDocumentList($model, $category_id, $limit = 20, $order = "id desc", $field = "*"){
|
||||
$map = [];
|
||||
if (!$model) {
|
||||
return [];
|
||||
}
|
||||
//判断model是否为内容模型
|
||||
$info = Model::where('name', $model)->where('status', 1)->findOrEmpty();
|
||||
|
||||
if ($info->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
$list = Db::name($model)->where($map)->limit($limit)->order($order)->field($field)->select();
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
175
app/model/module/Model.php
Normal file
175
app/model/module/Model.php
Normal file
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\model\module;
|
||||
|
||||
/**
|
||||
* 设置模型
|
||||
*/
|
||||
class Model extends \think\Model {
|
||||
|
||||
protected $auto = ['update_time'];
|
||||
protected $insert = ['name', 'create_time', 'status' => 1, 'list_grid' => "id:ID\r\ntitle:标题\r\ncreate_time:添加时间\r\nupdate_time:更新时间"];
|
||||
protected $type = array(
|
||||
'id' => 'integer',
|
||||
);
|
||||
|
||||
protected static function onBeforeInsert($data) {
|
||||
if ($data['name'] && $data['title']) {
|
||||
$db = new \com\Datatable();
|
||||
//检查表是否存在并创建
|
||||
if (!$db->CheckTable($data['name'])) {
|
||||
//创建新表
|
||||
return $db->initTable($data['name'], $data['title'], 'id')->query();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected static function onAfterInsert($data) {
|
||||
$data = $data->toArray();
|
||||
$fields = [
|
||||
'title' => ['name' => 'title', 'title' => '标题', 'type' => 'text', 'length' => 200, 'extra' => '', 'remark' => '标题', 'is_show' => 1, 'is_must' => 1, 'value' => ''],
|
||||
'category_id' => ['name' => 'category_id', 'title' => '栏目', 'type' => 'bind', 'length' => 10, 'extra' => 'category:tree', 'remark' => '栏目', 'is_show' => 1, 'is_must' => 1, 'value' => '0'],
|
||||
'uid' => ['name' => 'uid', 'title' => '用户UID', 'type' => 'num', 'length' => 11, 'extra' => '', 'remark' => '用户UID', 'is_show' => 0, 'is_must' => 1, 'value' => '0'],
|
||||
'cover_id' => ['name' => 'cover_id', 'title' => '内容封面', 'type' => 'image', 'length' => 10, 'extra' => '', 'remark' => '内容封面', 'is_show' => 1, 'is_must' => 0, 'value' => '0'],
|
||||
'description' => ['name' => 'description', 'title' => '内容描述', 'type' => 'textarea', 'length' => '', 'extra' => '', 'remark' => '内容描述', 'is_show' => 1, 'is_must' => 0, 'value' => ''],
|
||||
'content' => ['name' => 'content', 'title' => '内容', 'type' => 'editor', 'length' => '', 'extra' => '', 'remark' => '内容', 'is_show' => 1, 'is_must' => 0, 'value' => ''],
|
||||
'status' => ['name' => 'status', 'title' => '数据状态', 'type' => 'select', 'length' => 2, 'extra' => "-1:删除\r\n0:禁用\r\n1:正常\r\n2:待审核\r\n3:草稿", 'remark' => '数据状态', 'is_show' => 1, 'is_must' => 1, 'value' => '1'],
|
||||
'is_top' => ['name' => 'is_top', 'title' => '是否置顶', 'type' => 'bool', 'length' => 2, 'extra' => '', 'remark' => '是否置顶', 'is_show' => 0, 'is_must' => 1, 'value' => '0'],
|
||||
'view' => ['name' => 'view', 'title' => '浏览数量', 'type' => 'num', 'length' => 11, 'extra' => '', 'remark' => '浏览数量', 'is_show' => 0, 'is_must' => 1, 'value' => '0'],
|
||||
'update_time' => ['name' => 'update_time', 'title' => '更新时间', 'type' => 'datetime', 'length' => 11, 'extra' => '', 'remark' => '更新时间', 'is_show' => 0, 'is_must' => 1, 'value' => '0'],
|
||||
'create_time' => ['name' => 'create_time', 'title' => '添加时间', 'type' => 'datetime', 'length' => 11, 'extra' => '', 'remark' => '添加时间', 'is_show' => 0, 'is_must' => 1, 'value' => '0'],
|
||||
];
|
||||
$result = false;
|
||||
if (!empty($fields)) {
|
||||
foreach ($fields as $key => $value) {
|
||||
if ($data['is_doc']) {
|
||||
$fields[$key]['model_id'] = $data['id'];
|
||||
} else {
|
||||
if (in_array($key, ['uid', 'title', 'status', 'view', 'create_time', 'update_time'])) {
|
||||
$fields[$key]['model_id'] = $data['id'];
|
||||
} else {
|
||||
unset($fields[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$result = (new Attribute())->saveAll($fields);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected static function onAfterUpdate($data) {
|
||||
$data = $data->toArray();
|
||||
if (isset($data['attribute_sort']) && $data['attribute_sort']) {
|
||||
$attribute_sort = json_decode($data['attribute_sort'], true);
|
||||
|
||||
$attr = [];
|
||||
if (!empty($attribute_sort)) {
|
||||
foreach ($attribute_sort as $key => $value) {
|
||||
$attr[$key] = Attribute::where('id', 'IN', $value)->column('*', 'id');
|
||||
foreach ($value as $k => $v) {
|
||||
$attr[$key][$v]['group_id'] = $key;
|
||||
$attr[$key][$v]['sort'] = $k;
|
||||
}
|
||||
}
|
||||
}
|
||||
$save = [];
|
||||
foreach ($attr as $value) {
|
||||
if (!empty($value)) {
|
||||
$save = array_merge($save, $value);
|
||||
}
|
||||
}
|
||||
if (!empty($attr)) {
|
||||
(new Attribute())->saveAll($save);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected static function onAfterDelete($data) {
|
||||
$data = $data->toArray();
|
||||
(new Attribute())->where('model_id', $data['id'])->delete();
|
||||
$db = new \com\Datatable();
|
||||
if ($db->CheckTable($data['name'])) {
|
||||
$db->delTable($data['name'])->query();
|
||||
}
|
||||
}
|
||||
|
||||
protected function setAttributeSortAttr($value) {
|
||||
return $value ? json_encode($value) : '';
|
||||
}
|
||||
|
||||
public function setNameAttr($value) {
|
||||
return strtolower($value);
|
||||
}
|
||||
|
||||
public function getGridListAttr($value, $data) {
|
||||
$list = [];
|
||||
if ($data['list_grid'] !== '') {
|
||||
$row = explode(PHP_EOL, $data['list_grid']);
|
||||
foreach ($row as $r) {
|
||||
list($field, $title) = explode(":", $r);
|
||||
$list[$field] = ['field' => $field, 'title' => $title];
|
||||
if (strrpos($title, "|")) {
|
||||
$title = explode("|", $title);
|
||||
$list[$field] = ['field' => $field, 'title' => $title[0], 'format' => trim($title[1])];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function getAttrGroupAttr($value, $data) {
|
||||
$list = [];
|
||||
if ($data['attribute_group']) {
|
||||
$row = explode(";", $data['attribute_group']);
|
||||
foreach ($row as $r) {
|
||||
list($key, $label) = explode(":", $r);
|
||||
$list[$key] = ['key' => $key, 'label' => $label];
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function getStatusTextAttr($value, $data) {
|
||||
$status = [0 => '禁用', 1 => '启用'];
|
||||
return $status[$data['status']];
|
||||
}
|
||||
|
||||
public function del() {
|
||||
$id = input('id', '', 'trim,intval');
|
||||
$tablename = $this->where('id', $id)->value('name');
|
||||
|
||||
//删除数据表
|
||||
$db = new \com\Datatable();
|
||||
if ($db->CheckTable($tablename)) {
|
||||
//检测表是否存在
|
||||
$result = $db->delTable($tablename)->query();
|
||||
if (!$result) {
|
||||
return false;
|
||||
$this->error = "数据表删除失败!";
|
||||
}
|
||||
}
|
||||
db('Attribute')->where('model_id', $id)->delete(); //删除字段信息
|
||||
$result = $this->where('id', $id)->delete();
|
||||
if ($result) {
|
||||
return true;
|
||||
} else {
|
||||
$this->error = "模型删除失败!";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function attribute() {
|
||||
return $this->hasMany('Attribute');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user