NavMenuController.php
10.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: kane <chengjin005@163.com> 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\NavMenuModel;
use cmf\controller\AdminBaseController;
use tree\Tree;
/**
* Class NavMenuController 前台菜单管理控制器
* @package app\admin\controller
*/
class NavMenuController extends AdminBaseController
{
/**
* 导航菜单
* @adminMenu(
* 'name' => '导航菜单',
* 'parent' => 'admin/Nav/index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '导航菜单',
* 'param' => ''
* )
*/
public function index()
{
$intNavId = $this->request->param("nav_id", 0, 'intval');
$navMenuModel = new NavMenuModel();
if (empty($intNavId)) {
$this->error("请指定导航!");
}
$objResult = $navMenuModel->where("nav_id", $intNavId)->order(["list_order" => "ASC"])->select();
$arrResult = $objResult ? $objResult->toArray() : [];
$tree = new Tree();
$tree->icon = [' │ ', ' ├─ ', ' └─ '];
$tree->nbsp = ' ';
$array = [];
foreach ($arrResult as $r) {
$r['str_manage'] = '<a class="btn btn-xs btn-primary" href="' . url("NavMenu/add", ["parent_id" => $r['id'], "nav_id" => $r['nav_id']]) . '">添加子菜单</a>
<a class="btn btn-xs btn-primary" href="' . url("NavMenu/edit", ["id" => $r['id'], "parent_id" => $r['parent_id'], "nav_id" => $r['nav_id']]) . '">编辑</a>
<a class="btn btn-xs btn-danger js-ajax-delete" href="' . url("NavMenu/delete", ["id" => $r['id'], 'nav_id' => $r['nav_id']]) . '">删除</a> ';
$r['status'] = $r['status'] ? "显示" : "隐藏";
$array[] = $r;
}
$tree->init($array);
$str = "<tr>
<td><input name='list_orders[\$id]' type='text' size='3' value='\$list_order' class='input input-order'></td>
<td>\$id</td>
<td >\$spacer\$name</td>
<td>\$status</td>
<td>\$str_manage</td>
</tr>";
$categories = $tree->getTree(0, $str);
$this->assign("categories", $categories);
$this->assign('nav_id', $intNavId);
return $this->fetch();
}
/**
* 添加导航菜单
* @adminMenu(
* 'name' => '添加导航菜单',
* 'parent' => 'index',
* 'display'=> false,
* 'order' => 10000,
* 'hasView'=> true,
* 'icon' => '',
* 'remark' => '添加导航菜单',
* 'param' => ''
* )
*/
public function add()
{
$navMenuModel = new NavMenuModel();
$intNavId = $this->request->param("nav_id", 0, 'intval');
$intParentId = $this->request->param("parent_id", 0, 'intval');
$objResult = $navMenuModel->where("nav_id", $intNavId)->order(["list_order" => "ASC"])->select();
$arrResult = $objResult ? $objResult->toArray() : [];
$tree = new Tree();
$tree->icon = [' │ ', ' ├─ ', ' └─ '];
$tree->nbsp = ' ';
$array = [];
foreach ($arrResult as $r) {
$r['str_manage'] = '<a href="' . url("NavMenu/add", ["parent_id" => $r['id']]) . '">添加子菜单</a> | <a href="'
. url("NavMenu/edit", ["id" => $r['id']]) . '">编辑</a> | <a class="J_ajax_del" href="'
. url("NavMenu/delete", ["id" => $r['id']]) . '">删除</a> ';
$r['status'] = $r['status'] ? "显示" : "隐藏";
$r['selected'] = $r['id'] == $intParentId ? "selected" : "";
$array[] = $r;
}
$tree->init($array);
$str = "<option value='\$id' \$selected>\$spacer\$name</option>";
$navTrees = $tree->getTree(0, $str);
$this->assign("nav_trees", $navTrees);
$navs = $navMenuModel->selectNavs();
$this->assign('navs', $navs);
$this->assign("nav_id", $intNavId);
return $this->fetch();
}
/**
* 添加导航菜单提交保存
* @adminMenu(
* 'name' => '添加导航菜单提交保存',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '添加导航菜单提交保存',
* 'param' => ''
* )
*/
public function addPost()
{
$navMenuModel = new NavMenuModel();
$arrData = $this->request->post();
if (isset($arrData['external_href'])) {
$arrData['href'] = htmlspecialchars_decode($arrData['external_href']);
} else {
$arrData['href'] = htmlspecialchars_decode($arrData['href']);
$arrData['href'] = base64_decode($arrData['href']);
}
$navMenuModel->allowField(true)->isUpdate(false)->save($arrData);
$this->success(lang("EDIT_SUCCESS"), url("NavMenu/index", ['nav_id' => $arrData['nav_id']]));
}
/**
* 编辑导航菜单
* @adminMenu(
* 'name' => '编辑导航菜单',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '编辑导航菜单',
* 'param' => ''
* )
*/
public function edit()
{
$navMenuModel = new NavMenuModel();
$intNavId = $this->request->param("nav_id", 0, 'intval');
$intId = $this->request->param("id", 0, 'intval');
$intParentId = $this->request->param("parent_id", 0, 'intval');
$objResult = $navMenuModel
->where("nav_id", $intNavId)
->where("id", "neq", $intId)
->order(["list_order" => "ASC"])
->select();
$arrResult = $objResult ? $objResult->toArray() : [];
$tree = new Tree();
$tree->icon = [' │ ', ' ├─ ', ' └─ '];
$tree->nbsp = ' ';
$array = [];
foreach ($arrResult as $r) {
$r['str_manage'] = '<a href="' . url("NavMenu/add", ["parent_id" => $r['id'], "nav_id" => $intNavId]) . '">添加子菜单</a> | <a href="'
. url("NavMenu/edit", ["id" => $r['id'], "nav_id" => $intNavId]) . '">编辑</a> | <a class="js-ajax-delete" href="'
. url("NavMenu/delete", ["id" => $r['id'], "nav_id" => $intNavId]) . '">删除</a> ';
$r['status'] = $r['status'] ? "显示" : "隐藏";
$r['selected'] = $r['id'] == $intParentId ? "selected" : "";
$array[] = $r;
}
$tree->init($array);
$str = "<option value='\$id' \$selected>\$spacer\$name</option>";
$nav_trees = $tree->getTree(0, $str);
$this->assign("nav_trees", $nav_trees);
$objNav = $navMenuModel->where("id", $intId)->find();
$arrNav = $objNav ? $objNav->toArray() : [];
$arrNav['href_old'] = $arrNav['href'];
if (strpos($arrNav['href'], "{") === 0 || $arrNav['href'] == 'home') {
$arrNav['href'] = base64_encode($arrNav['href']);
}
$this->assign($arrNav);
$navs = $navMenuModel->selectNavs();
$this->assign('navs', $navs);
$this->assign("nav_id", $intNavId);
$this->assign("parent_id", $intParentId);
return $this->fetch();
}
/**
* 编辑导航菜单提交保存
* @adminMenu(
* 'name' => '编辑导航菜单提交保存',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '编辑导航菜单提交保存',
* 'param' => ''
* )
*/
public function editPost()
{
$navMenuModel = new NavMenuModel();
$intId = $this->request->param('id', 0, 'intval');
$arrData = $this->request->post();
if (isset($arrData['external_href'])) {
$arrData['href'] = htmlspecialchars_decode($arrData['external_href']);
} else {
$arrData['href'] = htmlspecialchars_decode($arrData['href']);
$arrData['href'] = base64_decode($arrData['href']);
}
$navMenuModel->update($arrData, ["id" => $intId], true);
$this->success(lang("EDIT_SUCCESS"), url("NavMenu/index", ['nav_id' => $arrData['nav_id']]));
}
/**
* 删除导航菜单
* @adminMenu(
* 'name' => '删除导航菜单',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '删除导航菜单',
* 'param' => ''
* )
*/
public function delete()
{
$navMenuModel = new NavMenuModel();
$intId = $this->request->param("id", 0, "intval");
$intNavId = $this->request->param("nav_id", 0, "intval");
if (empty($intId)) {
$this->error(lang("NO_ID"));
}
$count = $navMenuModel->where("parent_id", $intId)->count();
if ($count > 0) {
$this->error("该菜单下还有子菜单,无法删除!");
}
$navMenuModel->where("id", $intId)->delete();
$this->success(lang("DELETE_SUCCESS"), url("NavMenu/index", ['nav_id' => $intNavId]));
}
/**
* 导航菜单排序
* @adminMenu(
* 'name' => '导航菜单排序',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '导航菜单排序',
* 'param' => ''
* )
*/
public function listOrder()
{
$navMenuModel = new NavMenuModel();
$status = parent::listOrders($navMenuModel);
if ($status) {
$this->success("排序更新成功!");
} else {
$this->error("排序更新失败!");
}
}
}