<?php
	error_reporting(E_ERROR | E_PARSE);
	include('includes/config.php'); 
	include('includes/function.php'); 
	$chmod=0777;
	
	if($_POST['category_file'] || $_POST['news_file'] || $_POST['szkolenia_file']){
		$uploadFile=true;
	}
		
	$fileTemp = $_FILES['Filedata']['tmp_name'];
	$fileName = $_FILES['Filedata']['name'];
	$fileSize = $_FILES['Filedata']['size'];   	
	$a=explode(".",$fileName);		
	$fileNameNew = trim(cleanString($a[0]));	
	if(!$uploadFile){
		$fileNameNew=$fileNameNew.uniqid();	
	}
	$fileNameNew=$fileNameNew.".".$a[1];
	


	if($_POST['news'])	{ 
		$itemID=$_POST['news'];
		$itemName='news';
		$mainDir='../media/photos/'.$itemName;
		DeleteDir($mainDir.'/'.$itemID);
		$uploadDir=$mainDir.'/'.$itemID;
	}
	
	
	if($_POST['pages'])	{ 
		$itemID=$_POST['pages'];
		$itemName='pages';
		$mainDir='../media/photos/'.$itemName;
		DeleteDir($mainDir.'/'.$itemID);
		$uploadDir=$mainDir.'/'.$itemID;
	}	
	
	if($_POST['pagesMenu'])	{ 
		$itemID=$_POST['pagesMenu'];
		$itemName='pagesMenu';
		$mainDir='../media/photos/'.$itemName;
		DeleteDir($mainDir.'/'.$itemID);
		$uploadDir=$mainDir.'/'.$itemID;
	}		
	

	if($_POST['slider'])	{ 
		$itemID=$_POST['slider'];
		$itemName='slider';
		$mainDir='../media/photos/'.$itemName;
		DeleteDir($mainDir.'/'.$itemID);
		$uploadDir=$mainDir.'/'.$itemID;
	}
	
	if($_POST['partners'])	{ 
		$itemID=$_POST['partners'];
		$itemName='partners';
		$mainDir='../media/photos/'.$itemName;
		DeleteDir($mainDir.'/'.$itemID);
		$uploadDir=$mainDir.'/'.$itemID;
	}
		
		
	if($_POST['pages_gallery'])	{ 
		$itemID=$_POST['pages_gallery'];
		$itemName='pages_gallery';
		$mainDir='../media/photos/'.$itemName;	
		$uploadDir=$mainDir.'/'.$itemID;
	}	
	if($_POST['lang'])	{ 
		$itemID=$_POST['lang'];	
		$itemName='lang';
		$mainDir='../media/photos/'.$itemName;
		DeleteDir($mainDir.'/'.$itemID);
		$uploadDir=$mainDir.'/'.$itemID;
	}	
	if($_POST['gallery'])	{ 
		$itemID=$_POST['gallery'];
		$itemName='gallery';
		$mainDir='../media/photos/'.$itemName;
		DeleteDir($mainDir.'/'.$itemID);
		$uploadDir=$mainDir.'/'.$itemID;
	}
	if($_POST['images'])	{ 
		$itemID=$_POST['images'];
		$itemName='images';
		$mainDir='../media/photos/'.$itemName;
		$uploadDir=$mainDir.'/'.$itemID;
	}
	if($_POST['pytania'])	{ 
		$itemID=$_POST['pytania'];
		$itemName='pytania';
		$mainDir='../media/photos/'.$itemName;
		$uploadDir=$mainDir.'/'.$itemID;
	}		
		
			
	
	//files	
	if($_POST['category_file'])	{ 
		$itemID=$_POST['category_file'];	
		$itemName='category';
		$mainDir='../media/files/'.$itemName;
		$uploadDir=$mainDir.'/'.$itemID;
	}
	if($_POST['news_file'])	{ 
		$itemID=$_POST['news_file'];	
		$itemName='news';
		$mainDir='../media/files/'.$itemName;
		$uploadDir=$mainDir.'/'.$itemID;
	}
	if($_POST['szkolenia_file'])	{ 
		$itemID=$_POST['szkolenia_file'];	
		$itemName='szkolenia';
		$mainDir='../media/files/'.$itemName;
		$uploadDir=$mainDir.'/'.$itemID;
	}	
	
	//eof files		
	
	
	if(!is_dir($mainDir))
	{
		mkdir($mainDir, $chmod);
		chmod($mainDir, $chmod);
	}	

	if($uploadFile)
	{ 	
		if(!is_dir($uploadDir))
		{
			mkdir($uploadDir, $chmod);
			chmod($uploadDir, $chmod);
		}
		move_uploaded_file($fileTemp, "{$uploadDir}/$fileNameNew");	
	}
	else 
	{	
		if(is_uploaded_file($fileTemp))
		{
		
			if(!is_dir($uploadDir) || !is_dir($uploadDir."/medium") )
			{
				mkdir($uploadDir, $chmod);
				chmod($uploadDir, $chmod);
		
				mkdir($uploadDir."/min", $chmod);
				chmod($uploadDir."/min", $chmod);
		
				mkdir($uploadDir."/medium", $chmod);
				chmod($uploadDir."/medium", $chmod);						
		
			}
		}
		
		move_uploaded_file($fileTemp, "{$uploadDir}/medium/$fileNameNew");	
	}
	

		
	
	$file = $_SERVER['DOCUMENT_ROOT']."/media/files/$itemName/$itemID/".$fileNameNew;	
	$size = filesize($file); 
	$newSize = FileSizeConvert($size);		
	
	
	if($_POST['category_file']) {     		
		$type=rozszerzenie_pliku($fileNameNew);
		mysql_query("INSERT INTO `files` (`id`, `title`, `file`, `type`, `parent_id`, `parent`, `position`,`size`,`date_added`) VALUES ('', '', '$fileNameNew', '$type', '$itemID', '$itemName', '','$newSize',NOW())");				
	} 
		
	if($_POST['news_file']) {     		
		$type=rozszerzenie_pliku($fileNameNew);
		mysql_query("INSERT INTO `files` (`id`, `title`, `file`, `type`, `parent_id`, `parent`, `position`) VALUES ('', '', '$fileNameNew', '$type', '$itemID', '$itemName', '')");				
	} 	
	if($_POST['szkolenia_file']) {     		
		$type=rozszerzenie_pliku($fileNameNew);
		mysql_query("INSERT INTO `files` (`id`, `title`, `file`, `type`, `parent_id`, `parent`, `position`) VALUES ('', '', '$fileNameNew', '$type', '$itemID', '$itemName', '')");				
	} 			
		
	
	        
?>  