Files
orico-official-website-old/app/admin/controller/DownloadCategory.php
2024-10-29 14:04:59 +08:00

305 lines
13 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use think\Lang;
use think\Loader;
use think\Config;
class DownloadCategory extends BaseController {
public function index() {
$this->redirect('/admin/download_category/lists');
}
public function lists() {
$skeyword = $this->request->get('skeyword', '', 'urldecode');
$arg_where = array('pid' => 0, 'stat' => 0, 'siteid' => $this->siteid, 'country_code' => $this->country_code);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort', 'isshow', 'recommend', 'picture');
if (!empty($skeyword)) {
$skeyword = trim($skeyword);
$arg_where['name'] = ['like', '%' . $skeyword . '%'];
$search['skeyword'] = $skeyword;
Config::set('paginate.query', ['skeyword' => $skeyword]); //分页参数
} else {
$search['skeyword'] = '';
}
$category_list = model('download_category')->getCategoryLists($arg_where, $arg_order, $arg_field, 24);
$value = ['list' => $category_list, 'pid' => 0, 'search' => $search,];
$this->assign($value);
return $this->fetch();
}
public function add($pid = 0) {
$pid = is_numeric($pid) ? intval($pid) : 0;
$arg_where = array('pid' => 0, 'stat' => 0, 'country_code' => $this->country_code);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort');
$categoryOptions = model('download_category')->getCategoryOptions($pid, $arg_where, $arg_order, $arg_field, 100);
$value = ['categoryOptions' => $categoryOptions, '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' => 'number|between:0,2147483647',];
$validatemsg = ['name.require' => '名称不能为空', 'pid.between' => '所属上级值无效',];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$data['sort'] = intval($data['sort']);
$data['siteid'] = $this->siteid;
$data['country_code'] = $this->country_code;
$categoryModel = model('download_category');
if (isset($data['pid']) && $data['pid']) {
$categoryModel::update(['haschild' => 1], ['id' => $data['pid'], 'haschild' => 0]);
}
$model = $categoryModel->insertRow($data);
if ($model && $model->getData('id')) {
return $this->redirect(url('/admin/download_category/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function copy($id = 0) {
$categoryModel = model('download_category');
$id = intval($id);
if ($id > 0) {
$category = $categoryModel->getRow($id);
if (empty($category)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['download_category'] = $category;
} else {
return $this->error(Lang::get('incorrect operation'));
}
$pid = isset($category['pid']) ? $category['pid'] : 0;
$arg_where = array('pid' => 0, 'stat' => 0);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort');
$categoryOptions = $categoryModel->getCategoryOptions($pid, $arg_where, $arg_order, $arg_field, 100);
$value['categoryOptions'] = $categoryOptions;
$value['pid'] = $pid;
$this->assign($value);
return $this->fetch();
}
public function edit($id = 0) {
$categoryModel = model('download_category');
$id = intval($id);
if ($id > 0) {
$category = $categoryModel->getRow($id);
if (empty($category)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['download_category'] = $category;
} else {
return $this->error(Lang::get('incorrect operation'));
}
$pid = isset($category['pid']) ? $category['pid'] : 0;
$arg_where = array('pid' => 0, 'stat' => 0);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort');
$categoryOptions = $categoryModel->getCategoryOptions($pid, $arg_where, $arg_order, $arg_field, 100);
$value['categoryOptions'] = $categoryOptions;
$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' => 'number|between:0,2147483647',];
$validatemsg = ['name.require' => '名称不能为空', 'pid.between' => '所属上级值无效',];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$data['sort'] = intval($data['sort']);
$oldpid = $data['oldpid'];
unset($data['oldpid']);
$categoryModel = model('download_category');
$childIDArray = $categoryModel->getChildIDArray($data['id']);
if (in_array($data['pid'], $childIDArray)) {
return $this->error('不可选择自己的子节点作为父节点');
}
if (isset($data['pid']) && $data['pid'] && $oldpid != $data['pid']) {
$categoryModel::update(['haschild' => 1], ['id' => $data['pid'], 'haschild' => 0]);
}
$model = $categoryModel->updateRow($data);
if (isset($oldpid) && $oldpid && $oldpid != $data['pid']) {
$oneObject = $categoryModel->getRow(['stat' => 0, 'pid' => $oldpid]);
if (!$oneObject) {
$categoryModel::update(['haschild' => 0], ['id' => $oldpid, 'haschild' => 1]);
}
}
if ($model && $model->getData('id')) {
return $this->redirect(url('/admin/download_category/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
} else {
return $this->error(Lang::get('operation failed'));
}
}
public function subcolumn() {
$column = $this->request->get('subcolumn/s', '', 'urldecode');
$columns = explode(',', $column);
$arg_where = array('pid' => 0, 'stat' => 0, 'siteid' => $this->siteid, 'country_code' => $this->country_code);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort');
$category_list = model('download_category')->getCategoryTree($arg_where, $arg_order, $arg_field, 20);
$value = ['list' => $category_list, 'columns' => $columns,];
$this->assign($value);
Config::set('default_ajax_return', 'html');
$this->view->engine(['type' => 'php', 'view_suffix' => 'html', 'tpl_replace_string' => [],]);
return $this->fetch();
}
public function listcategory($pid = 0) {
$pid = is_numeric($pid) ? intval($pid) : 0;
$categoryModel = model('download_category');
$arg_where = array('pid' => $pid, 'stat' => 0, 'siteid' => $this->siteid, 'country_code' => $this->country_code);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort', 'isshow', 'recommend', 'picture');
$category_list = $categoryModel->getList($arg_where, $arg_order, $arg_field, 50);
$category_breadcrumb = $categoryModel->getBreadCrumb($pid);
$value = ['list' => $category_list, 'pid' => $pid, 'breadcrumb' => $category_breadcrumb, 'level' => 0];
$this->assign($value);
return $this->fetch();
}
public function childcat($pid = 0) {
$pid = $this->request->get('pid', 0);
$level = $this->request->get('level', 1);
$arg_where = array('pid' => $pid, 'stat' => 0, 'siteid' => $this->siteid, 'country_code' => $this->country_code);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort', 'isshow', 'recommend', 'picture');
$category_list = model('download_category')->getList($arg_where, $arg_order, $arg_field, 50);
$value = ['list' => $category_list, 'pid' => $pid, 'level' => $level + 1];
$this->assign($value);
Config::set('default_ajax_return', 'html');
$this->view->engine(['type' => 'php', 'view_suffix' => 'html', 'tpl_replace_string' => [],]);
return $this->fetch();
}
public function updatesort() {
$id = $this->request->param('id', 0);
$sort = $this->request->param('sort', 0);
$sort = intval($sort);
if ($id && $sort < 2147483647) {
$model = model('download_category')->updateRow(['id' => $id, 'sort' => $sort]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/download_category/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function togglestat() {
$id = $this->request->get('id', 0);
$flag = $this->request->get('flag', 0);
$id = intval($id);
if ($id > 0) {
$model = model('download_category')->updateRow(['id' => $id, 'stat' => !$flag]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/download_category/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 = model('download_category')->updateRow(['id' => $id, 'isshow' => $flag]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/download_category/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function togglerecommend() {
$id = $this->request->param('id', 0);
$flag = $this->request->param('flag', 0);
$id = intval($id);
if ($id > 0) {
$model = model('download_category')->updateRow(['id' => $id, 'recommend' => $flag]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/download_category/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) {
$row = model('download_category')->getRow(['cid' => $id, 'stat' => 0], ['id', 'name']);
if ($row) {
return $this->error('此节点包含内容[ID:' . $row['id'] . '名称:' . $row['name'] . '],不能进行删除');
}
$categoryModel = model('download_category');
$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'] . '],不能进行删除');
}
}
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]);
}
}
$result = $categoryModel->destroyRow($id);
if ($result) {
return $this->success(Lang::get('operation successed'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
}