<?php
session_start();

include "../functions.php";
checkAdmin(session_id());

require("fileupload-class.php");
$path = "files/";
$upload_file_name = "userfile";
$acceptable_file_types = "image/gif|image/jpeg|image/pjpeg";
$default_extension = "";
$mode = 2;

$submit = $_POST["submit"];

$id = $_POST["id"];

if (isset($submit))
{
	$mediaName = $_POST["mediaName"];
	$mediaDescrip = $_POST["mediaDescrip"];
	$categoryID = $_POST["categoryID"];
	$mediaURL = $_POST["mediaURL"];

	if (!isset($mediaName))
	{
		$err = "you did not specify a Name";
	}
	elseif(!isset($mediaDescrip))
	{
		$err = "you did not specify a  Description";
	}
	else
	{
		$my_uploader = new uploader('en');
		$my_uploader->max_filesize(100000);
		$my_uploader->max_image_size(800, 800);
		if ($my_uploader->upload($upload_file_name, $acceptable_file_types, $default_extension)) 
		{
			$my_uploader->save_file($path, $mode);
		}
		
		if ($my_uploader->error && $my_uploader->error != 'No file was uploaded') 
		{
			$err .= $my_uploader->error;
			//echo $err;
		}
		
		if (! $err)
		{	
			$query = "update media_media set mediaName='$mediaName', mediaDescrip='$mediaDescrip', categoryID='$categoryID', lastdate=now() where mediaID='$id'";
			//echo $query;
			mysql_query($query) or die (mysql_error());
			
			if (! $my_uploader->error)
			{
				$query = "select mediaURL from media_media where mediaID='$id'";
				$result = mysql_query($query) or die (mysql_error());
				if ($row = mysql_fetch_array($result))
				{
					$oldURL = $row[0];
					
				}
				
				$mediaURL = $path . $my_uploader->file['name'];
				
				$query = "update media_media set mediaURL = '$mediaURL' where mediaID='$id'";
				mysql_query($query) or die (mysql_error());
				
				@unlink($oldURL);
			}
			
			header ("location: press.php");
		}
		
	}
}
else
{
	include "header.php";
	$query = "select mediaName, categoryID, mediaDescrip, mediaURL from media_media where mediaID='$id'";
	$result = mysql_query($query);
	
	$row = mysql_fetch_array($result);
	
?>

<center>

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
<table width="80%" border="0">
  <tr>
    <td colspan="2" align="center">only upload files if you want to change the file</td>
  </tr>
  
  <tr><td>&nbsp;</td></tr>
  
  <tr>
    <td align="right">Name:</td>
    <td align="left"><input type="text" name="mediaName" size=30 value="<?php echo $row[0]?>"></td>
  </tr>
  
  <?php
  /*
  <tr>
    <td align="right">
    Category Name:
    </td>
    
    <td align="left">
    <?php
    $sql = "select categoryID, categoryName from media_category";
    $result2 = mysql_query($sql);
        
    echo "<select name=categoryID>";
    echo "<option value=0>Default Category</option>";
    
    while ($row2 = mysql_fetch_array($result2))
    {
    	if ($row2[0] == $row[1])
    	{
    		echo "<option value=$row2[0] selected>$row2[1]</option>";
    	}
    	else
    	{
    		echo "<option value=$row2[0]>$row2[1]</option>";
    	}
    }
    echo "</select>";
    ?>
    </td>
  </tr>
  */
  ?>
  <tr>
    <td align="right" valign="top">Description:</td>
    <td align="left"><textarea name="mediaDescrip" rows=5 cols=30 wrap="virtual"><?php echo $row[2];?></textarea></td>
  </tr>
  
  <tr>
    <td align="right">File:</td>
    <td align="left"><input type="file" name="<?php echo $upload_file_name; ?>" size="30"></td>
  </tr>
  
  <tr>
    <td colspan=2 align="center"><input type="hidden" name="submit" value="1"><input type="hidden" name="id" value=<?php echo $id;?>><input type="submit" value="Modify">&nbsp;<input type="reset" name="reset" value="Cancel"></td>
  </tr>
  
</table>

</form>

</center>

<?php
}
include "footer.php";
?>