<?php
require "commonincludefiles.php";
error_reporting(E_ALL);
$userid = Escape($_REQUEST['userid']);

$password = Escape($_REQUEST['password']);
 //echo  $userid;  exit;


$_SESSION['c_login'] = $userid;
$_SESSION['c_pass'] = $password;
$year_id = Escape($_POST['year_hidden']);
$year_name = Escape($_POST['yearname']);


if ($_SESSION['c_login'] && $_SESSION['c_pass']) {
	$Qr = "select 
				 u.*,sm.company_name,sm.company_address
				  from lms_usermaster u left join lms_companymaster sm on sm.company_id=u.company_id
				  where user_name='{$_SESSION['c_login']}' &&  user_password='{$_SESSION['c_pass']}'";

		//echo $Qr; exit;

	$out = dbQuery($Qr);
	if (dbRows($out)) {

		$admins = dbObject($out);
		$user_id = $admins->user_id;
		$user_name = $admins->user_name;
		$company_id = $admins->company_id;



		$_SESSION['_err'] = 'pass_ok';
	$_SESSION["cUserId"] = $admins->user_id;

		$_SESSION["cName"] = $admins->name;
		$_SESSION["cUserName"] = $admins->user_name;
		$_SESSION["cUserRole"] = $admins->user_role;
		$_SESSION["cCompany_id"] = $admins->company_id;
		$_SESSION["cCompany_name"] = $admins->company_name;
		$_SESSION["cCompany_address"] = str_replace("\n", "<br/>", $admins->company_address);
		$_SESSION["cMenuAccess"] = $admins->menu_ids;
		$_SESSION["cPageAccess"] = $admins->page_ids;
		$_SESSION["cYear"] = '5';
		$_SESSION["cYearname"] = '2012-2013';
		$_SESSION["cUserMenuFile"] = "menu-file-" . time() . ".js";
		//  $out=dbQuery("update lms_usermaster set user_lastlogin = user_currentlogin where user_id='$admins->user_id'");

		//$out=dbQuery("update lms_usermaster set user_currentlogin=now() where user_id='$admins->user_id'");

		$redfile = "loginhome.php";
		getMenu();

	} else {
		$_SESSION['_err'] = 'pass_bad';
		unset($root);
		$redfile = "index.php?verr=Invalid";
	}
} else {
	unset($root);
	$redfile = "index.php?verr=Invalid";
}

function getMenu()
{

	$objUserMgmt = new UserMgmt();
	$limit = 0;
	$start = 0;
	$condition = "";
	$condition = " AND user_id='" . $_SESSION["cUserId"] . "'";
	$order_field = "";
	$order_type = "";


	$out = $objUserMgmt->getAllRecords($limit, $start, $condition, $order_field, $order_type);

	if (dbRows($out) > 0) {

		$admins = dbObject($out);
		$user_id = $admins->user_id;
		$user_name = $admins->user_name;
		$company_id = $admins->company_id;
		$menu_ids = $admins->menu_ids;
		$page_ids = $admins->page_ids;
		$subpage_ids = $admins->subpage_ids;
	}
	$menuid = "";
	$flg = 1;
	$output = "";
	$menuname2 = "";
	$tempary = explode(",", $menu_ids);
	//echo $tempary;

	$strMenu = "";

	$objMenuMgmt = new MenuMgmt();

	$limit = 0;
	$start = 0;
	$condition = "";
	$condition = " AND menu_active ='1' AND menu_id in ($menu_ids)";
	$order_field = "menu_order";
	$order_type = "ASC";
	$outputMenuName = "";
	$outputMenuId = "";

	$result = $objMenuMgmt->getAllRecords($limit, $start, $condition, $order_field, $order_type);

	if (dbRows($result) > 0) {
		while ($row = dbArray($result)) {
			$menu_name = $row["menu_name"];
			$menu_id = $row["menu_id"];
			$temp = strtolower(str_replace(" ", "", trim($menu_name)));
			$strMenu .= "{text:'" . $menu_name . "',menu:" . $temp . "}" . "\r\n";
			$strMenu .= ",";

			$outputMenuName .= strtolower(str_replace(" ", "", $menu_name));
			$outputMenuName .= ",";	

			$outputMenuId .= $menu_id;
			$outputMenuId .= ",";
		}

		dbFreeResult($result);
	}
	$array_outputMenuName = explode(",", $outputMenuName);
	$array_outputMenuId = explode(",", $outputMenuId);
	//echo $outputMenuId;exit;
	getPage($page_ids, $subpage_ids, $array_outputMenuName, $array_outputMenuId, $strMenu);
}

function getPage($page_ids, $subpage_ids, $array_outputMenuName, $array_outputMenuId, $strMenu)
{

	if (count($array_outputMenuId) > 0) {

		$objPageMgmt = new PageMgmt();
		$strMenuPage = "";
		$outputPageName = "";
		$outputPageId = "";
		$strMenuPage = "";
		$subMenu = "";
		for ($i = 0; $i < count($array_outputMenuId); $i++) {

			$limit = 0;
			$start = 0;
			$condition = " AND page_active ='1'  AND page_id in ($page_ids) AND menu_id='$array_outputMenuId[$i]'";
			$order_field = "page_order";
			$order_type = "ASC";
			$outpage = $objPageMgmt->getAllRecords($limit, $start, $condition, $order_field, $order_type);

			

			if (dbRows($outpage) > 0) {

				$strMenuPage = "";

				while ($admins = dbObject($outpage)) {
					
					$page_id = $admins->page_id;
					$page_name = $admins->page_name;
					$page_link = $admins->page_link;

					if ($page_link != "") {

						$strMenuPage .= "{text:'" . $page_name . "',href:'" . $page_link . "'}" . "\r\n";
						$strMenuPage .= ",";

					} else {

						$temp = strtolower(str_replace(" ", "", trim($page_name)));
						$strMenuPage .= "{text:'" . $page_name . "',menu:" . $temp . "}" . "\r\n";
						$strMenuPage .= ",";

						$outputPageName .= strtolower(str_replace(" ", "", $page_name));
						$outputPageName .= ",";

						$outputPageId .= $page_id;
						$outputPageId .= ",";

					}

				}
				$strMenuPage = substr($strMenuPage, 0, strlen($strMenuPage) - 1);
				$subMenu .= " var " . $array_outputMenuName[$i] . " = new Ext.menu.Menu({ id:'" .
					$array_outputMenuName[$i] . "', items: [" . $strMenuPage . "]}); ";
				$subMenu .= "\r\n" . "\r\n";
			}
		}

	}
	//echo $subMenu;exit;
	dbFreeResult($outpage);
	$array_outputPageName = explode(",", $outputPageName);
	$array_outputPageId = explode(",", $outputPageId);
	getSubPage($subpage_ids, $array_outputPageName, $array_outputPageId, $strMenu, $subMenu);

}
function getSubPage($subpage_ids, $array_outputPageName, $array_outputPageId, $strMenu, $subMenu)
{

	if (count($array_outputPageId) > 0) {

		$objSubPageMgmt = new SubPageMgmt();
		$subSubMenu = "";
		for ($i = 0; $i < count($array_outputPageId); $i++) {
		
			$limit = 0;
			$start = 0;
			$condition = " AND subpage_active ='1'  AND subpage_id in ($subpage_ids) AND page_id='$array_outputPageId[$i]'";
			$order_field = "subpage_order";
			$order_type = "ASC";
			$outsubpage = $objSubPageMgmt->getAllRecords($limit, $start, $condition, $order_field, $order_type);

			if (dbRows($outsubpage) > 0) {

				$strSubMenuPage = "";
				while ($admins = dbObject($outsubpage)) {

					$subpage_id = $admins->subpage_id;
					$subpage_name = $admins->subpage_name;
					$subpage_link = $admins->subpage_link;


					$strSubMenuPage .= "{text:'" . $subpage_name . "',href:'" . $subpage_link . "'}" . "\r\n";
					$strSubMenuPage .= ",";


				}
				$strSubMenuPage = substr($strSubMenuPage, 0, strlen($strSubMenuPage) - 1);
				$subSubMenu .= " var " . $array_outputPageName[$i] . " = new Ext.menu.Menu({ id:'" .
					$array_outputPageName[$i] . "', items: [" . $strSubMenuPage . "]}); ";
				$subSubMenu .= "\r\n" . "\r\n";
			}
		}
	}

	$filename = "js/main-menu.js";

	$strHtmlFileName = $filename;

	$fso = fopen($strHtmlFileName, "r");

	$strHtmlContent = "";

	while (!feof($fso))

		$strHtmlContent .= fgets($fso, 4096);

	fclose($fso);

	$otherfields = "SUBSUBMENU,SUBMENU,MAINMENU,GENERALSERACH,COMPANYID";

	/*if($_SESSION['aModuleFolder']=='school') {
				 $strGeneralSearch = "{
					 text: 'General Search'
					 ,handler: function(btn)
						 { 
						   general_search();
						 }
					 
				 },";
			 }else{*/
	$strGeneralSearch = "";
	//}
	$aryOtherValues[0] = $subSubMenu;
	$aryOtherValues[1] = $subMenu;
	$aryOtherValues[2] = $strMenu;
	$aryOtherValues[3] = $strGeneralSearch;
	$aryOtherValues[4] = $_SESSION["cCompany_id"];

	$aryOtherFields = explode(",", $otherfields);

	for ($intX = 0; $intX < count($aryOtherFields); $intX++) {

		$strHtmlContent = str_replace("#" . $aryOtherFields[$intX] . "#", $aryOtherValues[$intX], $strHtmlContent);
	}
	$fso = fopen("menu/" . $_SESSION["cUserMenuFile"], "w");
	fwrite($fso, $strHtmlContent);

	fclose($fso);
}

?>

<script language="javascript">
	location.href = '<?php echo $redfile; ?>';
</script>