<?php 

	if (!isset($_SESSION['FirstName'])) {
		session_start();
	}

	include ('includes/db1config.php');
	include ('../includes/mysql_connect.php');
	include ('includes/admin-config.php');	//get site options


	$query = "SELECT * from marketing_materials where recordkey = ".$_POST['recordKey'];
	$rt    = mysql_query($query); // query executed
	echo mysql_error();           // if any error is there that will be printed to the screen
	$row   = mysql_fetch_array($rt);

	if( explode('.',$row['file_name'] )[0]  !=  $_POST['file_name'] ) {// Name has been Changed

		if( isset($_FILES) && isset($_FILES['thumbnail']['name']) && $_FILES['thumbnail']['name'] != '' ){
			$thumbnail = uploadThumbnail();
		} else {
			$thumbnail = $row['thumbnail'];
		}

		$ext     = explode('.',$row['file_name'] )[1];
		$oldname = explode('.',$row['file_name'] )[0];
		$newname = $_POST['file_name'];
		$res     = changeImageName($ext,$oldname,$newname);


		$query = "UPDATE marketing_materials SET 
			description = '".mysql_escape_string($_POST['description'])."',
			detail      = '".mysql_escape_string($_POST['detail'])."',
			file_name   = '".mysql_escape_string($res)."',
			thumbnail   = '".mysql_escape_string($thumbnail)."',
			status      = '".mysql_escape_string($_POST['status'])."'
		WHERE recordkey = ".$_POST['recordKey'];
		mysql_query($query);   // Query executed
		echo mysql_error();
		echo "<center><h2>Record Successfully Updated</h2></center>";
		echo "<script> setTimeout(function(){ parent.location.reload(true); }, 1000);</script>";

	
	} else {

		if( isset($_FILES) && isset($_FILES['thumbnail']['name']) && $_FILES['thumbnail']['name'] != '' ){
			$thumbnail = uploadThumbnail();
		} else {
			$thumbnail = $row['thumbnail'];
		}

		$query = "UPDATE marketing_materials SET 
			description = '".mysql_escape_string($_POST['description'])."',
			detail      = '".mysql_escape_string($_POST['detail'])."',
			file_name   = '".mysql_escape_string($row['file_name'])."',
			thumbnail   = '".mysql_escape_string($thumbnail)."',
			status      = '".mysql_escape_string($_POST['status'])."'
		WHERE recordkey = ".$_POST['recordKey'];
		mysql_query($query);   // Query executed
		echo mysql_error();

		echo "<center><h2>Record Successfully Updated</h2></center>";
		echo "<script> setTimeout(function(){ parent.location.reload(true); }, 1000);</script>";

	}



	function uploadThumbnail(){
		include('includes/smart_resize_image.php');

	    $dir           = realpath('../custom/marketing/');
	    if (!file_exists($dir) && !is_dir($dir)) {
	        mkdir($dir);
	    } 

	    $imageName     = '';
	    $filenameParts = pathinfo($_FILES['thumbnail']['name']);
	    $filename      = strtolower(preg_replace("/[^a-zA-Z0-9]+/", "", $filenameParts['filename']));
	    $ext           = strtolower($filenameParts['extension']);
	    $newFilename   = $filename.'.'.$ext;
	    $thumbName     = $filename.'_thumb'.'.'.$ext;
	    $tmp           = $_FILES['thumbnail']['tmp_name'];
	    $i             = time();

	    while (file_exists($dir.'/'.$newFilename)) {
	        $newFilename = $filename.'-'.$i.'.'.$ext;
	    }

	    $dest   = $dir.'/'.$thumbName;
	    $dest1  = $dir.'/'.$thumbName;
	    
	    if ( move_uploaded_file($tmp,$dest) ) {
	        
	    	if( $ext != 'pdf' ){
		        copy($dest, $dest1);
		        list($width, $height) = getimagesize($dest1);
		        if ($width > 200 || $height > 200) {
		            if ($width >= $height) {
		                smart_resize_image($dest1, 200, 0, true, $dest1);
		            } else{
		                smart_resize_image($dest1, 0, 200, true, $dest1);
		            }
		        }
		        copy($dest1, $dir.'/'.$thumbName);
	    	} else {
	    		
	    		$thumbName     = '';
	    	}	

	    }

	    return $thumbName;
	}


	function changeImageName($ext,$oldname,$newname) {
		    
	    include('includes/smart_resize_image.php');

	    $dir           = realpath('../custom/marketing/');
	    $oldname       = $oldname.'.'.$ext;
		$newname       = $newname.'.'.$ext;
	    $dest          = $dir.'/'.$oldname;
	    $dest1         = $dir.'/'.$newname;
	    
    	if( $ext != 'pdf' ){
	        copy($dest, $dest1);
	        unlink($dest);
    	} 	

	    return $newname;
	}


?>