更新功能
This commit is contained in:
@@ -34,7 +34,7 @@ class Category extends Admin {
|
||||
|
||||
$map[] = ['status', '>', -1];
|
||||
if (isset($param['model_id']) && $param['model_id']) {
|
||||
$map[] = ['model_id', '=', $model_id];
|
||||
$map[] = ['model_id', '=', $param['model_id']];
|
||||
}
|
||||
$list = $category->where($map)->order('sort asc,id asc')->select();
|
||||
|
||||
@@ -42,13 +42,14 @@ class Category extends Admin {
|
||||
$tree = new \sent\tree\Tree();
|
||||
$list = $tree->toFormatTree($list->toArray());
|
||||
}
|
||||
$subsql = $attr->where('name', 'category_id')->fetchSql(true)->column('model_id');
|
||||
$model_list = $model->where('id IN ('. $subsql.')')->select();
|
||||
$model_list = Model::where('id', 'IN', function($query){
|
||||
$query->name('Attribute')->where('name', 'category_id')->field('model_id');
|
||||
})->select();
|
||||
|
||||
$this->data = [
|
||||
'tree' => $list,
|
||||
'module_id' => isset($param['module_id']) ? $param['module_id'] : 0,
|
||||
'model_list' => []
|
||||
'model_id' => isset($param['model_id']) ? $param['model_id'] : 0,
|
||||
'model_list' => $model_list
|
||||
];
|
||||
return $this->fetch();
|
||||
}
|
||||
@@ -72,29 +73,20 @@ class Category extends Admin {
|
||||
*/
|
||||
public function edit($id = null, $pid = 0) {
|
||||
if ($this->request->isPost()) {
|
||||
$category = model('Category');
|
||||
//提交表单
|
||||
$result = $category->change();
|
||||
$data = $this->request->post();
|
||||
|
||||
$result = CategoryM::update($data, ['id' => $data['id']]);
|
||||
if (false !== $result) {
|
||||
//记录行为
|
||||
action_log('update_category', 'category', $id, session('user_auth.uid'));
|
||||
return $this->success('编辑成功!', url('index'));
|
||||
return $this->success('修改成功!', url('/admin/category/index'));
|
||||
} else {
|
||||
$error = $category->getError();
|
||||
return $this->error(empty($error) ? '未知错误!' : $error);
|
||||
return $this->error('修改失败!');
|
||||
}
|
||||
} else {
|
||||
$cate = [];
|
||||
// if ($pid) {
|
||||
// /* 获取上级分类信息 */
|
||||
// $cate = db('Category')->find($pid);
|
||||
// if (!($cate && 1 == $cate['status'])) {
|
||||
// return $this->error('指定的上级分类不存在或被禁用!');
|
||||
// }
|
||||
// }
|
||||
// $subsql = db('Attribute')->where('name', 'category_id')->fetchSql(true)->column('model_id');
|
||||
// $model_list = model('Model')->where('id IN ('. $subsql.')')->select();
|
||||
$model_list = [];
|
||||
$category = CategoryM::getCategoryTree();
|
||||
|
||||
$model_list = Model::where('id', 'IN', function($query){
|
||||
$query->name('Attribute')->where('name', 'category_id')->field('model_id');
|
||||
})->select();
|
||||
|
||||
/* 获取分类信息 */
|
||||
$info = $id ? CategoryM::find($id) : [];
|
||||
@@ -102,7 +94,7 @@ class Category extends Admin {
|
||||
$this->data = [
|
||||
'info' => $info,
|
||||
'model_list' => $model_list,
|
||||
'category' => $cate
|
||||
'category' => $category
|
||||
];
|
||||
return $this->fetch();
|
||||
}
|
||||
@@ -111,36 +103,32 @@ class Category extends Admin {
|
||||
/**
|
||||
* @title 添加分类
|
||||
*/
|
||||
public function add($pid = 0) {
|
||||
$Category = model('Category');
|
||||
|
||||
public function add() {
|
||||
if ($this->request->isPost()) {
|
||||
//提交表单
|
||||
$id = $Category->change();
|
||||
if (false !== $id) {
|
||||
action_log('update_category', 'category', $id, session('user_auth.uid'));
|
||||
return $this->success('新增成功!', url('index'));
|
||||
$data = $this->request->post();
|
||||
|
||||
$result = CategoryM::create($data);
|
||||
if (false !== $result) {
|
||||
return $this->success('新增成功!', url('/admin/category/index'));
|
||||
} else {
|
||||
$error = $Category->getError();
|
||||
return $this->error(empty($error) ? '未知错误!' : $error);
|
||||
return $this->error('添加失败!');
|
||||
}
|
||||
} else {
|
||||
$cate = array();
|
||||
if ($pid) {
|
||||
/* 获取上级分类信息 */
|
||||
$cate = $Category->info($pid, 'id,name,title,status');
|
||||
if (!($cate && 1 == $cate['status'])) {
|
||||
return $this->error('指定的上级分类不存在或被禁用!');
|
||||
}
|
||||
}
|
||||
$subsql = db('Attribute')->where('name', 'category_id')->fetchSql(true)->column('model_id');
|
||||
$model_list = model('Model')->where('id IN ('. $subsql.')')->select();
|
||||
$pid = $this->request->param('pid', 0);
|
||||
|
||||
$category = CategoryM::getCategoryTree();
|
||||
|
||||
$model_list = Model::where('id', 'IN', function($query){
|
||||
$query->name('Attribute')->where('name', 'category_id')->field('model_id');
|
||||
})->select();
|
||||
|
||||
/* 获取分类信息 */
|
||||
$this->assign('info', null);
|
||||
$this->assign('model_list', $model_list);
|
||||
$this->assign('category', $cate);
|
||||
$this->setMeta('新增分类');
|
||||
$this->data = [
|
||||
'info' => ['pid' => $pid],
|
||||
'model_list' => $model_list,
|
||||
'category' => $category
|
||||
];
|
||||
return $this->fetch('edit');
|
||||
}
|
||||
}
|
||||
@@ -153,8 +141,9 @@ class Category extends Admin {
|
||||
return $this->error('参数错误!');
|
||||
}
|
||||
//判断该分类下有没有子分类,有则不允许删除
|
||||
$child = db('Category')->where(array('pid' => $id))->field('id')->select();
|
||||
if (!empty($child)) {
|
||||
$child = CategoryM::where('pid', $id)->field('id')->select();
|
||||
|
||||
if (!$child->isEmpty()) {
|
||||
return $this->error('请先删除该分类下的子分类');
|
||||
}
|
||||
//判断该分类下有没有内容
|
||||
@@ -163,10 +152,8 @@ class Category extends Admin {
|
||||
// return $this->error('请先删除该分类下的文章(包含回收站)');
|
||||
// }
|
||||
//删除该分类信息
|
||||
$res = db('Category')->where(array('id' => $id))->delete();
|
||||
if ($res !== false) {
|
||||
//记录行为
|
||||
action_log('update_category', 'category', $id, session('user_auth.uid'));
|
||||
$result = CategoryM::where('id', $id)->delete();
|
||||
if ($result !== false) {
|
||||
return $this->success('删除分类成功!');
|
||||
} else {
|
||||
return $this->error('删除分类失败!');
|
||||
@@ -268,19 +255,20 @@ class Category extends Admin {
|
||||
* @author huajie <banhuajie@163.com>
|
||||
*/
|
||||
public function status() {
|
||||
$id = $this->getArrayParam('id');
|
||||
$status = input('status', '0', 'trim,intval');
|
||||
|
||||
if (!$id) {
|
||||
return $this->error("非法操作!");
|
||||
$id = $this->request->param('id', 0);
|
||||
$status = $this->request->param('status', 0);
|
||||
$map = [];
|
||||
if (is_array($id)) {
|
||||
$map[] = ['id', 'IN', $id];
|
||||
}else{
|
||||
$map[] = ['id', '=', $id];
|
||||
}
|
||||
|
||||
$map['id'] = array('IN', $id);
|
||||
$result = db('Category')->where($map)->setField('status', $status);
|
||||
if ($result) {
|
||||
return $this->success("设置成功!");
|
||||
$result = CategoryM::update(['status'=> $status], $map);
|
||||
if ($result !== false) {
|
||||
return $this->success('操作成功!');
|
||||
} else {
|
||||
return $this->error("设置失败!");
|
||||
return $this->error('操作失败!');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user