<?php
/**
 * A class for rendering html breadcrumb menus.
 */
class view_widget_crumb
{
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.

    /**
     * Renders an html breadcrumb menu.
     *
	 * @param string/array $url The breadcrumb url(s).
	 * @param string (optional) $base The base url.
	 * @param array (optional) $get The get parameters.
	 * @return string The rendered menu.
     */
    public static function render($url, $base = '', array $get = array())
    {
        if (!is_array($url))
        {
            $url = explode('/', trim($url, '/'));
            $url = array_combine($url, $url);
        }
        
		$get = ($get) ? '?' . http_build_query($get) : '';
		$crumb = array();
		$href = '';
        $count = count($url);
        $i = 0;
        
        foreach ($url as $k => $v)
        {
			$i++;
			$href .= $k . '/';
            $crumb[] = '<li>' . (($i !== $count) ? '<a href="' . $base . $href . $get . '">' . htmlspecialchars($v) . '</a>' : '<span>' . htmlspecialchars($v) . '</span>') . '</li>';
        }
        
        return '<ul>' . implode($crumb) . '</ul>';
    }

    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
