<?php
include "functions.php";
include "general_functions.php";
include ("class_thumbnail.php");
class metal{
function viewmetal($pid)
{
	
	   if(isset($_POST['delete']))
		{
		$del_all=$_POST['chk'];		
		$j= 0;
		for($i=0;$i<count($del_all);$i++)
		{
		if(is_numeric($del_all[$i]))
		{
		$orderid = $del_all[$i];
		del_real($orderid);		
		$j = $j +1;
		}
		}
		$rmsg = $j ." records deleted";
		}
	
	if(isset($_GET['del']))
	{
	$id=$_GET['del'];
	$z=funrecall($pid,$id);
	}
	else
	{
	$z="";
	}

?>
<link href="ssi.css" rel="stylesheet" type="text/css">
<script language="javascript">
<!--
function docheck() {
	var abc = confirm("Are you sure you want to delete this record ?.");
	if(abc) {
	return true;
	} else {
		return false;
	}
}
//-->
</script>
<script src="includenp/functions.js"></script>
<link href="ssi.css" rel="stylesheet" type="text/css">
<table width="98%" border="0" cellspacing="0" cellpadding="2" align="center">
  <tr>
    <td width="1%"></td>
	<td width="98%"><?php include_once "top.php";?></td>
	<td width="1%"></td>
  </tr>
  <tr>
   <td width="1%"></td>
    <td width="98%"><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="centertable">
      <tr>
        <td><table width="80%"  border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="40">&nbsp;</td>
          </tr>
          <tr>
            <td>
            
        
            
         <table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0">
          <?php 
		  if(isset($_GET['pid'])){?> 
          <tr>
            <td height="35" valign="top"><span class="bigblackTxt"><a href="newphoto.php" class="normalLink">Add New Photo</a>::<a href="photogallery.php" class="normalLink">Manage Photo</a></span></td>
          </tr>
          <?php 
		  }
		  else {
		  ?>
          <tr>
            <td height="35" valign="top"><span class="bigblackTxt"><a href="newphoto.php" class="normalLink">Add New Photo</a>::<a href="photogallery.php" class="normalLink">Manage Photo</a></span></td>
          </tr>
          
          <tr>
            <td class="bluebold"><?php 
			
			if(isset($z))
			{
			if(strlen($z) >0)
			{		 
		  	$errmsg = $z;
			include "errmsgs.php";
			echo "<br>";
		   	}
			}
			?>
              </td>
          </tr>
          <tr>
            <td>
              <table width="100%"  border="0" cellpadding="2" cellspacing="0">
               
                  <tr>
                    <td width="100%">
					<?php 				
				$page_name="photogallery.php";  
				if(isset($_GET['start']))
				{
				$start=$_GET['start'];
				}				                     
				else
				{
				$start = 0;
				}
				
				$eu = ($start - 0); 
				$limit = 10;                                 
				$thiss = $eu + $limit; 
				$back = $eu - $limit; 
				$next = $eu + $limit; 
				
				
				
			   $sqln=mysql_query("SELECT id FROM images");
			   $numrows=mysql_num_rows($sqln);
			    if(mysql_num_rows($sqln) > 0 )
				{
				?>
				<TABLE cellSpacing=1 cellPadding=0 width="100%" align=center border=0>
				<tr>
				<td>
				<form action="<?php echo $_SERVER['PHP_SELF'];?>?<?php echo $serchstr;?>" method="post"  name="form1" ID="form1">
				<TABLE cellSpacing=3 cellPadding=3 width="100%" align=center border=0>
                      <TR class="thbg">
                      <TH align= "center"  class="bluebold">Delete</TH>
					  <TH align= "center"  class="bluebold">Edit</TH>
                      <TH width="39%" align= "center"  class="bluebold">Image Title </TH>
                      <TH width="35%" align= "center"  class="bluebold">Images</TH>
                      </TR>
			 <?php							 	
			 $sql_image =mysql_query("SELECT id,imgname,image FROM images order by id DESC limit $eu,$limit");				
			 while($row=mysql_fetch_object($sql_image))
			 {			 							 
			 ?>
                    <TR class="trbg">
                      <TD align="center" width="13%">
         <input name="chk[]" type="checkbox" id="chk[]" value="<?php echo $row->id;?>" size="4" />
                      </TD>
                      <TD align="center" width="13%"><a href="edit_photo.php?editid=<?php echo $row->id;?>"><img src="panelimages/b_edit.png" width="16" height="16" border="0" /></a></TD>
                      <TD align="center" class="normalTxt"><?php
					  echo $row->imgname; 
					  ?></TD>
                      <TD align="center" class="normalTxt">
                        <?php
						if((file_exists("smallthumbnail/".$row->image)) && (strlen($row->image) > 3))
						{?>												
						 <img src="smallthumbnail/<?php echo  $row->image;?>" width="60" height="60">
						<?php
						}						
					  ?></TD>
                    </TR>
			  <?php
				}				
				?>	
				<tr>
				<td><a href="JavaScript:checkAll(document.form1)" class="normalLink">Check All</a></td>
				<td><a href="JavaScript:uncheckAll(document.form1)" class="normalLink">Unckeck All</a></td>
				<td><input name="delete" type="submit"  onclick="return docheck()" class="hbutton" id="delete" value="DELETE SELECTED" /></td>
				<td>&nbsp;</td>
				</tr>	
                </TABLE>
				</form>
				</td>
				</tr>		
                </TABLE>
				<?php
				}
				else
				{
				?>				
				<TABLE class=table cellSpacing=0 cellPadding=0 width="100%" align=center border=0>            
                    <TR>
                      <TD height="30" align=center valign="top" class="bluebold">No Records to view </td>
                    </TR>
                </TABLE>
				<?php
				}
				?></td>
                  </tr>                      
					   <tr>
					   <td>
					   <?php
echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";
if($back >=0) { 
print "<a href='$page_name?start=$back&pid=$pid'><font face='Verdana, Arial, Helvetica, sans-serif' size='1'>Prev</font></a>"; 
} 
echo "</td><td align=center width='30%'>";
$i=0;
$l=1;
for($i=0;$i < $numrows;$i=$i+$limit){
if($i <> $eu){
echo " <a href='$page_name?start=$i&pid=$pid'><font face='Verdana, Arial, Helvetica, sans-serif' size='1'>$l</font></a> ";
}
else { echo "<font face='Verdana, Arial, Helvetica, sans-serif' size='1' color=blue>$l</font>";}        
$l=$l+1;
}
echo "</td><td  align='right' width='30%'>";
if($thiss < $numrows) { 
print "<a href='$page_name?start=$next&pid=$pid'><font face='Verdana, Arial, Helvetica, sans-serif' size='1'>Next</font></a>";} 
echo "</td></tr></table>";
?>	
					   </td>
					   </tr>
            </table>
            </td>
          </tr>
          <?php }?>
        </table>
        
               
        
        </td>
          </tr>
        </table></td>
      </tr>
    </table></td>
    <td width="1%"></td>
  </tr>
  <tr>
    <td width="1%"></td>
	<td><?php include_once "footer.php";?></td>
	<td width="1%"></td>
  </tr>
</table>
<?php
}
function newmetal($pid)
{

   $previous=$_SERVER["HTTP_REFERER"];
	$found = strpos($previous,"?");
	if($found <> false)
	{
	$previous = substr($previous,0,$found);
	}
	$previous = $previous."?pid=$pid";
   $imgname="";
  
	if(isset($_POST['save']))
	{
	$rmsg = "";
	$proceed ="";
	
	$imgname=trim($_POST['imgname']);
	if(strlen($imgname) < 1)
	{
	$rmsg = $rmsg . "<li>Please Enter Event Category name</li>";
	$proceed = $proceed + 1; 	
	}
 
$query=mysql_query("SELECT imgname FROM photogallery where imgname='$imgname' and pid='$pid'");
	if(mysql_num_rows($query) > 0)
	{
	$proceed = $proceed + 1; 	
	$rmsg = $rmsg . "<li>Event Category Name already exists</li>";
	}
	else
	{
	$proceed = $proceed + 0;
	}
	if($proceed < 1)
	{
	
	    $sqlimg="SELECT MAX(id) As higID FROM  photogallery";
		$resultimg = mysql_query($sqlimg); 
		$rowimg = mysql_fetch_object($resultimg);
		$newID = $rowimg->higID;
			if(empty($newID))
			{
			$newID ="1";
			}
			else
			{
			$newID =$newID + 1;
			}	
		$image_tmp_name=$_FILES["cimage"]['tmp_name'];
		$image_name=$_FILES["cimage"]['name'];	 								
	 if(strlen($image_tmp_name)>3){	
	 $ext=getfileExtension($_FILES["cimage"]['name']);
			         $original_file="bigthumbnail/".$newID.".".$ext;					 
			         move_uploaded_file($image_tmp_name,$original_file);					 
					 $thumb_img_name=$newID;
			         $thumb_img_file=$thumb_img_name.".".$ext;				
				  createThumbimage($thumb_img_name,$original_file,"smallthumbnail","166","106","w");
		
	
	$image=$thumb_img_file;					
	}
	else{
	$image="";		
	}	
	$qid =mysql_query("INSERT INTO photogallery (imgname,pid,image) VALUES('$imgname','$pid','$image')");		
	$rmsg = "New record added";
	$imgname = "";
	}
	else
	{
	$haderr="yes";
	$rmsg = $rmsg;
	}

}
?>
<link href="ssi.css" rel="stylesheet" type="text/css">
<table width="98%" border="0" cellspacing="0" cellpadding="2" align="center">
  <tr>
    <td width="1%"></td>
	<td width="98%"><?php include_once "top.php";?></td>
	<td width="1%"></td>
  </tr>
  <tr>
   <td width="1%"></td>
    <td width="98%"><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="centertable">
      <tr>
        <td><table width="80%"  border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="40">&nbsp;</td>
          </tr>
           <tr>
            <td height="35" valign="top"><span class="bigblackTxt"><a href="photogallery.php?pid=0&type=view" class="normalLink">View Photogallery Categories </a></span></td>
          </tr>
          <tr>
            <td><span class="bluebold">
              <?php 
			//display messages
		  if(isset($rmsg))
		  	{
				$errmsg = $rmsg;
				include "errmsgs.php";
				echo "<br>";
			}
			?>
            </span></td>
          </tr>
          <tr>
            <td>
			
              <table width="100%"  border="0" align="center" cellpadding="2" cellspacing="3">
                <form action="" method="POST" enctype="multipart/form-data" name="SaveForm" ID="SaveForm">                 
                  <tr>
                    <td colspan="2" class="bigblackTxt" >New Photogallery Category</td>
                  </tr>
                 
                  <tr>
                    <td width="27%" class="bluebold">Category Name :</td>
                    <td width="73%"><input name="imgname" type="text" class="inputBOX" id="imgname" value="<?php echo $imgname; ?>">
                      <input name="id" type="hidden" id="id" value="<?php echo $id; ?>">                      </td>
                  </tr>
                  <tr>
                    <td class="bluebold">Category Image</td>                    
                     <td><input name="cimage" type="file" class="inputBOX" /></td>
                  </tr>
                  <tr>
                    <td class="bluebold">&nbsp;</td>
                    <td><input name="save" type="submit" class="hbutton" id="save" value="ADD" />
                      <input name="cancel" type="button" class="hbutton" id="cancel"  onclick="location.href='<?php echo $previous;?>'" value="CANCEL" />
                      <input name="moveto2" type="hidden" id="moveto2" value="<?php echo $previous ;?>" /></td>
                  </tr>                  
                  <tr>
                    <td colspan="2">&nbsp;</td>
                  </tr>
                  <tr>
                    <td colspan="2">&nbsp;</td>
                  </tr>
                </form>
            </table></td>
          </tr>
        </table></td>
      </tr>
    </table></td>
    <td width="1%"></td>
  </tr>
  <tr>
    <td width="1%"></td>
	<td><?php include_once "footer.php";?></td>
	<td width="1%"></td>
  </tr>
</table>

<?php
}
function editmetal($pid,$id)
{


	$previous=$_SERVER["HTTP_REFERER"];
	$found=strpos($previous,"?");
	if($found <> false)
	{
	$previous=substr($previous,0,$found);
	}
	$previous=$previous."?pid=$pid";
	
	$sqlev=mysql_query("SELECT id,imgname,image FROM photogallery where id='$id'");
	$row=mysql_fetch_object($sqlev);	
	$imgname=$row->imgname;	
	$image=$row->image;	
	$haderr="yes";

	if(isset($_POST['save']))
	{
	$rmsg = "";
	$proceed ="";
	$image=""; 
	$previous = $_POST['moveto'];
    $imgname=trim($_POST["imgname"]);
	$id=$_POST['id'];
	$oldimg=$_POST['oldimg'];
	
	if(strlen($imgname) < 1)
	{
	$rmsg = $rmsg . "<li>Please enter event categiry  name</li>";
	$proceed = $proceed + 1; 	
	}
		
		$query="SELECT imgname FROM photogallery where imgname='$imgname' and pid='$pid'";
		$result=mysql_query($query);		
		if(mysql_num_rows($result) > 1)
		{
		$proceed = $proceed + 1; 	
		$rmsg = $rmsg . "<li>Photogallery category name already exists</li>";
		}
		else
		{
		$proceed = $proceed + 0;
		}

			
			if($proceed < 1)
			{
			
			$sqlimg="SELECT MAX(id) As higID FROM  photogallery";
		$resultimg = mysql_query($sqlimg); 
		$rowimg = mysql_fetch_object($resultimg);
		$newID = $rowimg->higID;
			if(empty($newID))
			{
			$newID ="1";
			}
			else
			{
			$newID =$newID + 1;
			}	
		$image_tmp_name=$_FILES["cimage"]['tmp_name'];
		$image_name=$_FILES["cimage"]['name'];	 								
	 if(strlen($image_tmp_name)>3){	
	 $ext=getfileExtension($_FILES["cimage"]['name']);
			         $original_file="smallthumbnail/".$id.".".$ext;					 
			         move_uploaded_file($image_tmp_name,$original_file);					 
					 $thumb_img_name=$id;
			         $thumb_img_file=$thumb_img_name.".".$ext;	
				  createThumbimage($thumb_img_name,$original_file,"smallthumbnail","166","106","w");
	
	$image=$thumb_img_file;					
	}
	else{
	$image=$oldimg;		
	}	
			
			
			
		    $qid="Update photogallery  set imgname='$imgname',image='$image' where id='$id'";					
			$updateresult=mysql_query($qid);
			$rmsg="Record updated";
			//echo mysql_error();
			//$rmsg = urlencode("Record updated");
			//header("Location: $previous?rmsg=$rmsg");
			}
			else
			{
			$haderr="yes";
			$rmsg = $rmsg;
			}
	}

?>
<link href="ssi.css" rel="stylesheet" type="text/css">
<table width="98%" border="0" cellspacing="0" cellpadding="2" align="center">
  <tr>
    <td width="1%"></td>
	<td width="98%"><?php include_once "top.php";?></td>
	<td width="1%"></td>
  </tr>
  <tr>
   <td width="1%"></td>
    <td width="98%"><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="centertable">
      <tr>
        <td><table width="80%"  border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="35" valign="top">&nbsp;</td>
          </tr>
          <tr>
            <td height="35" valign="top"><span class="bigblackTxt"><a href="photogallery.php?pid=0&type=view" class="normalLink">View Photogallery Categories </a></span></td>
          </tr>
          <tr>
            <td><span class="bluebold">
              <?php 
			//display messages
		  if(isset($rmsg))
		  	{
				$errmsg = $rmsg;
				include "errmsgs.php";
				echo "<br>";
			}
			?>
            </span></td>
          </tr>
          <tr>
            <td>
			
              <table width="100%"  border="0" align="center" cellpadding="2" cellspacing="3">
                <form action="" method="POST" enctype="multipart/form-data" name="SaveForm" ID="SaveForm">
                 
                  <tr>
                    <td colspan="2" class="bigblackTxt" >Edit Photogallery Category</td>
                  </tr>
                  <tr>
                    <td width="27%" class="bluebold">  Category Name :</td>
                    <td width="73%"><input name="imgname" type="text" class="inputBOX" id="imgname" value="<?php echo $imgname;?>">
                      <input name="id" type="hidden" id="id" value="<?php echo $id;?>"></td>
                  </tr>
                 
                  <tr valign="bottom">
                    <td align="left" class="bluebold">Category Image : </td>
                    <td><input name="cimage" type="file" class="inputBOX" />
					 <?php
					    if(isset($image)){
						if((file_exists("smallthumbnail/".$image)) && (strlen($image) > 3))
						{?>												
						 <img src="smallthumbnail/<?php echo $image;?>" width="20" height="20">
						<?php
						}	
						}
						?>
                      <input type="hidden" name="oldimg" value="<?php echo $image;?>" /></td>
                  </tr>
                  <tr valign="bottom">
                    <td align="right">&nbsp;</td>
                    <td><input name="save" type="submit" class="hbutton" id="save" value="UPDATE" />
                           <input name="cance" type="button" class="hbutton" id="cance"  onclick="location.href='<?php echo $previous;?>'" value="CANCEL">
                      <input name="moveto" type="hidden" id="moveto" value="<?php echo $previous;?>"></td>
                  </tr>
                  <tr>
                    <td colspan="2">&nbsp;</td>
                  </tr>
                  <tr>
                    <td colspan="2">&nbsp;</td>
                  </tr>
                </form>
            </table></td>
          </tr>
        </table></td>
      </tr>
    </table></td>
    <td width="1%"></td>
  </tr>
  <tr>
    <td width="1%"></td>
	<td><?php include_once "footer.php";?></td>
	<td width="1%"></td>
  </tr>
</table>
<?php
}
}
?>
<?php
function funrec($pid)
{
if($pid !=0)
{
$sqlrec="select * from photogallery   where id='$pid'";

$resultrec=mysql_query($sqlrec);
$numrowrec=mysql_num_rows($resultrec);
$rowrec=mysql_fetch_object($resultrec);
	if($numrowrec >0)
	{
		if($rowrec->pid==0)
		{
		return "<a href=photogallery.php?pid=$rowrec->id class=normallink>$rowrec->imgname</a> >>";
		}
		else
		{	
		return funrec($rowrec->pid)."<a href=photogallery.php?pid=$rowrec->id class=normallink>$rowrec->imgname</a> >>";
		}	
	}
}
	
}
?>
<?php
function funrecname($pid)
{
if($pid !=0)
{
$sqlrec="select * from photogallery   where id='$pid'";

$resultrec=mysql_query($sqlrec);
$numrowrec=mysql_num_rows($resultrec);
$rowrec=mysql_fetch_object($resultrec);
	if($numrowrec >0)
	{
		if($rowrec->pid==0)
		{
		return "$rowrec->imgname ->";
		}
		else
		{	
		return funrecname($rowrec->pid)."$rowrec->imgname ->";
		}	
	}
}

}
?>

<?php
function funrecall($pid,$id)
{
$sqlrec="select * from photogallery   where pid='$id'";
$resultrec=mysql_query($sqlrec);
$numrowrec=mysql_num_rows($resultrec);

			if($numrowrec >0)
			{
			while($rowrec=mysql_fetch_object($resultrec))
			{
			funrecall($pid,$rowrec->id);			
			fundel($pid,$rowrec->id);	
			}		
			}
			else
			{
			
			$sqlrec="delete from photogallery   where id='$id'";
            $resultrec=mysql_query($sqlrec);		
			return "record deleted";	
			}
//funprodel($id);
$sqlrec="delete from photogallery   where id='$id'";
$resultrec=mysql_query($sqlrec);
}

    function fundel($pid,$id){
	//funprodel($id);
	$query_del="delete from photogallery   where id='$id'";
	$exec_query=mysql_query($query_del);	
	return "record deleted";
}

        function del_real($delid)
		{
		mysql_query("delete from images where id='$delid'");
		$errormsg = "Record deleted";		
		}
	
?>
