<?php
defined('VSIGN') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");

class ModuleManager
{
	private $db;
	private $user;
	
	public $message;
	
	private $theMenu;
	private $current;
	private $numberItems;
	
	/**
	 * Constructs the Module Manager.
	 * @return 
	 * @param $db Object
	 * @param $user Object
	 */
	public function __construct(database $db, User $user, $regionSpecific = -1, $module = '')
	{
		$this->db 		=& $db;
		$this->user 	=& $user;
			
		if (!$this->theMenu = $user->ModuleAuth($regionSpecific, $module))
		{
			$this->message = 'No enabled modules.';
			return false;
		}

		// Set some information about this menu		
		$this->current = 0;
		$this->numberItems = count($this->theMenu);
		
		// We dont want to do 0 items
		if ($this->numberItems == 0) $this->numberItems = -1;
		
		$this->message = $this->numberItems . ' modules loaded';

		return true;
	}
	
	/**
	 * Returns the internal message
	 * @return 
	 */
	public function GetMessage()
	{
		return $this->message;
	}
	
	/**
	 * Gets the next menu item in the queue
	 * @return 
	 */
	public function GetNextModule()
	{
		if (!$item = $this->GetModule($this->current))
		{
			$message = 'No more modules.';
			return false;
		}
		
		$this->current++;
		
		return $item;
	}
	
	/**
	 * Gets the menu item at position i
	 * @return 
	 * @param $i Object
	 */
	public function GetModule($i)
	{
		if ($i >= $this->numberItems)
		{
			$this->message = 'There are only ' . $this->numberItems . ' modules.';
			return false;
		}
		
		return $this->theMenu[$i];
	}
}
?>
