Menu.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\admin;
  10. use fec\helpers\CUrl;
  11. use fecshop\services\Service;
  12. use Yii;
  13. /**
  14. * Page Menu services.
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class Menu extends Service
  19. {
  20. /**
  21. * @var array 后台菜单配置, 参看@fecshop/config/services/Page.php的配置
  22. */
  23. public $menuConfig;
  24. /**
  25. * @return Array , 得到后台菜单配置。
  26. */
  27. public function getConfigMenu(){
  28. $menu = $this->menuConfig;
  29. return $menu;
  30. }
  31. public function getLeftMenuHtml(){
  32. $menuArr = $this->getConfigMenu();
  33. return $this->getLeftMenuTreeHtml($menuArr);
  34. }
  35. public function getRoleUrlKey(){
  36. return Yii::$service->admin->role->getCurrentRoleResources();
  37. }
  38. # 得到后台显示菜单(左侧)
  39. public function getLeftMenuTreeHtml($treeArr='', $i=1){
  40. $str = '';
  41. foreach($treeArr as $node){
  42. // 二次开发的过程中,如果fecshop后台的某些菜单想不显示,那么可以在配置中将active设置成false
  43. if (isset($node['active']) && $node['active'] === false) {
  44. continue;
  45. }
  46. $name = Yii::$service->page->translate->__($node["label"]);
  47. $url_key = $node["url_key"];
  48. $roleUrlKeys = $this->getRoleUrlKey();
  49. if($url_key && (!isset($roleUrlKeys[$url_key]) || !$roleUrlKeys[$url_key])){
  50. continue;
  51. }
  52. if($i == 1){
  53. $str .= '<div class="accordionHeader">
  54. <h2><span>Folder</span>'.$name .'
  55. <span class="first_collapsable"></span>
  56. </h2>
  57. </div>
  58. <div class="accordionContent">';
  59. if($this->hasChild($node)){
  60. $str .='<ul class="tree treeFolder">';
  61. $str .= $this->getLeftMenuTreeHtml($node['child'],$i+1);
  62. $str .='</ul>';
  63. }
  64. $str .= '</div>';
  65. }else{
  66. if($this->hasChild($node)){
  67. //$str .= '<li><a href="'.CUrl::getUrl($url_key).'" target="navTab" rel="page1">'.$name.'</a>';
  68. $str .= '<li><a href="javascript:void(0)" >'.$name.'</a>';
  69. $str .= '<ul>';
  70. $str .= $this->getLeftMenuTreeHtml($node['child'],$i+1);
  71. $str .= '</ul>';
  72. $str .= '</li>';
  73. }else{
  74. $str .='<li><a href="'.CUrl::getUrl($url_key).'" target="navTab" rel="page1">'.$name.'</a></li>';
  75. }
  76. }
  77. }
  78. return $str;
  79. }
  80. public function hasChild($node){
  81. if(isset($node['child']) && !empty($node['child'])){
  82. return true;
  83. }
  84. return false;
  85. }
  86. }