<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/espace_personnel/inc/head-lib.php');

//echo "<pre>";print_r($_POST);echo "</pre>";

// FORMATAGE DATES

	//$_DATE_DEBUT = explode(" / ",$_POST['debut_exercice']) ;
	//$date_debut = $_DATE_DEBUT['1'].'-'.$_DATE_DEBUT['0'].'-01';

	//$_DATE_FIN = explode(" / ",$_POST['fin_exercice']) ;
	//$date_fin = $_DATE_FIN['1'].'-'.$_DATE_FIN['0'].'-01';
	
	$affichage_recherche = '';

	$date_debut = $_POST['debut_annee'].'-'.$_POST['debut_mois'].'-01';

	if($_POST['en_cours'] == 'on')	
		$date_fin	= '0000-00-00';
	else	
		$date_fin	= $_POST['fin_annee'].'-'.$_POST['fin_mois'].'-01';
	
// CHECK

	if( strlen($_POST['fonction']) < 2 || strlen($_POST['lieu']) < 3){
		Header("Location: ".$_SERVER['HTTP_REFERER']."?alert=".urlencode('Il y a une erreur. Veuillez réessayer.')."#".$_GET['tab']);
		exit();
	}


// DUREE EXPERIENCE (en mois)
	
	if($_POST['en_cours'] == 'on'){
		$mois	= date('m');
		$annee	= date('Y');
		$duree_experience = ($annee - $_POST['debut_annee'])*12 + ($mois - $_POST['debut_mois']);
	}
	else{
		$duree_experience = ($_POST['fin_annee'] - $_POST['debut_annee'])*12 + ($_POST['fin_mois'] - $_POST['debut_mois']);
	}

	if( $duree_experience <= 0){
		Header("Location: ".$_SERVER['HTTP_REFERER']."?alert=".urlencode('Il y a une erreur. Veuillez réessayer.')."#".$_GET['tab']);
		exit();
	}

// DETECTION NOUVELLE EXPERIENCE OU NON

	else{
		if($_POST['id_experience'] == "new"){
			dbquery("INSERT INTO cv_experiences(id_cv) VALUES('".$_SESSION['user']['id_cv']."')") ;
			$sql			= dbquery("SELECT * FROM cv_experiences WHERE id_cv = '".$_SESSION['user']['id_cv']."' ORDER BY id_experience_cv DESC LIMIT 1");
			$id_experience	= dbresult($sql,0,"id_experience_cv");

			$sql			= dbquery("SELECT * FROM cv_experiences WHERE id_cv = '".$_SESSION['user']['id_cv']."' AND affichage_recherche = '1' ");
			if(dbnumrows($sql) == 0)	$affichage_recherche = ",affichage_recherche	= '1'";
			else						$affichage_recherche = "";

		}
		else{
			$id_experience = $_POST['id_experience'] ;
		}

// DB UPDATE

		$q="UPDATE cv_experiences 

					SET id_cv = '".$_SESSION['user']['id_cv']."', 
						entreprise			= '".addslashes($_POST['entreprise'])."', 
						secteur_activite	= '".addslashes($_POST['secteur_activite'])."',
						taille_entreprise	= '".addslashes($_POST['taille_entreprise'])."',
						fonction			= '".addslashes($_POST['fonction'])."',
						csp					= '".addslashes($_POST['csp'])."',
						date_debut			= '".addslashes($date_debut)."',
						date_fin			= '".addslashes($date_fin)."',
						duree_experience	= '".addslashes($duree_experience)."',
						lieu				= '".addslashes($_POST['lieu'])."',
						descriptif			= '".addslashes($_POST['descriptif'])."'
						".$affichage_recherche."

					WHERE id_experience_cv = ".$id_experience."
		";

		//echo $q;

		dbquery($q);

		Header("Location: ".$_SERVER['HTTP_REFERER']."#".$_GET['tab']);
		exit();	
		
	}
?>