1 <?php 2 3 /** 4 * Menu(菜单管理) 5 */ 6 namespace AdminController; 7 use CommonControllerAdminbaseController; 8 class MenuController extends AdminbaseController { 9 10 protected $menu_model; 11 protected $auth_rule_model; 12 13 function _initialize() { 14 parent::_initialize(); 15 $this->menu_model = D("Common/Menu"); 16 $this->auth_rule_model = D("Common/AuthRule"); 17 } 18 19 /** 20 * 显示菜单 21 */ 22 public function index() { 23 $_SESSION['admin_menu_index']="Menu/index"; 24 $result = $this->menu_model->order(array("listorder" => "ASC"))->select(); 25 import("Tree"); 26 $tree = new Tree(); 27 $tree->icon = array(' │ ', ' ├─ ', ' └─ '); 28 $tree->nbsp = ' '; 29 30 $newmenus=array(); 31 foreach ($result as $m){ 32 $newmenus[$m['id']]=$m; 33 34 } 35 foreach ($result as $n=> $r) { 36 37 $result[$n]['level'] = $this->_get_level($r['id'], $newmenus); 38 $result[$n]['parentid_node'] = ($r['parentid']) ? ' class="child-of-node-' . $r['parentid'] . '"' : ''; 39 40 $result[$n]['str_manage'] = '<a href="' . U("Menu/add", array("parentid" => $r['id'], "menuid" => $_GET['menuid'])) . '">添加子菜单</a> | <a target="_blank" href="' . U("Menu/edit", array("id" => $r['id'], "menuid" => $_GET['menuid'])) . '">修改</a> | <a class="J_ajax_del" href="' . U("Menu/delete", array("id" => $r['id'], "menuid" => I("get.menuid")) ). '">删除</a> '; 41 $result[$n]['status'] = $r['status'] ? "显示" : "隐藏"; 42 if(APP_DEBUG){ 43 $result[$n]['app']=$r['app']."/".$r['model']."/".$r['action']; 44 } 45 } 46 47 $tree->init($result); 48 $str = "<tr id='node-$id' $parentid_node> 49 <td style='padding-left:20px;'><input name='listorders[$id]' type='text' size='3' value='$listorder' class='input input-order'></td> 50 <td>$id</td> 51 <td>$app</td> 52 <td>$spacer$name</td> 53 <td>$status</td> 54 <td>$str_manage</td> 55 </tr>"; 56 $categorys = $tree->get_tree(0, $str); 57 $this->assign("categorys", $categorys); 58 $this->display(); 59 } 60 61 /** 62 * 获取菜单深度 63 * @param $id 64 * @param $array 65 * @param $i 66 */ 67 protected function _get_level($id, $array = array(), $i = 0) { 68 69 if ($array[$id]['parentid']==0 || empty($array[$array[$id]['parentid']]) || $array[$id]['parentid']==$id){ 70 return $i; 71 }else{ 72 $i++; 73 return $this->_get_level($array[$id]['parentid'],$array,$i); 74 } 75 76 } 77 78 public function lists(){ 79 $_SESSION['admin_menu_index']="Menu/lists"; 80 $result = $this->menu_model->order(array("app" => "ASC","model" => "ASC","action" => "ASC"))->select(); 81 $this->assign("menus",$result); 82 $this->display(); 83 } 84 85 /** 86 * 添加 87 */ 88 public function add() { 89 import("Tree"); 90 $tree = new Tree(); 91 $parentid = intval(I("get.parentid")); 92 $result = $this->menu_model->order(array("listorder" => "ASC"))->select(); 93 foreach ($result as $r) { 94 $r['selected'] = $r['id'] == $parentid ? 'selected' : ''; 95 $array[] = $r; 96 } 97 $str = "<option value='$id' $selected>$spacer $name</option>"; 98 $tree->init($array); 99 $select_categorys = $tree->get_tree(0, $str); 100 $this->assign("select_categorys", $select_categorys); 101 $this->display(); 102 } 103 104 /** 105 * 添加 106 */ 107 public function add_post() { 108 if (IS_POST) { 109 if ($this->menu_model->create()) { 110 if ($this->menu_model->add()!==false) { 111 $app=I("post.app"); 112 $model=I("post.model"); 113 $action=I("post.action"); 114 $name=strtolower("$app/$model/$action"); 115 $menu_name=I("post.name"); 116 $mwhere=array("name"=>$name); 117 118 $find_rule=$this->auth_rule_model->where($mwhere)->find(); 119 if(!$find_rule){ 120 $this->auth_rule_model->add(array("name"=>$name,"module"=>$app,"type"=>"admin_url","title"=>$menu_name));//type 1-admin rule;2-user rule 121 } 122 $to=empty($_SESSION['admin_menu_index'])?"Menu/index":$_SESSION['admin_menu_index']; 123 $this->success("添加成功!", U($to)); 124 } else { 125 $this->error("添加失败!"); 126 } 127 } else { 128 $this->error($this->menu_model->getError()); 129 } 130 } 131 } 132 133 /** 134 * 删除 135 */ 136 public function delete() { 137 $id = intval(I("get.id")); 138 $count = $this->menu_model->where(array("parentid" => $id))->count(); 139 if ($count > 0) { 140 $this->error("该菜单下还有子菜单,无法删除!"); 141 } 142 if ($this->menu_model->delete($id)!==false) { 143 $this->success("删除菜单成功!"); 144 } else { 145 $this->error("删除失败!"); 146 } 147 } 148 149 /** 150 * 编辑 151 */ 152 public function edit() { 153 import("Tree"); 154 $tree = new Tree(); 155 $id = intval(I("get.id")); 156 $rs = $this->menu_model->where(array("id" => $id))->find(); 157 $result = $this->menu_model->order(array("listorder" => "ASC"))->select(); 158 foreach ($result as $r) { 159 $r['selected'] = $r['id'] == $rs['parentid'] ? 'selected' : ''; 160 $array[] = $r; 161 } 162 $str = "<option value='$id' $selected>$spacer $name</option>"; 163 $tree->init($array); 164 $select_categorys = $tree->get_tree(0, $str); 165 $this->assign("data", $rs); 166 $this->assign("select_categorys", $select_categorys); 167 $this->display(); 168 } 169 170 /** 171 * 编辑 172 */ 173 public function edit_post() { 174 if (IS_POST) { 175 if ($this->menu_model->create()) { 176 if ($this->menu_model->save() !== false) { 177 $app=I("post.app"); 178 $model=I("post.model"); 179 $action=I("post.action"); 180 $name=strtolower("$app/$model/$action"); 181 $menu_name=I("post.name"); 182 $mwhere=array("name"=>$name); 183 184 $find_rule=$this->auth_rule_model->where($mwhere)->find(); 185 if(!$find_rule){ 186 $this->auth_rule_model->add(array("name"=>$name,"module"=>$app,"type"=>"admin_url","title"=>$menu_name));//type 1-admin rule;2-user rule 187 }else{ 188 $this->auth_rule_model->where($mwhere)->save(array("name"=>$name,"module"=>$app,"type"=>"admin_url","title"=>$menu_name));//type 1-admin rule;2-user rule 189 } 190 191 $this->success("更新成功!"); 192 } else { 193 $this->error("更新失败!"); 194 } 195 } else { 196 $this->error($this->menu_model->getError()); 197 } 198 } 199 } 200 201 //排序 202 public function listorders() { 203 $status = parent::_listorders($this->menu_model); 204 if ($status) { 205 $this->success("排序更新成功!"); 206 } else { 207 $this->error("排序更新失败!"); 208 } 209 } 210 211 public function spmy_export_menu(){ 212 $menus=$this->menu_model->get_menu_tree(0); 213 214 $menus_str= var_export($menus,true); 215 $menus_str=preg_replace("/s+d+s=>s( | )/", " ", $menus_str); 216 217 foreach ($menus as $m){ 218 $app=$m['app']; 219 $menudir=SPAPP.$app."/Menu"; 220 if(!file_exists($menudir)){ 221 mkdir($menudir); 222 } 223 $model=strtolower($m['model']); 224 225 $menus_str= var_export($m,true); 226 $menus_str=preg_replace("/s+d+s=>s( | )/", " ", $menus_str); 227 228 file_put_contents($menudir."/admin_$model.php", "<?php return $menus_str;"); 229 230 } 231 $this->display("export_menu"); 232 } 233 234 /* public function dev_import_menu(){ 235 $menus=F("Menu"); 236 if(!empty($menus)){ 237 $table_menu=C('DB_PREFIX')."menu"; 238 $this->menu_model->execute("TRUNCATE TABLE $table_menu;"); 239 240 foreach($menus as $menu){ 241 $this->menu_model->add($menu); 242 } 243 } 244 245 $this->display(); 246 } */ 247 248 private function _import_menu($menus,$parentid=0,&$error_menus=array()){ 249 foreach ($menus as $menu){ 250 251 $app=$menu['app']; 252 $model=$menu['model']; 253 $action=$menu['action']; 254 255 $where['app']=$app; 256 $where['model']=$model; 257 $where['action']=$action; 258 $children=isset($menu['children'])?$menu['children']:false; 259 unset($menu['children']); 260 $find_menu=$this->menu_model->where($where)->find(); 261 if($find_menu){ 262 $newmenu=array_merge($find_menu,$menu); 263 $result=$this->menu_model->save($newmenu); 264 if($result===false){ 265 $error_menus[]="$app/$model/$action"; 266 $parentid2=false; 267 }else{ 268 $parentid2=$find_menu['id']; 269 } 270 }else{ 271 $menu['parentid']=$parentid; 272 $result=$this->menu_model->add($menu); 273 if($result===false){ 274 $error_menus[]="$app/$model/$action"; 275 $parentid2=false; 276 }else{ 277 $parentid2=$result; 278 } 279 } 280 281 $name=strtolower("$app/$model/$action"); 282 $mwhere=array("name"=>$name); 283 284 $find_rule=$this->auth_rule_model->where($mwhere)->find(); 285 if(!$find_rule){ 286 $this->auth_rule_model->add(array("name"=>$name,"module"=>$app,"type"=>"admin_url","title"=>$menu['name']));//type 1-admin rule;2-user rule 287 }else{ 288 $this->auth_rule_model->where($mwhere)->save(array("module"=>$app,"type"=>"admin_url","title"=>$menu['name']));//type 1-admin rule;2-user rule 289 } 290 291 if($children && $parentid!==false){ 292 $this->_import_menu($children,$parentid2,$error_menus); 293 } 294 } 295 296 } 297 298 public function spmy_import_menu(){ 299 300 $apps=sp_scan_dir(SPAPP."*",GLOB_ONLYDIR); 301 $error_menus=array(); 302 foreach ($apps as $app){ 303 if(is_dir(SPAPP.$app)){ 304 $menudir=SPAPP.$app."/Menu"; 305 $menu_files=sp_scan_dir($menudir."/admin_*.php",null); 306 if(count($menu_files)){ 307 foreach ($menu_files as $mf){ 308 //是php文件 309 $mf_path=$menudir."/$mf"; 310 if(file_exists($mf_path)){ 311 $menudatas=include $mf_path; 312 if(is_array($menudatas) && !empty($menudatas)){ 313 $this->_import_menu(array($menudatas),0,$error_menus); 314 } 315 } 316 317 318 } 319 } 320 321 } 322 } 323 $this->assign("errormenus",$error_menus); 324 $this->display("import_menu"); 325 } 326 327 private function _import_submenu($submenus,$parentid){ 328 foreach($submenus as $sm){ 329 $data=$sm; 330 $data['parentid']=$parentid; 331 unset($data['items']); 332 $id=$this->menu_model->add($data); 333 if(!empty($sm['items'])){ 334 $this->_import_submenu($sm['items'],$id); 335 }else{ 336 return; 337 } 338 } 339 } 340 341 private function _generate_submenu(&$rootmenu,$m){ 342 $parentid=$m['id']; 343 $rm=$this->menu_model->menu($parentid); 344 unset($rootmenu['id']); 345 unset($rootmenu['parentid']); 346 if(count($rm)){ 347 348 $count=count($rm); 349 for($i=0;$i<$count;$i++){ 350 $this->_generate_submenu($rm[$i],$rm[$i]); 351 352 } 353 $rootmenu['items']=$rm; 354 355 }else{ 356 return ; 357 } 358 359 } 360 361 362 public function spmy_getactions(){ 363 $apps_r=array("Comment"); 364 $groups=C("MODULE_ALLOW_LIST"); 365 $group_count=count($groups); 366 $newmenus=array(); 367 $g=I("get.app"); 368 if(empty($g)){ 369 $g=$groups[0]; 370 } 371 372 if(in_array($g, $groups)){ 373 if(is_dir(SPAPP.$g)){ 374 if(!(strpos($g, ".") === 0)){ 375 $actiondir=SPAPP.$g."/Controller"; 376 $actions=sp_scan_dir($actiondir."/*"); 377 if(count($actions)){ 378 foreach ($actions as $mf){ 379 if(!(strpos($mf, ".") === 0)){ 380 if($g=="Admin"){ 381 $m=str_replace("Controller.class.php", "",$mf); 382 $noneed_models=array("Public","Index","Main"); 383 if(in_array($m, $noneed_models)){ 384 continue; 385 } 386 }else{ 387 if(strpos($mf,"adminController.class.php") || strpos($mf,"Admin")===0){ 388 $m=str_replace("Controller.class.php", "",$mf); 389 }else{ 390 continue; 391 } 392 } 393 $class=A($g."/".$m); 394 $adminbaseaction=new CommonControllerAdminbaseController(); 395 $base_methods=get_class_methods($adminbaseaction); 396 $methods=get_class_methods($class); 397 $methods=array_diff($methods, $base_methods); 398 399 foreach ($methods as $a){ 400 if(!(strpos($a, "_") === 0) && !(strpos($a, "spmy_") === 0)){ 401 $where['app']=$g; 402 $where['model']=$m; 403 $where['action']=$a; 404 $count=$this->menu_model->where($where)->count(); 405 if(!$count){ 406 $data['parentid']=0; 407 $data['app']=$g; 408 $data['model']=$m; 409 $data['action']=$a; 410 $data['type']="1"; 411 $data['status']="0"; 412 $data['name']="未知"; 413 $data['listorder']="0"; 414 $result=$this->menu_model->add($data); 415 if($result!==false){ 416 $newmenus[]= $g."/".$m."/".$a.""; 417 } 418 } 419 420 $name=strtolower("$g/$m/$a"); 421 $mwhere=array("name"=>$name); 422 423 $find_rule=$this->auth_rule_model->where($mwhere)->find(); 424 if(!$find_rule){ 425 $this->auth_rule_model->add(array("name"=>$name,"module"=>$g,"type"=>"admin_url","title"=>""));//type 1-admin rule;2-user rule 426 } 427 } 428 } 429 } 430 431 432 } 433 } 434 } 435 } 436 437 $index=array_search($g, $groups); 438 $nextindex=$index+1; 439 $nextindex=$nextindex>=$group_count?0:$nextindex; 440 if($nextindex){ 441 $this->assign("nextapp",$groups[$nextindex]); 442 } 443 $this->assign("app",$g); 444 } 445 446 $this->assign("newmenus",$newmenus); 447 $this->display("getactions"); 448 449 } 450 451 }