PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/www/app/FrontModule/components/Menu.php

https://github.com/bazo/Mokuji
PHP | 87 lines | 81 code | 6 blank | 0 comment | 20 complexity | 6947b487f71557a4ba3e29cb1f7a63fc MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. class Menu extends Control
  3. {
  4. protected $model, $translator, $navigation;
  5. public function __construct()
  6. {
  7. $this->model = new Front_MenuModel();
  8. $this->navigation = new NavigationBuilder();
  9. return $this;
  10. }
  11. public function setTranslator(ITranslator $translator)
  12. {
  13. $this->translator = $translator;
  14. return $this;
  15. }
  16. public function render($menu_name)
  17. {
  18. $this->navigation->items = array();
  19. $this->navigation->template->setTranslator($this->translator);
  20. $menu = $this->model->getByName($menu_name);
  21. if($menu->template != '') $this->navigation->setTemplate(APP_DIR.$this->getPresenter()->pathToTheme.'/templates/Menus/menu-'.$menu->template.'.phtml');
  22. if($menu != false)
  23. {
  24. $items = $this->getItems($menu->id);
  25. $this->fillNavigation($items);
  26. $this->navigation->render();
  27. }
  28. }
  29. private function getItems($menu_id, $parent = 0)
  30. {
  31. return $this->model->getMenuItems($menu_id);
  32. }
  33. private function fillNavigation($items)
  34. {
  35. foreach($items as $key => $item)
  36. {
  37. $lang = $this->getPresenter()->lang;
  38. $parts = explode('/', $item->url);
  39. if($item->level == 1)
  40. {
  41. if($item->link_type == 'internal')
  42. {
  43. if($item->url == 'homepagelink')
  44. $this->navigation->add($item->title, $this->getPresenter()
  45. ->link(':Front:HomePage:homepage'));
  46. elseif ( count($parts) == 1 )
  47. $this->navigation->add($item->title, $this->getPresenter()
  48. ->link(':Front:page:categoryView', array('category' => $item->url, 'lang' => $lang)));
  49. elseif ( count($parts) == 2 )
  50. $this->navigation->add($item->title, $this->getPresenter()
  51. ->link(':Front:page:pageView', array('category' => $parts[0], 'page' => $parts[1], 'lang' => $lang)));
  52. }
  53. else $this->navigation->add($item->title, $item->url);
  54. }
  55. else
  56. {
  57. try
  58. {
  59. if($item->link_type == 'internal')
  60. {
  61. if($item->url == 'homepagelink')
  62. $this->navigation->getR($item->parent)->add($item->title, $this->getPresenter()
  63. ->link(':Front:HomePage:homepage'));
  64. elseif ( count($parts) == 1 )
  65. $this->navigation->getR($item->parent)->add($item->title, $this->getPresenter()
  66. ->link(':Front:page:categoryView', array('category' => $item->url, 'lang' => $lang)));
  67. elseif ( count($parts) == 2 )
  68. $this->navigation->getR($item->parent)->add($item->title, $this->getPresenter()
  69. ->link(':Front:page:pageView', array('category' => $parts[0], 'page' => $parts[1], 'lang' => $lang)));
  70. }
  71. else $this->navigation->getR($item->parent)->add($item->title, $item->url);
  72. }
  73. catch(Exception $e)
  74. {
  75. echo $e->getMessage();
  76. }
  77. }
  78. }
  79. }
  80. }
  81. ?>