<?php



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



//check the id passed
if(isset($_POST['departments']))
{
	$id =  $_POST['departments'];
}





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


////collect the philosophy
//$philosophy = (!empty($_POST['philosophy']))? (mysqli_real_escape_string($dbc,$_POST['philosophy'])): 
// NULL;
//$_SESSION['philosophy'] = $philosophy;
//
//
//
////department logo 
//
////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/logos/');
//	
//	//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';
//		
//		//collect the former name of the image
//			$query = "SELECT logo FROM departments WHERE '$id' = id";
//			$result = mysqli_query($dbc,$query);
//			
//			if(mysqli_num_rows($result) > 0)
//			{
//				while($row_i = mysqli_fetch_array($result, MYSQLI_ASSOC))
//				{
//					$image = $row_i['logo']; 
//				}
//				
//			}
//		
//		
//	}
//	
//	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 department logo
//
//
//
//
////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;
//
//
//
////validate the faculty
//if(($_POST['faculty'] == '0'))
//{
//	$errors[] = '<p> * </p>';
// 
// 
//}
//else
//{	  
//
//	$faculty = mysqli_real_escape_string($dbc, $_POST['faculty']);
//    $_SESSION['faculty'] = $faculty;
//   
//}
//
//
//
//
////check for pdf staff list
//if(array_key_exists('upload_staff_pdf_list', $_POST))
//{
//	define('MAX_STAFF_PDF_FILE_SIZE', 104000);
//	
//	//define the path
//	define('UPLOAD_DIRECTORY_2', '../departments/uploads/pdf_files/');
//	
//	//getting my caption now 
//	//replace any space with an underscore 
//	$staff_pdf_file = $_FILES['staff_pdf_file']['name'];
//	$staff_pdf_file_r = str_replace(' ', '_',$staff_pdf_file);
//	
//	//prevent overwritten
//	$now = date('Y-m-d-His');
//	$staff_pdf_file_r = $now.$staff_pdf_file_r;
//	
//	
//	
//	//convert the maximum size to kilobyte
//	$maximum_size = number_format(MAX_STAFF_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['staff_pdf_file']['type'] )
//		{
//			$type_ok = true;
//			
//			break;
//		}
//		
//	}
//	
//	//check file if is within the permitted size
//	if (($_FILES['staff_pdf_file']['size']) > 0  && ($_FILES['staff_pdf_file']['size'] <= MAX_STAFF_PDF_FILE_SIZE))
//	{
//		$size_ok = true;
//	}
//	
//	
//	if($size_ok and $type_ok)
//	{
//		switch($_FILES['staff_pdf_file']['error'])
//		{
//			case 0: 
//			{
//			
//				//move the file to the upload folder and rename it
//				$success = move_uploaded_file($_FILES['staff_pdf_file']['tmp_name'], UPLOAD_DIRECTORY_2.$staff_pdf_file_r);
//				
//				if($success)
//				{
//					$staff_pdf_file = $staff_pdf_file_r;
//					$info['staff_pdf_file_result_message'] = "$staff_pdf_file uploaded successfully";
//				}
//				else
//				{
//					$errors['staff_pdf_file_result_message'] = "Error uploading $staff_pdf_file";
//					
//					//pass the session to show the error
//					$_SESSION['staff_pdf_file_result_message'] = $errors['staff_pdf_file_result_message']; 
//				}
//			break;	
//			}
//			
//			case 3:
//			{
//				$errors['staff_pdf_file_result_message'] = "Error Uploading $staff_pdf_file. Please try again";
//				$_SESSION['staff_pdf_file_result_message'] = $errors['staff_pdf_file_result_message']; //pass the error
//			}
//			default:
//			{
//				$errors['staff_pdf_file_result_message'] = "System error uploading $file. Contact Webmaster.";
//				$_SESSION['staff_pdf_file_result_message'] = $errors['staff_pdf_file_result_message'];  //pass the error
//			}
//			
//			
//		}//end of switch
//	}//end of size ok
//	else if($_FILES['staff_pdf_file']['error'] == 4)
//	{
//		//$errors['image_result_message'] = 'No file selected';
//			//collect the former name of the image
//			$qslf= "SELECT staff_list_file FROM departments WHERE '$id' = id";
//			$rslf = mysqli_query($dbc,$qslf);
//			
//			if(mysqli_num_rows($rslf) > 0)
//			{
//				while($row_slf = mysqli_fetch_array($rslf, MYSQLI_ASSOC))
//				{
//					$staff_pdf_file = $row_slf['staff_list_file']; 
//				}
//				
//			}
//	}
//	
//	else
//	{
//		$errors['staff_pdf_file_result_message'] = "$staff_pdf_file cannot be uploaded. Maximum size: $maximum_size. Types: 
//		pdf.";
//		$_SESSION['staff_pdf_file_result_message'] = $errors['staff_pdf_file_result_message']; //pass the info
//	}
//	
//									
//
//}//end of if upload staff_ 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/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';
//			//collect the former name of the image
//			$qpf= "SELECT department_details FROM departments WHERE '$id' = id";
//			$rpf = mysqli_query($dbc,$qpf);
//			
//			if(mysqli_num_rows($rpf) > 0)
//			{
//				while($row_pf = mysqli_fetch_array($rpf, MYSQLI_ASSOC))
//				{
//					$pdf_file = $row_pf['department_details']; 
//				}
//				
//			}
//		
//	}
//	
//	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 hod_name
//$hod_name = (!empty($_POST['hod_name']))? (mysqli_real_escape_string($dbc,$_POST['hod_name'])): 
// NULL;
//$_SESSION['hod_name'] = $hod_name;
//
//
//
////check  hod_ image
////check for image using my own custom script
//if(array_key_exists('upload_hod_image', $_POST))
//{
//	define('MAX_HOD_FILE_SIZE',104000);
//	
//	//define the path
//	define('UPLOAD_DIRECTORY_4', '../departments/hods/');
//	
//	//getting my caption now 
//	//replace any space with an underscore 
//	$hod_file = $_FILES['hod_image']['name'];
//	$hod_r = str_replace(' ', '_',hod_file);
//	
//	//prevent overwritten
//	$now = date('Y-m-d-His');
//	$hod_r = $now.$hod_r;
//	
//	
//	
//	//convert the maximum size to kilobyte
//	$maximum_size = number_format(MAX_HOD_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['hod_image']['type'] )
//		{
//			$type_ok = true;
//			
//			break;
//		}
//		
//	}
//	
//	//check file if is within the permitted size
//	if (($_FILES['hod_image']['size']) > 0  && ($_FILES['hod_image']['size'] <= MAX_HOD_FILE_SIZE))
//	{
//		$size_ok = true;
//	}
//	
//	
//	if($size_ok and $type_ok)
//	{
//		switch($_FILES['hod_image']['error'])
//		{
//			case 0: 
//			{
//			
//				//move the file to the upload folder and rename it
//				$success = move_uploaded_file($_FILES['hod_image']['tmp_name'], UPLOAD_DIRECTORY_4.$hod_r);
//				
//				if($success)
//				{
//					$hod_image = $hod_r;
//					$info['hod_image_result_message'] = "$hod_file uploaded successfully";
//				}
//				else
//				{
//					$errors['hod_image_result_message'] = "Error uploading $hod_file";
//					
//					$_SESSION['hod_image_result_message'] = $errors['hod_image_result_message']; //pass the error
//				}
//			break;	
//			}
//			
//			case 3:
//			{
//				$errors['hod_image_result_message'] = "Error Uploading $hod_file. Please try again";
//				
//				$_SESSION['hod_image_result_message'] = $errors['hod_image_result_message']; //pass the error
//			}
//			default:
//			{
//				$errors['hod_image_result_message'] = "System error uploading $hod_file. Contact Webmaster.";
//				$_SESSION['hod_image_result_message'] = $errors['hod_image_result_message']; //pass the error
//			}
//			
//			
//		}//end of switch
//	}//end of size ok
//	else if($_FILES['hod_image']['error'] == 4)
//	{
//		//$errors['image_result_message'] = 'No file selected';
//			//collect the former name of the image
//			$qhi= "SELECT hod_picture FROM departments WHERE '$id' = id";
//			$rhi = mysqli_query($dbc,$qhi);
//			
//			if(mysqli_num_rows($rhi) > 0)
//			{
//				while($row_hi = mysqli_fetch_array($rhi, MYSQLI_ASSOC))
//				{
//					$hod_image = $row_hi['hod_picture']; 
//				}
//				
//			}
//	}
//	
//	else
//	{
//		$errors['hod_image_result_message'] = "$hod_file cannot be uploaded. Maximum size: $maximum_size. Types: 
//		gif, jpg, 	png.";
//		
//		$_SESSION['hod_image_result_message'] = $errors['hod_image_result_message']; //pass the error
//	}
//	
//									
//
//}//end of if upload hod__ image


//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['philosophy']);
	unset($_SESSION['description']);
    //unset($_SESSION['code']);
	unset($_SESSION['status']);
	//unset($_SESSION['faculty']);
//	unset($_SESSION['image_result_message']);
//	unset($_SESSION['staff_pdf_file_result_message']);
//	unset($_SESSION['pdf_file_result_message']);
//	unset($_SESSION['hod_image_result_message']);
//	unset($_SESSION['location']);
//	unset($_SESSION['contacts']);
//	unset($_SESSION['hod_name']);
//	unset($_SESSION['em']);
//		
	
	 //check if the query is for adding den add
	
	
		$q = "UPDATE `departments` SET `title`='$title',`description`='$description', `status`='$status',  
		`date_modified`=NOW(), modified_by = '$security_id' 
			WHERE id = '$id'";
										
							
		$r = mysqli_query($dbc,$q);
	
	if($r)
	{
		$info_success['success'] = 'Updated Successfully!';
		header("Location: gateway.php?act=view_departments&is=".urlencode($info_success['success']).""); //is - info success
		
	}
	else
	{
		$errors['error'] =  'Department not updated due to a system error. We apologize for any inconvenience.';
										
		header("Location: gateway.php?act=view_departments&error=".urlencode($errors['error'])."");
	}
	
}
else
{//stay back in departments


	$errors['error'] =  'Department not updated, Please re-select and Fill the form appropriately, take note of <span class="ast">*</span>.';
										
	header("Location: gateway.php?act=view_departments&error=".urlencode($errors['error'])."");

}



?>