<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class is used to create our Menu/Navbars
 *
 * @package    lnApp
 * @category   Helpers
 * @author     Deon George
 * @copyright  (c) 2009-2013 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
class lnApp_Menu {
	public static function items($type) {
		$result = array();

		if (empty(URL::$method_directory[$type]))
			return NULL;

		$list = Kohana::list_files('classes/Controller/'.ucfirst($type));

		// This will be used a lot!
		$ext_length = strlen(EXT);

		foreach ($list as $name => $path)
			if (is_array($path)) {
				$result += self::items($path);

			} elseif (substr($name, -$ext_length) === EXT) {
				// Remove "classes/" and the extension
				$class = substr($name, 8, -$ext_length);

				// Convert slashes to underscores
				$class = str_replace(DIRECTORY_SEPARATOR, '_', $class);

				if (method_exists($class,'menu')) {
					$cat = strtolower(str_replace('Controller_'.ucfirst($type).'_','',$class));

					foreach ($class::menu() as $k=>$v)
						$result[$cat][$k] = $v;
				}
			}

		return $result;
	}

	public static function ul($type,array $result,array $append=NULL,$sub=FALSE,$method=NULL) {
		$output = $sub ? '<ul class="dropdown-menu">' : '<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">';

		foreach ($result as $k => $v)
			if (is_array($v))
				$output .= sprintf('<li class="dropdown-submenu">%s%s',HTML::anchor('#',$k,array('nocg'=>TRUE)),self::ul($type,$v,NULL,TRUE,$k).'</li>');
			else
				$output .= '<li>'.HTML::anchor(URL::$method_directory[$type].'/'.$method.'/'.$k,$v,array('tabindex'=>-1,'nocg'=>TRUE)).'</li>';

		if ($append) {
			$output .= '<li class="divider"></li>';

			foreach ($append as $k => $v)
				$output .= sprintf('<li>%s</li>',HTML::anchor($k,$v,array('nocg'=>TRUE)));
		}

		$output .= '</ul>';

		return $output;
	}
}
?>