Last active
July 30, 2020 14:07
-
-
Save skerbis/591cca8c4f7201263beb96d4c2ac6cb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Function zur Generierung eines Navigationsarrays | |
// Benötigt für den Aufruf werden nur $start,$depth und $ignoreoffline | |
// Alle weiteren Angaben dienen der internen Verarbeitung | |
// Alle weiteren Informationen aus rex_structure fintet man in catObject | |
// DEMO siehe unten | |
if (!function_exists('structureArray')) { | |
function structureArray($start = 0, $depth = 0, $ignoreoffline = true, $depth_saved = 0, $level = 0, $id = 0) | |
{ | |
$result = array(); | |
if ($start != 0) { | |
$startCat = rex_category::get($start); | |
$startPath = $startCat->getPathAsArray(); | |
$depth = count($startPath) + $depth; | |
$startCats = $startCat->getChildren($ignoreoffline); | |
if ($depth_saved != 0) { | |
$depth = $depth_saved; | |
} else { | |
$depth_saved = $depth; | |
} | |
} else { | |
$startCats = rex_category::getRootCategories(true); | |
$depth = $depth; | |
} | |
if ($startCats) { | |
foreach ($startCats as $cat) { | |
$children['child'] = array(); | |
$hasChildren = false; | |
$catId = $cat->getId(); | |
$path = $cat->getPathAsArray(); | |
$listlevel = count($path); | |
if ($listlevel > $depth) { | |
continue; | |
} | |
if ($listlevel <= $depth && $depth != 0 && $cat && $cat->getChildren($ignoreoffline)) { | |
$level++; | |
$hasChildren = true; | |
// Unterkategorien ermitteln, function ruft sich selbst auf | |
$children['child'] = structureArray($catId, $depth, $ignoreoffline = true, $depth_saved, $level); | |
$level--; | |
} | |
// Name der Kategorie | |
$catName = $cat->getName(); | |
// Url anhand ID ermitteln | |
$catUrl = rex_getUrl($catId); | |
// Aktiven Pfad ermitteln | |
$active = false; | |
$currentCat = rex_category::getCurrent(); | |
// verhindern, dass es bei Root-Kategorien knallt | |
if ($currentCat) | |
{ | |
$currentCatpath = $currentCat->getPathAsArray(); | |
$currentCat_id = $currentCat->getId(); | |
} | |
else | |
{ | |
$currentCatpath = array(); | |
$currentCat_id = 0; | |
} | |
if (in_array($catId, $currentCatpath) or $currentCat_id == $catId) { | |
$active = true; | |
} | |
// Ergebnis speichern | |
$result[$id++] = array( | |
'catId' => $catId, | |
'parentId' => $start, | |
'level' => $level, | |
'active' => $active, | |
'catName' => $catName, | |
'url' => $catUrl, | |
'hasChildren' => $hasChildren, | |
'children' => $children['child'], | |
'path' => $path, | |
'catObject' => $cat | |
); | |
} | |
} | |
return $result; | |
} | |
} | |
// --- | |
// DEMO :: Eigene Funktion zur Verarbeitung des Arrays | |
// --- | |
function myNavi($data = array()) | |
{ | |
foreach ($data as $cat) { | |
$subnavi =''; | |
// hat die aktulle Kategorie Subkategorien? | |
if ($cat['hasChildren'] == true) { | |
$sub = []; | |
$sub[] = '<ul>'; | |
// die Funktion noch mal aufrufen und Kinder auslesen | |
$sub[] = myNavi($cat['children']); | |
$sub[] = '</ul>'; | |
$subnavi = join("\n", $sub); | |
} | |
$catname = $cat['catName']; | |
// Aktivstatus der Kategorie ermitteln, die Information ist bereits im Array | |
if ($cat['active'] == true) { | |
$catname = '<strong>' . $catname . '</strong>'; | |
} | |
$output[] = '<li><a href="' . $cat['url'] . '">' . $catname . '</a>' . $subnavi . '</li>'; | |
} | |
return join("\n", $output); | |
} | |
// Erstellen des Arrays | |
$daten = structureArray($start = 0, $depth = 1, $ignoreoffline = true); | |
// Aufruf der eigenen Function | |
// die Funktion liefert nicht das umschließende ul, dieses muss manuell gesetzt werden. | |
echo "\n<ul>\n" . myNavi($daten) . "\n</ul>\n"; | |
// ---- | |
// ENDE DEMO | |
// ---- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment