<?php
require_once("lib/mainClass.php");
class saptrainingDetails extends  mainClass {

	function deleteAllEducation(){
		$this->mysqlConnect();
		if($_SESSION['sap_u_741'] == ""){
			return;
		}
		$user_id = $_SESSION['sap_u_741'];
		$queryDetails = "delete from tbl_certification_details where user_id='".mysql_escape_string($user_id)."'";
		$this->mysqlQuery($queryDetails);
		
		$queryFormDetails = "delete from tbl_saptraining_details where user_id='".mysql_escape_string($user_id)."'";
		$this->mysqlQuery($queryFormDetails);
	}
	
	function addDetails($info){
		$this->mysqlConnect();
		if($_SESSION['sap_u_741'] == ""){
			return;
		}
		$user_id = $_SESSION['sap_u_741'];
		//$query = "select count(*) from tbl_education_details where user_id='".mysql_real_escape_string($user_id)."'";
		//$result = $this->mysqlFetchRow($query);
		//$count = $result[0];
		//if(!$count){
		if($_FILES["hrconcent_file"]["name"]){
			$this->addhrconcent($info);
			
							
			$result1	 = $this->mysqlQuery("UPDATE 	tbl_certification_details	SET status=1	 WHERE user_id='".mysql_escape_string($user_id)."'");
			$result2	 = $this->mysqlQuery("UPDATE 	tbl_education_details	SET status=1	 WHERE user_id='".mysql_escape_string($user_id)."'");
			$result3	 = $this->mysqlQuery("UPDATE 	tbl_employment_details	SET status=1	 WHERE user_id='".mysql_escape_string($user_id)."'");
			$result4	 = $this->mysqlQuery("UPDATE 	tbl_functionalexperience_details	SET status=1	 WHERE user_id='".mysql_escape_string($user_id)."'");
			$result5	 = $this->mysqlQuery("UPDATE 	tbl_personal_details	SET status=1	 WHERE user_id='".mysql_escape_string($user_id)."'");
			$result7	 = $this->mysqlQuery("UPDATE 	tbl_sapexperience_details	SET status=1	 WHERE user_id='".mysql_escape_string($user_id)."'");
			$result8	 = $this->mysqlQuery("UPDATE 	tbl_saptraining_details	SET status=1	 WHERE user_id='".mysql_escape_string($user_id)."'");
			//$result9	 = $this->mysqlQuery("UPDATE 	tbl_registration SET status=1	 WHERE email='".mysql_escape_string($user_id)."'");
			
			return;
		}
		
		//if($info['cource_count'] <= 1 && $info['saptraining'] == "Yes"){ //maximum 10 cources can be added
		if($info['cource_count'] <= 1){ //maximum 10 cources can be added		
			$this->addCertificates($info);
		
			for ($value=1; $value <= $info['cource_count'];$value++){
				$count = 0;
				$cource_id = 'courceid_'.$value;
				if(isset($info[$cource_id])){
					$query = "select count(*) from tbl_saptraining_details where id='".mysql_real_escape_string($info[$cource_id])."'";
					$result = $this->mysqlFetchRow($query);
					$count = $result[0];
					
				}
				if(!$count){
					$queryU = "select count(*) from tbl_saptraining_details where user_id='".mysql_escape_string($user_id)."'";
					$resultU = $this->mysqlFetchRow($queryU);
					$countU = $resultU[0];
					if($countU){
						$queryFormDetails = "delete from tbl_saptraining_details where user_id='".mysql_escape_string($user_id)."'";
						$this->mysqlQuery($queryFormDetails);
					}
					
					
					$attendance = 'attendance_'.$value;
					$cource_name = 'courcename_'.$value;
					$center = 'center_'.$value;
					$cource_type = 'type_'.$value;
					$organization = 'organisation_'.$value;
					$test_code = 'code_'.$value;
					$location = 'location_'.$value;
					$city = 'city_'.$value;
					$others = 'others_'.$value;
					$issaved = 'issaved_'.$value;
					$issaved_value = 0;
					if($info[$issaved] == '1'){
					$issaved_value = 1;
					}
					
					if($info["saptraining"]=="No"){
						$queryFormDetails = "delete from tbl_saptraining_details where user_id='".mysql_escape_string($user_id)."'";
						$this->mysqlQuery($queryFormDetails);
						$queryDetailsU = "delete from tbl_certification_details where user_id='".mysql_escape_string($user_id)."'";
						$this->mysqlQuery($queryDetailsU);						
						$queryDetails = sprintf("insert into tbl_saptraining_details(user_id,attendance,cource_name,center,cource_type,organization,test_code,location,city,others,issaved) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%d')",mysql_escape_string($user_id),"","","","","",mysql_escape_string($info[$test_code]),"","","",mysql_escape_string($issaved_value));
							$this->mysqlQuery($queryDetails);
					
					} else {
					
						//if($info[$attendance] != "" && $info[$cource_name] != "" && $info[$center] != "" && $info[$cource_type] != "" && $info[$organization] != "" && $info[$test_code] != "" && $info[$location] != "" && $info[$city] != "") {					
						//if($info[$attendance] != "" && $info[$cource_name] != "" && $info[$center] != "" && $info[$cource_type] != "") //{					
							$queryDetails = sprintf("insert into tbl_saptraining_details(user_id,attendance,cource_name,center,cource_type,organization,test_code,location,city,others,issaved) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%d')",mysql_escape_string($user_id),mysql_escape_string($info[$attendance]),mysql_escape_string($info[$cource_name]),mysql_escape_string($info[$center]),mysql_escape_string($info[$cource_type]),mysql_escape_string($info[$organization]),mysql_escape_string($info[$test_code]),mysql_escape_string($info[$location]),mysql_escape_string($info[$city]),mysql_escape_string($info[$others]),mysql_escape_string($issaved_value));
							$this->mysqlQuery($queryDetails);
						//}
					}
				}
			
			}					
			
		}			
			
			//$this->fileup_resume($info);  //not working properly we can enable later
			
		//}
	}
	
	function addCertificates($info){
		$this->mysqlConnect();
		$user_id = $_SESSION['sap_u_741'];
		$query = "select count(*) from tbl_certification_details where user_id='".mysql_real_escape_string($user_id)."'";
		
		
		$result = $this->mysqlFetchRow($query);
		$count = $result[0];
		if(!$count){
		
		
		$certificate_name = $this->fileup_certificate($info);
		
			$queryDetails = sprintf("insert into tbl_certification_details(user_id,certificates,hrconcent) values ('%s','%s','%s')",mysql_escape_string($user_id),mysql_escape_string($certificate_name),"");
			
			$this->mysqlQuery($queryDetails);
		}

	}
	
	function addhrconcent($info){
		$this->mysqlConnect();
		$user_id = $_SESSION['sap_u_741'];
		$hrconcent_name = $this->fileup_hrconcent($info);
		
		$query = "select count(*) from tbl_certification_details where user_id='".mysql_real_escape_string($user_id)."'";
		
		
		$result = $this->mysqlFetchRow($query);
		$count = $result[0];
		if(!$count){
			$queryDetails = sprintf("insert into tbl_certification_details(user_id,certificates,hrconcent) values ('%s','%s','%s')",mysql_escape_string($user_id),"",mysql_escape_string($hrconcent_name));
			
			$this->mysqlQuery($queryDetails);
		} else {
			
			$queryDetails = "UPDATE tbl_certification_details SET hrconcent = '".mysql_escape_string($hrconcent_name)."' WHERE user_id='".mysql_real_escape_string($user_id)."'";
			
			$this->mysqlQuery($queryDetails);
		
		}
		
	}
	
	function fileup_certificate($info){
		$target_path_home = "";
		$target_path = "certificates/";
		$filename = $_FILES["certificate_file"]["name"];
		$file_basename = substr($filename, 0, strripos($filename, '.')); // strip extention
		$file_ext = substr($filename, strripos($filename, '.')); // strip name		
		
		if(($file_ext == ".doc" || $file_ext == ".docx" || $file_ext == ".pdf")) //can't be larger than 1 MB
		{			
		
			$random_digit=rand(0000,9999);
			
			//$new_file_name = $random_digit."_". strtolower($_FILES['resume_file']['tmp_name']);
			
			$target_path_home = md5($file_basename) . $file_ext;
			//$target_path_home = $target_path . basename( $_FILES['resume_file']['name']); 

			if(move_uploaded_file($_FILES['certificate_file']['tmp_name'], "certificates/".$target_path_home)) {
				//return $target_path_home;
			} else{
				//return "";
			}
		}
		
		return $target_path_home;
	}
	
	function fileup_hrconcent($info){
		$target_path_home = "";
		$target_path = "certificates/";
		$filename = $_FILES["hrconcent_file"]["name"];
		$file_basename = substr($filename, 0, strripos($filename, '.')); // strip extention
		$file_ext = substr($filename, strripos($filename, '.')); // strip name		
		
		if(($file_ext == ".doc" || $file_ext == ".docx" || $file_ext == ".pdf")) //can't be larger than 1 MB
		{			
		
			$random_digit=rand(0000,9999);
			
			$target_path_home = md5($file_basename) . $file_ext;

			if(move_uploaded_file($_FILES['hrconcent_file']['tmp_name'], "certificates/".$target_path_home)) {
				//return $target_path_home;
			} else{
				//return "";
			}
		}
		
		return $target_path_home;
	}
	
	function getDetails($uid){
		$this->mysqlConnect();

		$query = "select * from tbl_saptraining_details where user_id='".mysql_real_escape_string($uid)."' order by id desc limit 1";	

		//return $this->mysqlAssoc($query);
		$result = $this->mysqlQuery($query);

		$records = array();
		
		while ($row = mysql_fetch_assoc($result)) {
			$records[] = $row;
		}
		return $records;

	}
	
	function getCertDetails($uid){
		$this->mysqlConnect();

		$query = "select * from tbl_certification_details where user_id='".mysql_real_escape_string($uid)."'";	

		return $this->mysqlArray($query);
	}
	
	function getCountCertDetails($uid){
		$this->mysqlConnect();

		$query = "select count(*) from tbl_certification_details where user_id='".mysql_real_escape_string($uid)."' and hrconcent !=''";	

		return $this->mysqlArray($query);
	}
	
	function issaved($uid){
		$this->mysqlConnect();

		$query = "select count(*) from tbl_saptraining_details where user_id='".mysql_real_escape_string($uid)."' and issaved = 0";	

		return $this->mysqlArray($query);
	}
	
	function issavedOne($uid){
		$this->mysqlConnect();

		$query = "select count(*) from tbl_saptraining_details where user_id='".mysql_real_escape_string($uid)."' and issaved = 1";	

		return $this->mysqlArray($query);
	}
	
	function getModulesDetails($uid){
		$this->mysqlConnect();

		$query = "select * from sap where solution_short_text='".mysql_real_escape_string($uid)."' limit 1";	

		return $this->mysqlArray($query);
	}
	
	function getModulesDetailsByProduct($uid){
		$this->mysqlConnect();
		

		//echo a.$uid;

		$query = "select * from sap where product_name='".mysql_real_escape_string($uid)."' limit 1";	

		return $this->mysqlArray($query);
	}
	
}
?>