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

141 lines
4.8 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use think\Lang;
use think\Loader;
use think\Config;
class QuestionCategory extends BaseController {
public function lists() {
$where['stat'] = 0;
$where['country_code'] = $this->country_code;
$question_catelist = model('question_category')->where($where)->select();
$this->assign('question_catelist', $question_catelist);
return $this->fetch();
}
public function add() {
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',];
//验证提示信息
$validatemsg = ['name.require' => '标题需要填写'];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$data['country_code'] = $this->country_code;
$data['stat'] = 0;
$data['create_time'] = time();
$model = model('question_category')->insertGetId($data);
if ($model) {
return $this->redirect(url('/admin/question_category/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function edit($id = 0) {
$id = intval($id);
if ($id > 0) {
$question_category = model('question_category')->getRow($id);
if (empty($question_category)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['question_category'] = $question_category;
} else {
return $this->error(Lang::get('incorrect operation'));
}
$value['id'] = $id;
$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',];
//验证提示信息
$validatemsg = ['name.require' => '名称需要填写',];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$model = model('question_category')->updateRow($data);
if ($model && $model->getData('id')) {
return $this->redirect(url('/admin/question_category/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
} else {
return $this->error(Lang::get('incorrect operation'));
}
}
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 = model('question_category')->updateRow(['id' => $id, 'sort' => $sort]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/question_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) {
$result = model('question_category')->deleteRow($id);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/question_category/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function deletes() {
$ids = $this->request->post('ids');
if ($this->request->isPost() && $ids) {
$result = model('question_category')->deleteRows($ids);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/question_category/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
}