<?php

/**
 * get the top-navi for the bootstrap-framework (HTML)
 *
 * @param $naviArray
 * @param $tmpArrayChildOf
 *
 * @return string
 */
function getHTMLNaviBootstrap($naviArray, $tmpArrayChildOf)
{
  $naviString = '';
  $depth = 0;
  foreach ($naviArray as $index => $item) {

    $hasDropDown = false;
    $newDepth = (int)$item['level'];

    // is the navi-element "active"?
    if (in_array($item['id'], $tmpArrayChildOf, true)) {
      $tmpClass = ' active ';
    } else {
      $tmpClass = '';
    }

    // add template-class
    $tmpClass .= ' menu_' . $item['template'] . ' ' . ' menu_pageid_' . $item['id'] . ' ';

    // check for sub-"ul"
    foreach ($naviArray as $item_tmp) {
      if ($item['id'] == $item_tmp['root_id']) {
        $tmpClass .= ' dropdown ';
        $hasDropDown = true;
        break;
      }
    }

    if ($newDepth > $depth) {

      while ($newDepth > $depth) {

        if ($depth > 0) {
          $ul_extra = 'mainnavSub dropdown-menu';
          $li_extra = '';
        } else {
          $ul_extra = 'nav navbar-nav';
          $li_extra = 'mainnav';
        }

        $naviString .= '
        <ul class="' . $ul_extra . '">
          <li class="' . $li_extra . ' ' . $tmpClass . '">';
        $depth++;
      }

    } else if ($newDepth < $depth) {

      while ($newDepth < $depth) {
        $naviString .= '
          </li>
        </ul>';
        $depth--;
      }
      $naviString .= '
      </li>
      <li class="mainnav ' . $tmpClass . '">';

    } else if ($newDepth === $depth) {

      if ($index === 0) {
        $naviString .= '
        <ul>
          <li class="mainnav ' . $tmpClass . '">';
      } else if ($newDepth == 1) {
        $naviString .= '
        </li>
        <li class="mainnav ' . $tmpClass . '">';
      } else {
        $naviString .= '
        </li>
        <li class="' . $tmpClass . '">';
      }

    }

    if ($hasDropDown === true) {
      $extrasForLink = 'class="dropdown-toggle" data-toggle="dropdown"';
      $extraIconForLink = '<b class="caret"></b>';
    } else {
      $extrasForLink = '';
      $extraIconForLink = '';
    }

    $naviString .= '<a ' . $extrasForLink . ' href="' . $item['url'] . '">' . $item['name'] . ' ' . $extraIconForLink . '</a>';
    $depth = $newDepth;
  }

  if (count($naviArray) > 0) {
    do {
      $naviString .= '
        </li>
      </ul>';
      $depth--;
    }
    while ($depth > 0);
  }

  return $naviString;
}

$naviArray = array (
  array (
    'id' => 1,
    'root_id' => 0,
    'priority' => 0,
    'title' => 'story-title',
    'name' => 'Home',
    'level' => 1,
    'offline' => 0,
    'template' => 'index',
    'url' => 'http://f...content-available-to-author-only...r.com/index1'
  ),
  array (
    'id' => 13,
    'root_id' => 1,
    'priority' => 0,
    'title' => 'story-title',
    'name' => 'Sub_Home',
    'level' => 2,
    'offline' => 0,
    'template' => 'sub_index',
    'url' => 'http://f...content-available-to-author-only...r.com/sub_index13',
  ),
  array (
      'id' => 5,
      'root_id' => 0,
      'priority' => 0,
      'title' => 'story-title',
      'name' => 'Sub_Home',
      'level' => 1,
      'offline' => 0,
      'template' => 'blog',
      'url' => 'http://f...content-available-to-author-only...r.com/blog5'
  )
);

echo getHTMLNaviBootstrap($naviArray, array());