Здесь рассмотрен вариант создания меню из альбомов модуля Gallery
, но можно использовать и в других случаях, убрав лишние конструкции и заменив ссылки в элементах.
<?php
//Set options for sideGalleri
$galleryId = $modx->getOption('galleryId', $scriptProperties, 5);
$itemClass = $modx->getOption('itemClass', $scriptProperties, '');
$wrapperClass = $modx->getOption('wrapperClass', $scriptProperties, '');
$dropdownWrapperClass = $modx->getOption('dropdownWrapperClass', $scriptProperties, '');
$arrConfig = array(
'galleryId' => $galleryId,
'itemClass' => $itemClass,
'wrapperClass' => $wrapperClass,
'dropdownWrapperClass' => $dropdownWrapperClass
);
if(!function_exists('view_album')) {
function view_album($modx, $arrConfig, $data, $iteration) {
$arr = "";
$iteration = isset($iteration) ? $iteration : 0;
++$iteration;
foreach ($data as $menu) {
if(!empty($menu['childs'])) {
$arr .= '<li class="' . $arrConfig['itemClass'] . ' ' . $menu['galleri-active'] . '"><a href="' . $modx->makeUrl($arrConfig['galleryId']) . '?galAlbum=' . $menu['id'] . '">' . $menu['name'] . '</a>';
$arr .= view_album($modx, $arrConfig, $menu['childs'], $iteration) . '</li>';
}else{
$arr .= '<li class="' . $arrConfig['itemClass'] . ' ' . $menu['galleri-active'] . '"><a href="' . $modx->makeUrl($arrConfig['galleryId']) . '?galAlbum=' . $menu['id'] . '">' . $menu['name'] . '</a></li>';
}
}
if($iteration > 1) {
return '<ul class="' . $arrConfig['dropdownWrapperClass'] . ' ' . $active . '">' . $arr . '</ul>';
}else{
return '<ul class="' . $arrConfig['wrapperClass'] . ' ' . $active . '">' . $arr . '</ul>';
}
}
}
if(!function_exists("map_tree")) {
function map_tree($row) {
$tree = array();
$currAlbum = $_GET['galAlbum'];
foreach ($row as $k => &$v) {
if (!$v['parent']) {
$tree[$k] = &$v;
}else{
$row[$v['parent']]['childs'][$k] = &$v;
}
}
return $tree;
}
}
if(!function_exists("checkActive")) {
function checkActive(&$row, $currAlbum) {
$row[$currAlbum]['galleri-active'] = 'active';
if($row[$currAlbum]['parent']) {
checkActive($row, $row[$currAlbum]['parent']);
}
}
}
$stmt = $modx->query("SELECT * FROM modx_gallery_albums");
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
$data = array();
foreach($row as $k => $v) {
$data[$v['id']] = $v;
}
$currAlbum = $_GET['galAlbum'];
checkActive($data, $currAlbum);
$allMenu = map_tree($data);
return view_album($modx, $arrConfig, $allMenu, $iteration);
/*echo "<pre>";
print_r($allMenu);*/