<?php
/**
* @file $Id: Menu.php 616 2007-07-26 18:56:28Z AIS Integral $
* @package AIS Integral
* @copyright Copyright (C) 2010 Pavel Koshelev. All rights reserved.
*/
if($menu)
	$old_menu = $menu;

if(!is_array($_SYSTEM['Menu']))
//if(is_countable($_SYSTEM['Menu'])&&!count($_SYSTEM['Menu']))	



{
	$menu = array();
	$IntegralMods = array(
			'My_Child'=>true,
			'My_Info'=>true,				
			'mod_students'=>true,
			'mod_users'=>true,
			'mod_curriculum'=>true,
			'mod_attendance'=>true,
			'mod_grades'=>true,
			'mod_evaluation'=>true,
			'mod_activity'=>true,
			'mod_discipline'=>true,
			'mod_billing'=>true,
			'mod_apply'=>true,
			'mod_cafe'=>true,
			'mod_messaging'=>true,			
			'mod_library'=>true,
			'mod_savedinfo'=>true,
			'mod_ssetup'=>true,
			'My_School'=>true
			);

/*
echo "<pre>";
print_r($IntegralMods);
echo "</pre>"; */
//die('zzzzzz');

//die('kk'); 
	foreach($IntegralMods as $module=>$include)
	{
	
		$menu[$module] = array();
	}

	foreach($IntegralMods as $module=>$include)
	{
		if($module!='My_Child' && $module!='My_Info' && $module!='My_School')
		{
			// do this since usrprofiles.php & usrpermissions.php need Menu.php to be re-included.  But, this can mess up the top menu in other circumstances by overwriting the arrays.
			// if($_SYSTEM['force_include']===true)
			// 	include($staticpath."modules/$module/Menu.php");
			// else	
			// {	if(file_exists($staticpath."modules/$module/Menu.php"))
			// 		include_once($staticpath."modules/$module/Menu.php");
			// }
		}
	}
	if($handle = opendir($staticpath."modules/")) 
	{
		if(!is_array($IgnoreFiles))
			$IgnoreFiles=Array();
// Убрать mod_portal если понадобится добавить Menu.php	
		while (false !== ($folder = readdir($handle)))
		{
			if($folder != "." && $folder != ".." && $folder != "mod_portal" && $folder != "index.html" && $folder != ".svn" && !in_array($folder,$IgnoreFiles))
			{
				// do this since profiles.php & permissions.php need Menu.php to be re-included.  But, this can mess up the top menu in other circumstances by overwriting the arrays.
				if($_SYSTEM['force_include']===true)
					include($staticpath."modules/$folder/Menu.php");
				else
					include_once($staticpath."modules/$folder/Menu.php");
			}
		}
	}
	// to make the reports menu the last one
	if(get_user('PROFILE')=='teacher')
	{
	    if($menu['mod_discipline'])
	    {
		$last_menu = $menu['My_School'];
		unset($menu['My_School']);
		$menu['My_School'] = $last_menu;
	    }
	}elseif(get_user('PROFILE')=='student'){
	    if($menu['mod_billing'])
	    {
		$last_menu = $menu['My_School'];
		unset($menu['My_School']);
		$menu['My_School'] = $last_menu;
	    }
	}else{
	    if($menu['mod_savedinfo'])
	    {
		$last_menu = $menu['mod_ssetup'];
		unset($menu['mod_ssetup']);
		$menu['mod_ssetup'] = $last_menu;
	    }
	}
	$profile = get_user('PROFILE');
        
	if(is_numeric(get_user('PROFILE_ID')))
		$can_use_array = db_request(db_execute("SELECT MODNAME FROM PROFILE_SETTINGS WHERE PROFILE_ID='".get_user('PROFILE_ID')."' AND VIEWER='N'"),array(),array('MODNAME'));
	else
		$can_use_array = db_request(db_execute("SELECT MODNAME FROM USER_SETTINGS WHERE USERNAME='".get_user('USERNAME')."' AND VIEWER='N'"),array(),array('MODNAME'));

	foreach($menu as $modcat=>$profiles)
	{
		if($profiles[$profile])
			$_SYSTEM['Menu'][$modcat] = $profiles[$profile];
		if(get_user('PROFILE')=='admin' || true)
		{
			if(count($can_use_array))
			{
			    if(!$_SYSTEM['Menu']) $_SYSTEM['Menu'] = array();
				foreach($_SYSTEM['Menu'] as $modcat=>$programs)
				{
					foreach($programs as $program=>$title)
					{
						if($can_use_array[$program] && !($_REQUEST['servicemod']=='mod_users/UsrPermissions.php' && !$can_use_array['mod_users/Permissions.php']) && !($_REQUEST['servicemod']=='mod_users/UsrProfile.php' && !$can_use_array['mod_users/UsrProfile.php']))
							unset($_SYSTEM['Menu'][$modcat][$program]);
						if(!is_numeric($program) && strpos($program,'&#8658;'))
						{
							if(count($title))
							{
								foreach($title as $linkUrl=>$linkName)
								{
									if($can_use_array[$linkUrl] && !($_REQUEST['servicemod']=='mod_users/UsrPermissions.php' && !$can_use_array['mod_users/Permissions.php']) && !($_REQUEST['servicemod']=='mod_users/UsrProfile.php' && !$can_use_array['mod_users/UsrProfile.php']))
										unset($_SYSTEM['Menu'][$modcat][$program][$linkUrl]);
								}
							}else{
								if($can_use_array[$linkUrl] && !($_REQUEST['servicemod']=='mod_users/UsrPermissions.php' && !$can_use_array['mod_users/Permissions.php']) && !($_REQUEST['servicemod']=='mod_users/UsrProfile.php' && !$can_use_array['mod_users/UsrProfile.php']))
									unset($_SYSTEM['Menu'][$modcat][$program]);
							}
						}
					}
				}
			}
			if(is_array($exceptions[$modcat])&&count($exceptions[$modcat]))
			{
				foreach($exceptions[$modcat] as $program=>$true)
				{
					if(!AllowEdit($program) && $_REQUEST['servicemod']!='mod_users/UsrPermissions.php' && $_REQUEST['servicemod']!='mod_users/UsrProfile.php')
						unset($_SYSTEM['Menu'][$modcat][$program]);
				}
			}
		}
		$_SYSTEM['icons'][$modcat] = $menu[$modcat]['icon'];
	}
//var_dump($_SYSTEM);
	if(get_user('PROFILE')=='student' || get_user('PROFILE')=='applicant')
		unset($_SYSTEM['Menu']['mod_users']);
}
//echo "<pre>";print_r($menu);die;
//var_dump($_SYSTEM['Menu']);
unset($menu);
if($old_menu)
	$menu = $old_menu;
	
?>