<? include_once("../includes/main.inc.php");
$headerTitleName = "Add New Portfolio";
$page_id = $_REQUEST['page_id'];
$id = '1';


if(is_post_back()) {
	if($page_id!='') {		 		
		$sql = "update `tbl_portfolio` set s_name='".addslashes($s_name)."', alt_tag='".addslashes($alt_tag)."',status='".addslashes($status)."' where id = $page_id ";
		db_query($sql);
		########### Uploading Image ################
		if($_FILES['sv_image']['name']!='')
		{
			if(strlen($sv_image) && file_exists(UP_FILES_FS_PATH."/portfolio/thumb/".$sv_image)){
				@unlink(UP_FILES_FS_PATH."/portfolio/thumb/".$sv_image);
			}
			$sv_image_1 = md5(uniqid(rand(), true)).'.'.file_ext($_FILES[sv_image]['name']);
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/portfolio/thumb/".$sv_image_1) or die("Image is not uploaded");
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/portfolio/original/".$sv_image_1) or die("Image is not uploaded");
			$n_width=360; // Fix the width of the thumb nail images
			$n_height=260; // Fix the height of the thumb nail imaage
			
			$tsrc=UP_FILES_FS_PATH."/portfolio/thumb/".$sv_image_1;
			$add=UP_FILES_FS_PATH."/portfolio/original/".$sv_image_1; // Path where thumb nail image will be stored
			
			//echo $tsrc;
			if (!($_FILES[sv_image][type] =="image/pjpeg" OR $_FILES[sv_image][type]=="image/jpeg" OR $_FILES[sv_image][type]=="image/png")){echo "Your uploaded file must be of JPG or GIF or PNG. Other file types are not allowed<BR>";
			exit;}
				///////// starting of JPG thumb nail creation//////////
			if($_FILES[sv_image][type]=="image/pjpeg" OR $_FILES[sv_image][type]=="image/jpeg")
			{
				$im=ImageCreateFromJPEG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImageJpeg($newimage,$tsrc);
				chmod("$tsrc",0777);
			}
			else{
				$im=ImageCreateFromPNG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImagePng($newimage,$tsrc);
				chmod("$tsrc",0777);
			}
			
			db_query("update tbl_portfolio set img='".$sv_image_1."' where id = $page_id ");
		}
		set_session_msg("Portfolio updated successfully.");	
	    header("Location: portfolio.php?start=$start");
	} else{	
	
	     ########### Uploading Image ################
		if($_FILES['sv_image']['name']!='')
		{
			if(strlen($sv_image) && file_exists(UP_FILES_FS_PATH."/portfolio/thumb/".$sv_image)){
				@unlink(UP_FILES_FS_PATH."/portfolio/thumb/".$sv_image);
			}
			$sv_image_1 = md5(uniqid(rand(), true)).'.'.file_ext($_FILES[sv_image]['name']);
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/portfolio/thumb/".$sv_image_1) or die("Image is not uploaded");
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/portfolio/original/".$sv_image_1) or die("Image is not uploaded");
			$n_width=360; // Fix the width of the thumb nail images
			$n_height=260; // Fix the height of the thumb nail imaage
			
			$tsrc=UP_FILES_FS_PATH."/portfolio/thumb/".$sv_image_1;
			$add=UP_FILES_FS_PATH."/portfolio/original/".$sv_image_1; // Path where thumb nail image will be stored
			
			//echo $tsrc;
			if (!($_FILES[sv_image][type] =="image/jpeg" OR $_FILES[sv_image][type]=="image/jpeg" OR $_FILES[sv_image][type]=="image/png")){echo "Your uploaded file must be of JPG or GIF or PNG. Other file types are not allowed<BR>";
			exit;}
				///////// starting of JPG thumb nail creation//////////
			if($_FILES[sv_image][type]=="image/jpeg" OR $_FILES[sv_image][type]=="image/jpeg")
			{
				$im=ImageCreateFromJPEG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImageJpeg($newimage,$tsrc);
				chmod("$tsrc",0777);
			}
			else{
				$im=ImageCreateFromPNG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImagePng($newimage,$tsrc);
				chmod("$tsrc",0777);
			}
			
		}
		$sql = "insert into `tbl_portfolio` set status='".addslashes($status)."', alt_tag='".addslashes($alt_tag)."',s_name='".$s_name."', img='".$sv_image_1."' ";
		db_query($sql);
		set_session_msg("New Portfolio added successfully.");
	header("Location: portfolio.php?start=$start");
	exit;
	}
}

if($page_id!='') {
	$sql = "select * from `tbl_portfolio` where id = '$page_id'";
	$result = db_query($sql);
	if ($line_raw = mysql_fetch_array($result)) {	
		@extract($line_raw);
	}
}
?>


<link href="styles.css" rel="stylesheet" type="text/css">
<? include("top.inc.php");?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td id="pageHead"><div id="txtPageHead">
       Add/Edit - Portfolio</div></td>
  </tr>
</table>
<table width="80%" align="left">
		  	<tr><td>
		  	<form method="post" action="" enctype="multipart/form-data" name="form1" id="form1" onSubmit="return validate(this);">
        <table width="80%" align="center" border=0 cellpadding="0"  cellspacing="0" class="text">
    	<tr>
		<td  colspan="2"height="30"  align="right"><a href="portfolio.php?start=<?=$start?>" class="redcolor"><strong>Back To Portfolio</strong></a></td>
		</tr>    
        <tr><td height="15"><div align="center"><span class="msg"><?=display_sess_msg();?></span></div><br /></td></tr>  

        <tr><td>&nbsp;</td></tr>
		 <tr>
			<td class="lables"><b>Provide Categaries</b></td>
			<td class="feildstxt">  	 
                <select name="s_name" id="s_name" style="width:300px;">	
					<?php					
						$indusmenusql = mysql_query("select * from `tbl_portfoliocatp` where status=1"); 
						while($row =mysql_fetch_array($indusmenusql)):?>		   
						<option value="<?php echo ucwords($row['catP_Name']); ?>"  <?php if($row['catP_Name']==$s_name) echo 'selected="selected"'; ?>><?php echo ucwords($row['catP_Name']);?></option>
						<?php endwhile; ?>		 
				</select> 
				</td>
		</tr>
		

        <tr><td>&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Services Title</b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="alt_tag" id="alt_tag"  ><?=stripslashes($alt_tag)?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        
		<tr>
		  <td class="lables"><b>Status</b></td>
          <td class="feildstxt"><select name="status" id="status">
          <option value="1" <?php if($status=='1'){ echo 'selected="selected"'; } ?>>Active</option>
          <option value="0" <?php if($status=='0'){ echo 'selected="selected"'; } ?>>InActive</option>          
          </select></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
		  <td class="lables" colspan="2"><hr/></td>
        </tr>
       
        <tr><td>&nbsp;</td></tr>
        <tr>
		   <td class="lables"><b>Portfolio Image</b></td>
           <td class="feildstxt"><input type="file" name="sv_image" id="sv_image"  /></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
		  <td class="lables"><b>Current Portfolio Image</b></td>
          <td class="feildstxt">
        <? if($page_id!='')
            {
                $sv_image = $img;
                if(strlen($sv_image) && file_exists(UP_FILES_FS_PATH."/portfolio/thumb/".$sv_image)){
                $imgsrc=SITE_WS_PATH."/uploaded_files/portfolio/thumb/".$sv_image;
                $size=getimagesize(UP_FILES_FS_PATH."/portfolio/thumb/".$sv_image);
				echo '<img src="'.$imgsrc.'" alt="No Image"  />';
                }
				else{  echo 'NO Images'; }
				
				
                if(strlen($sv_image) && file_exists(UP_FILES_FS_PATH."/portfolio/original/".$sv_image)){
                $imgsrcBig=SITE_WS_PATH."/uploaded_files/portfolio/original/".$sv_image;
                $size=getimagesize(UP_FILES_FS_PATH."/portfolio/original/".$sv_image);
                $img=ImageCreateFromJPEG($imgsrcBig);
                $width=ImageSx($img); 
                $height=ImageSy($img); 				
			}
		}
        ?>
             
            </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
        <td align="left" class="lables">&nbsp;</td>
        <td align="left" class="feildstxt"><input class="button" type="submit" name="submit" value="Save"></td>
        </tr>
        <tr><td height="25">&nbsp;</td></tr>
   </table>
    </form>
    </td>
</tr>
</table>  


<? include("bottom.inc.php");?>