<?php
// $Id: menuClass.php,v 1.3 2003/09/20 08:35:29 rcastley Exp $
/**
* Menu class code
* @package Mambo Open Source
* @Copyright (C) 2000 - 2003 Miro International Pty Ltd
* @ All rights reserved
* @ Mambo Open Source is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version $Revision: 1.3 $
**/

// ensure this file is being included by a parent file
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

class Menu
{
	var $hostname;
	var $user;
	var $pass;

	var $db;
	var $table;

	function Menu()
	{
		global $mosConfig_host, $mosConfig_user, $mosConfig_db, $mosConfig_password;
		// TODO: should pass these vars in the constructor
		$this->hostname = $mosConfig_host;
		$this->user = $mosConfig_user;
		$this->db = $mosConfig_db;
		$this->pass = $mosConfig_password;
	}

	function query($query)
	{
		$connection = mysql_connect($this->hostname, $this->user, $this->pass) or die("Cannot connect to database");
		$ret = mysql_db_query($this->db, $query, $connection) or die("Error in query: $query");

		return $ret;
	}

	function get_parent($Itemid)
	{
		GLOBAL $mosConfig_dbprefix;
		$query = "SELECT parent FROM ".$mosConfig_dbprefix."menu WHERE id='$Itemid'";
		$result = $this->query($query);
		$row = mysql_fetch_row($result);
		return $row[0];
	}

	function is_root_node($Itemid)
	{
		if ($this->get_parent($Itemid)==0)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}

	function get_label($Itemid)
	{
		GLOBAL $mosConfig_dbprefix;

		$query = "SELECT name FROM ".$mosConfig_dbprefix."menu WHERE id='$Itemid'";
		$result = $this->query($query);
		$row = mysql_fetch_row($result);
		return $row[0];
	}

	function get_link($Itemid)
	{
		GLOBAL $mosConfig_dbprefix;
		$query = "SELECT link FROM ".$mosConfig_dbprefix."menu WHERE id='$Itemid'";
		$result = $this->query($query);
		$row = mysql_fetch_row($result);
		return $row[0];
	}

	function get_children($Itemid)
	{
		GLOBAL $mosConfig_dbprefix;
		$query = "SELECT id, name, link FROM ".$mosConfig_dbprefix."menu
			WHERE parent='$Itemid' AND menutype='mainmenu' AND published='1'";
		$result = $this->query($query);
		$count = 0;
		while ($row = mysql_fetch_array($result))
		{
			$children[$count]["Itemid"] = $row["id"];
			$children[$count]["label"] = $row["name"];
			$children[$count]["link"] = $row["link"];
			$count++;
		}
		return $children;
	}

	function get_type($Itemid)
	{
		if ($this->get_children($Itemid))
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}

	function get_ancestors($Itemid, $count = 0)
	{
		$parent = $this->get_parent($Itemid);
		if ($parent)
		{
			$this->ancestors[$count]["Itemid"] = $parent;
			$this->ancestors[$count]["label"] = $this->get_label($parent);
			$this->ancestors[$count]["link"] = $this->get_link($parent);

			$this->get_ancestors($this->ancestors[$count]["Itemid"], $count+1);

			return array_reverse($this->ancestors);
		}
	}

	function get_children_recursive($Itemid, $count=0)
	{
		global $children;
		global $openedMenus;
		GLOBAL $mosConfig_dbprefix;

		$query = "SELECT id, name, link FROM ".$mosConfig_dbprefix."menu
			WHERE parent='$Itemid' AND menutype='mainmenu' AND published='1' GROUP BY id";
		$result = $this->query($query);
		while ($row = mysql_fetch_array($result))
		{
			$children[$count]["Itemid"] = $row["id"];
			$children[$count]["label"] = $row["name"];
			$children[$count]["link"] = $row["link"];

			$count++;
			#check if the children of this item should be displayed
			$check=in_array($row["id"], $openedMenus);
			if ($check == 1)
			{
				$this->get_children_recursive($row["id"], $count);
			}
		}

		return $children;

	}

	function print_menu_tree($Itemid=0)
	{
		$result = $this->get_children($Itemid);
		echo "<UL>";
		for ($x=0; $x<sizeof($result); $x++)
		{
			echo "<LI>". $result[$x]["label"]."[".$result[$x]["Itemid"]."]";
			$this->print_menu_tree($result[$x]["Itemid"]);
			echo "</LI>";
		}
		echo "</ul>";
	}
} ?>
