<?php




//this page saves updated profile
//if the person is a new student


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


//collect the qlk to pass it for fliping
if(isset($_POST['qlk']))
{
	$qlk = $_POST['qlk'];
}








//validate the firstname
if(!empty($_POST['first_name']))
{
   $first_name = mysqli_real_escape_string($dbc, $_POST['first_name']);

   $_SESSION['fn'] = $first_name;

}
else
{


   $errors[] = '<p> * </p>';
}


//validate the middle name
if(isset($_POST['middle_name']))
{
   $middle_name = mysqli_real_escape_string($dbc, $_POST['middle_name']);

   	if ($middle_name !== '') {


	   if (strlen($middle_name) < 3){
			$errors['error'] =  'Middle name should be more than two(2) letters.'.PHP_EOL;
			//.$prog .' '.$dept_id;

			header("Location: dashboard.php?act=my_profile&error=".urlencode($errors['error'])."&qlk=".$encripted_qlk."");
			exit();

		}

	}else{

		$middle_name = '';
	}

   $_SESSION['mn'] = $middle_name;

}
else
{

   $errors['mn'] = 'Middle name should be more than two(2) letters.';
}


	//validating middle name error

	if (isset($errors['mn'])) {

		header("Location: dashboard.php?act=my_profile&error=".urlencode($errors['mn'])."&qlk=".$encripted_qlk."");
		exit();

	}



//validate the other_names
if(!empty($_POST['other_names']))
{
   $other_names = mysqli_real_escape_string($dbc, $_POST['other_names']);

   $_SESSION['on'] = $other_names;

}
else
{
   $errors[] = '<p> * </p>';
}



  //validate the marital_status
  if($_POST['marital_status'] == '0')
  {
	   $_SESSION['ms'] = $_POST['marital_status']; //pas to the parent page for feed back
	   $errors[] = '<p> * </p>';



  }
  else
  {
	  $marital_status= mysqli_real_escape_string($dbc, $_POST['marital_status']);
	  $_SESSION['ms'] = $marital_status;

  }



  //validate the gender
  if($_POST['gender'] == '0')
  {

	  $_SESSION['g'] =  $_POST['gender'];
	   $errors[] = '<p> * </p>';


  }
  else
  {
	  $gender = mysqli_real_escape_string($dbc, $_POST['gender']);
	  $_SESSION['g'] = $gender;

  }


    //validate the religion
  if($_POST['religion'] == '0')
  {

	  $_SESSION['r'] =  $_POST['religion'];
	   $errors[] = '<p> * </p>';


  }
  else
  {
	  $religion = mysqli_real_escape_string($dbc, $_POST['religion']);
	  $_SESSION['r'] = $religion;

  }



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



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


  }
  else
  {
	  $day = mysqli_real_escape_string($dbc, $_POST['day']);
	  $_SESSION['d'] = $day;

  }



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


  }
  else
  {
	  $month = mysqli_real_escape_string($dbc, $_POST['month']);
	  $_SESSION['m'] = $month;

  }


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


  }
  else
  {
	  $year = mysqli_real_escape_string($dbc, $_POST['year']);
	  $_SESSION['y'] = $year;

  }



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


  }
  else
  {
	  $nationality = mysqli_real_escape_string($dbc, $_POST['nationality']);
	  $_SESSION['n'] = $nationality;

  }



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

 //validate the state
  if($_POST['state'] == '0')
  {

	   $_SESSION['s'] = mysqli_real_escape_string($dbc, $_POST['state']);
	   $errors[] = '<p> * </p>';


  }
  else
  {
	  $state = mysqli_real_escape_string($dbc, $_POST['state']);
	  $_SESSION['s'] = $state;

  }


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

  }
  else
  {
	  $lga = mysqli_real_escape_string($dbc, $_POST['lga']);
	  $_SESSION['l'] = $lga;

  }


//validate the phone_no
if(!empty($_POST['phone_no']))
{
   $phone_no = mysqli_real_escape_string($dbc, $_POST['phone_no']);

   $_SESSION['pn'] = $phone_no;

}
else
{
   $errors[] = '<p> * </p>';
}



//validate the email
if(!empty($_POST['email']))
{
   $email = mysqli_real_escape_string($dbc, $_POST['email']);

   $_SESSION['e'] = $email;

}
else
{
   $errors[] = '<p> * </p>';
}



//validate the address
if(!empty($_POST['address']))
{
   $address = mysqli_real_escape_string($dbc, $_POST['address']);

   $_SESSION['add'] = $address;

}
else
{
   $errors[] = '<p> * </p>';
}



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



//collect the  next of kin name
$nok_name = (!empty($_POST['nok']))? (mysqli_real_escape_string($dbc,$_POST['nok'])):
 NULL;
$_SESSION['nok'] = $nok_name;



//collect the  next of kin relationship
$nok_r = (!empty($_POST['nokr']))? (mysqli_real_escape_string($dbc,$_POST['nokr'])):
 NULL;
$_SESSION['nokr'] = $nok_r;


//collect the  next of kin tel
$nok_t = (!empty($_POST['nokt']))? (mysqli_real_escape_string($dbc,$_POST['nokt'])):
 NULL;
$_SESSION['nokt'] = $nok_t;

//collect the  next of kin address
$nok_add = (!empty($_POST['nok_address']))? (mysqli_real_escape_string($dbc,$_POST['nok_address'])):
 NULL;
$_SESSION['nok_add'] = $nok_add;




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


  $sponsorship_type = (!empty($_POST['sponsorship_type']))? (mysqli_real_escape_string($dbc,$_POST['sponsorship_type'])):
 NULL;
$_SESSION['st'] = $sponsorship_type;


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


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


//validate the Programme type
  if($_POST['programme_type'] =='0')
  {

	  $_SESSION['pt'] = mysqli_real_escape_string($dbc, $_POST['programme_type']);
	   $errors[] = '<p> * </p>';


  }
  else
  {
	  $programme_type = mysqli_real_escape_string($dbc, $_POST['programme_type']);
	  $_SESSION['pt'] = $programme_type;

  }



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


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



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




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

 /* //validate the department
  if($_POST['department'] =='0')
  {

	  $_SESSION['dep'] = mysqli_real_escape_string($dbc, $_POST['department']);
	   $errors[] = '<p> * </p>';


  }
  else
  {
	  $department = mysqli_real_escape_string($dbc, $_POST['department']);
	  $_SESSION['dep'] = $department;

  }*/


    //validate the programme
  if($_POST['programme'] == '')
  {
	   $programme = (!empty($_POST['programme']))? (mysqli_real_escape_string($dbc,$_POST['programme'])): NULL;
       $_SESSION['prog'] =  $programme ;
	  // $errors[] = '<p> * </p>';


  }
  else
  {
	  $programme = mysqli_real_escape_string($dbc, $_POST['programme']);
	  $_SESSION['prog'] = $programme;

  }


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



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

 //collect the max programme_duration
/*$max_programme_duration = (!empty($_POST['max_programme_duration']))? (mysqli_real_escape_string($dbc,$_POST['max_programme_duration'])):
 NULL;
$_SESSION['mpd'] = $max_programme_duration;*/





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


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

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



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




//validate the Health status
  if($_POST['H_status'] =='0')
  {

	  $_SESSION['Hs'] = mysqli_real_escape_string($dbc, $_POST['H_status']);
	   $errors[] = '<p> * </p>';


  }
  else
  {
	  $H_status = mysqli_real_escape_string($dbc, $_POST['H_status']);
	  $_SESSION['Hs'] = $H_status;

  }


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


  }
  else
  {
	  $disability = mysqli_real_escape_string($dbc, $_POST['disability']);
	  $_SESSION['dis'] = $disability;

  }



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


  }
  else
  {
	  $blood_type = mysqli_real_escape_string($dbc, $_POST['blood_type']);
	  $_SESSION['bt'] = $blood_type;

  }

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


  }
  else
  {
	  $medi = mysqli_real_escape_string($dbc, $_POST['medi']);
	  $_SESSION['med'] = $medi;

  }
  //end of medi



	//check for image using my own custom script
//	if (array_key_exists('upload', $_POST))
//	{
//		//define the path
//		define('UPLOAD_DIRECTORY', 'application_files/'.$file_name.'/');
//		define('MAX_PDF_FILE_SIZE', 104000);
//
//
//
//
//		//getting my caption now
//		//replace any space with an underscore
//		$file = $_FILES['pdf_file']['name'];
//		$pdf_file = str_replace(' ', '_',$file);
//
//
//
//
//		//change the name by adding timestamp
//		$now = date('Y-m-d-His');
//
//		//add unique timestamp
//		$pdf_file = $now.$pdf_file;
//
//
//
//
//
//
//
//		//convert the maximum size to kilobyte
//		$maximum_size = number_format(MAX_PDF_FILE_SIZE/1024 ,1 ).'KB';
//
//		//create an array of permitted files
//		$permitted_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 filoe is of permitted type
//		foreach($permitted_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.$pdf_file);
//
//					if($success)
//					{
//						//change name
//						$pdf_file = $pdf_file;
//
//						//update query
//
//						$q = "UPDATE users SET pdf_file = '$pdf_file' WHERE application_number = '$application_number'";
//						$r = mysqli_query($dbc, $q);
//
//						if($r)
//						{
//							//then pass succesful messgae
//							$info_success['result_message'] = "$file uploaded successfully";
//						}
//
//
//
//
//					}
//					else
//					{
//						$errors['result_message'] = "Error uploading $file";
//					}
//				break;
//				}
//
//				case 3:
//				{
//					$errors['result_message'] = "Error Uploading $file. Please try again";
//				}
//				default:
//				{
//					$errors['result_message'] = "System error uploading $file. Contact Webmaster.";
//				}
//
//
//			}//end of switch
//		}//end of size ok
//		else if($_FILES['pdf_file']['error'] == 4)
//		{
//			$errors['result_message'] = 'No file  added';
//		}
//
//		else
//		{
//			$errors['result_message'] = "$file cannot be uploaded. Maximum size: $maximum_size. Types:
//			pdf.";
//		}
//
//
//	}//end of if upload pldf files



//
////collect the description1
//$description1 = (!empty($_POST['description1']))? (mysqli_real_escape_string($dbc,$_POST['description1'])):
// NULL;
//$_SESSION['description1'] = $description1;
//
//
//
////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();
//get sch id from programme selected
 $sch = get_school_id_by_programme($programme);

//
//$security_id = get_security_id($user_id);
//





if(empty($errors))
{
	//destroy session if there is no error

	unset($_SESSION['fn']); //clear first name
	unset($_SESSION['mn']); //clear middle name
	unset($_SESSION['on']); //clear other names
	unset($_SESSION['ms']); //clear marital status
	unset($_SESSION['g']); //clear gender
	unset($_SESSION['r']); //clear religion
	unset($_SESSION['pob']); //clear place of birth
	unset($_SESSION['d']); //clear day
	unset($_SESSION['m']); //clear month
	unset($_SESSION['y']); //clear year
	unset($_SESSION['n']); //clear nationality
	//unset($_SESSION['c']); //clear country
	unset($_SESSION['s']); //clear state
	unset($_SESSION['l']); //clear lga
	unset($_SESSION['pn']); //clear phone number
	unset($_SESSION['e']); //clear email
	unset($_SESSION['add']); //clear present address
	unset($_SESSION['add1']); //clear permanent add
	unset($_SESSION['nok']); //clear next of kin namme
	unset($_SESSION['nokr']); //clear  nok relationship
	unset($_SESSION['nokt']); //clear nok tel
	unset($_SESSION['nok_add']); //clear nok address
	unset($_SESSION['st']); //clear sponsorship type
	unset($_SESSION['sp_n']); //clear sponsorship name
	unset($_SESSION['sp_add']); //clear sponsorship address
	unset($_SESSION['pt']); //clear programme type
	//unset($_SESSION['sm']); //clear study mode
	//unset($_SESSION['em']); //clear entry mode
	//unset($_SESSION['ey']); //clear entry year
	//unset($_SESSION['fac']); //clear faculty
	unset($_SESSION['dep']); //clear department
	unset($_SESSION['prog']); //clear programme
	//unset($_SESSION['aiv']); //clear award in view
	//unset($_SESSION['pd']); //clear programme duration
	//unset($_SESSION['mpd']); //clear max programme duration
	//unset($_SESSION['acti']); //clear extra activities
	//unset($_SESSION['hiq']); //clear higest qualification
	//unset($_SESSION['inst']); //clear institution obtained
	//unset($_SESSION['fd']);//clear first degree
	unset($_SESSION['Hs']); //clear heakth status
	unset($_SESSION['dis']); //clear disability
	unset($_SESSION['bt']); //clear blood type / group
	unset($_SESSION['med']); //clear medication





		$q = "UPDATE `applicants_nce_` SET  `state_id`='$state',`lga_id`='$lga',`first_name`='$first_name',`middle_name`='$middle_name',
			 `other_names`='$other_names',`phone_no`='$phone_no',`gender`='$gender',`religion`='$religion', `place_of_birth`='$pob',
			 `nationality`='$nationality',
			 `day`='$day',`month`='$month',`year`='$year',`marital_status`='$marital_status',
			 `email`='$email',`address`='$address',`permanent_address`='$p_address', `guardian_name`='$nok_name', `guardian_relationship`='$nok_r',
			 `guardian_tel`='$nok_t',`guardian_address`='$nok_add', `sponsorship_type`='$sponsorship_type',`sponsorship_name`='$sp_n',
			 `sponsorship_address`='$sp_add', `school_applied_for`='$sch', `programme_applied_for`='$programme',
			 `blood_type`='$blood_type',`disability`='$disability',`H_status`='$H_status',`medi`='$medi'

			 WHERE id = '$user_id'";


		$r = mysqli_query($dbc,$q);

	if($r)
	{
		$info_success['success'] = 'Bio-data is successfully updated..';
		header("Location: dashboard.php?act=my_profile&is=".urlencode($info_success['success'])."&qlk=".$encripted_qlk.""); //is - info success


	}
	else
	{
		$errors['error'] =  'Bio-data not updated due to a system error. We apologize for any inconvenience.';

		header("Location: dashboard.php?act=my_profile&error=".urlencode($errors['error'])."&qlk=".$encripted_qlk."");
	}

}
else
{//stay back in update profile form


	$errors['error'] =  'Fill the form appropriately, take note of <span class="ast">*</span>.';

	header("Location: dashboard.php?act=my_profile&error=".urlencode($errors['error'])."&qlk=".$encripted_qlk."");

}



?>
