init commit
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\wdsxh\questionnaire;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use think\exception\DbException;
|
||||
use think\exception\PDOException;
|
||||
|
||||
/**
|
||||
* 问卷调查分类
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Category extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Category模型对象
|
||||
* @var \app\admin\model\wdsxh\questionnaire\Category
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\wdsxh\questionnaire\Category;
|
||||
$this->view->assign("statusList", $this->model->getStatusList());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param $ids
|
||||
* @return void
|
||||
* @throws DbException
|
||||
* @throws DataNotFoundException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function del($ids = null)
|
||||
{
|
||||
if (false === $this->request->isPost()) {
|
||||
$this->error(__("Invalid parameters"));
|
||||
}
|
||||
$ids = $ids ?: $this->request->post("ids");
|
||||
if (empty($ids)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
$pk = $this->model->getPk();
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds)) {
|
||||
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
||||
}
|
||||
$list = $this->model->where($pk, 'in', $ids)->select();
|
||||
|
||||
$questionnaireModel = new \app\admin\model\wdsxh\questionnaire\Questionnaire();
|
||||
$count = 0;
|
||||
Db::startTrans();
|
||||
try {
|
||||
foreach ($list as $item) {
|
||||
|
||||
if ($questionnaireModel->where('questionnaire_category_id',$item->id)->count()) {
|
||||
$this->error("分类【{$item->name}】下存在问卷,无法删除");
|
||||
}
|
||||
$count += $item->delete();
|
||||
}
|
||||
Db::commit();
|
||||
} catch (PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($count) {
|
||||
$this->success();
|
||||
}
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 麦沃德科技赋能开发者,助力商协会发展
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2024 www.wdsxh.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | 沃德商协会系统并不是自由软件,不加密,并不代表开源,未经许可不可自由转售和商用
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: MY WORLD Team <bd@maiwd.cn> www.maiwd.cn
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\admin\controller\wdsxh\questionnaire;
|
||||
use EasyWeChat\Factory;
|
||||
use Exception;
|
||||
use app\admin\model\wdsxh\member\Member;
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
use think\exception\DbException;
|
||||
use think\exception\PDOException;
|
||||
use think\exception\ValidateException;
|
||||
|
||||
/**
|
||||
* 问卷调查
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Questionnaire extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Questionnaire模型对象
|
||||
* @var \app\admin\model\wdsxh\questionnaire\Questionnaire
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\wdsxh\questionnaire\Questionnaire;
|
||||
$this->view->assign("statusList", $this->model->getStatusList());
|
||||
$current_date = date('Y-m-d',time());
|
||||
$memberObj = (new Member())->where('expire_time','>=',$current_date)->field('id,name')->order('id desc')->select();
|
||||
$memberIdArray = array_map(function ($item) {
|
||||
return $item->toArray();
|
||||
}, $memberObj);
|
||||
$memberdata = [0 => ['id' => '-1', 'name' => '平台']];
|
||||
foreach ($memberIdArray as $k => $v) {
|
||||
$memberdata[$v['id']] = $v;
|
||||
}
|
||||
$this->view->assign("memberList", $memberdata);
|
||||
$this->view->assign("nonMemberAnswerSheetStatusList", $this->model->getNonMemberAnswerSheetStatusList());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*
|
||||
* @return string
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if (false === $this->request->isPost()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
$params = $this->request->post('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
|
||||
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
||||
$params[$this->dataLimitField] = $this->auth->id;
|
||||
}
|
||||
$result = false;
|
||||
Db::startTrans();
|
||||
try {
|
||||
//是否采用模型验证
|
||||
if ($this->modelValidate) {
|
||||
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
|
||||
$this->model->validateFailException()->validate($validate);
|
||||
}
|
||||
if ($params['member_id'] == -1){
|
||||
$params['wechat_id'] = -1;
|
||||
}else{
|
||||
$params['wechat_id'] = (new Member())->where('id',$params['member_id'])->value('wechat_id');
|
||||
}
|
||||
$result = $this->model->allowField(true)->save($params);
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result === false) {
|
||||
$this->error(__('No rows were inserted'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*
|
||||
* @param $ids
|
||||
* @return string
|
||||
* @throws DbException
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$row = $this->model->get($ids);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
if (false === $this->request->isPost()) {
|
||||
$show_applet_questionnaire_qrcode = 2;
|
||||
$applet_questionnaire_qrcode_path = '';
|
||||
$save_path = '/uploads/wdsxh/applet_questionnaire_qrcode/'.$row['id'].'/'.$row['createtime'].'.png';
|
||||
if (is_file(ROOT_PATH."public".$save_path)) {
|
||||
$applet_questionnaire_qrcode_path = $this->request->domain().$save_path;
|
||||
$show_applet_questionnaire_qrcode = 1;
|
||||
} else {
|
||||
$configObj = (new \app\admin\model\wdsxh\Config())->where('id',1)->find();
|
||||
if (!empty($configObj['applet_appid']) && !empty($configObj['applet_secret'])) {
|
||||
$path = 'pagesTools/questionnaire/details';
|
||||
$config = [
|
||||
'app_id' => $configObj['applet_appid'],
|
||||
'secret' => $configObj['applet_secret'],
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
],
|
||||
];
|
||||
|
||||
$app = Factory::miniProgram($config);
|
||||
|
||||
$response = $app->app_code->getUnlimit($ids, [
|
||||
'page' => $path,
|
||||
]);
|
||||
|
||||
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
|
||||
$response->saveAs('uploads/wdsxh/applet_questionnaire_qrcode/'.$row['id'], $row['createtime'].'.png');
|
||||
$applet_questionnaire_qrcode_path = $this->request->domain().$save_path;
|
||||
$show_applet_questionnaire_qrcode = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->view->assign('show_applet_questionnaire_qrcode', $show_applet_questionnaire_qrcode);
|
||||
$this->view->assign('applet_questionnaire_qrcode_path', $applet_questionnaire_qrcode_path);
|
||||
$this->view->assign('row', $row);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
$params = $this->request->post('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
$result = false;
|
||||
Db::startTrans();
|
||||
try {
|
||||
//是否采用模型验证
|
||||
if ($this->modelValidate) {
|
||||
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
|
||||
$row->validateFailException()->validate($validate);
|
||||
}
|
||||
if ($params['member_id'] == -1){
|
||||
$params['wechat_id'] = -1;
|
||||
}else{
|
||||
$params['wechat_id'] = (new Member())->where('id',$params['member_id'])->value('wechat_id');
|
||||
}
|
||||
$result = $row->allowField(true)->save($params);
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if (false === $result) {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* Desc 分享图片
|
||||
* Create on 2024/4/8 16:08
|
||||
* Create by @小趴菜
|
||||
*/
|
||||
public function config(){
|
||||
$row = (new \app\admin\model\wdsxh\Config())->get(1);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
if (false === $this->request->isPost()) {
|
||||
$this->view->assign('row', $row);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
$params = $this->request->post('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
$result = false;
|
||||
Db::startTrans();
|
||||
try {
|
||||
//是否采用模型验证
|
||||
if ($this->modelValidate) {
|
||||
$name = str_replace("\\model\\", "\\validate\\", get_class($this->modelConfig));
|
||||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
|
||||
$row->validateFailException()->validate($validate);
|
||||
}
|
||||
$result = $row->allowField(true)->save($params);
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if (false === $result) {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
231
application/admin/controller/wdsxh/questionnaire/Render.php
Normal file
231
application/admin/controller/wdsxh/questionnaire/Render.php
Normal file
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 麦沃德科技赋能开发者,助力商协会发展
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2024 www.wdsxh.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | 沃德商协会系统并不是自由软件,不加密,并不代表开源,未经许可不可自由转售和商用
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: MY WORLD Team <bd@maiwd.cn> www.maiwd.cn
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\admin\controller\wdsxh\questionnaire;
|
||||
|
||||
use app\admin\model\wdsxh\user\Wechat;
|
||||
use app\common\controller\Backend;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use think\exception\DbException;
|
||||
use think\response\Json;
|
||||
|
||||
/**
|
||||
* 问卷提交
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Render extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Render模型对象
|
||||
* @var \app\admin\model\wdsxh\questionnaire\Render
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\wdsxh\questionnaire\Render;
|
||||
$questionnaire_id = input('questionnaire_id', 0, 'intval');
|
||||
$this->view->assign('questionnaire_id',$questionnaire_id);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*
|
||||
* @return string|Json
|
||||
* @throws \think\Exception
|
||||
* @throws DbException
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$questionnaire_id = input('questionnaire_id', 0, 'intval');
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
if (false === $this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
//如果发送的来源是 Selectpage,则转发到 Selectpage
|
||||
if ($this->request->request('keyField')) {
|
||||
return $this->selectpage();
|
||||
}
|
||||
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
|
||||
$list = $this->model
|
||||
->where('questionnaire_id',$questionnaire_id)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
foreach ($list as $item){
|
||||
$item->wechat_id = (new Wechat())->where('id',$item['wechat_id'])->value('nickname');
|
||||
}
|
||||
|
||||
$result = ['total' => $list->total(), 'rows' => $list->items()];
|
||||
return json($result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
|
||||
public function details($ids = null){
|
||||
$row = $this->model->get($ids);
|
||||
$data = html_entity_decode($row->content_render);
|
||||
$formData = json_decode($data, true);
|
||||
foreach ($formData as $k=>$v){
|
||||
if ($v['type'] == 'images'){
|
||||
if ($v['content']){
|
||||
if (strpos($v['content'], ',') !== false) {
|
||||
$img = explode(',', $v['content']);
|
||||
} else {
|
||||
$img = array($v['content']);
|
||||
}
|
||||
$images = [];
|
||||
foreach ($img as $item){
|
||||
$images[] = wdsxh_full_url($item);
|
||||
}
|
||||
$formData[$k]['content'] = $images;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
$this->view->assign("content", $formData);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Desc 问卷提交导出
|
||||
* Create on 2024/3/22 13:36
|
||||
* Create by @小趴菜
|
||||
*/
|
||||
public function export($ids = ""){
|
||||
|
||||
if ($this->request->isPost()) {
|
||||
$questionnaire_id = input('questionnaire_id', 0, 'intval');
|
||||
set_time_limit(0);
|
||||
ini_set('memory_limit', '2048M');
|
||||
$search = $this->request->post('search');
|
||||
$ids = $ids ? $ids : $this->request->post("ids");
|
||||
$pk = $this->model->getPk();
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds)) {
|
||||
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
||||
}
|
||||
$op = $this->request->post('op');
|
||||
|
||||
$this->request->get(['search' => $search, 'ids' => $ids,'op' => $op]);
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
if ($ids == 'all'){
|
||||
$sql = $this->model
|
||||
->where($where)
|
||||
->where('questionnaire_id',$questionnaire_id)
|
||||
->field('content_render')
|
||||
->select();
|
||||
}else{
|
||||
$sql = $this->model
|
||||
->where($pk, 'in', $ids)
|
||||
->where($where)
|
||||
->where('questionnaire_id',$questionnaire_id)
|
||||
->field('content_render')
|
||||
->select();
|
||||
}
|
||||
$sql = collection($sql)->toArray();
|
||||
if (!$sql){
|
||||
$this->error('暂无数据导出');
|
||||
}
|
||||
foreach ($sql as $k=>$v){
|
||||
if ($v['content_render']){
|
||||
$data = html_entity_decode($v['content_render']);
|
||||
$sql[$k]['content_render'] = json_decode($data,true);
|
||||
}
|
||||
}
|
||||
$newExcel = new Spreadsheet(); //创建一个新的excel文档
|
||||
$objSheet = $newExcel->getActiveSheet(); //获取当前操作sheet的对象
|
||||
$questionnaire_name = (new \app\api\model\wdsxh\questionnaire\Questionnaire())->where('id',$questionnaire_id)->value('title');
|
||||
$objSheet->setTitle($questionnaire_name); //设置当前sheet的标题
|
||||
$topic = (new \app\admin\model\wdsxh\questionnaire\Topic())->where('questionnaire_id', $questionnaire_id)->order('weigh asc')->select();
|
||||
$topic_data = collection($topic)->toArray();
|
||||
$labels = range('A', 'Z'); // 生成 A 到 Z 的数组作为列标的数组
|
||||
|
||||
// 设置列宽度
|
||||
for ($i = 0; $i < count($labels); $i++) {
|
||||
$column = $labels[$i];
|
||||
$newExcel->getActiveSheet()->getColumnDimension($column)->setWidth(20);
|
||||
}
|
||||
|
||||
// 设置每一行的标签(label)
|
||||
for ($i = 0; $i < count($topic_data); $i++) {
|
||||
$label = isset($topic_data[$i]['topic']) ? $topic_data[$i]['topic'] : '';
|
||||
$objSheet->setCellValue($labels[$i] . '1', $label);
|
||||
}
|
||||
// 外部循环遍历 $sql 数组
|
||||
foreach ($sql as $rowIndex => $row) {
|
||||
// 内部循环遍历 content 数组,设置每一行的值
|
||||
for ($j = 0; $j < count($row['content_render']); $j++) {
|
||||
// 查询提交的数据是否开始说明
|
||||
if ($row['content_render'][$j]['is_explain'] == '1') {
|
||||
$value = $row['content_render'][$j]['content'] . '。' . '说明:' . $row['content_render'][$j]['explain'];
|
||||
} else {
|
||||
$value = isset($row['content_render'][$j]['content']) ? $row['content_render'][$j]['content'] : '';
|
||||
}
|
||||
|
||||
// 循环图片,拼接完整的链接
|
||||
if ($row['content_render'][$j]['type'] == 'images') {
|
||||
$array = explode(',', $row['content_render'][$j]['content']);
|
||||
foreach ($array as $k=>$v) {
|
||||
$array[$k] = wdsxh_full_url($v);
|
||||
}
|
||||
$value = implode(',',$array);
|
||||
}
|
||||
$objSheet->setCellValue($labels[$j] . ($rowIndex + 2), $value);
|
||||
}
|
||||
}
|
||||
/*--------------下面是设置其他信息------------------*/
|
||||
$title = date("Ymd-问卷调查");
|
||||
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||||
header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');
|
||||
header('Cache-Control: max-age=0');
|
||||
$objWriter = IOFactory::createWriter($newExcel, 'Xlsx');
|
||||
$objWriter->save('php://output');
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function multi($ids = null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public function del($ids = null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public function edit($ids = null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public function add()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
185
application/admin/controller/wdsxh/questionnaire/Topic.php
Normal file
185
application/admin/controller/wdsxh/questionnaire/Topic.php
Normal file
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 麦沃德科技赋能开发者,助力商协会发展
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2024 www.wdsxh.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | 沃德商协会系统并不是自由软件,不加密,并不代表开源,未经许可不可自由转售和商用
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: MY WORLD Team <bd@maiwd.cn> www.maiwd.cn
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\admin\controller\wdsxh\questionnaire;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
use think\exception\DbException;
|
||||
use think\exception\PDOException;
|
||||
use think\exception\ValidateException;
|
||||
use think\response\Json;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* 问卷题目管理
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Topic extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Topic模型对象
|
||||
* @var \app\admin\model\wdsxh\questionnaire\Topic
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\wdsxh\questionnaire\Topic;
|
||||
$this->view->assign("typeList", $this->model->getTypeList());
|
||||
$this->view->assign("isExplainList", $this->model->getIsExplainList());
|
||||
$this->view->assign("mustList", $this->model->getMustList());
|
||||
$this->view->assign("statusList", $this->model->getStatusList());
|
||||
// 获取自定义按钮传递的questionnaire_id
|
||||
$questionnaire_id = input('questionnaire_id', 0, 'intval');
|
||||
// 将questionnaire_id传递给模板
|
||||
$this->assignconfig('questionnaire_id', $questionnaire_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*
|
||||
* @return string|Json
|
||||
* @throws \think\Exception
|
||||
* @throws DbException
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// 获取自定义按钮传递的questionnaire_id
|
||||
$questionnaire_id = input('questionnaire_id', 0, 'intval');
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
if (false === $this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
//如果发送的来源是 Selectpage,则转发到 Selectpage
|
||||
if ($this->request->request('keyField')) {
|
||||
return $this->selectpage();
|
||||
}
|
||||
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
|
||||
$list = $this->model
|
||||
->where('questionnaire_id',$questionnaire_id)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
$result = ['total' => $list->total(), 'rows' => $list->items()];
|
||||
return json($result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*
|
||||
* @return string
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if (false === $this->request->isPost()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
// 从 URL 中获取 questionnaire_id 参数
|
||||
$questionnaire_id = $this->request->get('questionnaire_id');
|
||||
$params = $this->request->post('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
|
||||
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
||||
$params[$this->dataLimitField] = $this->auth->id;
|
||||
}
|
||||
$result = false;
|
||||
Db::startTrans();
|
||||
try {
|
||||
//是否采用模型验证
|
||||
if ($this->modelValidate) {
|
||||
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
|
||||
$this->model->validateFailException()->validate($validate);
|
||||
}
|
||||
$params['questionnaire_id'] = $questionnaire_id;
|
||||
$result = $this->model->allowField(true)->save($params);
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result === false) {
|
||||
$this->error(__('No rows were inserted'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*
|
||||
* @param $ids
|
||||
* @return string
|
||||
* @throws DbException
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$row = $this->model->get($ids);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
if (false === $this->request->isPost()) {
|
||||
$this->view->assign('row', $row);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
$params = $this->request->post('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
$result = false;
|
||||
Db::startTrans();
|
||||
try {
|
||||
//是否采用模型验证
|
||||
if ($this->modelValidate) {
|
||||
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
|
||||
$row->validateFailException()->validate($validate);
|
||||
}
|
||||
$result = $row->allowField(true)->save($params);
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if (false === $result) {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user