<?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>';
}




//check  faculty image
//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', '../faculties/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 aculty image


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


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


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



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



//check  faculty image
//check for image 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_2', '../faculties/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_2.$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 dean_name
$dean_name = (!empty($_POST['dean_name']))? (mysqli_real_escape_string($dbc,$_POST['dean_name'])): 
 NULL;
$_SESSION['dean_name'] = $dean_name;



//check  deans image
//check for image using my own custom script
if(array_key_exists('upload_dean_image', $_POST))
{
	define('MAX_DEAN_FILE_SIZE',104000);//100 kb for now
	
	//define the path
	define('UPLOAD_DIRECTORY_3', '../faculties/deans/');
	
	//getting my caption now 
	//replace any space with an underscore 
	$dean_file = $_FILES['dean_image']['name'];
	$dean_r = str_replace(' ', '_',$dean_file);
	
	//prevent overwritten
	$now = date('Y-m-d-His');
	$dean_r = $now.$dean_r;
	
	
	
	//convert the maximum size to kilobyte
	$maximum_size = number_format(MAX_DEAN_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['dean_image']['type'] )
		{
			$type_ok = true;
			
			break;
		}
		
	}
	
	//check file if is within the permitted size
	if (($_FILES['dean_image']['size']) > 0  && ($_FILES['dean_image']['size'] <= MAX_DEAN_FILE_SIZE))
	{
		$size_ok = true;
	}
	
	
	if($size_ok and $type_ok)
	{
		switch($_FILES['dean_image']['error'])
		{
			case 0: 
			{
			
				//move the file to the upload folder and rename it
				$success = move_uploaded_file($_FILES['dean_image']['tmp_name'], UPLOAD_DIRECTORY_3.$dean_r);
				
				if($success)
				{
					$dean_image = $dean_r;
					$info['dean_image_result_message'] = "$dean_file uploaded successfully";
				}
				else
				{
					$errors['dean_image_result_message'] = "Error uploading $dean_file";
					
					$_SESSION['dean_image_result_message'] = $errors['dean_image_result_message']; //pass the error
				}
			break;	
			}
			
			case 3:
			{
				$errors['dean_image_result_message'] = "Error Uploading $dean_file. Please try again";
				
				$_SESSION['dean_image_result_message'] = $errors['dean_image_result_message']; //pass the error
			}
			default:
			{
				$errors['dean_image_result_message'] = "System error uploading $dean_file. Contact Webmaster.";
				$_SESSION['dean_image_result_message'] = $errors['dean_image_result_message']; //pass the error
			}
			
			
		}//end of switch
	}//end of size ok
	else if($_FILES['dean_image']['error'] == 4)
	{
		//$errors['image_result_message'] = 'No file selected';
	}
	
	else
	{
		$errors['dean_image_result_message'] = "$dean_file cannot be uploaded. Maximum size: $maximum_size. Types: 
		gif, jpg, 	png.";
		
		$_SESSION['dean_image_result_message'] = $errors['dean_image_result_message']; //pass the error
	}
	
									

}//end of if upload dean_ image


//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['image_result_message']);
	unset($_SESSION['pdf_file_result_message']);
	unset($_SESSION['dean_image_result_message']);
	unset($_SESSION['location']);
	unset($_SESSION['contacts']);
	unset($_SESSION['dean_name']);
	unset($_SESSION['em']);
    unset($_SESSION['code']);
	unset($_SESSION['status']);
		
	
	 //check if the query is for adding den add
	
	
		$q = "INSERT INTO `faculties`(`id`, `title`, `code`,`faculty_image`,`location`,`phone_contacts`, `email`, `description`,`dean_name`, `dean_picture`, `pdf_details`, `status`, `date_added`, `added_by`) 
			  VALUES (NULL,'$title', '$code', '$image','$location' , '$contacts', '$email','$description', '$dean_name','$dean_image', '$pdf_file', '$status',NOW(),'$security_id')";
							
							
		$r = mysqli_query($dbc,$q);
		
		if($r)
		{
			$info_success['success'] = 'Successfully added!';
			header("Location: gateway.php?act=view_faculties&is=".urlencode($info_success['success']).""); //is - info success
			
		}
		else
		{
			$errors['error'] =  'faculty not added due to a system error. We apologize for any inconvenience.';
											
			header("Location: gateway.php?act=add_faculties&error=".urlencode($errors['error'])."");
		}
	
}
else
{//stay back in faculties form


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

}



?>