<?php
    session_start();    
	if(empty($_SESSION['p0B_c0ff33'])) header('location:login.php?goto=index');

	if(empty ($_GET['p'])){
		$_GET['p']="index";
	}

	include('templates/header.php'); 

?>
<body> 
<!-- Start: page-top-outer -->
<?php 
	
include('templates/menu.php'); 
include "../db_function.php";  

if(empty ($_GET['page'])){
	$_GET['page']=1;
}
if(empty ($_GET['l'])){
	$_GET['l']='English';
}

$table="tbl_all";
$imagelocation = "../Site/images/all/";
$bigimagelocation = "../Site/images/all/b/";
			
if(isset($_POST['save'])){
	$id=$_POST['txtID'];		
	$name=$_POST['name'];

		//////Resize Picture
error_reporting(0);
$change="";
$abc="";
 define ("MAX_SIZE","1024");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
 $errors=0;
  if($_SERVER["REQUEST_METHOD"] == "POST")
 {
 	$image =$_FILES["image"]["name"];
	$uploadedfile = $_FILES['image']['tmp_name'];
	 ///Rename Image
	 if(!empty($image)){
				$i=1;
				$str="SELECT * FROM $table WHERE image ='".$image."' ";
				$sql=mysql_query($str);
				while ($row=mysql_fetch_array($sql)){
				$str="SELECT * FROM $table WHERE image ='".$image."' ";
				$sql=mysql_query($str);
				$num_rows = mysql_num_rows($sql);
					 if($num_rows > 0) {
						$nimage=$_FILES["image"]["name"];
						list ($iname,$ext)=preg_split('/[\.,]+/',$nimage);
						$image=$iname."_".$i.".".$ext;
					 }

					$i++;
				}
			///End Rename/////////////////////////////////////////////////////////
	}
 	if ($image)
 	{
 		$filename = stripslashes($image);
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);

 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
 		{
 			$change='<div class="msgdiv">Unknown Image extension</div> ';
 			$errors=1;
 		}
 		else
 		{

$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1440)
{
	$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
	$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);}
else if($extension=="png")
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);
	if($width > 600 ){
		$newwidth = 600;
	}else{
		$newwidth = $width;
	}
	$newheight=($height/$width)*$newwidth;
	$tmp=imagecreatetruecolor($newwidth,$newheight);

	$newwidth1=202;
	$newheight1=($height/$width)*$newwidth1;
	$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
	

	if($extension=="png" || $extension=="gif")
	{
		imagealphablending($tmp, false);
		imagesavealpha($tmp,true);
		$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
		imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);

		imagealphablending($tmp1, false);
		imagesavealpha($tmp1,true);
		$transparent = imagecolorallocatealpha($tmp1, 255, 255, 255, 127);
		imagefilledrectangle($tmp1, 0, 0, $newwidth, $newheight, $transparent);
		
	}

	imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
	imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
	
	$filename1 = $imagelocation.$image;
	$filename = $bigimagelocation.$image;

	if($extension=="png" || $extension=="gif")
	{
		imagepng($tmp,$filename);
		imagepng($tmp1,$filename1);
	}
	else{
		imagejpeg($tmp,$filename,90);
		imagejpeg($tmp1,$filename1,90);
	}

	imagedestroy($src);
	imagedestroy($tmp);
	imagedestroy($tmp1);
}}

}


if(!$errors) {	
				
//Update here ///////////////////////////////////////////////////////////////////
			if(!$image)
			{
			$str="UPDATE $table SET title='".mysql_real_escape_string($name)."' WHERE id=$id";			
			}
			else
			{
			$str="UPDATE $table SET image='$image', title='".mysql_real_escape_string($name)."' WHERE id=$id";			
			$str1="SELECT image FROM $table WHERE id=$id";
				$sql1=mysql_query($str1);
				while($row=mysql_fetch_array($sql1)){
					if(!empty($row['image'])){
					$dr=$row['image'];
					$file=fopen("$imagelocation$dr","w");
					$file=fopen("$bigimagelocation$dr","w");
					fclose($file);
					unlink("$imagelocation$dr");
					unlink("$bigimagelocation$dr");
					}				
				}
			}
			$suse=mysql_query($str);
			if($suse){
				$masg=0;
				}else{
				$masg=1;
			}
}//end if
}
//Delete here/////////////////////////////////////////////////////////////
		else if(isset($_POST['subdelete'])){
			$id=$_POST['txtID'];
			
			$str="SELECT image FROM $table WHERE id=$id";
			$sql=mysql_query($str);
			while($row=mysql_fetch_array($sql)){
				if(!empty($row['image'])){
				$dr=$row['image'];
				$file=fopen("$imagelocation$dr","w");
				$file=fopen("$bigimagelocation$dr","w");
				fclose($file);
				unlink("$imagelocation$dr");
				unlink("$bigimagelocation$dr");
				}
			}
			
			$delete = "DELETE FROM $table WHERE id= $id";
			mysql_query($delete);			
			$masg=3;				
		}
?>

<!-- start content-outer ........................................................................................................................START -->
<div id="content-outer">
<!-- start content -->
<div id="content">

	<!--  start page-heading -->
	<div id="page-heading">
		<h1>View All</h1>               
        
 	<!--  end top-search -->
 	<div class="clear"></div>
	</div>
	<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
	<tr>
		<th rowspan="3" class="sized"><img src="images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
		<th class="topleft"></th>
		<td id="tbl-border-top">&nbsp;</td>
		<th class="topright"></th>
		<th rowspan="3" class="sized"><img src="images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
	</tr>
	<tr>
		<td id="tbl-border-left"></td>
		<td>
		<!--  start content-table-inner ...................................................................... START -->
		<div id="content-table-inner">
		<?php
	if(isset($masg) && $masg==0 ){
	echo'
	<div id="message-green">
		<table border="0" width="100%" cellpadding="0" cellspacing="0">
		<tr>
			<td class="green-left">Your update suces.   </td>
			<td class="green-right"><a class="close-green"><img src="images/table/icon_close_green.gif" alt="" /></a></td>
		</tr>
		</table>
	</div>';
	}
	else if(isset($masg) && $masg==1){
	echo'
	<div id="message-red">
		<table border="0" width="100%" cellpadding="0" cellspacing="0">
			<tr>
					<td class="red-left">Your update is Fail. Please try again!</td>
				<td class="red-right"><a class="close-red"><img src="images/table/icon_close_red.gif"   alt="" /></a></td>
			</tr>
		</table>
	</div>';
	}
	else if(isset($masg) && $masg==2){
	echo'
	<div id="message-yellow">
		<table border="0" width="100%" cellpadding="0" cellspacing="0">
			<tr>
					<td class="yellow-left">'.$change.'</td>
				<td class="yellow-right"><a class="close-yellow"><img src="images/table/icon_close_red.gif"   alt="" /></a></td>
			</tr>
		</table>
	</div>';
	}
	else if(isset($masg) && $masg==3 ){
	echo'
	<div id="message-green">
		<table border="0" width="100%" cellpadding="0" cellspacing="0">
		<tr>
			<td class="green-left">Your delete suces.   </td>
			<td class="green-right"><a class="close-green"><img src="images/table/icon_close_green.gif" alt="" /></a></td>
		</tr>
		</table>
	</div>';
	}
	?>

	<div id="table-content">
	<?php
	if(isset($_POST['search']))
	{
		$kernh=$_POST['kernh'];

		$query2 = "SELECT * FROM $table WHERE title LIKE '%$kernh%' ORDER BY id DESC";
		
		include("include/paginator.php");
		//include the extension that makes the pre-made links
		include("include/paginator_html.php");
		//Makes the array used in this example.
		
		
		$result2=mysql_query($query2);
		echo mysql_error();
		$num_rows=mysql_num_rows($result2);
		
		$vlimit=500;
		//gets the total number of items
		//========================================================================
		//Parts used to make a new paginator 
		//========================================================================
		//Makes new Paginator_html.  Current page here is sent by the get method. 
		//$num_rows is the total items in the source.
		$a =new Paginator_html($_GET['page'],$num_rows);
		//sets the number of records displayed
		//defaults to five
		$a->set_Limit($vlimit); 
		// if using numbered links this will set the number before and behind 
		//the current page.
		//defaults to five
		$a->set_Links(3);  
		//gets starting point.
		$limit1 = $a->getRange1();  
		//gets number of items displayed on page.
		$limit2 = $a->getRange2();  
		//=========================================================================
		//Printing out the items in the array
		
		$query = "SELECT * FROM $table WHERE title LIKE '%$kernh%' ORDER BY id DESC limit $limit1, $limit2 ";
	}
	else
	{
		$query2 = "SELECT * FROM $table ORDER BY id DESC";
		
		include("include/paginator.php");
		//include the extension that makes the pre-made links
		include("include/paginator_html.php");
		//Makes the array used in this example.
		
		
		$result2=mysql_query($query2);
		echo mysql_error();
		$num_rows=mysql_num_rows($result2);
		
		$vlimit=5;
		//gets the total number of items
		//========================================================================
		//Parts used to make a new paginator 
		//========================================================================
		//Makes new Paginator_html.  Current page here is sent by the get method. 
		//$num_rows is the total items in the source.
		$a =new Paginator_html($_GET['page'],$num_rows);
		//sets the number of records displayed
		//defaults to five
		$a->set_Limit($vlimit); 
		// if using numbered links this will set the number before and behind 
		//the current page.
		//defaults to five
		$a->set_Links(3);  
		//gets starting point.
		$limit1 = $a->getRange1();  
		//gets number of items displayed on page.
		$limit2 = $a->getRange2();  
		//=========================================================================
		//Printing out the items in the array
		
		$query = "SELECT * FROM $table ORDER BY id DESC limit $limit1, $limit2";
	}
	
	?>		
			
	<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
        <tr>
            <th class="table-header-check"><a id="toggle-all"></a> </th>
            <th class="table-header-repeat line-left minwidth-1"><a href="">Title</a></th>
            <th class="table-header-repeat line-left minwidth-1"><a href="">Image</a></th>
            <th class="table-header-options line-left"><a href="">Option</a></th>
        </tr>
				
	<?php		
    $result =mysql_query($query);
    echo mysql_error();
	$bgcolor="#d8d8d8";
	$i=1;
	$linkUpload='&table=tbl_imagegallery&fid=id&ftype=type&falbumname=aid&fitem=item&folder_th=gallery_th&folder=gallery&folder_sth=gallery_sth';
	while ($row = mysql_fetch_array($result))
	{
		echo'<form name="" method="POST" action="" enctype="multipart/form-data">';
		echo'
		<tr>
			<td><input name="checkbox" id="checkbox" type="checkbox"/></td>

			<td align="center">
				<input type="hidden" name="txtID" value="'.$row['id'].'" size="2" >
				<input type="text" class="inp-form" id="name" value="'.$row['title'].'" name="name" style="width: 500px;" /><br/><br/>
			</td>						
			
			<td>
				<img src="'.$imagelocation.$row['image'].'" width=200px" />
				
				<!--<a href="addmoreimage/viewmoreimage.php?aid='.$row['id'].$linkUpload.'" rel="facebox" style="color:#168a10; float:right;"> 
					View images in this album
				</a>-->
				<br /><input name="image" class="imagefial" size="1" type="file" /><br /><br />
				<div class="bubble-left"></div><div class="bubble-inner">JPG, PNG, GIF Size (600 x 422)px</div><div class="bubble-right"></div>
			</td>

			<td class="options-width">
			<input type="submit" name="subdelete" value="&nbsp;" title="Delete" class="icon-2 info-tooltip" '; ?> onclick="return confirm('Are you sure want to delete this Item?')" <?php echo ' >
			<input type="submit" name="save" value="&nbsp;" title="Save" class="icon-5 info-tooltip">
			<a href="edit.php?table='.$table.'&row=detail&fildID=id&id='.$row['id'].'" rel="facebox" class="icon-3 info-tooltip" title="detail" style="margin-left:10px;"></a>
		</tr>';
		
		$i++;
		echo'</form>';
	}
	?>

		</table>		<!--  end product-table................................... --> 
	</form>
		
			</div>
		<table border="0" cellpadding="0" cellspacing="0" id="paging-table">
			<tr>
			<td valign="top" class="paging">
			<?php
			$a->previousNext();
			echo '<br /><br /><br />';
			echo'<font size=2><b>Total: </b>';
			echo ceil($num_rows/$vlimit);
			echo' pages';
			echo ' ('.$num_rows.' Items) </font>';			
			?>
				
			</td>
			</tr>
			</table>
			<div class="clear"></div>
		 
		</div>
		<!--  end content-table-inner ............................................END  -->
		</td>
		<td id="tbl-border-right"></td>
	</tr>
	<tr>
		<th class="sized bottomleft"></th>
		<td id="tbl-border-bottom">&nbsp;</td>
		<th class="sized bottomright"></th>
	</tr>
	</table>
	<div class="clear">&nbsp;</div>

</div>
<!--  end content -->
<div class="clear">&nbsp;</div>
</div>
<!--  end content-outer........................................................END -->

<div class="clear">&nbsp;</div>
    
 <?php include('templates/footer.php'); ?>

 