<?php 
include('header.php'); 


$type = 'event';
$type_upper=$type;

if(isset($_REQUEST['check'])) { 
			
			
			$artist = mysqli_escape_string($conn, $_REQUEST['artist']);
			$title = mysqli_escape_string($conn, $_REQUEST['title']);
								
			$date_day = mysqli_escape_string($conn, $_REQUEST['date_day']);
			$date_month = mysqli_escape_string($conn, $_REQUEST['date_month']);
			$date_year = mysqli_escape_string($conn, $_REQUEST['date_year']);
			
			$comm3 = mysqli_escape_string($conn, $_REQUEST['body']);
			
			$type = mysqli_escape_string($conn, $_REQUEST['type']);
			
			
			
			
			
			include("upload_pix.php");
						
				
					$sql="INSERT INTO  post (image_name, artist_id, category, title, date_day, date_month, date_year, body, posted_by) 
					VALUES 
					('$fn', '$artist', '$type', '$title', '$date_day', '$date_month', '$date_year', '$comm3', '$_SESSION[type]')"; 		
					mysqli_query($conn, $sql) or die(mysqli_error($conn));	
					$msg="<font color=\"blue\"><b>POSTING WAS SUCCESSFUL</b></font>";
					
					$last_id = mysqli_insert_id($conn);
					
				
					
					
				
}

if((isset($_REQUEST['action']))&&($_REQUEST['action']=="del")){
	$id = mysqli_escape_string($conn, $_REQUEST['id']);
	
				// DELETE PHYSICAL FILE 
				$query  = "SELECT image_name FROM post WHERE id ='$id'";
				$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
				$info = mysqli_fetch_array($result);
				$f_name= $info['image_name'];
				
				if(file_exists("piks/".$f_name)) { 
					unlink("piks/".$f_name);
				}else{
					
				}
				
				
	$query  = "DELETE FROM post WHERE id = '$id'";
	mysqli_query($conn, $query) or die(mysqli_error($conn));
	$msg="<font color=\"delete\"><b>Record deleted</b></font>";
	
	
}

if(isset($_REQUEST['editing'])){
	
	$select_id = mysqli_escape_string($conn, $_REQUEST['id']);

	$sql = "SELECT * FROM  post WHERE id='$select_id'";

	$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

	$info = mysqli_fetch_array($result);
		$id=$info['id'];
		$image_name=$info['image_name'];
		$title=$info['title'];
		
		$date_day=$info['date_day'];
		$date_month=$info['date_month'];
		$date_year=$info['date_year'];
		//$body=strip_tags($info['body']);
		$body=$info['body'];
		$posted_by=$info['posted_by'];
		
		$artist_id=$info['artist_id'];
}



if(isset($_REQUEST['edit_id'])){
	
			$edit_id = mysqli_escape_string($conn, $_REQUEST['edit_id']);
			
			$title = mysqli_escape_string($conn, $_REQUEST['title']);
			$category_series = mysqli_escape_string($conn, $_REQUEST['type']);
			$date_day = mysqli_escape_string($conn, $_REQUEST['date_day']);
			$date_month = mysqli_escape_string($conn, $_REQUEST['date_month']);
			$date_year = mysqli_escape_string($conn, $_REQUEST['date_year']);
			$artist = mysqli_escape_string($conn, $_REQUEST['artist']);
		
			$comm3 = mysqli_escape_string($conn, $_REQUEST['body']);
			
			
			
				$sql1 = "UPDATE  post SET  			 	
						title = '$title',						
						date_day = '$date_day',
						date_month = '$date_month',
						date_year = '$date_year', 
						artist_id = '$artist', 
						body = '$comm3' 
					WHERE id = '$edit_id'";
			mysqli_query($conn, $sql1) or die(mysqli_error($conn));	
					
					
					//Update 
			$msg="<font color=\"green\"><b>EDITING WAS SUCCESSFUL</b></font>";
			
			
}
?>


      <!--main content start-->
      <section id="main-content">
        <section class="wrapper">
		 
			  
              <div class="row">
                  <div class="col-lg-12">
                      <section class="panel">
                          <header class="panel-heading">
                              <b>
                              <?php
                              print $type_upper;
                              ?></b>
						  </header>
                          <div class="panel-body">
                          
						<?php
						if(isset($msg)){
							print $msg;
						}
						?>                         
                          
                          
                        <div class="form">
                                  <form class="form-validate form-horizontal" method="POST" action="" enctype="multipart/form-data">
                                  
                                                                                                        
                                      
									  <div class="form-group ">
                                          <label for="cname" class="control-label col-lg-2">Title <span class="required">*</span></label>
                                          <div class="col-lg-5">
                                              <input class="form-control" name="title" minlength="1" type="text" <?php if(isset($_REQUEST['editing'])){ print "value=\"$title\""; } ?> required />
                                          </div>
                                      </div>
								 <?php
								  if( (!isset($_REQUEST['editing']))&&(!isset($_REQUEST['edit_id'])) ){
									//if((!$_REQUEST['action'])&&(!$_REQUEST['action']=="do_upload")){
								  ?>  
									  
                                  <div class="form-group ">
                                          <label for="cname" class="control-label col-lg-2">Picture</label>
                                          <div class="col-lg-5">
                                             
                                              <input class="form-control" name="userfile" type="file" id="userfile" />
                                          Format: PNG, BMP, GIV, JPG or JPEG </div>
                                    </div>
                                   <?php
								  }
								   ?>  
                                    
                                      
									  
									 
                                     
                                      
                                       <div class="form-group ">
                                          <label for="curl" class="control-label col-lg-2">Date</label>
                                          <div class="col-lg-5"><span class="col-lg-3">
                                          <select class="form-control"  name="date_day">
                                              <?php
											  $curr_day=date('d');
                                                for($d=1;$d<=32;$d++){														
													if((isset($date_day))&&($date_day==$d)){
														print "<option selected=\"selected\">".$d."</option>";
													}elseif((!$date_day)&&($d == $curr_day)){
														print "<option selected=\"selected\">".$d."</option>";
													}else{
                                                    	print "<option>".$d."</option>";
													}
                                                }
                                                ?>
                                            </select>
                                          </span><span class="col-lg-3">
                                          <select class="form-control"  name="date_month">
                                            <?php
											$curr_month=date('m');
                                                for($m=1;$m<=12;$m++){
													if((isset($date_month))&&($date_month==$m)){
														print "<option selected=\"selected\">".$m."</option>";
													}elseif((!$date_month)&&($m == $curr_month)){
														print "<option selected=\"selected\">".$m."</option>";
													}else{
                                                    	print "<option>".$m."</option>";
													}
                                                }
                                                ?>
                                          </select>
										 
                                          </span><span class="col-lg-4">
                                          <select class="form-control"  name="date_year">
                                            <?php
											$curr_year=date('Y');
                                                for($y=2010;$y<=$curr_year;$y++){
													if((isset($date_year))&&($date_year==$y)){
														print "<option selected=\"selected\">".$y."</option>";
													}elseif((!$date_year)&&($y == $curr_year)){
														print "<option selected=\"selected\">".$y."</option>";
													}else{
                                                    	print "<option>".$y."</option>";
													}
                                                }
                                                ?>
                                          </select>
                                          </span></div>
                                      </div>
                                      
                                                                     
                                      <div class="form-group ">
                                          <label for="body" class="control-label col-lg-2">Detail</label>
                                          <div class="col-lg-5">
                                              <textarea class="form-control "  name="body" rows="10"><?php if(isset($_REQUEST['editing'])){ print $body; } ?></textarea>
                                          </div>
                                      </div>
                                      
                                      
                                      
                                      
                                      <div class="form-group">
                                          <div class="col-lg-offset-2 col-lg-10">
                                          
											  
											  <?php
											  print "<input name=\"type\" type=\"hidden\" value=\"$type\" />";
											  
											  if(isset($_REQUEST['editing'])){
												  $select_id = mysqli_escape_string($conn, $_REQUEST['id']);
												  print "<input name=\"edit_id\" type=\"hidden\" value=\"$select_id\" />";
												  print '<button class="btn btn-warning" type="submit">Update</button>';
											  }else{
												  print '
												  <button class="btn btn-primary" type="submit">Save</button>
													<input type="hidden" name="action" value="do_upload" />
													<input name="check" type="hidden" id="check" value="yes" />
												  ';
											  }
											  ?>
                                          </div>
                                      </div>
                                      
                                     
                                  </form>
                              </div>

                          </div>
                      </section>
                  </div>
              </div>
			  
			  
	

              <div class="row">
                  <div class="col-lg-12">
                      <section class="panel">
                         <header class="panel-heading">
                                                         
                            Record
                             </header>
                          <div class="panel-body">
<?php
// how many rows to show per page
$rowsPerPage = 20;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$query  = "SELECT * FROM post WHERE category = '$type' ORDER BY id DESC LIMIT $offset, $rowsPerPage";
$result = mysqli_query($conn,$query) or die(mysqli_error($conn));

if (mysqli_num_rows($result) == 0) {
 print "SORRY: no data...";
 exit;
}

print "
<table class=\"table table-striped table-advance table-hover\">
<tr>
	<th><b>Id</b></th>
	<th><b>Type</b></th>
	<th><b>Category</b></th>
	<th><b>Image</b></th>
	<th><b>Title</b></th>	
	<th><b>Date</b></th>
	<th><b>Body</b></th>
	<th><b></b></th>
	<th></th>
</tr>
";
// print the random numbers
while($info = mysqli_fetch_array($result))
{
 		 
	$id=$info['id'];
	$artist_id=$info['artist_id'];
	$image_name=$info['image_name'];
	$title=$info['title'];
	$date_day=$info['date_day'];
	$date_month=$info['date_month'];
	$date_year=$info['date_year'];
	$body=$info['body'];
	$posted_by=$info['posted_by'];
	$category=$info['category'];
	
	
	
	
	
	$limit_body = substr($body, 0, 300)."...";
	$limit_body = str_replace("\n", '<br />', $limit_body);	
	
		
		$query_more  = "SELECT * FROM post_more_picture WHERE art_id = '$id' ORDER BY id DESC";
		$result_more = mysqli_query($conn,$query_more) or die(mysqli_error($conn));
		$more = mysqli_num_rows($result_more);
		
		
		 
	print " 
	<tr>
	<td>$id</td>
	<td>$category</td>
	
	<td>$image_name <br/><a href=\"change_pix.php?id=$id&type=$type\">Add/Change Picture</a> </td>
	<td>($more) <a href=\"more.php?art=$id&type=$type\">$title</a></td>	
	<td>$date_day/$date_month/$date_year</td>
	<td>$limit_body</td>
	<td>
";
		
	print "<a href=\"?id=$id&editing=1&type=$type\">Edit</a> | <a href=\"?id=$id&action=del&type=$type\">Delete</a>";

print "		
	</td>	
	</tr>
	
	";
	}
	
	//close up ther table

	print "</table><p>";

	



// how many rows we have in database
$query   = "SELECT COUNT(id) AS numrows FROM post WHERE category = '$type'";
$result  = mysqli_query($conn,$query) or die(mysqli_error($conn));
$row     = mysqli_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];


// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
	if ($page == $pageNum)
	{
		$nav .= " $page ";   // no need to create a link to current page
	}
	else
	{
		$nav .= " <a href=\"$self?page=$page&type=$type\">$page</a> ";
	}		
}

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1)
{
	$page = $pageNum - 1;
	$prev = " <a href=\"$self?page=$page&type=$type\">[Prev]</a> ";
	
	$first = " <a href=\"$self?page=1&type=$type\"> [First Page]</a> ";
} 
else
{
	$prev  = '&nbsp;'; // we're on page one, don't print previous link
	$first = '&nbsp;'; // nor the first page link
}

if ($pageNum < $maxPage)
{
	$page = $pageNum + 1;
	$next = " <a href=\"$self?page=$page&type=$type\">[Next]</a> ";
	
	$last = " <a href=\"$self?page=$maxPage&type=$type\">[Last Page]</a> ";
} 
else
{
	$next = '&nbsp;'; // we're on the last page, don't print next link
	$last = '&nbsp;'; // nor the last page link
}

// print the navigation link
echo $first . $prev . $nav . $next . $last;

// and close the database connection
// include '../conn/closedb.php';
?>
                          </div>
                      </section>
                  </div>
              </div>	
			  
			  
              <!-- page end-->
          </section>
      </section>
      <!--main content end-->
  </section>
  <!-- container section end -->

    <!-- javascripts -->
    <script src="../js/jquery.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <!-- nice scroll -->
    <script src="../js/jquery.scrollTo.min.js"></script>
    <script src="../js/jquery.nicescroll.js" type="text/javascript"></script>
    <!-- jquery validate js -->
    <script type="text/javascript" src="../js/jquery.validate.min.js"></script>

    <!-- custom form validation script for this page-->
    <script src="../js/form-validation-script.js"></script>
    <!--custome script for all page-->
    <script src="../js/scripts.js"></script>    


  </body>
</html>
