<?php
require "commonincludefiles.php";

    $userid			  = Escape($_POST['userid']); 
	$password			= Escape($_POST['password']); 
	$_SESSION['c_login'] = $userid;
	$_SESSION['c_pass']  = $password;
	
	
	$module_id	   	   = Escape($_POST['module_hidden']);


if ($_SESSION['c_login'] && $_SESSION['c_pass']){
    
	$out=dbQuery("select 
				u.*,em.employee_active,cc.college_name,cc.college_logo,cc.college_address,cc.college_phone,cc.college_email,cc.college_url
				  from cms_usermaster u left join cms_collegemaster cc on cc.college_id=u.college_id 
				  LEFT JOIN cms_employeemaster as em ON u.user_id = em.user_id
				  where  user_name='{$_SESSION['c_login']}' &&  user_password='{$_SESSION['c_pass']}' 
				  && FIND_IN_SET($module_id, module_ids) AND(em.employee_active='Yes' OR u.user_role='Poweruser')");
    
//    echo $out; exit;
	/*Module Name*/
	
	$sql_module = "SELECT module_shortname FROM cms_modulemaster WHERE module_id = $module_id";
	$res_module = dbQuery($sql_module);
	$rec_module = dbArray($res_module);
	$module_folder = $rec_module['module_shortname'];
	
	
	
	
	if (dbRows($out)>0){
        
		$admins		= dbObject($out);
		$user_id	= $admins->user_id;
	    $user_name	= $admins->user_name;
        $college_id	= $admins->college_id;
		$menu_ids	= $admins->menu_ids;
        $page_ids	= $admins->page_ids;				
		
        $_SESSION['_err']				= 'pass_ok';
		$_SESSION["Employee_Id"]		=0;
		$_SESSION["employee_active"]		='Yes';
		$_SESSION['marks_date']         = $admins->marks_date;
		
		$_SESSION['student_id']         = $admins->student_id;
        $_SESSION["VedantnrUserId"]			= $admins->user_id;       
		$_SESSION["VedantnrUserName"]			= $admins->user_name;		
		$_SESSION["VedantnrUserRole"]			= $admins->user_role;	
		$_SESSION["VedantnrUserMenuFile"]		= "menu-file-".time().".js";	       		
		
		$_SESSION["VedantnrCollegeId"]			= $admins->college_id;       
		$_SESSION["VedantnrCollegeLogo"]		= ($admins->college_logo!="") ? $admins->college_logo : "default.gif";       
		$_SESSION["VedantnrCollegeName"]		= $admins->college_name;       
		$_SESSION["VedantnrCollegeAddress"]	= $admins->college_address;       
		$_SESSION["VedantnrCollegePhone"]		= $admins->college_phone;       				
		$_SESSION["VedantnrCollegeEmail"]		= $admins->college_email; 	
		$_SESSION["VedantnrCollegeUrl"]		= $admins->college_url;
		
		$_SESSION["VedantnrYearId"] 			= $_POST['year_hidden'];
		$_SESSION["VedantnrYearName"] 			= $_POST['yearname'];
		$_SESSION['VedantnrModuleFolder']  = $module_folder;
		$_SESSION["aModuleId"]		= $module_id;
		
		if($_SESSION["VedantnrUserId"]!=""){
			$sql = "select employee_id,branch_name from cms_employeemaster where user_id ='".$_SESSION["VedantnrUserId"]."'";
			$sql_res = dbQuery($sql);
			$sql_rec = dbArray($sql_res);
			$_SESSION["Employee_Id"] = $sql_rec[0];
			$_SESSION["branch_name"] = $sql_rec[1];
			
		}
		
		
	 if($_SESSION['VedantnrModuleFolder']=="library")
	 {

		 $redfile = $_SESSION['VedantnrModuleFolder']."/"."loginhome.php";
		 
	 }
	 else
		$redfile = "download-backup.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["VedantnrUserId"]."'";
	$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);

	$strMenu="";
		
	$objMenuMgmt = new MenuMgmt();
	
	$limit=0;
	$start=0;
	$condition   = "";
	$condition	 = " AND menu_active ='1' AND menu_id in ($menu_ids) AND module_id = '".$_SESSION["aModuleId"]."'";
	$order_field = "menu_order";
	$order_type  = "ASC";
			
			
	 $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 .= ",";								
				}
				
				dbResult($result);
		}
		$array_outputMenuName = explode(",",$outputMenuName);
		$array_outputMenuId   = explode(",",$outputMenuId);		
		
		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();
		
		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);
			
			$strMenuPage="";							
			
			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";
			}		
		}
		
	}
		dbResult($outpage);
		$array_outputPageName = explode(",",$outputPageName);
		$array_outputPageId   = explode(",",$outputPageId);		
		
		$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['VedantnrModuleFolder']=='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 = split(",", $otherfields);
		
			for($intX = 0; $intX < count($aryOtherFields); $intX++) 
			{
		
				$strHtmlContent = str_replace("#".$aryOtherFields[$intX]."#", $aryOtherValues[$intX], $strHtmlContent);
			}
	$fso = fopen("menu/".$_SESSION["VedantnrUserMenuFile"], "w");
	fwrite($fso, $strHtmlContent);	

	fclose($fso);
	//	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();
		
		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['VedantnrModuleFolder']=='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 = split(",", $otherfields);
		
			for($intX = 0; $intX < count($aryOtherFields); $intX++) 
			{
		
				$strHtmlContent = str_replace("#".$aryOtherFields[$intX]."#", $aryOtherValues[$intX], $strHtmlContent);
			}
	$fso = fopen("menu/".$_SESSION["VedantnrUserMenuFile"], "w");
	fwrite($fso, $strHtmlContent);	

	fclose($fso);
 }		

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