<?php 
session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');

if(isset($_SESSION['user_droits'])) {
	if($_REQUEST['item']=="seq_nom") {
		$check_n = mysqli_query($bdd,"SELECT seq_nom FROM ".$prefix."_sequences WHERE seq_nom = '".$_REQUEST['item']."' AND seq_id <> '".$_REQUEST['seq_id']."'");
		if(mysqli_num_rows($check_n)>0) { $checknom=1; }
		else { $checknom=0; }
	}
	else { $checknom=0; }
	if($_REQUEST['item']=="progression") {
		if($_REQUEST['texte']=='') { $_REQUEST['texte']='1000'; }
	}
	if($_REQUEST['item']=="niveau") {
		$check_n = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT niveau FROM ".$prefix."_sequences WHERE seq_id = '".$_REQUEST['seq_id']."'"));
		if($_REQUEST['texte']!=$check_n['niveau']) {
			mysqli_query($bdd,"DELETE FROM ".$prefix."_seqsav WHERE seq_id = '".$_REQUEST['seq_id']."'");
		}
	}
	if($checknom==0) {
		mysqli_query($bdd,"UPDATE ".$prefix."_sequences SET ".$_REQUEST['item']." = '".htmlspecialchars(mysqli_real_escape_string($bdd,$_REQUEST['texte']))."' WHERE seq_id = '".$_REQUEST['seq_id']."'");
		if($_REQUEST['item']=="notions") {
			mysqli_query($bdd,"DELETE FROM ".$prefix."_seqnot WHERE seq_id = '".$_REQUEST['seq_id']."'");
			for($j=0;$j<=14;$j++) {
				if($_REQUEST['noti'.$j]!='') {
					$translatedNotion = $_REQUEST['noti'.$j];
                	$originalNotion = isset($inverseTranslations[$translatedNotion]) ? $inverseTranslations[$translatedNotion] : $translatedNotion;
		            $id = mysqli_query($bdd,"SELECT * FROM ".$prefix."_savnotions WHERE notion_nom = '".mysqli_real_escape_string($bdd, $originalNotion)."'");
					if(mysqli_num_rows($id)>0) {
						$idn = mysqli_fetch_assoc($id);
						mysqli_query($bdd,"INSERT INTO ".$prefix."_seqnot (seq_id, notion_id) VALUES ('".$_REQUEST['seq_id']."', '".$idn['notion_id']."')");
					}
				}
			}
		}
	}
	$rep_xml = "../../xml";
	$rep_xml_open = opendir($rep_xml);
	$chemin = $rep_xml."/idlog-".$_SESSION['seq_niv']."e-".$_SESSION['seq_nom'].".xml";
	@unlink($chemin);
	closedir($rep_xml_open);
}
mysqli_close($bdd);
?>