<?php



$errors = array();
$info = array();
$info_success = array();






//validate the title
if(!empty($_POST['title']))
{
   $title = mysqli_real_escape_string($dbc, $_POST['title']);
   
   //collect title to fill the static form
   $_SESSION['title'] = $title;
 
}
else
{
   $errors[] = '<p> * </p>';
   
   
}



//validate the control
if(!empty($_POST['control']))
{
   $control = mysqli_real_escape_string($dbc, $_POST['control']);
   
   $_SESSION['control'] = $control;
 
}
else
{
   $errors[] = '<p> * </p>';
}

//collect the description
$description = mysqli_real_escape_string($dbc, $_POST['description']);
$_SESSION['description'] = $description;


	
//validate optional active_status
$active_status= (!empty($_POST['active_status']))? (mysqli_real_escape_string($dbc,$_POST['active_status'])): 
NULL;

$_SESSION['active_status'] =$active_status;






//collect the status
//$status = (!empty($_POST['status']))? (mysqli_real_escape_string($dbc,$_POST['status'])): 
 //NULL;
//$_SESSION['status'] = $status;



//added_by 
$user_id = get_user_id();
$sid = get_security_id($user_id);


if(empty($errors))
{
	//destroy session if there is no error
	unset($_SESSION['title']);
	unset($_SESSION['control']);
	unset($_SESSION['description']);
	unset($_SESSION['active_status']);
	//unset($_SESSION['status']);

	
	 //check if the query is for adding den add
	
	
	$q = "INSERT INTO `controls`(`id`, `controls_value`, `title`, `description`, `active`, `date_added`,  `added_by`)
		 VALUES (NULL,'$control','$title','$description','$active_status',NOW(),'$sid')";
							
							
	$r = mysqli_query($dbc,$q);
	
	if($r)
	{
		$info_success['success'] = 'Successfully added!';
		header("Location: gateway.php?act=view_control_boxs&is=".urlencode($info_success['success']).""); //is - info success
		
	}
	else
	{
		$errors['error'] =  'Not added due to a system error. We apologize for any inconvenience.';
										
		header("Location: gateway.php?act=add_control_boxs&error=".urlencode($errors['error'])."");
	}
	
}
else
{//stay back in add_users form


	$errors['error'] =  'Fill the form appropriately, take note of <span class="ast">*</span>.';
										
	header("Location: gateway.php?act=add_control_boxs&error=".urlencode($errors['error'])."");

}



?>