<?php include('../includes/head.php'); ?>
<?php
	if(isset($_POST['submit'])){
		$cname = mysql_real_escape_string($_POST['nname']);
		$corder = $_POST['norder'];
		
		$thumb = $_FILES['thumb']['name'];
		
		$addNews = mysql_query("INSERT INTO `news` (`id`, `name`, `norder`, `thumb`) VALUES (NULL, '$cname', '$corder', '$thumb');");
		
		if($addNews){
			$nId = mysql_insert_id();
			
			if (!file_exists('../../news/'.$nId.'')) {
				mkdir('../../news/'.$nId.'', 0777, true);
			}
			
			if (!file_exists('../../news/'.$nId.'/thumb')) {
				mkdir('../../news/'.$nId.'/thumb', 0777, true);
			}
			
			$uploaddir = '../../news/'.$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['newsGallery']['name']);
			//echo $_FILES['gallery']['name'][0];
			
			if (!file_exists('../../news/'.$nId.'/gallery')) {
				mkdir('../../news/'.$nId.'/gallery', 0777, true);
			}
			
			for($i=0;$i<$uploadSize;$i++){
				
				$imgName = $_FILES['newsGallery']['name'][$i];
				
				// Insert into DB
				$insertIntoDB = mysql_query("INSERT INTO `news_gallery` (`id`, `nid`, `path`, `date_of_added`) VALUES (NULL, '$nId', '$imgName', now());");
				
				$uploaddir3 = '../../news/'.$nId.'/gallery/';
				$uploadfile3 = $uploaddir3 . basename($_FILES['newsGallery']['name'][$i]);
				
				if (move_uploaded_file($_FILES['newsGallery']['tmp_name'][$i], $uploadfile3)) {
					echo "File is valid, and was successfully uploaded.\n";
					echo '<br/>';
				}
			}
			
			echo "<script>alert('News Added');window.location.href ='news-pr.php';</script>";
		}
	}
?>

<?php
	$get_productlist = mysql_query("select * from news");
	$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="../../news/'.$id.'/thumb/'.$thumb.'" class="img-responsive" style="width:60px;"></th>
								<th>'.$name.'</th>
								<th>'.$norder.'</th>
								<th><a href="edit-news.php?cid='.$id.'">Edit</a> | <a href="news-pr.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 `news` WHERE `id` = '$delId'");
		if($delProperty){
			unlink('../../news/'.$delId.'/thumb/'.$thumbDel.'');
			rmdir('../../news/'.$delId.'/thumb');
			
			$getGallery = mysql_query("DELETE FROM `news_gallery` WHERE `id` = '$delId'");
			if($getGallery){
				$files = glob('../../news/'.$delId.'/gallery/*'); // get all file names
				foreach($files as $file){ // iterate files
				  if(is_file($file))
					unlink($file); // delete file
				}
				rmdir('../../news/'.$delId.'/gallery');
			}
			rmdir('../../news/'.$delId.'');
			
			echo "<script>alert('News deleted');window.location.href ='news-pr.php';</script>";
		}
	}
?>


<?php
	if(isset($_POST['submitVideo'])){
		$vname = $_POST['vname'];
		$vurl = $_POST['vurl'];
		
		$insertVideo = mysql_query("INSERT INTO `news_videos` (`id`, `name`, `url`) VALUES (NULL, '$vname', '$vurl');");
		if($insertVideo){
			echo "<script>alert('Video Added');window.location.href ='news-pr.php';</script>";
		}
	}
?>

<?php
	// Get Video List
	$getVideoList=mysql_query("select * from news_videos");
	$videoList='';
	$vCount = mysql_num_rows($getVideoList);
	if($vCount>0){
		while($vRows = mysql_fetch_array($getVideoList)){
			$vid = $vRows['id'];
			$name = $vRows['name'];
			$url = $vRows['url'];
			
			$videoList.='<tr>
							<th><iframe width="100%" height="100" src="'.$url.'" frameborder="0" allowfullscreen></iframe></th>
							<th>'.$name.'</th>
							<th><a href="news-pr.php?deleteVideo='.$vid.'">Delete</a></th>
						</tr>';
		}
	}else{
		$videoList.='Videos not found!';
	}
?>

<?php
	if(isset($_GET['deleteVideo'])){
		$delId = $_GET['deleteVideo'];
		$delProperty = mysql_query("DELETE FROM `news_videos` WHERE `id` = '$delId'");
		if($delProperty){
			echo "<script>alert('Video deleted');window.location.href ='news-pr.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-6">
				<h2 style="font-family:Philosopher; font-size:24px;">Add News / PR Media</h2>
				<form action="news-pr.php" method="post" enctype="multipart/form-data" class="collectionForm">
				<div class="col-sm-12">
					<h3 style="color:#000;">News Name: </h3>
					<input type="text" name="nname" class="form-control" required placeholder="News Name">
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">News Order: </h3>
					<input type="text" name="norder" class="form-control" required placeholder="News Order">
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">News 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;">News Gallery Images: </h3>
					<input type="file" name="newsGallery[]" 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;">News/PR Media 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 class="col-sm-6">
				<h2 style="font-family:Philosopher; font-size:24px;">Add News / PR Videos</h2>
				<form action="news-pr.php" method="post" enctype="multipart/form-data" class="collectionForm">
				<div class="col-sm-12">
					<h3 style="color:#000;">Video Name: </h3>
					<input type="text" name="vname" class="form-control" required placeholder="Video Name">
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">Video URL: </h3>
					<input type="url" name="vurl" class="form-control" required placeholder="Video URL">
				</div>
				<div class="col-sm-12">
				<br/>
				</div>
				<div class="col-sm-12">
					<input type="submit" name="submitVideo" class="btn btn-primary">
				</div>
				<div class="col-sm-12">
					<h2 style="font-family:Philosopher; font-size:24px;">News/PR Video List</h2>
					<table class="table table-bordered">
						<tr>
							<th>video</th>
							<th>Name</th>
							<th>Action</th>
						</tr>
						<?php echo $videoList; ?>
					</table>
				</div>
				</form>
			</div>
        </div><!--//container-->
    </section><!--//contact-->
<?php include('../includes/footerjs.php'); ?>
</body>
</html>