Skip to content

Instantly share code, notes, and snippets.

@Palleas
Created February 2, 2010 09:33
Show Gist options
  • Save Palleas/292543 to your computer and use it in GitHub Desktop.
Save Palleas/292543 to your computer and use it in GitHub Desktop.
<?php
/**
* render_nested_set
*
* This function is a helper used to render a nested set tree
* @param Doctrine_Collection $coll (nodes contained in current branch)
* @param array $path (path to reach the selected node)
* @return void
* @author Romain Pouclet
*/
function render_nested_set(Doctrine_Collection $coll, array $path = array())
{
$lastItem = !is_null($path) && sizeof($path) > 0 ? (int)$path[sizeof($path) - 1] : null;
$output = '';
$output.= '<ul>';
foreach ($coll as $item)
{
$output.= '<li>';
$output.= sprintf('<a href="%s"><span>%s</span></a>', url_for('noe_rubric_front_show', array('slug' => $item->getSlug())), $item->getLabel());
if ($item->getNode()->hasChildren() && (in_array($item->getId(), $path) || (int)$item->getId() === $lastItem))
{
$output.= render_nested_set($item->getNode()->getChildren(), $path);
}
$output.= '</li>';
}
$output .= '</ul>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment