<?php
/*
	Stempora web framework
	copyright (c) 2002-2014 Stempora
		web:  www.stempora.com
		mail: support@stempora.com

*/

// dependencies

/**
* description
*
* @library	
* @author	
* @since	
*/

global $site;

class CAdminMenu{
	
	var $template; 
	var $menu;
	var $sort = true;

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function CAdminMenu() {
	}
	
	

	function Render() {
		global $base;
		global $_IS_POPUP;

		if ($_IS_POPUP) {
			return "";
		}
		
		if (is_array($this->menu)) {


			//preprocess for parents
			foreach ($this->menu as $key => $item) {

				if (is_array($item["link"]) && !is_array($item["link"][key($item["link"])])) {

					$item["link"] = array(
						$item["link"]
					);
				}

				if ($item["id"]) {
					$menu[$item["id"]] = $item;
				} else{
					$menu[] = $item;
				}				
			}


			$this->menu = $menu;

			foreach ($this->menu as $key => $item) {

				if ($item["parent"] && is_array($this->menu[$item["parent"]])) {

					if (!is_array($this->menu[$item["parent"]]["link"]) && isset($this->menu[$item["parent"]]["link"])) {
						$this->menu[$item["parent"]]["link"] = array(
							array(
								"title"		=> $this->menu[$item["parent"]]["title"],
								"icon"		=> $this->menu[$item["parent"]]["icon"],
								"link"		=> $this->menu[$item["parent"]]["link"],
								"subtitle"	=> $this->menu[$item["parent"]]["subtitle"],
							)
						);

					}
										
					$this->menu[$item["parent"]]["link"][] = $item;
					unset($this->menu[$key]);
				}				
			}

			if ($this->sort) {
				usort($this->menu, "compare_menu_item_position");
			}

			
			foreach ($this->menu as $key => $item) {

				if ($item["icon"]) {
					$item["icon"] = $this->template->blockReplace("Icon" , $item);
				} else {
					$item["icon"] = "";
				}

				$item["selected"] = $item["selected"] && $this->template->blockExists("Selected") ? $this->template->blockReplace("Selected" , $item) : "";

				if (is_array($item["link"])) {

					//usort($item["link"], "compare_menu_item_position");


					if ($this->sort) {
						usort($item["link"], "compare_menu_item_position");
					}


					foreach ($item["link"] as $k => $v) {						

						if ($v["icon"]) {
							$item["link"][$k]["icon"] = $this->template->blockReplace("Icon" , $v);
						} else {
							$item["link"][$k]["icon"] = "";
						}

						if ($v["subtitle"]) {
							$item["link"][$k]["subtitle"] = $this->template->blockReplace($v["icon"] ? "SubtitleIcon" : "Subtitle" , $v);
						} else {
							$item["link"][$k]["subtitle"] = "";
						}


						$item["link"][$k]["selected"] = $v["selected"] && $this->template->blockExists("Selected") ? $this->template->blockReplace("Selected" , $v) : "";

						
					}

					
					$this->menu[$key]["submenu"] = $items["submenu"] = $base->html->Table(
						$this->template , 
						"Links" , 
						$item["link"]
					);
					$this->menu[$key]["key"] = $key;
					$this->menu[$key]["content"] = $this->template->BlockReplace("MenuTitle" , $item);
				} else {
					$this->menu[$key]["submenu"] = $item["submenu"] = "";

					$this->menu[$key]["content"] = $this->template->BlockReplace("MenuTitleLink" , $item);
				}

								
				
			}
					

			if (count($this->menu)) {

				return $this->template->blockReplacE(
					"Menu" , 
					array(
						"data"	=> $base->html->table(
							$this->template , 
							"Menu" , 
							$this->menu
						)
					)
				);
			}

			return "";
			
		}

	}
	
	

/*	
*/	
}


function compare_menu_item_position($a, $b)
{
	if ($a["prio"] == $b["prio"]) {
		return 0;
	}
	return ($a["prio"] < $b["prio"]) ? -1 : 1;
}


?>