<?php
session_start();
include_once('connection.php');
include_once('loginfunctions.php');
include_once('multiple_inc_gallery.php');

$final_report="";

if(checkLoggedin()) 
{ 
}
else
{
 header("Location: index.php");
 exit;
}



$str='';
$aid=0;
$gid = '';
$error = 0;
$type="gallery";
$title ='';
$text = '';
date_default_timezone_set('Asia/Kolkata');
$adate = date("d-m-Y");
$edit = 0;
$order="";
$active='2';
$arr_count = 0;
 $slct='';

function record_exist($title)
{
$res = mysql_query("select * from `pages` where `title`='$title' and `type`='gallery' ") or die(mysql_error()) ;
if(mysql_num_rows($res)>0)return true;
else return false;
}







// get all details corresponding to id

if(isset($_GET['aid'])&&($_GET['aid']!=''))
{
$edit=1;	
$aid=injection_free($_GET['aid']);

$qry="select * from `pages` where `aid`='$aid' ";
$res = mysql_query($qry) or die(mysql_error());

	if(mysql_num_rows($res)>0)
	{
	$row = mysql_fetch_array($res);
	$title = $row['title'];
	$text = $row['text'];
	$date = $row['adate'];
	list($yr,$mt,$dt) = explode('-',$date);
	$adate = $dt.'-'.$mt.'-'.$yr;
	$order = $row['slct'];
	//$thumb_img = $row['thumb'];
	$active= $row['active'];
	
	}

}
	

// delete image

if(isset($_GET['gid'])&&($_GET['gid']!='') && isset($_GET['aid'])&&($_GET['aid']!=''))
{
  $gid= injection_free($_GET['gid']);
  $query ="select * from `gallery_image` where `gid`='$gid' ";
  $res = mysql_query($query) or die(mysql_error());
  $row = mysql_fetch_array($res);
  $imgname = $row['imgname'];
  $tmb_img = $row['tmb_img'];
  
  $query ="delete from `gallery_image` where `gid`='$gid' ";
  mysql_query($query);
			if(mysql_affected_rows()==1)
			{
			unlink("gallery/".$imgname);
			unlink("gallery/".$tmb_img);
			header("Location: gallery.php?aid=$aid");
			}//deleted
			else{}//not deleted
		   
}
else{}
	  
	  





if(isset($_POST['submit']))
{
  $title = injection_free($_POST['title']);
  $text = injection_free($_POST['text']);
  $slct = $_POST['slct'];
  

/* insertion part*/  
  
if($edit==0)
{
	
  if(record_exist($title))
  {
	  $str= "Record exists.";
  }
  
  else
  {
	
		if( $title!='' && $slct!='' )
		{
			
			$insert = mysql_insert_array('pages', $_POST, 'submit') ;
			
			if($insert['mysql_error']){$str.=$insert['mysql_error'];}
			else
			{
				
			if($insert['mysql_affected_rows']==1){$last_id = $insert['mysql_insert_id'] ;}
			
					
			/* no image */
			if(file_count($_FILES)==0)
			{
			$str.="Data inserted successfully.<meta http-equiv='Refresh' content='1; URL=gallery_man.php'/>";	
			}
			
			
			/* if there are images */
			else
			{
			$files_input = multiple_image_upload($_FILES['fields'], 'gallery_image', 'gallery', $last_id, 'gallery_man.php');
			
			$str.= $files_input;
			}  
					
			}//else
			
			
		}/* end of if( $title!='' && $order!='' ) */
		
	
   }/*end of else */
   

}//end insertion


	


/* modification edit part */

if($edit==1)
{
	$aid=injection_free($_GET['aid']);
	
		if( $title!='' && $order!='' )
		{
			
			$insert = mysql_update_array('pages', $_POST, 'submit', 'aid', $aid)  ;

			if($insert['mysql_error']){	$str.=$insert['mysql_error']; }
			else
			{
				
			if($insert['mysql_affected_rows']==1){ $last_id = $insert['mysql_insert_id'] ; }
			
					
			/* no image */
			if(file_count($_FILES)==0)
			{
			$str.="Data updated successfully.<meta http-equiv='Refresh' content='1; URL=gallery_man.php'/>";	
			}
			
			
			/* if there are images */
			else
			{
			$files_input = multiple_image_upload($_FILES['fields'], 'gallery_image', 'gallery', $aid, 'gallery_man.php');
			$str.= $files_input;
			}  
					
			}

		
		}/* end of if( $title!='' && $order!='' ) */

}//end modification	

	
}// end submit	
	
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<link href="css/dropdown.css" media="screen" rel="stylesheet" type="text/css" />
<link href="css/default.css" media="screen" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
tinyMCE.init({
	mode : "exact",
	elements : "elm3",
	
	theme : "advanced",
	skin : "default",
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,cleanup,code,|,forecolor,backcolor,|,gallery",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		//theme_advanced_toolbar_align : "left",
		//theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal : false,
		language : 'en',
		disk_cache : true
});
</script>
<script type="text/javascript">
var count = 0;
$(function(){
	$('a#add_field').click(function(){
		count += 1;
		$('#container').append(
				'<label>Image ' + count + '</label><br />' 
				+ '<input id="field_' + count + '" name="fields[]' + '" type="file" size="40"/><br />' );
	
	});
});
</script>
<title>GALLERY</title>
</head>
<body>
<table width="100%" height="25" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td align="left"><img src="../images/logo2.jpg" border="0" height="70" width="322"/></td>
    <td align="right" valign="bottom"><?php include "menu.php"; ?></td>
  </tr>
</table>
<table class="flasline" width="100%" height="5" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td></td>
  </tr>
</table>
<table align="center" width="98%" style="margin-top:20px; text-align:center;">
  <tr>
    <td width="75%"></td>
    <td width="1%">|</td>
    <td width="9%"><a href="gallery.php">Create New</a></td>
    <td width="1%">|</td>
    <td width="9%"><a href="gallery_man.php">View Contents</a></td>
    <td width="1%">|</td>
  </tr>
</table>
<form name="form1" method="post" action="" enctype="multipart/form-data">
  <input type="hidden" name="type" value="<?php echo $type ; ?>"  />
  <input type="hidden" name="active" value="<?php echo $active ; ?>"  />
  <table width="600" border="0" cellpadding="0" cellspacing="0" align="center">
    <tr>
      <td><h3 align="center">GALLERY</h3></td>
      <td></td>
    </tr>
    <tr>
      <td colspan="2" align="left"><font color="red">
        <?=$str?>
        </font></td>
    </tr>
    <tr>
      <td width="450"><p>
          <label>Order Number *<br>
            <select name="slct"  style="width:100px;" onblur="return blank(document.form1.order)" >
              <option value="">-Select-</option>
              <?php for($i=1;$i<=20;$i++){ ?>
              <option value="<?=$i?>" <?php if($i==$order){echo "selected";}?> >
              <?=$i?>
              </option>
              <? } ?>
            </select>
          </label>
        </p></td>
      <td><?php if(isset($_POST['submit'])&&$_POST['slct']==''){?>
        <font color="red">Field Missing</font>
        <? } ?></td>
    </tr>
    <tr>
      <td width="450"><p>
          <label>Title *</label>
          <br />
          <input type="text" name="title" style="width:460px;" value="<?=$title?>" />
        </p></td>
      <td><?php if(isset($_POST['submit'])&&$_POST['title']==''){?>
        <font color="red">Field Missing</font>
        <? } ?></td>
    </tr>
    <tr>
      <td width="450"><p>
          <label>Text </label>
          <br>
          <textarea name="text" cols="50" rows="13" onblur="return blank(this)" id="elm3"><?=$text?></textarea>
        </p></td>
      <td></td>
    </tr>
    <tr height="10">
      <td colspan="2"></td>
    </tr>
    <?php
	if($edit==1)
	{
	$qry = "select * from `gallery_image` where `aid`='$aid' ";
	$res = mysql_query($qry) or die(mysql_error());
	while($row = mysql_fetch_array($res)){
	$gid = $row['gid'];
    ?>
    <tr height="90">
      <td width="450"><img src="gallery/<?=$row['tmb_img']?>" width="80" height="80" /> <a href='gallery.php?gid=<?=$gid?>&aid=<?=$aid?>' onClick="return confirm('Are you sure you want to delete?')"> <img src="web_images/delete.gif" width="20" height="20" /></a></td>
      <td></td>
    </tr>
    <? }} ?>
    <tr>
      <td width="450"><p> <a href="#" id="add_field">+ Click to add Images +</a>&nbsp;(500x500, Only jpeg, gif. Maxsize 1 Mb)
        <div id="container"></div>
        </p></td>
      <td></td>
    </tr>
    <tr>
      <td width="450" align="right"><input name="submit" class="button-primary" value="Submit" type="submit" >
        </p></td>
      <td width="150"></td>
    </tr>
    <tr height="80">
      <td colspan="2"></td>
    </tr>
  </table>
</form>
</body>
</html>
