fork download
  1. <?php
  2.  
  3. /**
  4.  * get the top-navi for the bootstrap-framework (HTML)
  5.  *
  6.  * @param $naviArray
  7.  * @param $tmpArrayChildOf
  8.  *
  9.  * @return string
  10.  */
  11. function getHTMLNaviBootstrap($naviArray, $tmpArrayChildOf)
  12. {
  13. $naviString = '';
  14. $depth = 0;
  15. foreach ($naviArray as $index => $item) {
  16.  
  17. $hasDropDown = false;
  18. $newDepth = (int)$item['level'];
  19.  
  20. // is the navi-element "active"?
  21. if (in_array($item['id'], $tmpArrayChildOf, true)) {
  22. $tmpClass = ' active ';
  23. } else {
  24. $tmpClass = '';
  25. }
  26.  
  27. // add template-class
  28. $tmpClass .= ' menu_' . $item['template'] . ' ' . ' menu_pageid_' . $item['id'] . ' ';
  29.  
  30. // check for sub-"ul"
  31. foreach ($naviArray as $item_tmp) {
  32. if ($item['id'] == $item_tmp['root_id']) {
  33. $tmpClass .= ' dropdown ';
  34. $hasDropDown = true;
  35. break;
  36. }
  37. }
  38.  
  39. if ($newDepth > $depth) {
  40.  
  41. while ($newDepth > $depth) {
  42.  
  43. if ($depth > 0) {
  44. $ul_extra = 'mainnavSub dropdown-menu';
  45. $li_extra = '';
  46. } else {
  47. $ul_extra = 'nav navbar-nav';
  48. $li_extra = 'mainnav';
  49. }
  50.  
  51. $naviString .= '
  52. <ul class="' . $ul_extra . '">
  53. <li class="' . $li_extra . ' ' . $tmpClass . '">';
  54. $depth++;
  55. }
  56.  
  57. } else if ($newDepth < $depth) {
  58.  
  59. while ($newDepth < $depth) {
  60. $naviString .= '
  61. </li>
  62. </ul>';
  63. $depth--;
  64. }
  65. $naviString .= '
  66. </li>
  67. <li class="mainnav ' . $tmpClass . '">';
  68.  
  69. } else if ($newDepth === $depth) {
  70.  
  71. if ($index === 0) {
  72. $naviString .= '
  73. <ul>
  74. <li class="mainnav ' . $tmpClass . '">';
  75. } else if ($newDepth == 1) {
  76. $naviString .= '
  77. </li>
  78. <li class="mainnav ' . $tmpClass . '">';
  79. } else {
  80. $naviString .= '
  81. </li>
  82. <li class="' . $tmpClass . '">';
  83. }
  84.  
  85. }
  86.  
  87. if ($hasDropDown === true) {
  88. $extrasForLink = 'class="dropdown-toggle" data-toggle="dropdown"';
  89. $extraIconForLink = '<b class="caret"></b>';
  90. } else {
  91. $extrasForLink = '';
  92. $extraIconForLink = '';
  93. }
  94.  
  95. $naviString .= '<a ' . $extrasForLink . ' href="' . $item['url'] . '">' . $item['name'] . ' ' . $extraIconForLink . '</a>';
  96. $depth = $newDepth;
  97. }
  98.  
  99. if (count($naviArray) > 0) {
  100. do {
  101. $naviString .= '
  102. </li>
  103. </ul>';
  104. $depth--;
  105. }
  106. while ($depth > 0);
  107. }
  108.  
  109. return $naviString;
  110. }
  111.  
  112. $naviArray = array (
  113. 'id' => 1,
  114. 'root_id' => 0,
  115. 'priority' => 0,
  116. 'title' => 'story-title',
  117. 'name' => 'Home',
  118. 'level' => 1,
  119. 'offline' => 0,
  120. 'template' => 'index',
  121. 'url' => 'http://f...content-available-to-author-only...r.com/index1'
  122. ),
  123. 'id' => 13,
  124. 'root_id' => 1,
  125. 'priority' => 0,
  126. 'title' => 'story-title',
  127. 'name' => 'Sub_Home',
  128. 'level' => 2,
  129. 'offline' => 0,
  130. 'template' => 'sub_index',
  131. 'url' => 'http://f...content-available-to-author-only...r.com/sub_index13',
  132. ),
  133. 'id' => 5,
  134. 'root_id' => 0,
  135. 'priority' => 0,
  136. 'title' => 'story-title',
  137. 'name' => 'Sub_Home',
  138. 'level' => 1,
  139. 'offline' => 0,
  140. 'template' => 'blog',
  141. 'url' => 'http://f...content-available-to-author-only...r.com/blog5'
  142. )
  143. );
  144.  
  145. echo getHTMLNaviBootstrap($naviArray, array());
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
        <ul class="nav navbar-nav">
          <li class="mainnav  menu_index  menu_pageid_1  dropdown "><a class="dropdown-toggle" data-toggle="dropdown" href="http://f...content-available-to-author-only...r.com/index1">Home <b class="caret"></b></a>
        <ul class="mainnavSub dropdown-menu">
          <li class="  menu_sub_index  menu_pageid_13 "><a  href="http://f...content-available-to-author-only...r.com/sub_index13">Sub_Home </a>
          </li>
        </ul>
      </li>
      <li class="mainnav  menu_blog  menu_pageid_5 "><a  href="http://f...content-available-to-author-only...r.com/blog5">Sub_Home </a>
        </li>
      </ul>