<?php



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





//check if action is save news



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


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


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


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


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


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


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

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


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





//collect the philosophy
//$philosophy = (!empty($_POST['philosophy']))? (mysqli_real_escape_string($dbc,$_POST['philosophy'])): 
// NULL;
//$_SESSION['philosophy'] = $philosophy;
//
////collect the objectives
//$objectives = (!empty($_POST['objectives']))? (mysqli_real_escape_string($dbc,$_POST['objectives'])): 
// NULL;
//$_SESSION['objectives'] = $objectives;
//
////collect the mission
//$mission = (!empty($_POST['mission']))? (mysqli_real_escape_string($dbc,$_POST['mission'])): 
// NULL;
//$_SESSION['mission'] = $mission;
//
//
//
////collect the vision
//$vision = (!empty($_POST['vision']))? (mysqli_real_escape_string($dbc,$_POST['vision'])): 
// NULL;
//$_SESSION['vision'] = $vision;
//
//
////collect the entry_requirements
//$entry_requirements = (!empty($_POST['entry_requirements']))? (mysqli_real_escape_string($dbc,$_POST['entry_requirements'])): 
// NULL;
//$_SESSION['entry_requirements'] = $entry_requirements;
//
//
////check for image using my own custom script
//if (array_key_exists('upload', $_POST))
//{
//	define('MAX_IMAGE_FILE_SIZE', 104000);
//	
//	//define the path
//	define('UPLOAD_DIRECTORY_1', '../departments/programmes/pictures/');
//	
//	//getting my caption now 
//	//replace any space with an underscore 
//	$file = $_FILES['image']['name'];
//	$image_r = str_replace(' ', '_',$file);
//	
//	//prevent overwritten
//	$now = date('Y-m-d-His');
//	$image_r = $now.$image_r;
//	
//	
//	
//	//convert the maximum size to kilobyte
//	$maximum_size = number_format(MAX_IMAGE_FILE_SIZE/1024 ,1 ).'KB';
//	
//	//create an array of permitted files
//	$permitted_images_type = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
//	
//	//begin by assume type is not ok
//	$type_ok = false;
//	
//	//begin by assuming the filesize aint aceptable
//	$size_ok = false;
//	
//	//check if he file is of permitted type
//	foreach($permitted_images_type as $type)
//	{
//		if ($type == $_FILES['image']['type'] )
//		{
//			$type_ok = true;
//			
//			break;
//		}
//		
//	}
//	
//	//check file if is within the permitted size
//	if (($_FILES['image']['size']) > 0  && ($_FILES['image']['size'] <= MAX_IMAGE_FILE_SIZE))
//	{
//		$size_ok = true;
//	}
//	
//	
//	if($size_ok and $type_ok)
//	{
//		switch($_FILES['image']['error'])
//		{
//			case 0: 
//			{
//			
//				//move the file to the upload folder and rename it
//				$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIRECTORY_1.$image_r);
//				
//				if($success)
//				{
//					$image = $image_r;
//					$info['image_result_message'] = "$file uploaded successfully";
//				}
//				else
//				{
//					$errors['image_result_message'] = "Error uploading $file";
//					//pass the session to show the error
//					
//					$_SESSION['image_result_message'] = $errors['image_result_message'];  
//				}
//			break;	
//			}
//			
//			case 3:
//			{
//				$errors['image_result_message'] = "Error Uploading $file. Please try again";
//				//pass the session to show the error
//					
//				$_SESSION['image_result_message'] = $errors['image_result_message']; 
//			
//			}
//			default:
//			{
//				$errors['image_result_message'] = "System error uploading $file. Contact Webmaster.";
//			
//				//pass the session to show the error
//					
//			    $_SESSION['image_result_message'] = $errors['image_result_message']; 
//			}
//			
//			
//		}//end of switch
//	}//end of size ok
//	else if($_FILES['image']['error'] == 4)
//	{
//		//$errors['image_result_message'] = 'No file selected';
//	}
//	
//	else
//	{
//		$errors['image_result_message'] = "$file cannot be uploaded. Maximum size: $maximum_size. Types: 
//		gif, jpg, 	png.";
//		
//		//pass the session to show the error
//		$_SESSION['image_result_message'] = $errors['image_result_message']; 
//	}
//	
//									
//
//}//end of if upload programmes image
//
//
//
////check for pdf cs list
//if(array_key_exists('upload_cs_pdf_list', $_POST))
//{
//	define('MAX_CS_PDF_FILE_SIZE', 104000);
//	
//	//define the path
//	define('UPLOAD_DIRECTORY_2', '../departments/programmes/uploads/pdf_files/');
//	
//	//getting my caption now 
//	//replace any space with an underscore 
//	$cs_pdf_file = $_FILES['cs_pdf_file']['name'];
//	$cs_pdf_file_r = str_replace(' ', '_',$cs_pdf_file);
//	
//	//prevent overwritten
//	$now = date('Y-m-d-His');
//	$cs_pdf_file_r = $now.$cs_pdf_file_r;
//	
//	
//	
//	//convert the maximum size to kilobyte
//	$maximum_size = number_format(MAX_CS_PDF_FILE_SIZE/1024 ,1 ).'KB';
//	
//	//create an array of permitted files
//	$permitted_pdf_file_type = array('application/pdf');
//	
//	//begin by assume type is not ok
//	$type_ok = false;
//	
//	//begin by assuming the filesize aint aceptable
//	$size_ok = false;
//	
//	//check if he file is of permitted type
//	foreach($permitted_pdf_file_type as $type)
//	{
//		if ($type == $_FILES['cs_pdf_file']['type'] )
//		{
//			$type_ok = true;
//			
//			break;
//		}
//		
//	}
//	
//	//check file if is within the permitted size
//	if (($_FILES['cs_pdf_file']['size']) > 0  && ($_FILES['cs_pdf_file']['size'] <= MAX_CS_PDF_FILE_SIZE))
//	{
//		$size_ok = true;
//	}
//	
//	
//	if($size_ok and $type_ok)
//	{
//		switch($_FILES['cs_pdf_file']['error'])
//		{
//			case 0: 
//			{
//			
//				//move the file to the upload folder and rename it
//				$success = move_uploaded_file($_FILES['cs_pdf_file']['tmp_name'], UPLOAD_DIRECTORY_2.$cs_pdf_file_r);
//				
//				if($success)
//				{
//					$cs_pdf_file = $cs_pdf_file_r;
//					$info['cs_pdf_file_result_message'] = "$cs_pdf_file uploaded successfully";
//				}
//				else
//				{
//					$errors['cs_pdf_file_result_message'] = "Error uploading $cs_pdf_file";
//					
//					//pass the session to show the error
//					$_SESSION['cs_pdf_file_result_message'] = $errors['cs_pdf_file_result_message']; 
//				}
//			break;	
//			}
//			
//			case 3:
//			{
//				$errors['cs_pdf_file_result_message'] = "Error Uploading $cs_pdf_file. Please try again";
//				$_SESSION['cs_pdf_file_result_message'] = $errors['cs_pdf_file_result_message']; //pass the error
//			}
//			default:
//			{
//				$errors['cs_pdf_file_result_message'] = "System error uploading $cs_pdf_file. Contact Webmaster.";
//				$_SESSION['cs_pdf_file_result_message'] = $errors['cs_pdf_file_result_message'];  //pass the error
//			}
//			
//			
//		}//end of switch
//	}//end of size ok
//	else if($_FILES['cs_pdf_file']['error'] == 4)
//	{
//		//$errors['image_result_message'] = 'No file selected';
//	}
//	
//	else
//	{
//		$errors['cs_pdf_file_result_message'] = "$cs_pdf_file cannot be uploaded. Maximum size: $maximum_size. Types: 
//		pdf.";
//		$_SESSION['cs_pdf_file_result_message'] = $errors['cs_pdf_file_result_message']; //pass the info
//	}
//	
//									
//
//}//end of if upload cs_ pdf_file
//
//
//
//
////check for pdf_ using my own custom script
//if(array_key_exists('upload_pdf_details', $_POST))
//{
//	define('MAX_PDF_FILE_SIZE', 104000);
//	
//	//define the path
//	define('UPLOAD_DIRECTORY_3', '../departments/programmes/uploads/pdf_files/');
//	
//	//getting my caption now 
//	//replace any space with an underscore 
//	$pdf_file = $_FILES['pdf_file']['name'];
//	$pdf_file_r = str_replace(' ', '_',$pdf_file);
//	
//	//prevent overwritten
//	$now = date('Y-m-d-His');
//	$pdf_file_r = $now.$pdf_file_r;
//	
//	
//	
//	//convert the maximum size to kilobyte
//	$maximum_size = number_format(MAX_PDF_FILE_SIZE/1024 ,1 ).'KB';
//	
//	//create an array of permitted files
//	$permitted_pdf_file_type = array('application/pdf');
//	
//	//begin by assume type is not ok
//	$type_ok = false;
//	
//	//begin by assuming the filesize aint aceptable
//	$size_ok = false;
//	
//	//check if he file is of permitted type
//	foreach($permitted_pdf_file_type as $type)
//	{
//		if ($type == $_FILES['pdf_file']['type'] )
//		{
//			$type_ok = true;
//			
//			break;
//		}
//		
//	}
//	
//	//check file if is within the permitted size
//	if (($_FILES['pdf_file']['size']) > 0  && ($_FILES['pdf_file']['size'] <= MAX_PDF_FILE_SIZE))
//	{
//		$size_ok = true;
//	}
//	
//	
//	if($size_ok and $type_ok)
//	{
//		switch($_FILES['pdf_file']['error'])
//		{
//			case 0: 
//			{
//			
//				//move the file to the upload folder and rename it
//				$success = move_uploaded_file($_FILES['pdf_file']['tmp_name'], UPLOAD_DIRECTORY_3.$pdf_file_r);
//				
//				if($success)
//				{
//					$pdf_file = $pdf_file_r;
//					$info['pdf_file_result_message'] = "$pdf_file uploaded successfully";
//				}
//				else
//				{
//					$errors['pdf_file_result_message'] = "Error uploading $pdf_file";
//					
//					//pass the session to show the error
//					$_SESSION['pdf_file_result_message'] = $errors['pdf_file_result_message']; 
//				}
//			break;	
//			}
//			
//			case 3:
//			{
//				$errors['pdf_file_result_message'] = "Error Uploading $pdf_file. Please try again";
//				$_SESSION['pdf_file_result_message'] = $errors['pdf_file_result_message']; //pass the error
//			}
//			default:
//			{
//				$errors['pdf_file_result_message'] = "System error uploading $pdf_file. Contact Webmaster.";
//				$_SESSION['pdf_file_result_message'] = $errors['pdf_file_result_message'];  //pass the error
//			}
//			
//			
//		}//end of switch
//	}//end of size ok
//	else if($_FILES['pdf_file']['error'] == 4)
//	{
//		//$errors['image_result_message'] = 'No file selected';
//	}
//	
//	else
//	{
//		$errors['pdf_file_result_message'] = "$pdf_file cannot be uploaded. Maximum size: $maximum_size. Types: 
//		pdf.";
//		$_SESSION['pdf_file_result_message'] = $errors['pdf_file_result_message']; //pass the info
//	}
//	
//									
//
//}//end of if upload pdf_file


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


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



//finaly get security id
$user_id = get_user_id();


$security_id = get_security_id($user_id);



 
 
 
if(empty($errors))
{
	//destroy session if there is no error
	
	unset($_SESSION['title']);
	unset($_SESSION['description']);
	unset($_SESSION['code']);
	unset($_SESSION['department']);
	unset($_SESSION['programme_type']);
	unset($_SESSION['utme_duration']);
	unset($_SESSION['utme_max_duration']);
	//unset($_SESSION['de_duration']);
//	unset($_SESSION['de_max_duration']);
	unset($_SESSION['min_credit_load']);
	unset($_SESSION['max_credit_load']);
	//unset($_SESSION['philosophy']);
//	unset($_SESSION['mission']);
//	unset($_SESSION['vision']);
//	unset($_SESSION['entry_requirements']);
//	unset($_SESSION['image_result_message']);
//	unset($_SESSION['cs_pdf_file_result_message']);
//	unset($_SESSION['pdf_file_result_message']);
	unset($_SESSION['status']);
		
	
	 //check if the query is for adding den add
	
	
		$q = "INSERT INTO `programmes`(`id`, `title`, `code`, `department_id`, `type_id`,`duration`,`maximum_duration`,
			  `maximum_credit_load`,`minimum_credit_load`,`description`, `status`,`date_added`, `added_by`) 
			 VALUES (NULL,'$title', '$code', '$department', '$programme_type', '$utme_duration', '$utme_max_duration','$max_credit_load', '$min_credit_load',  
			 '$description', '$status', NOW(),'$security_id')";
			 
			 
							
							
		$r = mysqli_query($dbc,$q);
	
	if($r)
	{
		$info_success['success'] = 'Successfully added!';
		header("Location: gateway.php?act=view_programmes&is=".urlencode($info_success['success']).""); //is - info success
		
	}
	else
	{
		$errors['error'] =  'Programme not added due to a system error. We apologize for any inconvenience.';
										
		header("Location: gateway.php?act=add_programmes&error=".urlencode($errors['error'])."");
	}
	
}
else
{//stay back in departments form


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

}



?>