Компонент

Многоуровневое меню BITRIX

19.02.2013 Bitrix, php скрипты No comments ,

result_modifier.php

if (!function_exists("getChild")){
  function getChild ( $arInput = array(), $parentIndex = 0 ){
    if (!isSet($arInput[$parentIndex])) {
      return $arInput;
    } else {
      if ($arInput[$parentIndex]["IS_PARENT"]){
        $parentDepth = $arInput[$parentIndex]["DEPTH_LEVEL"];
        $index = $parentIndex+1;
        $child = array();
        while (isset($arInput[$index]) && $arInput[$index]["DEPTH_LEVEL"] > $parentDepth){
          $child[] = $arInput[$index];
          unSet($arInput[$index]);
          $index++;
        }
        unSet($index);
        $child = getChild($child);
        $arInput[$parentIndex]["CHILD"] = $child;
        unSet($child);
      }
      $result = array();
      foreach($arInput as $index=>$item){
        $result[] = $item;
      }
      $arInput = $result;
      unSet($result);
      return getChild($arInput, $parentIndex+1);
    }
  }
}

$arResult = getChild($arResult);

template.php

<?if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?>

<?
$generateMenu = function ($arResult = array(), $depth = 1, $maxDepth = 4, $parentTitle = "") use (&$generateMenu){
    if ($depth > $maxDepth || $depth < 1) return false;
    if ($depth == 1){?>
    <ul class="header-navigation-list">
      <?
      foreach ($arResult as $index=>$arItem){
        if ($arItem["DEPTH_LEVEL"] == 1){
          $addClass = "";
          if (!isSet($arResult[$index-1])) {
            $addClass .= " first";
          } elseif (!isSet($arResult[$index+1])){
            $addClass .= " last";
          };
          if ($arItem["SELECTED"]){
            $addClass .= " selected";
          };
          ?>
          <li class="menu-inner<?=$addClass?>"><a href="<?=$arItem["LINK"]?>"><?=$arItem["TEXT"]?></a>
            <?if (isSet($arItem["CHILD"]) && ($depth+1<=$maxDepth))
                $generateMenu($arItem["CHILD"], $depth+1, $maxDepth, $arItem["TEXT"]);?>
          </li>
        <?
        }
      }?>
    </ul>
    <?
    } elseif ($depth == 2) {?>
      <div class="sub-menu-box">
        <div class="sub-menu-bottom-bg">
          <div class="sub-menu-inner">
            <?if (strpos($arResult[0]["LINK"], "services/")) {
              foreach ($arResult as $index=>$arItem){?>
                <div class="sub-menu-list-title">
                  <?=$arItem["TEXT"]?>
                </div>
                <?if (isSet($arItem["CHILD"]) && ($depth+1<=$maxDepth))
                  $generateMenu($arItem["CHILD"], $depth+1, $maxDepth, $arItem["TEXT"]);
              }
            } else {?>
              <div class="sub-menu-list-title"><?=$parentTitle;?></div>
                  <ul class="sub-menu-list">
                    <?foreach ($arResult as $index=>$arItem):?>
                      <li><a href="<?=$arItem["LINK"]?>"><?=$arItem["TEXT"]?></a>
                      <?if (isSet($arItem["CHILD"]) && ($depth+1<=$maxDepth))
                        $generateMenu($arItem["CHILD"], $depth+1, $maxDepth, $arItem["TEXT"]);?>
                      </li>
                    <?endforeach;?>
                  </ul>
            <?
            }
            ?>
          </div>
        </div>
      </div><?
    } else {?>
      <ul class="sub-menu-list">
        <?foreach ($arResult as $index=>$arItem):?>
          <li><a href="<?=$arItem["LINK"]?>"><?=$arItem["TEXT"]?></a>
          <?if (isSet($arItem["CHILD"]) && ($depth+1<=$maxDepth))
            $generateMenu($arItem["CHILD"], $depth+1, $maxDepth);?>
          </li>
        <?endforeach;?>
      </ul><?
    }
  }
?>

<div class="header-navigation-box">
<?if (!empty($arResult))
  $generateMenu($arResult, 1, $arParams["MAX_LEVEL"]);?>
</div>

Вызов компонента

25.03.2012 Bitrix No comments ,

Пример вызова компонента

<?
$APPLICATION->IncludeComponent("bitrixoid:bitrixoid.bitrixoid", ".default", array(),	false);
?>