Channel.php
9.4 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace app\admin\controller\cms;
use app\admin\model\Admin;
use app\admin\model\AuthGroupAccess;
use app\admin\model\cms\ChannelAdmin;
use app\common\controller\Backend;
use app\admin\model\cms\Channel as ChannelModel;
use fast\Tree;
use think\Exception;
/**
* 栏目表
*
* @icon fa fa-circle-o
*/
class Channel extends Backend
{
protected $channelList = [];
protected $modelList = [];
protected $multiFields = ['weigh', 'status'];
/**
* Channel模型对象
*/
protected $model = null;
protected $noNeedRight = ['check_element_available'];
public function _initialize()
{
parent::_initialize();
$this->request->filter(['strip_tags']);
$this->model = new \app\admin\model\cms\Channel;
$tree = Tree::instance();
$tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'parent_id');
$this->channelList = $tree->getTreeList($tree->getTreeArray(0), 'name');
$this->modelList = \app\admin\model\cms\Modelx::order('id asc')->select();
$this->view->assign("modelList", $this->modelList);
$this->view->assign("channelList", $this->channelList);
$this->view->assign("typeList", ChannelModel::getTypeList());
$this->view->assign("statusList", ChannelModel::getStatusList());
}
/**
* 查看
*/
public function index()
{
if ($this->request->isAjax()) {
$search = $this->request->request("search");
$model_id = $this->request->request("model_id");
//构造父类select列表选项数据
$list = [];
if ($search) {
foreach ($this->channelList as $k => $v) {
if (stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
$list[] = $v;
}
}
} else {
$list = $this->channelList;
}
foreach ($list as $index => $item) {
if ($model_id && $model_id != $item['model_id']) {
unset($list[$index]);
}
}
$list = array_values($list);
$modelNameArr = [];
foreach ($this->modelList as $k => $v) {
$modelNameArr[$v['id']] = $v['name'];
}
foreach ($list as $k => &$v) {
$v['model_name'] = $v['model_id'] && isset($modelNameArr[$v['model_id']]) ? $modelNameArr[$v['model_id']] : __('None');
}
$total = count($list);
$result = array("total" => $total, "rows" => $list);
return json($result);
}
return $this->view->fetch();
}
/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = basename(str_replace('\\', '/', get_class($this->model)));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
$this->model->validate($validate);
}
$nameArr = array_filter(explode("\n", str_replace("\r\n", "\n", $params['name'])));
if (count($nameArr) > 1) {
foreach ($nameArr as $index => $item) {
$itemArr = array_filter(explode('|', $item));
$params['name'] = $itemArr[0];
$params['diyname'] = isset($itemArr[1]) ? $itemArr[1] : '';
$result = $this->model->allowField(true)->isUpdate(false)->data($params)->save();
}
} else {
$result = $this->model->allowField(true)->save($params);
}
if ($result !== false) {
$this->success();
} else {
$this->error($this->model->getError());
}
} catch (\think\exception\PDOException $e) {
$this->error($e->getMessage());
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}
/**
* 栏目授权
*/
public function admin()
{
$act = $this->request->param('act');
$ids = $this->request->param('ids');
if ($act == 'remove') {
ChannelAdmin::where('admin_id', $ids)->delete();
$this->success('删除成功!');
} else if ($act == 'authorization') {
$selected = ChannelAdmin::getAdminChanneIds($ids);
$all = collection(ChannelModel::order("weigh desc,id desc")->select())->toArray();
foreach ($all as $k => $v) {
$state = ['opened' => true];
if ($v['type'] != 'list') {
$disabledIds[] = $v['id'];
}
if ($v['type'] == 'link') {
$state['checkbox_disabled'] = true;
}
$state['selected'] = in_array($v['id'], $selected);
$channelList[] = [
'id' => $v['id'],
'parent' => $v['parent_id'] ? $v['parent_id'] : '#',
'text' => __($v['name']),
'type' => $v['type'],
'state' => $state
];
}
$this->success('成功', '', $channelList);
} else if ($act == 'save') {
\think\Db::startTrans();
try {
ChannelAdmin::where('admin_id', $ids)->delete();
$channelIds = explode(",", $this->request->post("ids"));
if ($channelIds) {
$listChannelIds = ChannelModel::where('type', 'list')->column('id');
$channelIds = array_intersect($channelIds, $listChannelIds);
$data = [];
foreach ($channelIds as $key => $item) {
$data[] = ['admin_id' => $ids, 'channel_id' => $item];
}
$model = new ChannelAdmin();
$model->saveAll($data, true);
}
\think\Db::commit();
} catch (Exception $e) {
\think\Db::rollback();
$this->error($e->getMessage());
}
$this->success("保存成功!");
}
if ($this->request->isAjax()) {
$list = \think\Db::name("cms_channel_admin")
->group("admin_id")
->field("COUNT(*) as channels,admin_id")
->select();
$adminChannelList = [];
foreach ($list as $index => $item) {
$adminChannelList[$item['admin_id']] = $item['channels'];
}
$superAdminIds = AuthGroupAccess::where('group_id', 1)->column('uid');
$adminList = Admin::order('id', 'desc')->field('id,username')->select();
foreach ($adminList as $index => $item) {
$item->channels = isset($adminChannelList[$item['id']]) ? $adminChannelList[$item['id']] : 0;
$item->superadmin = in_array($item['id'], $superAdminIds);
}
$total = count($adminList);
$result = array("total" => $total, "rows" => $adminList);
return json($result);
}
return $this->view->fetch();
}
/**
* Selectpage搜索
*
* @internal
*/
public function selectpage()
{
return parent::selectpage();
}
/**
* 检测元素是否可用
* @internal
*/
public function check_element_available()
{
$id = $this->request->request('id');
$name = $this->request->request('name');
$value = $this->request->request('value');
$name = substr($name, 4, -1);
if (!$name) {
$this->error(__('Parameter %s can not be empty', 'name'));
}
if ($name == 'diyname') {
if ($id) {
$this->model->where('id', '<>', $id);
}
$exist = $this->model->where($name, $value)->find();
if ($exist) {
$this->error(__('The data already exist'));
} else {
$this->success();
}
} else if ($name == 'name') {
$nameArr = array_filter(explode("\n", str_replace("\r\n", "\n", $value)));
if (count($nameArr) > 1) {
foreach ($nameArr as $index => $item) {
$itemArr = array_filter(explode('|', $item));
if (!isset($itemArr[1])) {
$this->error('格式:分类名称|自定义名称');
}
$exist = \app\admin\model\cms\Channel::getByDiyname($itemArr[1]);
if ($exist) {
$this->error('自定义名称[' . $itemArr[1] . ']已经存在');
}
}
$this->success();
} else {
$this->success();
}
}
}
}