<?php
//error_reporting (E_ALL ^ E_NOTICE);
  session_start();
if(!isset($_SESSION['username'])){
     header('Location:index.php');
}

include('master/config.php');
	 $query=mysqli_query($conn, "SELECT * FROM `add-blog` where id = '".$_GET['id']."'");
     $rows=mysqli_fetch_assoc($query);
if(isset($_POST['update']))
{
	$postTitle 	= $_POST['title'];
	$postContent = $_POST['text'];
    $postImage = $_FILES["image"]["name"];  
//	$postImage = addslashes(file_get_contents($_FILES["image"]["name"]));  
    $dir = 'master/blog-image/';
    $dirpath = $dir . $postImage;
   // $dirtype = pathinfo($dirpath,PATHINFO_EXTENSION);
    //print_r($dirtype);die;
	$created = date("Y-m-d H:i:s");
        $slug = slugify($postTitle);
    //echo $postContent;die();

                   
	if($_FILES['image']['error'] != 0){
	    $image = $rows['image'];
	}else{
	    $image = $postImage;
	}
	
	
	$sql = 'UPDATE `add-blog` SET `title`="'.$postTitle.'",`slug`="'.$slug.'",`text`="'.$postContent.'", `image`="'.$image.'" where id = "'.$_GET['id'].'"';
    

    
  $image_file = move_uploaded_file($_FILES['image']['tmp_name'], $dirpath);
    
   // $image_result = mysqli_query($conn, $image_file);
	
	$result = mysqli_query($conn, $sql);
	
	if($result)
	{
		echo "<script>alert('Post has been saved successfully');</script>";
        
        
	}
	else
	{
		echo "<script>alert('Unable to save post');</script>";
	}
    
    
   
        
    
	
}


function slugify($text)
{
  // replace non letter or digits by -
  $text = preg_replace('~[^\pL\d]+~u', '-', $text);

  // transliterate
  //$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

  // remove unwanted characters
  $text = preg_replace('~[^-\w]+~', '', $text);

  // trim
  $text = trim($text, '-');

  // remove duplicate -
  $text = preg_replace('~-+~', '-', $text);

  // lowercase
  $text = strtolower($text);

  if (empty($text)) {
    return 'n-a';
  }

  return $text;
}


?>


<!DOCTYPE html>
<html dir="ltr" lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- Favicon icon -->
  

     <?php require('master/link.php');?>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>

    <div class="preloader">
        <div class="lds-ripple">
            <div class="lds-pos"></div>
            <div class="lds-pos"></div>
        </div>
    </div>

    <div id="main-wrapper" data-layout="vertical" data-navbarbg="skin5" data-sidebartype="full" data-sidebar-position="absolute" data-header-position="absolute" data-boxed-layout="full">

       <?php require('master/header.php');?>

       <?php require('master/sidebar.php');?>

        <div class="page-wrapper">

            <div class="page-breadcrumb">
                <div class="row align-items-center">
                    <div class="col-5">
                        <h4 class="page-title">Add Blog</h4>
                        <div class="d-flex align-items-center">
                            <nav aria-label="breadcrumb">
                                <ol class="breadcrumb">
                                    <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                                    <li class="breadcrumb-item active" aria-current="page">Add Blog</li>
                                </ol>
                            </nav>
                        </div>
                    </div>
<!--
                    <div class="col-7">
                        <div class="text-right upgrade-btn">
                            <a href="https://wrappixel.com/templates/xtremeadmin/" class="btn btn-danger text-white" target="_blank">Upgrade to Pro</a>
                        </div>
                    </div>
-->
                </div>
            </div>

            <div class="container-fluid">

                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                               Add Blog
                            </div>
                        </div>
                    </div>
                </div>
                
                
          
                 <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                    <form autocomplete="off" class="form" id="formLogin" name="formLogin" role="form" method="POST" enctype="multipart/form-data">
                  <div class="form-group">
                    <label for="uname1">Title</label> 
										<input value="<?php echo $rows['title']; ?>" name="title" class="form-control" id="uname1" name="uname1" required="" type="text">
                  </div>
                                        
                     <div class="form-group">                   
                      <div class="grid-container">
			<div class="grid-width-100" >
				<textarea id="editor" name="text">
					<?php echo $rows['text']; ?>
				</textarea>
			</div>
		</div>                  
                     </div>                    
                  <div class="form-group">
                   
                      <img src="">
                      <input value="<?php echo $rows['image']; ?>" name="image" type="file" class="my-pond" name="filepond"/><br/>
                      <div class="image-edit" style="width: 25%; margin-top: 17px;">
                      <?php echo '<img src="master/blog-image/'.$rows["image"].'" style="width:58%;">' ?>
                      </div>
                  </div>
                                        
                                        <input type="submit" name="update" value="update" class="btn btn-success btn-lg float-right" >
                  
<!--									<button name="submit" name="update" class="btn btn-success btn-lg float-right" type="submit">Add Blog</button>-->
                </form>
                            </div>
                        </div>
                    </div>
                </div>

            </div>

            <footer class="footer text-center">
                 <a href="https://www.iglobesolution.com/" target="_blank">iglobesolution.com</a>
            </footer>

        </div>

    </div>

       <?php require('master/footer.php');?>
    <script>
        initSample();
    </script>
</body>

</html>