Location: PHPKode > projects > ERPSOD > erpsod/load_tasks.php
<?PHP
include_once("config.php");

// Are you authorised?
$ok=role_check('Admin');
if (!$ok){
	$fout = 'No Access for chosen option';
	header("Location: login.php?error=$fout"); 
}

$form1=$_POST["form"];
$action=$_POST["command"];
if ($form1=="load_tasks"){
	if ($action == "Cancel") {
		header('Location: copyright.php');
	}
	
	$f=&$_FILES['csv_file'];
	$filelocal = $f['tmp_name'] ;
	$serverfile = _DIR_HOME . _DIR_UPLOAD . safe_filename($f['name']);
	
	// Do we have the upload directory?
	if (!is_dir(_DIR_HOME . _DIR_UPLOAD)) {
		//  Let's try to create it.
		if (!@mkdir(_DIR_HOME . _DIR_UPLOAD)) {
			//  If directory can't be made Set error message and output
			echo $serverfile;
			die(" problems creating upload directory, please verify installation");	
		}
	}
	if (!is_file($filelocal)) {
		echo $filelocal;
		die(" file does not exist");
	}
	// delete existing copy
	if (is_file($serverfile)) {
		unlink($serverfile);
	}	
	if (!copy($filelocal, $serverfile)) {
		//  Set error message and output
		die("cannot copy file to server, check installation");
	}

	// clean the table
	$sql="truncate table erptask";
	$result = mysql_query($sql);
	// load the fresh data
	$sql  ="LOAD DATA LOCAL INFILE '";
	$sql .= $serverfile;
	$sql .= "' INTO TABLE tasktemp FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' (taskname, taskdesc,taskname2)"; 
	$result = mysql_query($sql);
	?>
	Task Data has been loaded, now converting dataset........
	<?PHP
	// use the temp table to fill the relational table
	$sql1= "select * from tasktemp";
	$result1 = mysql_query($sql1);
	while ($row1 = mysql_fetch_array($result1, MYSQL_NUM)) {
		// first check if reference task exist, if not add with as reftask itself
		$taskname	= trim($row1[0]);
		$taskdesc	= trim($row1[1]);
		$taskname2	= trim($row1[2]);
		$sql2= "select taskid from erptask where taskname='$taskname2'";
		$result2 = mysql_query($sql2);
		$tasktel= mysql_num_rows($result2);
		if ($tasktel < 1){
			$sql3="insert into erptask (taskname, taskdesc) values('$taskname2','$taskdesc')";
			$result3 = mysql_query($sql3);
			$reftaskid = mysql_insert_id(); 
			// now update reftaskid
			$sql4 = "update erptask set reftaskid=$reftaskid where taskid=$reftaskid";
			$result4 = mysql_query($sql4);
		} else {
			$row2 = mysql_fetch_array($result2, MYSQL_NUM);
			$reftaskid=$row2[0];
		}
		// next check if taskname exists, if not add
		$sql5= "select taskid from erptask where taskname='$taskname'";
		$result5 = mysql_query($sql5);
		$tasktel= mysql_num_rows($result5);
		if ($tasktel < 1){
			$sql6="insert into erptask (taskname, taskdesc,reftaskid) values('$taskname','$taskdesc',$reftaskid)";
			$result6 = mysql_query($sql6);
		}
	}
	?>
	Tasks have been converted and fully loaded!
	<?PHP
}
?>
<HTML>
<HEAD>
<link rel="stylesheet" type="text/css" href="<?PHP echo $style ?>">
<script language="JavaScript1.2" src="js/coolmenus4.js">
</script>
</HEAD>
<body bgcolor="#ffffff">
<script language="JavaScript1.2" src="js/erpsod.js">
</script><br><br>
<center>
<h3>Loading Tasks</h3>
<form enctype="multipart/form-data" method="post" >
<input type="hidden" name="form" value="load_tasks" >
<table border=1 cellpadding=0 cellspacing=0 bgcolor="AQUA"><tr><td>
<tr><td align="center"><b>Select CSV file to reload tasks from</b></td></tr>
<tr><td>
<input type="file" name="csv_file" size="50" />
</td></td><td></tr>
<tr><td align="center">
<div>
<INPUT TYPE="SUBMIT" NAME="command" VALUE="Load" style="width: 80px;"   ONCLICK="return confirm('Replace Current Dataset?')">
<INPUT TYPE="SUBMIT" NAME="command" VALUE="Cancel" style="width: 80px;"><br>
</div>
</td></tr>
</table>
</center>
</FORM>
</BODY>
<?php
include_once("footer.php");
?>
</HTML>
Return current item: ERPSOD