Created
May 18, 2011 21:51
-
-
Save SilverPaladin/979656 to your computer and use it in GitHub Desktop.
Croogo nestedLinks()
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
public function nestedLinks($links, $options = array(), $depth = 1) { | |
$_options = array(); | |
$options = array_merge($_options, $options); | |
$output = ''; | |
// Remove locale part before comparing links | |
if (!empty($this->params['locale'])) { | |
$currentUrl = substr($this->params['url']['url'], strlen($this->params['locale'])); | |
} else { | |
$currentUrl = $this->params['url']['url']; | |
} | |
foreach ($links AS $link) { | |
$linkAttr = array( | |
'id' => 'link-' . $link['Link']['id'], | |
'rel' => $link['Link']['rel'], | |
'target' => $link['Link']['target'], | |
'title' => $link['Link']['description'], | |
); | |
foreach ($linkAttr AS $attrKey => $attrValue) { | |
if ($attrValue == null) { | |
unset($linkAttr[$attrKey]); | |
} | |
} | |
// if link is in the format: controller:contacts/action:view | |
if (strstr($link['Link']['link'], 'controller:')) { | |
$link['Link']['link'] = $this->linkStringToArray($link['Link']['link']); | |
} | |
if (Router::url($link['Link']['link']) == Router::url('/' . $currentUrl)) { | |
$linkAttr['class'] = $options['selected']; | |
} | |
$linkOutput = $this->Html->link($link['Link']['title'], $link['Link']['link'], $linkAttr); | |
if (isset($link['children']) && count($link['children']) > 0) { | |
$linkOutput .= $this->nestedLinks($link['children'], $options, $depth + 1); | |
} | |
$linkOutput = $this->Html->tag('li', $linkOutput); | |
$output .= $linkOutput; | |
} | |
if ($output != null) { | |
$tagAttr = $options['tagAttributes']; | |
if ($options['dropdown'] && $depth == 1) { | |
$tagAttr['class'] = $options['dropdownClass']; | |
} | |
$output = $this->Html->tag($options['tag'], $output, $tagAttr); | |
} | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment