include_once "includes/dbConnNew.php";
include_once "includes/loggedOnPop.php";
$recid = $_REQUEST["recid"];
if( isset($_REQUEST["recid"]) && $_REQUEST["theAction"] != "update"){
$sqlrp = "select * from recipes where (ID='$recid')";
$resultrp = mysql_query($sqlrp) or die(mysql_error());
if($myrowrp=MySQL_fetch_array($resultrp)){
$RID = $myrowrp["ID"] ;
} else {
header("Location: myCookbook.php");
exit ;
}
}
if( $_REQUEST['theAction'] == "update" && $_REQUEST["recid"] > " "){
$sql5 = "Update recipes Set Brief='" . $_REQUEST['Brief'] . "', Recipe_Name ='" . ereg_replace("'","´",$_REQUEST['Recipe_Name']) . "', Prep_Time_Hours = '" . ereg_replace("'","´",$_REQUEST['Prep_Time_Hours']) . "', Prep_Time_Minutes = '" . ereg_replace("'","´",$_REQUEST['Prep_Time_Minutes']) . "', Total_Time_Hours = '" . ereg_replace("'","´",$_REQUEST['Total_Time_Hours']) . "', Total_Time_Minutes = '" . ereg_replace("'","´",$_REQUEST['Total_Time_Minutes']) . "', Serving_Size = '" . ereg_replace("'","´",$_REQUEST['Serving_Size']) . "' , Category = '" . $_REQUEST['Category'] . "', Instructions = '" . ereg_replace("'","´",$_REQUEST['Instructions']) . "', Share = '" . $_REQUEST['Share'] . "' where ID = '" . $_REQUEST["recid"]. "' and User_ID = '" . $_SESSION["sessionUserID"] . "'" ;
$result5 = mysql_query($sql5) or die(mysql_error());
$recipeId = $recid;
$sql2 = "Delete from recipes_recipes_sub_cat where (recipe_ID = '$recid' and user_ID = '" . $_SESSION["sessionUserID"] . "')";
$result2 = mysql_query($sql2) or die(mysql_error());
//update sub cats
$count = count( $_REQUEST["subCatId"] );
$sessUserID = $_SESSION['sessionUserID'];
for ($i=0; $i<$count; $i++) {
//insert into ingredients table
if($_REQUEST["subCatId"][$i] > " "){
$subCatID = $_REQUEST["subCatId"][$i];
$sqlR = "INSERT INTO recipes_recipes_sub_cat (recipe_ID, recipe_sub_cat_ID, user_ID ) VALUES ($recipeId, $subCatID, $sessUserID)";
$resultR = mysql_query($sqlR) or die(mysql_error());
};
};
// end of subcats
$sql3 = "select ID from recipes_ingredients where (Recipe_ID='$recid')";
$result3 = mysql_query($sql3) or die(mysql_error());
while($myrow3=MySQL_fetch_array($result3)){
$IngID = $myrow3["ID"] ;
$sql3 = "delete from recipes_ingredients where (ID = '$IngID')";
$resultR = mysql_query($sql3) or die(mysql_error());
}
//insert ingredients
$count2 = count( $_REQUEST["Ingredient"] );
for ($i=0; $i<$count2; $i++) {
//insert into ingredients table
if($_REQUEST["Ingredient"][$i] > " "){
$InsertStr = "INSERT INTO recipes_ingredients (Recipe_ID, Ingredient, Amount ) VALUES ('$recipeId', '" . $_REQUEST["Ingredient"][$i] . "', '" . $_REQUEST["Amount"][$i] . "')";
mysql_query($InsertStr);
};
};
//***************************************************
//insert recipe photo
//***************************************************
if( $_FILES['Photo']['name'] > " ")
{
//GET FILE NAME TO DELETE
$sql = "SELECT Photo FROM recipes WHERE ID = '$recipeId'";
$sth = mysql_query($sql) or die(mysql_error());
$myrow=mysql_fetch_array($sth);
if ($myrow["Photo"])
{
unlink("recipePhotos/" . $myrow["Photo"]);
unlink("recipePhotos/thumbs/" . $myrow["Photo"]);
}
//make sure this directory is writable!
$path_thumbs = "recipePhotos/";
//the new width of the resized image, in pixels.
$img_thumb_width = 362; //
$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
//List of allowed extensions if extlimit = yes
$limitedext = array(".gif",".jpg",".png",".jpeg");
//the image -> variables
$file_type = $_FILES['Photo']['type'];
$file_name = $_FILES['Photo']['name'];
$file_size = $_FILES['Photo']['size'];
$file_tmp = $_FILES['Photo']['tmp_name'];
//check if you have selected a file.
if(!is_uploaded_file($file_tmp)){
echo "Error: Please select a file to upload!.
--back";
exit(); //exit the script and don't process the rest of it!
}
//check the file's extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
//uh-oh! the file extension is not allowed!
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension.
--back";
exit();
}
//so, whats the file's extension?
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];
//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
$rand_name = $recipeId . "_" . $_SESSION['sessionUserID']."_".time(); ;
//the new width variable
$ThumbWidth = $img_thumb_width;
/////////////////////////////////
// CREATE THE THUMBNAIL //
////////////////////////////////
//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list the width and height and keep the height ratio.
list($width, $height) = getimagesize($file_tmp);
//calculate the image ratio
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
//make sure the image is big enough to resize
if (($newwidth > $width) || ($newheight > $height) ){
$newwidth = $width;
$newheight = $height;
}
//function for resize image.
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Error: Please make sure you have GD library ver 2+");
}
//the resizing is going on here!
imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//finally, save the image
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
$pictureName= $rand_name.".".$file_ext ;
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
}
//now create smaller thumb **------ ////
//***************************************************
//insert recipe photo
//***************************************************
//make sure this directory is writable!
$path_thumbs = "recipePhotos/thumbs/";
//the new width of the resized image, in pixels.
$img_thumb_width = 80; //
$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
//List of allowed extensions if extlimit = yes
$limitedext = array(".gif",".jpg",".png",".jpeg");
//the image -> variables
$file_type = $_FILES['Photo']['type'];
$file_name = $_FILES['Photo']['name'];
$file_size = $_FILES['Photo']['size'];
$file_tmp = $_FILES['Photo']['tmp_name'];
//check if you have selected a file.
if(!is_uploaded_file($file_tmp)){
echo "Error: Please select a file to upload!.
--back";
exit(); //exit the script and don't process the rest of it!
}
//check the file's extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
//uh-oh! the file extension is not allowed!
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension.
--back";
exit();
}
//so, whats the file's extension?
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];
//create a random file name
// $rand_name = md5(time());
// $rand_name= rand(0,999999999);
//
// $rand_name = $recipeId . "-" . $_SESSION['sessionUserID'] ;
//the new width variable
$ThumbWidth = $img_thumb_width;
/////////////////////////////////
// CREATE THE THUMBNAIL //
////////////////////////////////
//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list the width and height and keep the height ratio.
list($width, $height) = getimagesize($file_tmp);
//calculate the image ratio
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
//make sure the image is big enough to resize
if (($newwidth > $width) || ($newheight > $height) ){
$newwidth = $width;
$newheight = $height;
}
//function for resize image.
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Error: Please make sure you have GD library ver 2+");
}
//the resizing is going on here!
imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//finally, save the image
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
}
$sql = "UPDATE recipes set Photo = '$pictureName' where ID = '$recipeId'";
$result5 = mysql_query($sql) or die(mysql_error());
}
//now redirect user to confirmation page
header("Location: myCookbookEditConfirm.php");
exit ;
}
//end of recipe insert
?>