<?php

error_reporting(E_ERROR | E_PARSE);

session_start(); 



if(!isset($_SESSION['tulsilogin']))

{

	header('Location: index.php');

	return;

}



	






$_SESSION['_token']=bin2hex(openssl_random_pseudo_bytes(16));	

	

	include("connect.php");
include("mylibrary.php");


	require('importfromexcel/php-excel-reader/excel_reader2.php');



    require('importfromexcel/SpreadsheetReader.php');

	

	

	

	$allowed =  array('xlsx');

	$filename = $_FILES['header']['name'];

	 $ext = pathinfo($filename, PATHINFO_EXTENSION);

	if(!in_array($ext,$allowed) ) 

	{

		$_SESSION['msg']="Unsupport File.Select Excel File only";

		header('location: college.php');

		return;

	}

	else

	{

	

	

	

	date_default_timezone_set("Asia/Kolkata");

	$filenameheader = date("YmdHis").$_FILES['header']['name'];

	$path = "./temp/";

	move_uploaded_file( $_FILES['header']['tmp_name'], $path ."/". $filenameheader );

	



    $Reader = new SpreadsheetReader('temp/'.$filenameheader);

	

	$i=0;
	$j=0;
	$k=0;

    foreach ($Reader as $Col)

    {
$j++;
       		

		$collegecode=checkvalidstring ($Col[0]);

		
		
		 $evels = "select collegemid,countrymid from res_college_master where collegecode='$collegecode'";

		$rels = mysqli_query($conn, $evels);

		if(mysqli_num_rows($rels) > 0)

		{

			/* Check The Level Of Study is added */

			while($rtls = mysqli_fetch_assoc($rels))

			{

				 $collegemid=$rtls['collegemid'];
				 $countrymid=$rtls['countrymid'];
				

			}

		}	

		else

		{
			if($i==0)

		{

			$i++;

			

		}
		continue;
		}

		

		$coursenamecode=checkvalidstring ($Col[1]);

		
		
		$evels = "select cnmid from res_coursename_master where coursenamecode='$coursenamecode'";

		$rels = mysqli_query($conn, $evels);

		if(mysqli_num_rows($rels) > 0)

		{

			/* Check The Level Of Study is added */

			while($rtls = mysqli_fetch_assoc($rels))

			{

				 $cnmid=$rtls['cnmid'];

			}

		}	

		else

		{
			if($i==0)

		{

			$i++;

			

		}
		continue;
		}

		
		

		$ccampuscode=checkvalidstring ($Col[2]);

		

		$evels = "select ccampusmid from res_collegecampus_master where ccampuscode='$ccampuscode'";

		$rels = mysqli_query($conn, $evels);

		if(mysqli_num_rows($rels) > 0)

		{

			/* Check The Level Of Study is added */

			while($rtls = mysqli_fetch_assoc($rels))

			{

				 $campusmid=$rtls['ccampusmid'];

			}

		}	

		else

		{
			if($i==0)

		{

			$i++;

			

		}
		continue;
		}


		

		

		

		$disciplinecode=checkvalidstring ($Col[3]);

		
$evels = "select disciplinename from res_discipline_master where disciplinecode='$disciplinecode'";

		$rels = mysqli_query($conn, $evels);

		if(mysqli_num_rows($rels) > 0)

		{

			/* Check The Level Of Study is added */

			while($rtls = mysqli_fetch_assoc($rels))

			{

				 $cprogramcategory=$rtls['disciplinename'];

			}

		}	

		else

		{
			if($i==0)

		{

			$i++;

			

		}
		continue;
		}

		

		
		$loscode=checkvalidstring ($Col[4]);

	
		
		$evels = "select losmid from res_levelofstudy_master where loscode='$loscode'";

		$rels = mysqli_query($conn, $evels);

		if(mysqli_num_rows($rels) > 0)

		{

			/* Check The Level Of Study is added */

			while($rtls = mysqli_fetch_assoc($rels))

			{

				 $losmid=$rtls['losmid'];

			}

		}	

		else

		{
			if($i==0)

		{

			$i++;

			

		}
		continue;
		}

		$costofliving=checkvalidstring($Col[5]);

		

		$cprogramavailibilty=checkvalidstring($Col[6]);

		

		$cprogramlength=checkvalidstring($Col[7]);

		$programsem=checkvalidstring($Col[8]);

		

		$cprogramlanguage=checkvalidstring($Col[9]);

		

		$cprogramtype=checkvalidstring($Col[10]);

		

		$cprogramdelivery=checkvalidstring($Col[11]);

	
		$cprogramcode=checkvalidstring($Col[12]);

		

		$cinternalcode=checkvalidstring($Col[13]);

		
		
		
		
		$programurl=checkvalidstring ($Col[14]);

		

		
		$cappurllink=checkvalidstring ($Col[15]);

		
		 
		
		$cconditionalacceptence=checkvalidstring ($Col[16]);

	
		

		$chighlycompetitive=checkvalidstring ($Col[17]);

		
		 
		

		$cmdesc=checkvalidstring ($Col[18]);

		
		

		$cpercentage=checkvalidstring ($Col[19]);

		
		
		
		
		$cbacklogaccepted=checkvalidstring ($Col[20]);

		
		
		
		$cedugapaccepted=checkvalidstring ($Col[21]);

	
		
		
		$capplicationfee=checkvalidstring ($Col[22]);

	
		
		
		$feesin=checkvalidstring ($Col[23]);

		
		
		
		 $ctutionfees=preg_replace('/[^0-9.]/','', $Col[24]);

		

		
			$ctutionfeesin=checkvalidstring ($Col[25]);

		
		
		
		
		$cregifees1=checkvalidstring ($Col[26]);

		
		
		
		
 $cregifees=preg_replace('/[^0-9.]/','', $Col[27]);
		
		
		
 $cgrossfees=preg_replace('/[^0-9.]/','', $Col[28]);
		

		
$cscholorship=preg_replace('/[^0-9.]/','', $Col[29]);
		
		
		$csubmissiondeadline=checkvalidstring($Col[30]);

		

		$cprocessingtime=checkvalidstring($Col[31]);
		
		
		$cjobprospect=checkvalidstring($Col[32]);

		

		
		$cavgannualsalary=checkvalidstring ($Col[33]);

	
		
		
				

		if($i==0)

		{

			$i++;

			continue;

		}

		
		

		

		 $eve = "select cprogramcode from res_course_master where cinternalcode='$cinternalcode' ";
	$re = mysqli_query($conn, $eve);
	if(mysqli_num_rows($re) > 0)
	{
		continue;
	}
	else
	{	
	
		 $eve1 = "INSERT INTO res_course_master (cnmid,cmdesc,collegemid,levelofstudy,  feesin,cregifees ,capplicationfee,ctutionfees,cscholorship,cgrossfees,cprogramcode,cprogramcategory,cprogramlength,cprogramtype,cprogramavailibilty,csubmissiondeadline,cprogramdelivery,cpercentage,cbacklogaccepted,cedugapaccepted,chighlycompetitive,ctutionfeesin,programurl,countrymid,costofliving,cjobprospect,cavgannualsalary,cappurllink,cconditionalacceptence,cprogramlanguage,campusmid,cprocessingtime,cregifees1,cinternalcode,programsem) VALUES('$cnmid','$cmdesc','$collegemid','$losmid','$feesin','$cregifees','$capplicationfee','$ctutionfees','$cscholorship','$cgrossfees','$cprogramcode','$cprogramcategory','$cprogramlength','$cprogramtype','$cprogramavailibilty','$csubmissiondeadline','$cprogramdelivery','$cpercentage','$cbacklogaccepted','$cedugapaccepted','$chighlycompetitive','$ctutionfeesin','$programurl','$countrymid','$costofliving','$cjobprospect','$cavgannualsalary','$cappurllink','$cconditionalacceptence','$cprogramlanguage','$campusmid','$cprocessingtime','$cregifees1','$cinternalcode','$programsem')";
	
		
		if($conn->query($eve1) === TRUE)
	{
		 $k++;
			
		$_SESSION['msg'] = "Record Saved successfully";
	}

			

	$_SESSION['msg'] = "Program Data Imported";

	

	}
	}
	}
	
	if($k == 0)
	{
		$_SESSION['msg1'] = "Program Data Imported Failed";
	}
	else if(($j-1) == $k)
	{
	$_SESSION['msg'] = "Program Data Imported";
	}else{
		$_SESSION['msg'] = "Program Data Partialy Imported";
	}
	header('Location: course.php');

?>