Enterform.php
8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace app\school\controller\setting;
use app\common\controller\DoubleBase;
use app\school\model\setting\ProjectContainer;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
class Enterform extends DoubleBase
{
protected $containerId = null;
public function _initialize()
{
parent::_initialize();
$this->containerId = $_COOKIE['container_id'];
}
public function index() {
if ($this->request->isAjax()) {
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$formField = ['id', 'school_id', 'project_type_id', 'stage_title', 'stage_type', 'icon_image'];
$total = Db::name('project_form_set')->where($where)->count();
$data = Db::name('project_form_set')
->field($formField)
->where($where)
->limit($offset, $limit)
->select();
$res = [
'total' => $total,
'rows' => $data
];
return $res;
}
$projectTypesWhere = ['container_id' => $this->containerId, 'deletetime' => null];
$projectTypes = Db::name('project_type')->where($projectTypesWhere)->select();
// 项目申报开启到哪个阶段了(0:未开启;1:立项;2:中期;3:结题)
$applyStatus = (new ProjectContainer())->where(['id' => $this->containerId])->value('apply_status_log');
$container = (new ProjectContainer())->where(['id' => $this->containerId])->find();
if (!$projectTypes) {
$this->error('当前容器未设置项目类型,请先设置');
}
$this->assign('project_types', $projectTypes);
$this->assign('can_edit', $applyStatus == 0);
$this->assignconfig('apply_status', $applyStatus);
$this->assign('container', $container);
return $this->fetch();
}
public function edit($ids = NULL) {
if ($this->request->isAjax()) {
$rows = $this->request->param('row/a');
$containerInfo = ProjectContainer::get($this->containerId);
$formInfo = $this->getFormSetData($rows['id']);
switch ($formInfo['stage_type'])
{
case 'meta_check':
$containerStartTime = $containerInfo['midterm_review_starttime'];
$containerEndTime = $containerInfo['midterm_review_endtime'];
$messgae = '中期检查';
break;
case 'conclusion':
$containerStartTime = $containerInfo['conclusion_starttime'];
$containerEndTime = $containerInfo['conclusion_endtime'];
$messgae = '项目结题';
break;
default:
$containerStartTime = $containerInfo['project_starttime'];
$containerEndTime = $containerInfo['project_endtime'];
$messgae = '项目';
}
$data = [];
$data['stage_title'] = $rows['stage_title'];
unset($rows['stage_title']);
$data['can_edit_data'] = $rows['can_edit_data'];
unset($rows['can_edit_data']);
$data['project_starttime'] = empty($rows['project_starttime']) ? NULL : strtotime($rows['project_starttime']);
unset($rows['project_starttime']);
$data['project_endtime'] = empty($rows['project_endtime']) ? NULL : strtotime($rows['project_endtime']);
unset($rows['project_endtime']);
if($data['project_endtime'] && !$data['project_starttime']) {
$this->error('请选择开始时间');
}
if($data['project_starttime'] && !$data['project_endtime']) {
$this->error('请选择结束时间');
}
if($data['project_starttime'] && $data['project_endtime']) {
if($data['project_starttime'] > $data['project_endtime']) {
$this->error('开始时间不能大于结束时间');
}
if($data['project_starttime'] > $containerEndTime) {
$this->error("开始时间不能大于 {$messgae}结束时间");
}
}
$data['setting'] = json_encode($rows);
Db::startTrans();
try {
Db::name('project_form_set')->where(['id' => $ids])->update($data);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('修改成功', '');
}
$row = $this->getFormSetData($ids);
$this->assign('row', $row);
return $this->fetch();
}
/**
* 自定义表单
*/
public function customForm() {
$stageType = $this->request->param('type');
$formSetId = $this->request->param('form_set_id');
// 当前表单对应的项目阶段
$applyNum = $this->request->param('apply_num');
try {
$fieldsData = Db::name('project_form_set')->where(['id' => $formSetId])->find();
} catch (Exception $e) {
$this->error($e->getMessage());
}
if ($this->request->isAjax()) {
$data['fields_setting'] = $this->request->param('setting');
Db::startTrans();
try {
Db::name('project_form_set')->where(['id' => $formSetId])->update($data);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('修改成功');
}
// 项目是否开启过申报(0: 没有; 1: 开启过)
$applyStatus = (new ProjectContainer())->where(['id' => $this->containerId])->value('apply_status_log');
$fieldsData = json_decode($fieldsData['fields_setting'], true);
// 当前表单对应的项目阶段大于进行中的项目阶段时,表单可修改
$this->assign('can_edit', $applyNum > $applyStatus);
$this->assignconfig('form_type', $stageType);
$this->assignconfig('fields_data', $fieldsData);
$this->assignconfig('form_set_id', $formSetId);
return $this->fetch();
}
/**
* 获取项目报名设置数据
* @return Array
*/
private function getFormSetData($ids = NULL) {
$row = Db::name('project_form_set')->where(['id' => $ids])->find();
if (!$row) {
$this->error('没有找到该条记录');
}
$row['setting'] = json_decode($row['setting'], true);
return $row;
}
public function add()
{
$type_id = request()->param('type_id');
$this->assign('type_id',$type_id);
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
if($this->storeIdFieldAutoFill && STORE_ID ){
$params['store_id'] = STORE_ID;
}
if($this->shopIdAutoCondition && SCHOOL_ID){
$params['school_id'] = SCHOOL_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(true)->validate($validate);
}
$result = $this->model->allowField(true)->save($params);
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were inserted'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->fetch();
}
}