<?php include('../includes/head.php'); ?>
<?php
	if(isset($_POST['submit'])){
		$cname = mysql_real_escape_string($_POST['cname']);
		$corder = $_POST['corder'];
		$thumb = $_FILES['thumb']['name'];
		
		$addNews = mysql_query("INSERT INTO `campaign` (`id`, `name`, `norder`, `thumb`) VALUES (NULL, '$cname', '$corder', '$thumb');");
		
		if($addNews){
			$nId = mysql_insert_id();
			
			if (!file_exists('../../campaign/'.$nId.'')) {
				mkdir('../../campaign/'.$nId.'', 0777, true);
			}
			
			if (!file_exists('../../campaign/'.$nId.'/thumb')) {
				mkdir('../../campaign/'.$nId.'/thumb', 0777, true);
			}
			
			$uploaddir = '../../campaign/'.$nId.'/thumb/';
			$uploadfile = $uploaddir . basename($_FILES['thumb']['name']);
			
			if (move_uploaded_file($_FILES['thumb']['tmp_name'], $uploadfile)) {
				echo "File is valid, and was successfully uploaded.\n";
				echo '<br/>';
			} else {
				echo "Possible file upload attack!\n";
				echo '<br/>';
			}
			
			// Add Gallery
			
			//print_r($_FILES);
			$uploadSize = sizeof($_FILES['campaignGallery']['name']);
			//echo $_FILES['gallery']['name'][0];
			
			if (!file_exists('../../campaign/'.$nId.'/gallery')) {
				mkdir('../../campaign/'.$nId.'/gallery', 0777, true);
			}
			
			for($i=0;$i<$uploadSize;$i++){
				
				$imgName = $_FILES['campaignGallery']['name'][$i];
				
				// Insert into DB
				$insertIntoDB = mysql_query("INSERT INTO `campaign_gallery` (`id`, `cid`, `path`, `date_of_added`) VALUES (NULL, '$nId', '$imgName', now());");
				
				$uploaddir3 = '../../campaign/'.$nId.'/gallery/';
				$uploadfile3 = $uploaddir3 . basename($_FILES['campaignGallery']['name'][$i]);
				
				if (move_uploaded_file($_FILES['campaignGallery']['tmp_name'][$i], $uploadfile3)) {
					echo "File is valid, and was successfully uploaded.\n";
					echo '<br/>';
				}
			}
			
			echo "<script>alert('Campaign Added');window.location.href ='campaign.php';</script>";
		}
	}
?>

<?php
	$get_productlist = mysql_query("select * from campaign");
	$product_list='';
	$get_count = mysql_num_rows($get_productlist);
	if($get_count>0){
		while($row = mysql_fetch_array($get_productlist)){
			$id = $row['id'];
			$name = $row['name'];
			$norder = $row['norder'];
			$thumb = $row['thumb'];
			
			$product_list.='<tr>
								<th><img src="../../campaign/'.$id.'/thumb/'.$thumb.'" class="img-responsive" style="width:60px;"></th>
								<th>'.$name.'</th>
								<th>'.$norder.'</th>
								<th><a href="edit-campaign.php?cid='.$id.'">Edit</a> | <a href="campaign.php?deleteNews='.$id.'&thumb='.$thumb.'" onclick="return checkDelete()">Delete</a></th>
							</tr>
							';
		}
	}
?>

<?php
	if(isset($_GET['deleteNews'])){
		$delId = $_GET['deleteNews'];
		$thumbDel = $_GET['thumb'];
		
		$delProperty = mysql_query("DELETE FROM `campaign` WHERE `id` = '$delId'");
		if($delProperty){
			unlink('../../campaign/'.$delId.'/thumb/'.$thumbDel.'');
			rmdir('../../campaign/'.$delId.'/thumb');
			
			$getGallery = mysql_query("DELETE FROM `campaign_gallery` WHERE `id` = '$delId'");
			if($getGallery){
				$files = glob('../../campaign/'.$delId.'/gallery/*'); // get all file names
				foreach($files as $file){ // iterate files
				  if(is_file($file))
					unlink($file); // delete file
				}
				rmdir('../../campaign/'.$delId.'/gallery');
			}
			rmdir('../../campaign/'.$delId.'');
			
			echo "<script>alert('Campaign deleted');window.location.href ='campaign.php';</script>";
		}
	}
?>
<script src="ckeditor/ckeditor.js"></script>
<style>
	h3{
		font: normal 14px/20px Philosopher;
	}
	.collectionForm .col-sm-12{
		padding:0;
	}
</style>
</head>
<body>
	<?php include('../includes/header.php'); ?>
	<!-- ******CONTACT****** --> 
    <section id="product">
        <div class="container">
            <div class="col-sm-12">
				<h2 style="font-family:Philosopher; font-size:24px;">Campaign</h2>
				<form action="campaign.php" method="post" enctype="multipart/form-data" class="collectionForm">
				<div class="col-sm-12">
					<h3 style="color:#000;">Campaign Name: </h3>
					<input type="text" name="cname" class="form-control" required placeholder="Campaign Name">
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">Campaign Order: </h3>
					<input type="text" name="corder" class="form-control" required placeholder="Campaign Order">
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">Campaign Thumb Image: (600px X 503px)</h3>
					<input type="file" name="thumb" class="form-control" required> 
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">Campaign Gallery: </h3>
					<input type="file" name="campaignGallery[]" class="form-control" multiple required /> 
				</div>
				<div class="col-sm-12">
				<br/>
				</div>
				<div class="col-sm-12">
					<input type="submit" name="submit" class="btn btn-primary">
				</div>
				<div class="col-sm-12">
					<h2 style="font-family:Philosopher; font-size:24px;">Campaign List</h2>
					<table class="table table-bordered">
						<tr>
							<th>Thumb</th>
							<th>Name</th>
							<th>Date</th>
							<th>Action</th>
						</tr>
						<?php echo $product_list; ?>
					</table>
				</div>
				</form>
			</div>
        </div><!--//container-->
    </section><!--//contact-->
<?php include('../includes/footerjs.php'); ?>
</body>
</html>