<?
include "header.php";

if(isset($_POST['task'])) { $task = $_POST['task']; } else { $task = "moveimg"; }
if(isset($_POST['i_id'])) { $i_id = $_POST['i_id']; } elseif(isset($_GET['i_id'])) { $i_id = $_GET['i_id']; } else { exit(); }
if(isset($_POST['f_id'])) { $f_id = $_POST['f_id']; } elseif(isset($_GET['f_id'])) { $f_id = $_GET['f_id']; } else { exit(); }
$image_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_image WHERE u_id='$user_info[u_id]' AND i_id='$i_id'"));
$folder_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_folders WHERE u_id='$user_info[u_id]' AND f_id='$f_id'"));
if($image_info[f_id] != $f_id) { exit(); }


if($task == "moveimg_do") {
$page = $_POST['page'];
$newf_id = $_POST['newf_id'];
$newfolder_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_folders WHERE u_id='$user_info[u_id]' AND f_id='$newf_id'"));

$images_inside = mysql_num_rows(mysql_query("SELECT * FROM ims_image WHERE u_id='$user_info[u_id]' AND f_id='$f_id'"));

if($images_inside == 100) {
echo $header;
echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<form action='move_images.php?f_id=$f_id&i_id=$i_id' method='POST'>
<td style='padding-bottom: 5px;'>
<b>An Error Has Occurred:</b><br><font class='csmall'>
The image could not be moved because the destination folder already contains the maximum allowed amount of images per folder (100).<br>
<br></font>
<input type='submit' class='button' value='Try Again'>
</td>
</tr>
</form>
</table>";
echo $footer;
exit;
}

copy("./u/$user_info[u_id]/$folder_info[f_id]/".$image_info[i_id].".".$image_info[ext], "./u/$user_info[u_id]/$newfolder_info[f_id]/".$image_info[i_id].".".$image_info[ext]);
unlink("./u/$user_info[u_id]/$folder_info[f_id]/".$image_info[i_id].".".$image_info[ext]);
copy("./u/$user_info[u_id]/$folder_info[f_id]/thumbs/$image_info[thumb]", "./u/$user_info[u_id]/$newfolder_info[f_id]/thumbs/$image_info[thumb]");
unlink("./u/$user_info[u_id]/$folder_info[f_id]/thumbs/$image_info[thumb]");
mysql_query("UPDATE ims_image SET f_id='$newfolder_info[f_id]' WHERE u_id='$user_info[u_id]' AND f_id='$f_id' AND i_id='$i_id'");
header("Location: view_folder.php?f_id=$f_id&page=$page");
exit;
}






if($task == "moveimg") {
echo $header;
$getfolders = mysql_query("SELECT * FROM ims_folders WHERE u_id = '$user_info[u_id]' AND f_id <> '$folder_info[f_id]' ORDER BY name");
if(mysql_num_rows($getfolders) < "1") { $nofolders = "yes"; }

if($nofolders == "yes") {
echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<td style='padding-bottom: 5px;'>
<b>Move Image Error</b><br>
You cannot move this image because there are no available destination folders.<br>
Please <a href='create_folder.php'>create another folder</a> and try again.
</td>
</tr>
</table>

<br>

<table cellpadding='0' cellspacing='0'>
<form action='view_folder.php?f_id=$f_id' method='POST'>
<tr>
<td>
<input type='submit' class='button' value='Cancel'>
</td>
</tr>
</form>
</table>
";

} else {

echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<td style='padding-bottom: 5px;'>
<b>Move Image: \"$image_info[title]\"</b><br>
Please choose a new folder for this image below.</font>
</td>
</tr>
</table>

<br>

<table width='520' cellpadding='0' cellspacing='0'>
<form action='move_images.php' method='post'> 
<tr>
<td style='padding-bottom: 5px;'>
<b>Move from&nbsp;
<select class='text'>
<option>$folder_info[name]</option>
</select> to 
<select class='text' name='newf_id'>
";

while($newfolder_info = mysql_fetch_assoc($getfolders)) {
echo "<option value='$newfolder_info[f_id]'>$newfolder_info[name]</option>";
}

echo "
</select>
<br><br>
<input type='hidden' name='task' value='moveimg_do'>
<input type='submit' class='button' value='Move Image'>
<input type='button' class='button' value='Cancel' onClick=\"gofolder('$f_id&page=$page')\">
<input type='hidden' name='f_id' value='$f_id'>
<input type='hidden' name='i_id' value='$i_id'>
<input type='hidden' name='page' value='$page'>
</td>
</tr>
</form>
</table>";
}

echo $footer;
}


?>