重新定义模型
This commit is contained in:
@@ -46,19 +46,7 @@ class Attribute extends Admin {
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function index($model_id = null) {
|
||||
$map['model_id'] = $model_id;
|
||||
if (!$model_id) {
|
||||
return $this->error("非法操作!");
|
||||
}
|
||||
|
||||
$list = model('Attribute')->where($map)->order('id desc')->paginate(25);
|
||||
|
||||
$data = array(
|
||||
'list' => $list,
|
||||
'model_id' => $model_id,
|
||||
'page' => $list->render(),
|
||||
);
|
||||
$this->assign($data);
|
||||
$this->getAttributeList($model_id);
|
||||
$this->setMeta('字段管理');
|
||||
return $this->fetch();
|
||||
}
|
||||
@@ -120,13 +108,15 @@ class Attribute extends Admin {
|
||||
* @var delattr 是否删除字段表里的字段
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function del() {
|
||||
$id = input('id', '', 'trim,intval');
|
||||
public function del(\think\Request $request) {
|
||||
$id = $request->param('id');
|
||||
$model_id = $request->param('model_id');
|
||||
|
||||
if (!$id) {
|
||||
return $this->error("非法操作!");
|
||||
}
|
||||
|
||||
$result = $this->model->del($id);
|
||||
$result = $this->model->del($id, $model_id);
|
||||
if ($result) {
|
||||
return $this->success("删除成功!");
|
||||
} else {
|
||||
@@ -134,6 +124,47 @@ class Attribute extends Admin {
|
||||
}
|
||||
}
|
||||
|
||||
public function generate($id = '') {
|
||||
if ($id) {
|
||||
$model = model('Model')->where('id', $id)->find();
|
||||
$result = $this->model->generate($model);
|
||||
if (false !== $result) {
|
||||
return $this->success('生成成功!', url('admin/model/index'));
|
||||
} else {
|
||||
return $this->error($this->model->getError());
|
||||
}
|
||||
} else {
|
||||
return $this->error('非法操作!');
|
||||
}
|
||||
}
|
||||
|
||||
public function insert(\think\Request $request) {
|
||||
if (IS_POST) {
|
||||
$model_id = $request->param('id');
|
||||
$attr = db('Model')->where('id', $model_id)->value('attribute_list');
|
||||
$attr = explode(',', $attr);
|
||||
$post = $request->post();
|
||||
$attribute = array_merge($attr, $post['id']);
|
||||
|
||||
$data = array(
|
||||
'attribute_list' => implode(',', $attribute),
|
||||
'table_status' => 2,
|
||||
'status' => 0,
|
||||
);
|
||||
$result = db('Model')->where('id', $model_id)->update($data);
|
||||
if (false !== $result) {
|
||||
return $this->success('成功导入!');
|
||||
}else{
|
||||
return $this->error('导入失败!');
|
||||
}
|
||||
} else {
|
||||
$this->getAttributeList();
|
||||
$this->assign('id', $request->param('id'));
|
||||
$this->setMeta('导入字段');
|
||||
return $this->fetch();
|
||||
}
|
||||
}
|
||||
|
||||
//字段编辑所需字段
|
||||
protected function getField() {
|
||||
return array(
|
||||
@@ -161,4 +192,21 @@ class Attribute extends Admin {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
protected function getAttributeList($model_id = '') {
|
||||
$map = array();
|
||||
if ($model_id) {
|
||||
$attribute = db('Model')->where('id', $model_id)->value('attribute_list');
|
||||
$map['id'] = array('IN', $attribute);
|
||||
}
|
||||
|
||||
$list = model('Attribute')->where($map)->order('id desc')->paginate(25);
|
||||
|
||||
$data = array(
|
||||
'list' => $list,
|
||||
'model_id' => $model_id,
|
||||
'page' => $list->render(),
|
||||
);
|
||||
$this->assign($data);
|
||||
}
|
||||
}
|
||||
@@ -16,20 +16,13 @@ class Content extends Admin {
|
||||
parent::_initialize();
|
||||
$this->getContentMenu();
|
||||
$this->model_id = $model_id = $this->request->param('model_id');
|
||||
$row = db('Model')->select();
|
||||
foreach ($row as $key => $value) {
|
||||
$list[$value['id']] = $value;
|
||||
}
|
||||
$list = db('Model')->column('*', 'id');
|
||||
|
||||
if (empty($list[$model_id])) {
|
||||
return $this->error("无此模型!");
|
||||
} else {
|
||||
$this->modelInfo = $list[$model_id];
|
||||
if ($this->modelInfo['extend'] > 1) {
|
||||
$this->model = model('Content')->extend($this->modelInfo['name']);
|
||||
} else {
|
||||
$this->model = model('Document')->extend($this->modelInfo['name']);
|
||||
}
|
||||
$this->model = model('Content')->extend($this->modelInfo['name']);
|
||||
}
|
||||
|
||||
$this->assign('model_id', $model_id);
|
||||
@@ -49,11 +42,7 @@ class Content extends Admin {
|
||||
$order = "id desc";
|
||||
$map = $this->buildMap();
|
||||
$field = array_filter($grid_list['fields']);
|
||||
if ($this->modelInfo['extend'] == 1) {
|
||||
array_push($field, 'is_top');
|
||||
} else {
|
||||
unset($map['model_id']);
|
||||
}
|
||||
|
||||
|
||||
$list = $this->model->lists($map, $order);
|
||||
|
||||
@@ -247,7 +236,7 @@ class Content extends Admin {
|
||||
*/
|
||||
protected function buildMap() {
|
||||
$map = array();
|
||||
$data = $this->request->get();
|
||||
$data = $this->request->param();
|
||||
foreach ($data as $key => $value) {
|
||||
if ($value) {
|
||||
if ($key == 'keyword') {
|
||||
@@ -264,12 +253,6 @@ class Content extends Admin {
|
||||
if (isset($map['page'])) {
|
||||
unset($map['page']);
|
||||
}
|
||||
if ($this->modelInfo['extend'] == 1) {
|
||||
$category = isset($data['category']) ? $data['category'] : '';
|
||||
$cate_list = parse_field_bind('category', $category, 0);
|
||||
$map['model_id'] = $this->model_id;
|
||||
$this->assign('cate_list', $cate_list);
|
||||
}
|
||||
$this->assign($data);
|
||||
return $map;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ class Model extends Admin {
|
||||
public function _initialize() {
|
||||
parent::_initialize();
|
||||
$this->getContentMenu();
|
||||
$this->model = model('Model');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,7 +26,7 @@ class Model extends Admin {
|
||||
$map = array('status' => array('gt', -1));
|
||||
|
||||
$order = "id desc";
|
||||
$list = model('Model')->where($map)->order($order)->paginate(10);
|
||||
$list = $this->model->where($map)->order($order)->paginate(10);
|
||||
|
||||
$data = array(
|
||||
'list' => $list,
|
||||
@@ -44,75 +45,73 @@ class Model extends Admin {
|
||||
* 新增页面初始化
|
||||
* @author huajie <banhuajie@163.com>
|
||||
*/
|
||||
public function add() {
|
||||
|
||||
//获取所有的模型
|
||||
$models = db('Model')->where(array('extend' => 0))->field('id,title')->select();
|
||||
|
||||
$this->assign('models', $models);
|
||||
$this->setMeta('新增模型');
|
||||
return $this->fetch();
|
||||
public function add(\think\Request $request) {
|
||||
if (IS_POST) {
|
||||
$result = $this->model->validate('Model.add')->save($request->post());
|
||||
if (false !== $result) {
|
||||
$this->success('创建成功!', url('admin/model/index'));
|
||||
}else{
|
||||
return $this->error($this->model->getError());
|
||||
}
|
||||
}else{
|
||||
$this->setMeta('新增模型');
|
||||
return $this->fetch();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑页面初始化
|
||||
* @author huajie <banhuajie@163.com>
|
||||
* @author molong <molong@tensent.cn>
|
||||
*/
|
||||
public function edit() {
|
||||
$id = input('id', '', 'trim,intval');
|
||||
if (empty($id)) {
|
||||
return $this->error('参数不能为空!');
|
||||
}
|
||||
|
||||
/*获取一条记录的详细数据*/
|
||||
$model = model('Model');
|
||||
$data = $model::find($id);
|
||||
if (!$data) {
|
||||
return $this->error($Model->getError());
|
||||
}
|
||||
$data['attribute_list'] = empty($data['attribute_list']) ? '' : explode(",", $data['attribute_list']);
|
||||
|
||||
// 是否继承了其他模型
|
||||
if ($data['extend'] == 1) {
|
||||
$map['model_id'] = array('IN', array($data['id'], $data['extend']));
|
||||
} else {
|
||||
$map['model_id'] = $data['id'];
|
||||
}
|
||||
$map['is_show'] = 1;
|
||||
$fields = db('Attribute')->where($map)->select();
|
||||
|
||||
// 梳理属性的可见性
|
||||
foreach ($fields as $key => $field) {
|
||||
if (!empty($data['attribute_list']) && !in_array($field['id'], $data['attribute_list'])) {
|
||||
$field['is_show'] = 0;
|
||||
public function edit(\think\Request $request) {
|
||||
if (IS_POST) {
|
||||
$result = $this->model->validate('Model.edit')->save($request->post(), array('id'=>$request->post('id')));
|
||||
if (false !== $result) {
|
||||
$this->success('更新成功!', url('admin/model/index'));
|
||||
}else{
|
||||
return $this->error($this->model->getError());
|
||||
}
|
||||
$field['group'] = -1;
|
||||
$field['sort'] = 0;
|
||||
$fields_tem[$field['id']] = $field;
|
||||
}
|
||||
}else{
|
||||
$info = $this->model->where('id', $request->param('id'))->find();
|
||||
|
||||
// 获取模型排序字段
|
||||
$field_sort = json_decode($data['field_sort'], true);
|
||||
if (!empty($field_sort)) {
|
||||
foreach ($field_sort as $group => $ids) {
|
||||
foreach ($ids as $key => $value) {
|
||||
if (!empty($fields_tem[$value])) {
|
||||
$fields_tem[$value]['group'] = $group;
|
||||
$fields_tem[$value]['sort'] = $key;
|
||||
//获取字段列表
|
||||
if ($info['attribute_list']) {
|
||||
$fields = model('Attribute')->where('id', 'IN', $info['attribute_list'])->where('is_show', 1)->select();
|
||||
// 梳理属性的可见性
|
||||
foreach ($fields as $key => $field) {
|
||||
$field['group'] = -1;
|
||||
$field['sort'] = 0;
|
||||
$fields_tem[$field['id']] = $field->toArray(); //数据对象转换为数组
|
||||
}
|
||||
|
||||
// 获取模型排序字段
|
||||
$field_sort = json_decode($info['attribute_sort'], true);//dump($field_sort);exit();
|
||||
if (!empty($field_sort)) {
|
||||
foreach ($field_sort as $group => $ids) {
|
||||
foreach ($ids as $key => $value) {
|
||||
if (!empty($fields_tem[$value])) {
|
||||
$fields_tem[$value]['group'] = $group;
|
||||
$fields_tem[$value]['sort'] = $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($fields_tem) && $fields_tem) {
|
||||
// 模型字段列表排序
|
||||
$fields = list_sort_by($fields_tem, "sort");
|
||||
}
|
||||
}else{
|
||||
$fields = array();
|
||||
}
|
||||
$data = array(
|
||||
'info' => $info,
|
||||
'fields' => $fields
|
||||
);
|
||||
$this->assign($data);
|
||||
$this->setMeta('编辑模型');
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
if (isset($fields_tem) && $fields_tem) {
|
||||
// 模型字段列表排序
|
||||
$fields = list_sort_by($fields_tem, "sort");
|
||||
}
|
||||
|
||||
$this->assign('fields', $fields);
|
||||
$this->assign('info', $data);
|
||||
$this->setMeta('编辑模型');
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,12 +119,11 @@ class Model extends Admin {
|
||||
* @author huajie <banhuajie@163.com>
|
||||
*/
|
||||
public function del() {
|
||||
$mdoel = model('Model');
|
||||
$result = $mdoel->del();
|
||||
$result = $this->model->del();
|
||||
if ($result) {
|
||||
return $this->success('删除模型成功!');
|
||||
} else {
|
||||
return $this->error($mdoel->getError());
|
||||
return $this->error($this->mdoel->getError());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,24 +144,27 @@ class Model extends Admin {
|
||||
* 更新数据
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function status() {
|
||||
$map['id'] = $this->request->param('ids');
|
||||
if (null == $map['id']) {
|
||||
public function status(\think\Request $request) {
|
||||
$map['id'] = $request->param('id');
|
||||
|
||||
$data['status'] = $request->param('status');
|
||||
|
||||
if (null == $map['id'] || null == $data['status']) {
|
||||
return $this->error('参数不正确!');
|
||||
}
|
||||
|
||||
$data['status'] = input('get.status');
|
||||
|
||||
if (null == $data['status']) {
|
||||
//实现单条数据数据修改
|
||||
$status = db('Model')->where($map)->field('status')->find();
|
||||
$data['status'] = $status['status'] ? 0 : 1;
|
||||
db('Model')->where($map)->update($data);
|
||||
} else {
|
||||
//实现多条数据同时修改
|
||||
$map['id'] = array('IN', $map['id']);
|
||||
db('Model')->where($map)->update($data);
|
||||
$model = $this->model->where($map)->find();
|
||||
if ($model['table_status'] != 1 && $data['status'] == 1) {
|
||||
return $this->error('数据表未创建或更新');
|
||||
}
|
||||
if ($model['list_grid'] == '' && $data['status'] == 1) {
|
||||
return $this->error('模型列表未定义');
|
||||
}
|
||||
$result = $this->model->where($map)->update($data);
|
||||
if (false !== $result) {
|
||||
return $this->success('状态设置成功!');
|
||||
}else{
|
||||
return $this->error($this->model->getError());
|
||||
}
|
||||
return $this->success('状态设置成功!');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user