<?php
	require_once(__DIR__.'/../includes/sysinit.php');
	require_once('upload_func.php');
	//include_once(SABLE_INCLUDES_PATH.'list_header.php');	
	
	function checkFileNameExists($filePath, $fileName)
	{
		$count = 0;
		$fullFileName = $filePath.$fileName;
		SableGeneralFunctions::replaceSpaces($fullFileName, '_');
		SableSystem::convertURLPathToWorkPath($fullFileName);
		//SableDebug::debugx($fullFileName);
		while(file_exists($fullFileName))
		{
			$count++;
			$fullFileName = $filePath.$count.'_'.$fileName;
			SableSystem::convertURLPathToWorkPath($fullFileName);
		}
		return trim($fullFileName);
	}
	
	function processSingleFile($filePath)
	{				
		if (isset($_FILES['edtFileName']['name']))
		{
			$ext		= explode(".",$_FILES['edtFileName']['name']);
			$tmp_name 	= $_FILES['edtFileName']['tmp_name'];
			$target_file = checkFileNameExists($filePath, $_FILES['edtFileName']['name']);
			//SableGeneralFunctions::replaceSpaces($target_file, '_');
			//SableSystem::convertURLPathToWorkPath($target_file);			
			if (!move_uploaded_file($tmp_name, $target_file)) echo('file transfer failed: '.$target_file);
			SableSystem::convertWorkPathToURLPath($target_file);
		}		
		return "'".$_POST["hddnField"]."', '".$target_file."', '" . $_POST["hddnImgField"]."', ". $_POST["hddnFileType"];
		//return the file name to the field...
	}
	
	function processMultiFiles($filePath)
	{
		$images_arr = array();
		foreach($_FILES['edtFileName']['name'] as $key=>$val)
		{
			$image_name = $_FILES['edtFileName']['name'][$key];
			$tmp_name 	= $_FILES['edtFileName']['tmp_name'][$key];
			$size 		= $_FILES['edtFileName']['size'][$key];
			$type 		= $_FILES['edtFileName']['type'][$key];
			$error 		= $_FILES['edtFileName']['error'][$key];
			
			//SableGeneralFunctions::replaceSpaces($image_name, '_');
			//$target_file = $filePath.$image_name;
			$target_file = checkFileNameExists($filePath, $image_name);
			//SableSystem::convertURLPathToWorkPath($target_file);	
			if (move_uploaded_file($tmp_name, $target_file)) 
			{
				SableSystem::convertWorkPathToURLPath($target_file);
				$images_arr[] = $target_file; 
			}
			else 
			{
				SableSystem::convertWorkPathToURLPath($target_file);
				echo('file transfer failed: '.$target_file);
			}
			
		}
		//send the first file
		$result = "'".$_POST["hddnField"]."', ".json_encode($images_arr).", '" . $_POST["hddnImgField"]."', ". $_POST["hddnFileType"];
		return $result;
	}/**/
	//Check if any files have been uploaded, and if so then process:

//	if (!empty($_POST['file'])) $_POST['edtFileName'] = $_POST['file']; //for dropzone.js which uses "file" for field name.
	switch ($_POST["hddnFileType"])
	{
		case("1"):{$filePath = SABLE_WORKING_PATH.'media/images/'; 			break;} //images	
		case("2"):{$filePath = SABLE_WORKING_PATH.'media/images/stock/'; 	break;} //stock
		case("3"):{$filePath = checkAccessLevel('images/profile/');			break;} //profile
		case("4"):{$filePath = checkAccessLevel('images/gallery/');			break;} //gallery
		case("5"):{$filePath = SABLE_WORKING_PATH.'media/images/blog/'; 	break;} //blog
		case("6"):{$filePath = SABLE_WORKING_PATH.'media/images/filetypes/';break;} //filetypes	
		case("7"):{$filePath = checkAccessLevel('images/inspection/');		break;} //inspection
			
		case("10"):{$filePath = SABLE_WORKING_PATH.'media/videos/';			break;}	//videos
		case("20"):{$filePath = checkAccessLevel('downloads/');				break;} //downloads/documents
		case("30"):{$filePath = SABLE_WORKING_PATH.'media/audio/'; 			break;} //audio

		default	 :{$filePath = SABLE_WORKING_PATH.'media/images/profile';	break;} //user profile
	}
	
	$params = ($_POST['hddnMulti']==="true")?processMultiFiles($filePath):processSingleFile($filePath); //in POST boolean is a string.
	//SableDebug::debugoutx($params);
	if(SABLE_SITE_DEBUGGING) echo(PHP_EOL.'<script src="'.SABLE_URL_ROOT.'scripts/debug.js"></script>'.PHP_EOL);
	//include_once(SABLE_INCLUDES_PATH.'list_header.php');
?>
<script src="<?php echo(SABLE_URL_ROOT); ?>scripts/jquery-2.1.4.min.js"></script>
<script src="<?php echo(SABLE_URL_ROOT); ?>scripts/sable.js"></script>

<script>insertUploadFileName(<?php echo($params); ?>);</script>