	<?php session_start();
	//ini_set('display_errors', 1);
    //ini_set('display_startup_errors', 1);
    //error_reporting(E_ALL);
	$_SESSION['loggedinuser'];
	$loggedinuser=$_SESSION['loggedinuser'];
    //check if the user is logged in
     if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
    } else {
    header('Location: index.php');
    }
	date_default_timezone_set("Africa/Nairobi");
    ?>
	<!DOCTYPE html>
	<html 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="">
	<link rel="icon" href="../images/favicon.png">

	<title>Deadly Force::People killed by the police in Kenya</title>

	<!-- Bootstrap core CSS -->
	<link href="../css/bootstrap.css" rel="stylesheet">
	<link href="../css/zebra_datepicker_metallic.css" rel="stylesheet">
	<!-- Bootstrap theme -->
	<link href="../css/bootswatch_min.css" rel="stylesheet">

	<!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
	<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
	<script src="../../assets/js/ie-emulation-modes-warning.js"></script>

	<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
	<!--[if lt IE 9]>
	<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
	<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
	<![endif]-->
	
	</head>

	<body role="document">

	<!-- Fixed navbar -->
	<div class="navbar navbar-default navbar-fixed-top" role="navigation">
	<div class="container">
	<div class="navbar-header">
	<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
	<span class="sr-only">Toggle navigation</span>
	<span class="icon-bar"></span>
	<span class="icon-bar"></span>
	<span class="icon-bar"></span>
	</button>
	<a class="navbar-brand" href="index.php">
	<img src="../images/newsplex_logo.png" width="145" height="70" alt="nation newsplex"/>
	<div class="header-slogan">
	<span class="big-slogan">Deadly force</span><br>
	<span class="slogan">People killed by the police in Kenya</span>
	</div>
	</a>
	</div>
	<div class="navbar-collapse collapse">
	<ul class="nav navbar-nav">
	<li><a href="../tip.php">Submit tip</a></li>
	<li><a href="../index.php#database">Database</a></li>
	<li><a href="../about.php">About</a></li>
	<li><a href="../articles.php">Read articles</a></li>
	</ul>
	</li>
	</ul>
	</div><!--/.nav-collapse -->
	</div>
	</div>

	<div class="container main">

	<p><h1>Add police killing victim &raquo;</h1></p>
	<!-- Forms
	================================================== -->
	<div class="bs-docs-section">
	<div class="row">
	<div class="col-lg-12">
	<div class="well bs-component">
	<form enctype='multipart/form-data' class="form-horizontal" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
	<fieldset>
	<?php

	if(isset($_POST['submit']))
	{ 
	
	ini_set("post_max_size", "20M");
    ini_set("upload_max_filesize", "20M");
    ini_set("memory_limit", "20000M");
	
	//upload the file to tmp directory for conversion
	$main_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
	$upload_dir = $_SERVER['DOCUMENT_ROOT'] .  dirname($_SERVER['PHP_SELF']).'/images/victims';
	$upload_url = '/';

	$temp_name = $_FILES['filename']['tmp_name'];
	$file_name = $_FILES['filename']['name'];

	$file_name=strtolower($file_name);
	
	//add date to the filename
	$file_name=date('d-m-Y').'_'.$file_name;

	$file_name = preg_replace('/\s+/', '_', $file_name);	

	$file_path = $upload_dir.$upload_url.$file_name;
	$mydir = "/images/victims/".$file_name;
	//echo $mydir."</br>";
	$link=$main_dir.$mydir;
	echo $link;
	exit();

	if(($_FILES["filename"]["type"] != "image/gif")
	&& ($_FILES["filename"]["type"] != "image/jpeg")
	&& ($_FILES["filename"]["type"] != "image/jpg")
	&& ($_FILES["filename"]["type"] != "image/png"))
	{
	echo "<div class=\"alert alert-danger\">File must be an image!</div>";
	exit;
	}

	if(($_FILES["filename"]["size"]/1024 > 20480))
	{
	echo "<div class=\"alert alert-danger\">File size must not exceeed 20MB!</div>";
	exit;
	}
	//check if file exists
    if (file_exists("../images/victims"  .$file_name))
      {
      echo "<div class=\"alert alert-danger\">File ".$_FILES["filename"]["name"] . " already exists on our database!. "."</div>";
	  exit();
      }
	if(move_uploaded_file($temp_name, $file_path))
	{
	//echo "<div class=\"alert alert-success\">File upload successful !</div>"."<br>";
	//echo "Link: "."<a href=\"#\" onclick=\"document.getElementById('txtpath').value='$link';\">$link</a>"."<br/>";
	//echo "File: " . $file_name . "<br>";
	//echo "Type: " . $_FILES["filename"]["type"] . "<br>";
	//echo "Size: " . ($_FILES["filename"]["size"] / 1024) . " kB<br>";
    }
	//end of file upload

	if (is_uploaded_file($link)) {
	//echo "<h1>" . "File: ". $link ." uploaded successfully." . "</h1>";
	}
	
	$servername = "localhost";
	$dbuser = "plexusrs";
	$dbpass = "sTUmTaQsSsPtRJy";
	$dbname = "newsplex_db";

	$names=trim($_POST['victim_names']);
	$gender=trim($_POST['gender']);
	$age=trim($_POST['victim_age']);
	$occupation=trim($_POST['victim_occupation']);
	$incident_date=trim($_POST['incident_date']);
	$incident_town=trim($_POST['incident_town']);
	$officer_involved=trim($_POST['officer_involved']);
	$victim_armed=trim($_POST['victim_armed']);
	$police_weapon=trim($_POST['police_weapon']);
	$occurence_book=trim($_POST['occurence_book']);
	$case_status=trim($_POST['case_status']);
	$summary=trim($_POST['summary']);
	$source=trim($_POST['source']);
	$keywords=trim($_POST['keywords']);
	$savedate=trim(date("Y-m-d H:i:s"));
	$date_modified=trim(date("Y-m-d H:i:s"));
	$modified_by=$loggedinuser;
	//exit();

	//check for empty values
	if (empty($names) ||   empty($gender) ||empty($summary))
	{
	echo "<div class=\"bs-component\">";
	echo "<div class=\"alert alert-dismissable alert-warning\">";
	echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
	echo "<strong>Go back and fill all empty fields!.</strong>";
	echo "</div>";
	echo "</div>";
	}


	try {
	$conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbuser, $dbpass);
	// set the PDO error mode to exception
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	

	//check if victim name exists in the database
	$sthandler = $conn->prepare("select victim_names from police_killings where victim_names = :name");
	$sthandler->bindParam(':name', $names);
	$sthandler->execute();

	if($sthandler->rowCount() > 0){
	echo "<div class=\"bs-component\">";
	echo "<div class=\"alert alert-dismissable alert-warning\">";
	echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
	echo "<strong>Victim name already exists in the database!.</strong>";
	echo "</div>";
	echo "</div>";
	exit();
	}
	
	//try to insert using pdo
	$sthandler = $conn->prepare("insert into police_killings (victim_names,victim_gender,victim_age,victim_occupation,date_of_incident,incident_county_or_town,officer_involved,victim_armed_unarmed,police_weapon,entry_in_occurence_book,status_of_case,incident_summary,source,keywords,entered_by,date_entered,date_modified,modified_by) values (:names,:gender,:age,:occupation,:incident_date,:incident_town,:officer_involved,:victim_armed,:police_weapon,:occurence_book,:case_status,:summary,:source,:keywords,:loggedinuser,:savedate,:date_modified,:modified_by)");
	$sthandler->bindParam(':names', $names);
	$sthandler->bindParam(':gender', $gender);
	$sthandler->bindParam(':age', $age);
	$sthandler->bindParam(':occupation', $occupation);
	$sthandler->bindParam(':incident_date', $incident_date);
	$sthandler->bindParam(':incident_town', $incident_town);
	$sthandler->bindParam(':officer_involved', $officer_involved);
	$sthandler->bindParam(':victim_armed', $victim_armed);
	$sthandler->bindParam(':police_weapon', $police_weapon);
	$sthandler->bindParam(':occurence_book', $occurence_book);
	$sthandler->bindParam(':case_status', $case_status);
	$sthandler->bindParam(':summary', $summary);
	$sthandler->bindParam(':source', $source);
	$sthandler->bindParam(':keywords', $keywords);
	$sthandler->bindParam(':loggedinuser', $loggedinuser);
	$sthandler->bindParam(':savedate', $savedate);
	$sthandler->bindParam(':date_modified', $date_modified);
	$sthandler->bindParam(':modified_by', $modified_by);
	$sthandler->execute();

	//$sql = "insert into police_killings (victim_names,victim_gender,victim_age,victim_occupation,date_of_incident,incident_county_or_town,officer_involved,victim_armed_unarmed,police_weapon,entry_in_occurence_book,status_of_case,incident_summary,source,entered_by,date_entered,date_modified,modified_by) values ('$names','$gender','$age','$occupation','$incident_date','$incident_town','$officer_involved','$victim_armed','$police_weapon','$occurence_book','$case_status','$summary','$source','$loggedinuser','$savedate','$date_modified','$loggedinuser')";
	// use exec() because no results are returned
	//$conn->exec($sql);
	
	
	echo "<div class=\"bs-component\">";
	echo "<div class=\"alert alert-dismissable alert-success\">";
	echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
	echo "<strong>Victim details successfully added!.</strong>";
	echo "</div>";
	echo "</div>";
	}
	catch(PDOException $e)
	{
	echo "error" . "<br>" . $e->getMessage();
	}
    $conn = null;
	}
	if(isset($_POST['cancel']))
	{ 
	header('Location:../index.php');
	}
	?>
	<legend>Use the form below to add victim details to the database</legend>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">First and last names*</label>
	<div class="col-lg-12">
	<input type="text" class="form-control" name="victim_names" id="victim_names" placeholder="First and last names" notrequired>
	</div>
	</div>
	<div class="form-group">
	<label for="textArea" class="col-lg-12 control-label">Gender (Male of female)*</label>
	<div class="col-lg-12">
	<select class="form-control" id="gender" name="gender" notrequired>
	<option>M</option>
	<option>F</option>
	</select>
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Age</label>
	<div class="col-lg-12">
	<input type="text" class="form-control" id="victim_age" name="victim_age" placeholder="Age">
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Occupation of victim</label>
	<div class="col-lg-12">
	<input type="text" class="form-control" id="victim_occupation" name="victim_occupation" placeholder="Occupation of victim">
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Date of incident  </label>
	<div class="col-lg-12">
	<input class="form-control" id="incident_date" name="incident_date" type="text">
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Incident county or town*</label>
	<div class="col-lg-12">
	<input type="text" class="form-control" id="incident_town" name="incident_town" placeholder="Incident county or town" notrequired>
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Officer(s) involved</label>
	<div class="col-lg-12">
	<input type="text" class="form-control" id="officer_involved" name="officer_involved" placeholder="Name(s) of officer(s) involved">
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Victim armed or unarmed</label>
	<div class="col-lg-12">
	<select class="form-control" id="victim_armed" name="victim_armed" notrequired>
	<option>Armed</option>
	<option>Unarmed</option>
	<option>Unknown</option>
	</select>
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Police weapon</label>
	<div class="col-lg-12">
	<input type="text" class="form-control" id="police_weapon" name="police_weapon" placeholder="Police weapon">
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Entry in police occurence book</label>
	<div class="col-lg-12">
	<input type="text" class="form-control" id="occurence_book" name="occurence_book" placeholder="Entry in police occurence book">
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Status of case</label>
	<div class="col-lg-12">
	<input type="text" class="form-control" id="case_status" name="case_status" placeholder="Status of the case">
	</div>
	</div>
	<div class="form-group">
	<label for="textArea" class="col-lg-2 control-label">Incident summary</label>
	<div class="col-lg-12">
	<textarea class="form-control" rows="3" id="summary" name="summary" notrequired></textarea>
	<span class="help-block">Give a summary of the incident</span>
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Source</label>
	<div class="col-lg-12">
	<input type="text" class="form-control" id="source" name="source" placeholder="Give a link to source">
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Keywords</label>
	<div class="col-lg-12">
	<input type="text" class="form-control" id="keywords" name="keywords" placeholder="keywords">
	</div>
	</div>
	<div class="form-group">
	<label for="input" class="col-lg-12 control-label">Image: 150*190px image grayscale. Max 100KB  </label>
	<div class="col-lg-12">
	
    <?php
	print "<input size='50' type='file' name='filename'>&nbsp;&nbsp;&nbsp;";
	
	?>
	
	</div>
	</div>
	<div class="form-group">
	<div class="col-lg-10 col-lg-offset">
	<button type="submit" name="submit" id="submit" class="btn btn-primary">Submit</button>
	<button type="submit" name="cancel" id="cancel" class="btn btn-default">Cancel</button>
	<a class= "btn btn-warning" href="logout.php">Logout</a>
	</div>
	</div>
	</fieldset>
	</form>
	 	
	</div>
	</div>
   </div>

	</div> <!-- /container -->


	<!-- Bootstrap core JavaScript
	================================================== -->
	<!-- Placed at the end of the document so the pages load faster -->
	<script src="../js/jquery.min.js"></script>
	<script src="../js/bootstrap.min.js"></script>
	<script src="../js/bootstrapValidator.js"></script>
	<script src="../js/bootswatch.js"></script>
	<script src="../js/docs.min.js"></script>
	<script src="../js/zebra_datepicker.js"></script>
	<script>
	$('#incident_date').Zebra_DatePicker();
	</script>
	<script src="../js/bootstrap.file-input.js"></script>
	<script>
	$('input[type=file]').bootstrapFileInput();
	$('.file-inputs').bootstrapFileInput();
	</script>
	<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
	<script src="../js/ie10-viewport-bug-workaround.js"></script>
	
	</body>
	</html>
