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

/**
 * This class is used to create our Menu/Navbars
 *
 * @package    OSB
 * @category   Helpers
 * @author     Deon George
 * @copyright  (c) 2009-2013 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Menu extends lnApp_Menu {
	private static function collapse(array $array) {
		$result = array();

		foreach ($array as $mmo) {
			if (isset($result[$mmo->module->name])) {
				if (! is_array($result[$mmo->module->name]))
					$result[$mmo->module->name] = array($result[$mmo->module->name]);

				array_push($result[$mmo->module->name],$mmo);

				continue;

			} else {
				$result[$mmo->module->name] = $mmo;
			}
		}

		return $result;
	}

	public static function items($type) {
		$result = array();

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

		$ao = Auth::instance()->get_user();

		foreach ($ao->methods() as $mmo)
			if ($mmo->menu_display AND $type == $mmo->directory())
				if (empty($result[$mmo->id]))
					$result[$mmo->id] = $mmo;

		return self::collapse($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).'</li>');
			else
				$output .= '<li>'.HTML::anchor($v->url(),$v->menu_display(),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;
	}
}
?>
