<?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;

    foreach ($Reader as $Col)

    {

       		

		$collegecode=str_replace ("'","\'",$Col[0]);

		$collegecode=str_replace ('"','\"',$collegecode);
		
		 $evels = "select collegemid 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'];

			}

		}	

		else

		{
			if($i==0)

		{

			$i++;

			

		}
		continue;
		}

		

		$campusname=str_replace ("'","\'",checkvalidstring($Col[1]));

		$campusname=str_replace ('"','\"',$campusname);

		$ccampuscode=str_replace ("'","\'",checkvalidstring($Col[2]));

		$ccampuscode=str_replace ('"','\"',$ccampuscode);

		

		

		$campsesctiondescription=str_replace ("'","\'",checkvalidstring($Col[3]));

		$campsesctiondescription=str_replace ('"','\"',$campsesctiondescription);

		

		
		$contactname=str_replace ("'","\'",checkvalidstring($Col[4]));

		$contactname=str_replace ('"','\"',$contactname);

		$cmiddlename=str_replace ("'","\'",checkvalidstring($Col[5]));

		$cmiddlename=str_replace ('"','\"',$cmiddlename);

		$csurname=str_replace ("'","\'",checkvalidstring($Col[6]));

		$csurname=str_replace ('"','\"',$csurname);

		$contactnumber=str_replace ("'","\'",checkvalidstring($Col[7]));

		$contactnumber=str_replace ('"','\"',$contactnumber);

		$cmobilenumber=str_replace ("'","\'",checkvalidstring($Col[8]));

		$cmobilenumber=str_replace ('"','\"',$cmobilenumber);

		$contactemail=str_replace ("'","\'",checkvalidstring($Col[9]));

		$contactemail=str_replace ('"','\"',$contactemail);

		$caltenateemail=str_replace ("'","\'",checkvalidstring($Col[10]));

		$caltenateemail=str_replace ('"','\"',$caltenateemail);

		$caddress1=str_replace ("'","\'",checkvalidstring($Col[11]));

		$caddress1=str_replace ('"','\"',$caddress1);

		
		
		
		$caddress2=str_replace ("'","\'",checkvalidstring($Col[12]));

		$caddress2=str_replace ('"','\"',$caddress2);

		
		$countrysortname=str_replace ("'","\'",$Col[13]);

		$countrysortname=str_replace ('"','\"',$countrysortname);
		 $evels = "select countrymid from res_country_master where countrysortname='$countrysortname'";

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

		if(mysqli_num_rows($rels) > 0)

		{

			/* Check The Level Of Study is added */

			while($rtls = mysqli_fetch_assoc($rels))

			{

				$ccountry=$rtls['countrymid'];

			}

		}	

		else

		{
			if($i==0)

		{

			$i++;

			

		}
		/* continue; */
		}
		
		$statecode=str_replace ("'","\'",$Col[13]);

		$statecode=str_replace ('"','\"',$statecode);
		 $evels = "select statemid from res_state_master where statecode='$statecode'";

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

		if(mysqli_num_rows($rels) > 0)

		{

			/* Check The Level Of Study is added */

			while($rtls = mysqli_fetch_assoc($rels))

			{

				$statemid=$rtls['statemid'];

			}

		}	

		else

		{
			if($i==0)

		{

			$i++;

			

		}
		/* continue; */
		}
		

		$citycode=str_replace ("'","\'",$Col[14]);

		$citycode=str_replace ('"','\"',$citycode);
		 $evels = "select citymid from res_city_master where citycode='$citycode'";

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

		if(mysqli_num_rows($rels) > 0)

		{

			/* Check The Level Of Study is added */

			while($rtls = mysqli_fetch_assoc($rels))

			{

				$citymid=$rtls['citymid'];

			}

		}	

		else

		{
			if($i==0)

		{

			$i++;

			

		}
		/* continue; */
		}
		

		$cpostalcode=str_replace ("'","\'",$Col[15]);

		$cpostalcode=str_replace ('"','\"',$cpostalcode);

		

		$cmsgappname=str_replace ("'","\'",checkvalidstring($Col[16]));

		$cmsgappname=str_replace ('"','\"',$cmsgappname);
		
		
		
		$cmsgappid=str_replace ("'","\'",checkvalidstring($Col[17]));

		$cmsgappid=str_replace ('"','\"',$cmsgappid);
		
		
		$cvideoappname=str_replace ("'","\'",checkvalidstring($Col[18]));

		$cvideoappname=str_replace ('"','\"',$cvideoappname);
		
		$cvideoappid=str_replace ("'","\'",checkvalidstring($Col[19]));

		$cvideoappid=str_replace ('"','\"',$cvideoappid);

		
		
		$contactdepartmnet='Head';
	
	$cposition='Head';
		
$csubcategory='Direct';
				

		if($i==0)

		{

			$i++;

			/* continue; */

		}

		
		

		

		 $eve = "select ccampuscode from res_collegecampus_master where ccampuscode='$ccampuscode' ";
	$re = mysqli_query($conn, $eve);
	if(mysqli_num_rows($re) > 0)
	{
		continue;
	}
	else
	{	
	
		   $eve2 = "INSERT INTO res_collegecampus_master (collegemid,campusname,campsesctiondescription,ccampuscode) VALUES('$collegemid','$campusname','$campsesctiondescription','$ccampuscode')";
		if ($conn->query($eve2) === TRUE)

	{	
	$last_id = $conn->insert_id;
		 $eve1 = "INSERT INTO res_contact_master (contactname, contactemail,contactnumber,cmobilenumber,contactdepartmnet,cposition,caltenateemail,caddress1,caddress2,ccountry,cstate,ccity,cpostalcode,cmiddlename,csurname,cmsgappname,cmsgappid,cvideoappname,cvideoappid,csubcategory,clinktype,clinkid) VALUES('$contactname','$contactemail','$contactnumber','$cmobilenumber','$contactdepartmnet','$cposition','$caltenateemail','$caddress1','$caddress2','$ccountry','$cstate','$ccity','$cpostalcode','$cmiddlename','$csurname','$cmsgappname','$cmsgappid','$cvideoappname','$cvideoappid','$csubcategory','Campus','$last_id')";
		
		$re1 = mysqli_query($conn, $eve1);
			
		$_SESSION['msg'] = "Record Saved successfully";
	}

			

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

	header('Location: campus.php');

	}
	}
	}
	header('Location: campus.php');

?>