<?php

$DBH = createGLSEdit();

$targetdir = '/home/jjglover/goallinestats';
if (!empty($_GET)) {
	$targetid = $_GET['playerID'];
	$targetclass = $_GET['playerClass'];
} else if (!empty($_POST)) {
	$targetid = $_POST['playerID'];
	$targetclass = $_POST['playerClass'];
	print_r($targetclass.'-'.$targetid);
}

//$targetdir = 'D:/JJCodeDepot/Goallinestats/public/playerimages/';

if (!empty($_GET) && $_GET['step'] == 'PlayerEdit-4') {

	$findImage = $DBH->prepare('SELECT imagelocation FROM cssplayerassignment WHERE PlayeridRef = :idplayer AND PlayertableRef = :profiletype LIMIT 1');
	$findImage->bindParam(':idplayer', $targetid, PDO::PARAM_INT);
	$findImage->bindParam(':profiletype', $targetclass, PDO::PARAM_STR);
	$findImage->execute();
	$row = $findImage->fetch(PDO::FETCH_ASSOC);

	$removeImage = $DBH->prepare('UPDATE cssplayerassignment SET imagelocation = null WHERE PlayeridRef = :idassigned AND PlayertableRef = :profiletype');
	$removeImage->bindParam(':idassigned', $targetid, PDO::PARAM_INT);
	$removeImage->bindParam(':profiletype', $targetclass, PDO::PARAM_STR);
	$removeImage->execute();

	unlink($targetdir.$row['imagelocation']);
	echo "Picture Deleted!";
	exit();
} else {

	$imageFileName = $_FILES['image']['name'];
	$imageFileType = $_FILES['image']['type'];
	$imageFileSize = $_FILES['image']['size'];
	$sqlFileName = '/userAssets/playerimages/'.$targetclass.'-'.$targetid.'/'.$imageFileName;
	$uploadOk = 1;

	if (empty($targetclass)) {
		echo "Please choose where you would like to submit this picture!";
		exit();
	} elseif (empty($_FILES)) {
		echo "Please choose a picture to upload!";
		exit();
	} else {
		if($imageFileType != "image/jpg" && $imageFileType != "image/png" && $imageFileType != "image/jpeg" && $imageFileType != "image/gif" ) {
			echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
			#echo "Result Extension: ".$imageFileType;
			$uploadOk = 0;
		}
		if($imageFileSize == 0) {
			$uploadOk = 0;
			echo "No files were submitted!";
		}
		
		$findImage = $DBH->prepare('SELECT idPlayerCSS, imagelocation FROM cssplayerassignment WHERE PlayeridRef = :idplayer AND PlayertableRef = :profiletype LIMIT 1');
		$findImage->bindParam(':idplayer', $targetid, PDO::PARAM_INT);
		$findImage->bindParam(':profiletype', $targetclass, PDO::PARAM_STR);
		$findImage->execute();
		$pictureexist = $findImage->rowCount();
		$picturedetail = $findImage->fetch(PDO::FETCH_ASSOC);

		if ($pictureexist > 0) {
			$updatePlayerImageInsert = $DBH->prepare('UPDATE cssplayerassignment SET imagelocation = :filename WHERE PlayeridRef = :idassigned AND PlayertableRef = :profiletype');
			$updatePlayerImageInsert->bindParam(':idassigned', $targetid, PDO::PARAM_INT);
			$updatePlayerImageInsert->bindParam(':profiletype', $targetclass, PDO::PARAM_STR);
			$updatePlayerImageInsert->bindParam(':filename', $sqlFileName, PDO::PARAM_STR);
			$updatePlayerImageInsert->execute();
		} else {
			$prepPlayerImageInsert = $DBH->prepare('INSERT INTO cssplayerassignment (PlayeridRef, PlayertableRef, imagelocation) VALUES (:idassigned, :profiletype, :filename)');
			$prepPlayerImageInsert->bindParam(':idassigned', $targetid, PDO::PARAM_INT);
			$prepPlayerImageInsert->bindParam(':profiletype', $targetclass, PDO::PARAM_STR);
			$prepPlayerImageInsert->bindParam(':filename', $sqlFileName, PDO::PARAM_STR);
			$prepPlayerImageInsert->execute();
		}
		
		if ($uploadOk == 0) {
			echo "Sorry, your file was not uploaded.";
		// if everything is ok, try to upload file
		} else {
			if (!is_dir($targetdir.'/'.$targetclass.'-'.$targetid.'/')) {
				mkdir($targetdir.'/userAssets/playerimages/'.$targetclass.'-'.$targetid.'/');
			}

			if (!empty($picturedetail['imagelocation'])) {			
				unlink($targetdir.$picturedetail['imagelocation']);
			}
			if (move_uploaded_file($_FILES["image"]["tmp_name"], $targetdir.$sqlFileName)) {
				echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded. ";
			} else {
				echo "Sorry, there was an error uploading your file.";
			}
		}
	}
}
?>