Menu.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\page;
  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. * whether display HOME in the menu.
  22. */
  23. public $displayHome;
  24. /**
  25. * custom menu that display in the front of product category.
  26. */
  27. public $frontCustomMenu;
  28. /**
  29. * custom menu that display in the behind of product category.
  30. */
  31. public $behindCustomMenu;
  32. protected $_homeUrl;
  33. /**
  34. * return menu array data, contains:
  35. * home,frontCustomMenu,productCategory,behindCustomMenu.
  36. * 得到网站的分类导航栏菜单。
  37. */
  38. protected function actionGetMenuData()
  39. {
  40. $this->_homeUrl = CUrl::getHomeUrl();
  41. $arr = [];
  42. if ($displayHome = $this->displayHome) {
  43. $enable = isset($displayHome['enable']) ? $displayHome['enable'] : '';
  44. $display = isset($displayHome['display']) ? $displayHome['display'] : '';
  45. if ($enable && $display) {
  46. $arr[] = [
  47. 'name' => Yii::$service->page->translate->__($display),
  48. 'url' => Yii::$service->url->homeUrl(),
  49. ];
  50. }
  51. }
  52. $first_custom_menu = $this->customMenuInit($this->frontCustomMenu);
  53. if (is_array($first_custom_menu) && !empty($first_custom_menu)) {
  54. foreach ($first_custom_menu as $m) {
  55. $arr[] = $m;
  56. }
  57. }
  58. $categoryMenuArr = $this->getProductCategoryMenu();
  59. //var_dump($categoryMenuArr);
  60. if (is_array($categoryMenuArr) && !empty($categoryMenuArr)) {
  61. foreach ($categoryMenuArr as $a) {
  62. $arr[] = $a;
  63. }
  64. }
  65. $behind_custom_menu = $this->customMenuInit($this->behindCustomMenu);
  66. if (is_array($behind_custom_menu) && !empty($behind_custom_menu)) {
  67. foreach ($behind_custom_menu as $m) {
  68. $arr[] = $m;
  69. }
  70. }
  71. return $arr;
  72. }
  73. /**
  74. * @param $customMenu | Array , 自定义菜单部分数组,从配置中取出
  75. * @return Array,获取处理后的自定义菜单数组
  76. */
  77. protected function customMenuInit($customMenu)
  78. {
  79. $cMenu = [];
  80. if (is_array($customMenu) && !empty($customMenu)) {
  81. foreach ($customMenu as $k=>$menu) {
  82. $name = Yii::$service->page->translate->__($menu['name']);
  83. $menu['name'] = $name;
  84. $urlPath = $menu['urlPath'];
  85. $menu['url'] = Yii::$service->url->getUrl($urlPath);
  86. $cMenu[$k] = $menu;
  87. if (isset($menu['childMenu'])) {
  88. $cMenu[$k]['childMenu'] = $this->customMenuInit($menu['childMenu']);
  89. }
  90. }
  91. }
  92. return $cMenu;
  93. }
  94. /**
  95. * get product category array as menu.
  96. */
  97. protected function getProductCategoryMenu()
  98. {
  99. return Yii::$service->category->menu->getCategoryMenuArr();
  100. }
  101. }