198 lines
7.0 KiB
PHP
Executable File
198 lines
7.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use think\Lang;
|
|
use think\Loader;
|
|
use think\Config;
|
|
|
|
class Question extends BaseController {
|
|
|
|
public function index() {
|
|
$this->redirect('/admin/question/lists');
|
|
}
|
|
|
|
public function lists() {
|
|
$data = $this->request->get();
|
|
//echo "<pre>=="; print_r($data); die;
|
|
$skeyword = $this->request->param('skeyword');
|
|
$search['skeyword'] = '';
|
|
if (!empty($skeyword))
|
|
{
|
|
$where['a.title|b.name'] = ['like', "%$skeyword%"];
|
|
$search['skeyword'] = $skeyword;
|
|
}
|
|
|
|
$where['a.stat'] = 0;
|
|
$where['a.country_code'] = $this->country_code;
|
|
$order = ['a.sort' => 'asc', 'a.id' => 'desc'];
|
|
$field = ['a.*', 'b.name' => 'cate_name'];
|
|
|
|
$list = model('question')->alias('a')->join('question_category b', 'a.cid = b.id', 'LEFT')->where($where)->field($field)->order($order)->paginate(12);
|
|
// echo model('question')->getLastSql();die;
|
|
// $dataObject = model('question')->getPageList($arg_where, $arg_order);
|
|
$value = [
|
|
'list' => $list->isEmpty() ? null : $list->items(),
|
|
'page' => $list->render(),
|
|
'search' => $search,
|
|
];
|
|
$this->assign($value);
|
|
return $this->fetch();
|
|
}
|
|
|
|
public function add() {
|
|
$question_catelist = model('question_category')->select();
|
|
|
|
$this->assign('question_catelist', $question_catelist);
|
|
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 = ['title' => 'require',];
|
|
//验证提示信息
|
|
$validatemsg = ['title.require' => '标题需要填写'];
|
|
$valid_result = $this->validate($data, $validaterule, $validatemsg);
|
|
if (true !== $valid_result) {
|
|
// 验证失败 输出错误信息
|
|
return $this->error($valid_result);
|
|
}
|
|
|
|
$data['country_code'] = $this->country_code;
|
|
$model = model('question')->insertRow($data);
|
|
if ($model && $model->getData('id')) {
|
|
return $this->redirect(url('/admin/question/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 = model('question')->getRow($id);
|
|
if (empty($question)) {
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
$value['question'] = $question;
|
|
} else {
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
|
|
$question_catelist = model('question_category')->select();
|
|
|
|
$value['question_catelist'] = $question_catelist;
|
|
$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 = ['title' => 'require',];
|
|
//验证提示信息
|
|
$validatemsg = ['title.require' => '名称需要填写',];
|
|
$valid_result = $this->validate($data, $validaterule, $validatemsg);
|
|
if (true !== $valid_result) {
|
|
// 验证失败 输出错误信息
|
|
return $this->error($valid_result);
|
|
}
|
|
$model = model('question')->updateRow($data);
|
|
if ($model && $model->getData('id')) {
|
|
return $this->redirect(url('/admin/question/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')->updateRow(['id' => $id, 'sort' => $sort]);
|
|
if ($model && $model->getData('id')) {
|
|
return $this->success(Lang::get('operation successed'), url('/admin/question/lists'));
|
|
} else {
|
|
return $this->error(Lang::get('operation failed'));
|
|
}
|
|
}
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
|
|
public function toggleheadline() {
|
|
$id = $this->request->param('id', 0);
|
|
$flag = $this->request->param('flag', 0);
|
|
$id = intval($id);
|
|
if ($id > 0) {
|
|
$model = model('question')->updateRow(['id' => $id, 'headline' => $flag]);
|
|
if ($model && $model->getData('id')) {
|
|
return $this->success(Lang::get('operation successed'), url('/admin/question/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('question')->updateRow(['id' => $id, 'recommend' => $flag]);
|
|
if ($model && $model->getData('id')) {
|
|
return $this->success(Lang::get('operation successed'), url('/admin/question/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')->deleteRow($id);
|
|
if ($result) {
|
|
return $this->success(Lang::get('operation successed'), url('/admin/question/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')->deleteRows($ids);
|
|
if ($result) {
|
|
return $this->success(Lang::get('operation successed'), url('/admin/question/lists'));
|
|
} else {
|
|
return $this->error(Lang::get('operation failed'));
|
|
}
|
|
}
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
|
|
}
|