<?php


 error_reporting(0);

include("../include/config.php"); 


##########################################################################################

//For Insert Data
if(array_key_exists("add_blogs_details",$_REQUEST)){
    
//    echo "add_blogs_details vvbgbh"; die;
	
    $b_title = mysqli_real_escape_string($conn,$_POST['blogs_title']);
	$postedBy = mysqli_real_escape_string($conn,$_POST['post_by']);
	$m_keywords = mysqli_real_escape_string($conn,$_POST['keyword']);
// 	$m_matetags = mysqli_real_escape_string($conn,$_POST['metatag']);
	$m_desc = mysqli_real_escape_string($conn,$_POST['description']);
    $b_details = mysqli_real_escape_string($conn,$_POST['ckeditor_library_course_details']);
    $current_date = date("d-M-Y");
	$c_date = date("d");
	$c_month = date("M");
        
    $url = create_slug($b_title);

	    if(isset($_FILES['image_a']['name']))

                {

                    $fileName1 = time().$_FILES["image_a"]["name"];   

                    $fileTmpLoc1 = $_FILES["image_a"]["tmp_name"];

                    $fileType1 = $_FILES["image_a"]["type"];

                    $fileSize1 = $_FILES["image_a"]["size"];

                    $fileErrorMsg1 = $_FILES["image_a"]["error"];

                    $kaboom1 = explode(".", $fileName1);

                    $fileExt1 = end($kaboom1);



                    $moveResult1 = move_uploaded_file($fileTmpLoc1, "blogs-files/$fileName1"); 

                }
	
    $query="INSERT INTO `blog`(`title`, `url`, `date_add`, `c_date`, `c_month`, `blogs_details`, `post_by`, `picture`, `keyword`, `description`,`status`) VALUES ('$b_title', '$url', '$current_date', '$c_date', '$c_month', '$b_details', '$postedBy', '$fileName1', '$m_keywords', '$m_desc', 'active')";
			
//     echo $query; die;       
            
			if (mysqli_query($conn, $query)) {
//				header("Location:add-blogs.php");
//				echo "in";
				echo "<script>window.location.href='add-blogs.php';</script>";
				exit;
			} 
            
			else {
				include('include/error-modal.php');
				echo "error";
			}
			mysqli_close($conn);
}


##########################################################################################


//For update Data
if(array_key_exists("update_blogs_details",$_REQUEST)){
//    echo "update_blogs_de tails"; die;
	$blogs_id = mysqli_real_escape_string($conn,$_POST['blogs_id']);
	$blogs_images = mysqli_real_escape_string($conn,$_POST['blogs_images']);
//	echo  "blogs id : " .$blogs_id; die;
	
   $b_title = mysqli_real_escape_string($conn,$_POST['blogs_title']);
	$postedBy = mysqli_real_escape_string($conn,$_POST['post_by']);
	$m_keywords = mysqli_real_escape_string($conn,$_POST['keyword']);
// 	$m_matetags = mysqli_real_escape_string($conn,$_POST['metatag']);
	$m_desc = mysqli_real_escape_string($conn,$_POST['description']);
    $b_details = mysqli_real_escape_string($conn,$_POST['ckeditor_library_course_details']);
  
        
    $url = create_slug($b_title);

	    if(isset($_FILES['image_a']['name']))

                {

                    $fileName1 = time().$_FILES["image_a"]["name"];   

                    $fileTmpLoc1 = $_FILES["image_a"]["tmp_name"];

                    $fileType1 = $_FILES["image_a"]["type"];

                    $fileSize1 = $_FILES["image_a"]["size"];

                    $fileErrorMsg1 = $_FILES["image_a"]["error"];

                    $kaboom1 = explode(".", $fileName1);

                    $fileExt1 = end($kaboom1);



                    $moveResult1 = move_uploaded_file($fileTmpLoc1, "blogs-files/$fileName1"); 

                }
	if(empty($_FILES['image_a']['name'])) 

                { 

                    $fileName1 = $blogs_images; 

                }
	
    
    
    $query1 = "UPDATE `blog` SET `title` = '$b_title', `url` = '$url', `blogs_details` = '$b_details', `post_by` = '$postedBy', `picture` = '$fileName1', `keyword` = '$m_keywords',  `description` = '$m_desc'  WHERE `blog`.`id` = $blogs_id";
    
//     echo $query1; die;
            
            
			if (mysqli_query($conn, $query1)) {
				echo "<script>window.location.href='add-blogs.php';</script>";
				exit;
			} 
            
			else {
				include('include/error-modal.php');
			}
			mysqli_close($conn);
}


##########################################################################################

// status active  data
if(array_key_exists("status_active_blogs",$_REQUEST)){
   
        $net_id = $_GET['id'];
        
        	 $query="UPDATE `blog` SET `status` = 'active' WHERE `blog`.`id` = '$net_id'";

			if (mysqli_query($conn, $query)) {
				header("location:add-blogs.php");
			} 
            
			else {
				include('include/error-modal.php');
			}
			mysqli_close($conn);
    
}

##########################################################################################
// status deactive data
if(array_key_exists("status_deactive_blogs",$_REQUEST)){
    
  $net_id = $_GET['id'];
        
        	 $query="UPDATE `blog` SET `status` = 'deactive' WHERE `blog`.`id` = '$net_id'";
		
            
			if (mysqli_query($conn, $query)) {
				header("location:add-blogs.php");
			} 
            
			else {
				include('include/error-modal.php');
			}
			mysqli_close($conn);
    
}


##################################################################################################

//For detete data
if(array_key_exists("Delete_blogs_details",$_REQUEST)){
    
    $t_id = $_GET['id'];
    
//  echo $t_id; die;
    
        
    $query="DELETE FROM `blog` WHERE `blog`.`id` = '$t_id'";
			
            
            
			if (mysqli_query($conn, $query)) {
				header("location:add-blogs.php");
			} 
            
			else {
				include('include/error-modal.php');
			}
			mysqli_close($conn);
}

####################################################################################

?>
