Archives.php
8.3 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
231
232
233
234
235
<?php
namespace app\index\controller\cms;
use addons\cms\model\Channel;
use addons\cms\model\Modelx;
use app\common\controller\Frontend;
use fast\Tree;
use think\Exception;
use think\Validate;
/**
* 会员中心
*/
class Archives extends Frontend
{
protected $layout = 'default';
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize();
}
/**
* 发表文章
* @return string
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function post()
{
$id = $this->request->get('id');
$archives = $id ? \app\admin\model\cms\Archives::get($id) : null;
if ($archives) {
$channel = Channel::get($archives['channel_id']);
if (!$channel) {
$this->error(__('未找到指定栏目'));
}
$model = \addons\cms\model\Modelx::get($channel['model_id']);
if (!$model) {
$this->error(__('未找到指定模型'));
}
if ($archives['user_id'] != $this->auth->id) {
$this->error("无法进行越权操作!");
}
} else {
$model = null;
$model_id = $this->request->request('model_id');
// 如果有model_id则调用指定模型
if ($model_id) {
$model = Modelx::get($model_id);
}
}
// 如果来源于提交
if ($this->request->isPost()) {
$row = $this->request->post('row/a');
$token = $this->request->post('token');
$rule = [
'title|标题' => 'require|length:3,100',
'channel_id|栏目' => 'require|integer',
'content|内容' => 'require',
'__token__' => 'token',
];
$msg = [
'title.require' => '标题不能为空',
'title.length' => '标题长度限制在3~100个字符',
'channel_id' => '栏目不能为空',
'content.require' => '内容不能为空',
];
$row['__token__'] = $token;
$validate = new Validate($rule, $msg);
$result = $validate->check($row);
if (!$result) {
$this->error($validate->getError(), null, ['token' => $this->request->token()]);
}
$row['user_id'] = $this->auth->id;
$row['status'] = 'hidden';
try {
if ($archives) {
$archives->allowField(true)->save($row);
} else {
(new \app\admin\model\cms\Archives)->allowField(true)->save($row);
}
} catch (Exception $e) {
$this->error("发生错误:" . $e->getMessage());
}
$this->success("发布成功!请等待审核!");
exit;
}
// 合并主副表
if ($archives) {
$addon = db($model['table'])->where('id', $archives['id'])->find();
if ($addon) {
$archives = array_merge($archives->toArray(), $addon);
}
}
$channel = new Channel();
if ($model) {
$channel->where('model_id', $model['id']);
}
// 读取可发布的栏目列表
$disabledIds = [];
$channelList = collection(
$channel->where('type', '<>', 'link')
->where("((type='list' AND iscontribute='1') OR type='channel')")
->order("weigh desc,id desc")
->cache(false)
->select()
)->toArray();
$channelParents = [];
foreach ($channelList as $index => $item) {
if ($item['parent_id']) {
$channelParents[] = $item['parent_id'];
}
}
foreach ($channelList as $k => $v) {
if ($v['type'] != 'list') {
$disabledIds[] = $v['id'];
}
if ($v['type'] == 'channel' && !in_array($v['id'], $channelParents)) {
unset($channelList[$k]);
}
}
$tree = Tree::instance()->init($channelList, 'parent_id');
$channelOptions = $tree->getTree(0, "<option value=@id @selected @disabled>@spacer@name</option>", $archives ? $archives['channel_id'] : '', $disabledIds);
$this->view->assign('channelOptions', $channelOptions);
$this->view->assign([
'archives' => $archives,
'channelOptions' => $channelOptions,
'categoryList' => ''
]);
$this->assignconfig('archives_id', $archives ? $archives['id'] : 0);
$modelName = $model ? $model['name'] : '文章';
$this->view->assign('title', $archives ? "修改{$modelName}" : "发布{$modelName}");
$this->view->assign('model', $model);
return $this->view->fetch();
}
/**
* 我的发布
* @return string
* @throws \think\Exception
* @throws \think\exception\DbException
*/
public function my()
{
$archives = new \addons\cms\model\Archives;
$model = null;
$model_id = $this->request->request('model_id');
// 如果有model_id则调用指定模型
if ($model_id) {
$model = Modelx::get($model_id);
if ($model) {
$archives->where('model_id', $model_id);
}
}
$config = ['query' => []];
if ($model) {
$config['query']['model_id'] = $model_id;
}
$archivesList = $archives->where('user_id', $this->auth->id)
->order('id', 'desc')
->paginate(10, null, $config);
$this->view->assign('archivesList', $archivesList);
$this->view->assign('title', '我发布的' . ($model ? $model['name'] : '文章'));
$this->view->assign('model', $model);
return $this->view->fetch();
}
/**
* 获取栏目列表
* @internal
*/
public function get_channel_fields()
{
$this->view->engine->layout(false);
$channel_id = $this->request->post('channel_id');
$archives_id = $this->request->post('archives_id');
$channel = Channel::get($channel_id, 'model');
if ($channel && $channel['type'] === 'list') {
$values = [];
if ($archives_id) {
$values = db($channel['model']['table'])->where('id', $archives_id)->find();
}
$fields = \app\admin\model\cms\Fields::where('model_id', $channel['model_id'])
->where('iscontribute', 1)
->where('status', 'normal')
->order('weigh desc,id desc')
->select();
foreach ($fields as $k => $v) {
//优先取编辑的值,再次取默认值
$v->value = isset($values[$v['name']]) ? $values[$v['name']] : (is_null($v['defaultvalue']) ? '' : $v['defaultvalue']);
$v->rule = str_replace(',', '; ', $v->rule);
if (in_array($v->type, ['checkbox', 'lists', 'images'])) {
$checked = '';
if ($v['minimum'] && $v['maximum'])
$checked = "{$v['minimum']}~{$v['maximum']}";
else if ($v['minimum'])
$checked = "{$v['minimum']}~";
else if ($v['maximum'])
$checked = "~{$v['maximum']}";
if ($checked)
$v->rule .= (';checked(' . $checked . ')');
}
if (in_array($v->type, ['checkbox', 'radio']) && stripos($v->rule, 'required') !== false) {
$v->rule = str_replace('required', 'checked', $v->rule);
}
if (in_array($v->type, ['selects'])) {
$v->extend .= (' ' . 'data-max-options="' . $v['maximum'] . '"');
}
}
$this->view->assign('fields', $fields);
$this->view->assign('values', $values);
$this->success('', null, ['html' => $this->view->fetch('fields')]);
} else {
$this->error(__('请选择栏目'));
}
$this->error(__('参数不能为空', 'ids'));
}
}