Files
2024-10-29 14:04:59 +08:00

229 lines
9.4 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use think\Lang;
use think\Loader;
use think\Config;
class Dept extends BaseController {
public function index() {
$this->redirect('/admin/dept/lists');
}
public function lists() {
$skeyword = $this->request->get('skeyword', '', 'urldecode');
$arg_where = array('pid' => 0, 'stat' => 0);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('*');
if (!empty($skeyword)) {
$skeyword = trim($skeyword);
$arg_where['name'] = ['like', '%' . $skeyword . '%'];
$search['skeyword'] = $skeyword;
Config::set('paginate.query', ['skeyword' => $skeyword]); //分页参数
} else {
$search['skeyword'] = '';
}
$dept_list = Loader::model('Dept')->getDeptLists($arg_where, $arg_order, $arg_field, 20);
$value = ['list' => $dept_list, 'pid' => 0, 'search' => $search,];
$this->assign($value);
return $this->fetch();
}
public function add($pid = 0) {
$pid = isset($pid) ? (int) $pid : 0;
$arg_where = array('pid' => 0, 'stat' => 0);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort');
$deptOptions = Loader::model('Dept')->getOptions($pid, $arg_where, $arg_order, $arg_field, 100);
$value = ['deptOptions' => $deptOptions, 'pid' => $pid];
$this->assign($value);
return $this->fetch();
}
public function create() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
$validaterule = ['name' => 'require', 'pid' => 'between:0,2147483647', 'module' => 'require', 'url' => 'require',];
$validatemsg = ['name.require' => '名称不能为空'];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$deptModel = Loader::model('Dept');
if (isset($data['pid']) && $data['pid']) {
$deptModel::update(['haschild' => 1], ['id' => $data['pid'], 'haschild' => 0]);
}
$model = $deptModel->insertRow($data);
if ($model && $model->getData('id')) {
$this->cacheClear('DeptTag');
return $this->success(Lang::get('operation successed'), url('/admin/dept/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
} else {
return $this->error(Lang::get('operation failed'));
}
}
public function edit($id = 0) {
$id = intval($id);
if ($id > 0) {
$dept = Loader::model('Dept')->getRow($id);
if (!$dept && !is_array($dept)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['dept'] = $dept;
}
$pid = isset($dept['pid']) ? (int) $dept['pid'] : 0;
$arg_where = array('pid' => 0, 'stat' => 0);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort');
$deptOptions = Loader::model('Dept')->getOptions($pid, $arg_where, $arg_order, $arg_field, 100);
$value['deptOptions'] = $deptOptions;
$ctrlOption = get_ctrl_names($dept['module']);
$value['ctrlOption'] = $ctrlOption;
$actionOption = get_action_names($dept['ctrl'], 'app\\' . $dept['module'] . '\\controller\\');
$value['actionOption'] = $actionOption;
$value['pid'] = $pid;
$this->assign($value);
return $this->fetch();
}
public function update() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
$validaterule = ['name' => 'require', 'pid' => 'between:0,2147483647', 'module' => 'require', 'url' => 'require',];
$validatemsg = ['name.require' => '名称不能为空'];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$deptModel = Loader::model('Dept');
$childIDArray = $deptModel->getChildIDArray($data['id']);
if (in_array($data['pid'], $childIDArray)) {
return $this->error('不可选择自己的子节点作为父节点');
}
if (isset($data['pid']) && $data['pid']) {
$deptModel::update(['haschild' => 1], ['id' => $data['pid'], 'haschild' => 0]);
}
$oldpid = $data['oldpid'];
unset($data['oldpid']);
$model = $deptModel->updateRow($data);
if (isset($oldpid) && $oldpid) {
$oneObject = $deptModel->getRow(['pid' => $oldpid]);
if (!$oneObject) {
$deptModel::update(['haschild' => 0], ['id' => $oldpid, 'haschild' => 1]);
}
}
if ($model && $model->getData('id')) {
$this->cacheClear('DeptTag');
return $this->success(Lang::get('operation successed'), url('/admin/dept/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
} else {
return $this->error(Lang::get('operation failed'));
}
}
public function updatesort() {
$id = $this->request->param('id', 0);
$sort = $this->request->param('sort', 0);
$sort = intval($sort);
$id = intval($id);
if ($id > 0 && $sort < 2147483647) {
$model = Loader::model('Dept')->updateRow(['id' => $id, 'sort' => $sort]);
if ($model && $model->getData('id')) {
$this->cacheClear('DeptTag');
return $this->success(Lang::get('operation successed'), url('/admin/dept/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function toggleisshow() {
$id = $this->request->param('id', 0);
$flag = $this->request->param('flag', 0);
$id = intval($id);
if ($id > 0) {
$model = Loader::model('Dept')->updateRow(['id' => $id, 'hidden' => !$flag]);
if ($model && $model->getData('id')) {
$this->cacheClear('DeptTag');
return $this->success(Lang::get('operation successed'), url('/admin/dept/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function recovery($id = 0) {
$id = intval($id);
if ($id > 0) {
$model = Loader::model('Dept')->updateRow(['id' => $id, 'stat' => 0]);
if ($model && $model->getData('id')) {
$this->cacheClear('DeptTag');
return $this->success(Lang::get('operation successed'), url('/admin/dept/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function delete($id = 0) {
$id = intval($id);
if ($id > 0) {
$categoryModel = Loader::model('Dept');
$category = $categoryModel->getRow(['id' => $id, 'stat' => 0], ['id', 'pid', 'haschild', 'name']);
if ($category && $category['haschild']) {
$child = $categoryModel->getRow(['pid' => $id, 'stat' => 0], ['id', 'pid', 'haschild', 'name']);
if ($child) {
return $this->error('此节点包含子节点[ID:' . $child['id'] . '],不能进行删除');
}
}
$model = $categoryModel->deleteRow($id);
if ($model && $model->getData('id')) {
if ($category && $category['pid']) {
$oneObject = $categoryModel->getRow(['stat' => 0, 'pid' => $category['pid'], 'id' => ['neq', $category['id']]]);
if (!$oneObject) {
$categoryModel::update(['haschild' => 0], ['id' => $category['pid'], 'haschild' => 1]);
}
}
$this->cacheClear('DeptTag');
return $this->success(Lang::get('operation successed'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function destroy($id = 0) {
$id = intval($id);
if ($id > 0) {
$result = Loader::model('Dept')->deleteRow($id);
if ($result) {
$this->cacheClear('DeptTag');
return $this->success(Lang::get('operation successed'), url('/admin/dept/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
}