Instantly share code, notes, and snippets.
Created
May 22, 2012 12:06
-
Save hirbod/8e775b3cbb1038e84624 to your computer and use it in GitHub Desktop.
extended redaxo navigation factory
This file contains 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 | |
class advanced_rex_navigation extends rex_navigation { | |
var $navigation_type; | |
var $seperator; | |
var $seperator_class; | |
// überschreibe methode | |
function _getNavigation($category_id, $ignore_offlines){ | |
static $depth = 0; | |
if($category_id < 1){ | |
$nav_obj = OOCategory::getRootCategories($ignore_offlines); | |
}else{ | |
$nav_obj = OOCategory::getChildrenById($category_id, $ignore_offlines); | |
} | |
$return = ""; | |
if(count($nav_obj)>0){ | |
$return .= '<ul class="rex-navi'. ($depth+1) .'[count_elements]">'; | |
} | |
$counter = 0; | |
foreach($nav_obj as $nav){ | |
if($nav->getValue('cat_navigationstyp') != $this->navigation_type){ | |
continue; | |
} | |
$counter++; | |
$liClass = ''; | |
$linkClass = ''; | |
// classes abhaengig vom pfad | |
if($nav->getId() == $this->current_category_id){ | |
$liClass .= ' rex-current'; | |
$linkClass .= ' rex-current'; | |
}elseif(in_array($nav->getId(),$this->path)){ | |
$liClass .= ' rex-active'; | |
$linkClass .= ' rex-active'; | |
}else{ | |
$liClass .= ' rex-normal'; | |
} | |
$liClass .= ' nav_'.$counter; | |
// classes abhaengig vom level | |
if(isset($this->classes[$depth])){ | |
$liClass .= ' '. $this->classes[$depth]; | |
} | |
if(isset($this->linkclasses[$depth])){ | |
$linkClass .= ' '. $this->linkclasses[$depth]; | |
} | |
$liClass = $liClass == '' ? '' : ' class="'. ltrim($liClass) .'"'; | |
$linkClass = $linkClass == ' class="level'.($depth+1).'"' ? '' : ' class="'. ltrim($linkClass) .' level'.($depth+1).'"'; | |
$linkUrl = $this->_getUrl($nav); | |
$return .= '<li '. $liClass .'>'; | |
$return .= '<a'. $linkClass .' href="'.$linkUrl.'">'.htmlspecialchars($nav->getName()).'</a>'; | |
$depth++; | |
if(($this->open || $nav->getId() == $this->current_category_id || in_array($nav->getId(),$this->path)) && ($this->depth > $depth || $this->depth < 0)){ | |
$return .= $this->_getNavigation($nav->getId(),$ignore_offlines); | |
} | |
$depth--; | |
$return .= '</li>'; | |
} | |
if(count($nav_obj)>0){ | |
$return .= '</ul>'; | |
} | |
if($this->seperator != '' && $depth == 0){ | |
$return = str_replace('</li><li','</li><li'.(trim($this->seperator_class) != '' ? ' class="'.$this->seperator_class.'"' : '').'>'.$this->seperator.'</li><li',$return); | |
} | |
$return = str_replace('[count_elements]', ' count_'.$counter, $return); | |
return $return; | |
} | |
// überschreibe methode | |
function get($category_id = 0,$depth = 3,$open = FALSE, $ignore_offlines = FALSE, $navigation_type = 'main'){ | |
if(!$this->_setActivePath()){ | |
return ''; | |
} | |
$this->depth = $depth; | |
$this->open = $open; | |
$this->ignore_offlines = $ignore_offlines; | |
$this->navigation_type = $navigation_type; | |
return $this->_getNavigation($category_id,$this->ignore_offlines); | |
} | |
// gibt erste Kategorie mit Inhalt zurück | |
function _getUrl($category){ | |
$url = $category->getUrl(); | |
$slice = OOArticleSlice::getFirstSliceForArticle($category->getId()); | |
if(!is_object($slice)){ | |
$children = $category->getChildren(true); | |
if(sizeof($children) > 0){ | |
$url = $this->_getUrl($children[0]); | |
} | |
} | |
return $url; | |
} | |
// Trennzeichen zwischen Navigationspunkten setzen | |
function setSeperator($seperator = '|', $seperator_class = 'pipe'){ | |
$this->seperator = $seperator; | |
$this->seperator_class = $seperator_class; | |
} | |
} | |
?> |
This file contains 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 | |
$nav = new advanced_rex_navigation(); | |
$main_navigation = $nav->get(0,3,FALSE,TRUE,'main'); | |
// 'main' is the name of the cat_navigationstyp | |
// change to any value you want | |
/* weitere setter methoden: | |
$nav->setClasses(array('menu_item')); | |
$nav->setSeperator('/'); | |
*/ | |
/* setSeperator() fügt nach jedem Menüpunkt ein echtes <li class="seperator">WERT</li> hinzu */ | |
echo $main_navigation; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment