<?php
$seq_id = $_GET['seq_id'];
echo EDIT_CONS1;
// Suppression
if(isset($_POST['suppr_confirm'])) {
	include('db/proc/edit_fiche_suppr.php');
}
//Création XML
if(isset($_POST['xmlcrea'])) {
	$pathinfo="creation";
	include('db/proc/edit_xml_create.php');
}
if(isset($_POST['supprxml'])) {
	$pathinfo="creation";
	// Suppression du fichier précédent :
	$ouverture = opendir("./xml/");
	$lecture = readdir($ouverture);
	$filename = './'.$_POST['supprxml'];
	if (file_exists($filename)) {
		unlink ('./'.$_POST['supprxml']);
	}
	// Recréation
	include('db/proc/edit_xml_create.php');
}
	
//Demande de confirmation de suppression
if(isset($_POST['suppr'])) {
	$brik_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE seq_id = '".$seq_id."'"));
	$event_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent WHERE seq_id = '".$seq_id."'"));
	if($brik_nb>0||$event_nb>0) {
		echo EDIT_DESACT1.'
		<form method="POST" action="edit.php?page=consult&seq_id='.$seq_id.'">
		<tr><td colspan="2"><input class="bouton boutseq" type="submit" value="'.EDIT_DESACT2.'" name="suppr_confirm" /></td></tr>
		</form><p />';
	}
	else {
		echo EDIT_DESACT3.'
		<form method="POST" action="edit.php?page=consult&seq_id='.$seq_id.'">
		<tr><td colspan="2"><input class="bouton boutseq" type="submit" value="'.EDIT_DESACT4.'" name="suppr_confirm" /></td></tr>
		</form><p />';
	}
}

//Affichage de la fiche
$seq = mysqli_query($bdd,"SELECT * FROM ".$prefix."_sequences WHERE seq_id = '$seq_id'");
$seq_nb = mysqli_num_rows($seq);
$seq_info = mysqli_fetch_array($seq);
// Duplication
if(isset($_POST['duplicate'])) {
	function rand_chars($c, $l, $u = FALSE) {
		if (!$u) for ($s = '', $i = 0, $z = strlen($c)-1; $i < $l; $x = rand(0,$z), $s .= $c[$x], $i++);
		else for ($i = 0, $z = strlen($c)-1, $s = $c[rand(0,$z)], $i = 1; $i != $l; $x = rand(0,$z), $s .= $c[$x], $s = ($s[$i] == $s[$i-1] ? substr($s,0,-1) : $s), $i=strlen($s));
		return $s;
	} 
	$rand_name = rand_chars("ABCDEFGHIJKLM", 5, TRUE);
	mysqli_query($bdd,"INSERT INTO ".$prefix."_sequences (seq_id, seq_nom, contexte, niveau, intervs, enjeux, requis, notions, autres, production, calendrier, ressources, synopsis, outils1, outils2, eval, reinves, bilan, conseils, auteur, licence, url, date_crea, date_maj, seq_actif, seq_complete, progression) VALUES ('', '".$rand_name.'_'.mysqli_real_escape_string($bdd,$seq_info['seq_nom'])."', '".mysqli_real_escape_string($bdd,$seq_info['contexte'])."', '".$seq_info['niveau']."', '".mysqli_real_escape_string($bdd,$seq_info['intervs'])."', '".mysqli_real_escape_string($bdd,$seq_info['enjeux'])."', '".mysqli_real_escape_string($bdd,$seq_info['requis'])."', '".mysqli_real_escape_string($bdd,$seq_info['notions'])."', '".mysqli_real_escape_string($bdd,$seq_info['autres'])."', '".mysqli_real_escape_string($bdd,$seq_info['production'])."', '".mysqli_real_escape_string($bdd,$seq_info['calendrier'])."', '".mysqli_real_escape_string($bdd,$seq_info['ressources'])."', '".mysqli_real_escape_string($bdd,$seq_info['synopsis'])."', '".mysqli_real_escape_string($bdd,$seq_info['outils1'])."', '".mysqli_real_escape_string($bdd,$seq_info['outils2'])."', '".mysqli_real_escape_string($bdd,$seq_info['eval'])."', '".mysqli_real_escape_string($bdd,$seq_info['reinves'])."', '".mysqli_real_escape_string($bdd,$seq_info['bilan'])."', '".mysqli_real_escape_string($bdd,$seq_info['conseils'])."', '".mysqli_real_escape_string($bdd,$seq_info['auteur'])."', '".$seq_info['licence']."', '".mysqli_real_escape_string($bdd,$seq_info['url'])."', '".$seq_info['date_crea']."', '".date('Y-m-d')."', '1', '".$seq_info['seq_complete']."', '".$seq_info['progression']."')");
	$check_id = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT seq_id FROM ".$prefix."_sequences ORDER BY seq_id DESC LIMIT 1"));
	$check_seqnot = mysqli_query($bdd,"SELECT * FROM ".$prefix."_seqnot WHERE seq_id = '$seq_id'");
	if(mysqli_num_rows($check_seqnot)>0) {
		foreach($check_seqnot as $c_seqnot) {
			mysqli_query($bdd,"INSERT INTO ".$prefix."_seqnot (seq_id, notion_id) VALUES ('".$check_id['seq_id']."', '".$c_seqnot['notion_id']."')");
		}
	}
	$check_seqsav = mysqli_query($bdd,"SELECT * FROM ".$prefix."_seqsav WHERE seq_id = '$seq_id'");
	if(mysqli_num_rows($check_seqsav)>0) {
		foreach($check_seqsav as $c_seqsav) {
			mysqli_query($bdd,"INSERT INTO ".$prefix."_seqsav (seq_id, s_id) VALUES ('".$check_id['seq_id']."', '".$c_seqsav['s_id']."')");
		}
	}
	$seq_id = $check_id['seq_id'];
	$pathinfo="creation";
	include('db/proc/edit_xml_create.php');
	echo '<script language="Javascript">
	<!--
	document.location.replace("edit.php?page=consult&seq_id='.$seq_id.'&modif");
	// -->
	</script>';
}
if($seq_nb==0) {
	echo '<script language="Javascript">
	<!--
	document.location.replace("edit.php?page=consult");
	// -->
	</script>';
}
else {
	echo '<div class="fiche_consult">';
	for($i=1;$i<6;$i++) {
		if(isset($_SESSION['alerte_taille'.$i])) {
			echo '<p class="signal">'.$_SESSION['alerte_taille'.$i].'</p>';
		}
		if(isset($_SESSION['alerte_ext'.$i])) {
			echo '<p class="signal">'.$_SESSION['alerte_ext'.$i].'</p>';
		}
	}
	$_SESSION['seq_id'] = $seq_info['seq_id'];
	if($seq_info['seq_actif']==0) {
		echo EDIT_DESACT5;
	}
	$txt_type = array('seq_nom', 'contexte', 'niveau', 'progression', 'date_crea', 'date_maj', 'intervs', 'enjeux', 'requis', 'notions', 'SAVOIRS', 'autres', 'production', 'calendrier', 'ressources', 'synopsis', 'outils1', 'outils2', 'eval', 'reinves', 'bilan', 'conseils', 'documents', 'auteur', 'licence', 'url');
	$iden_h3 = 0;
	$prea_h3 = 0;
	$deta_h3 = 0;
	$eval_h3 = 0;
	$ment_h3 = 0;
	for($i=0;$i<count($txt_type);$i++) {
		$case=0;
		if($txt_type[$i]!='SAVOIRS'&&$txt_type[$i]!="documents"&&$txt_type[$i]!="notions") {
			if($seq_info[$txt_type[$i]]!='') { $case=1; }
		}
		else { $case = 1; }
		if($case==1) {
			if(($txt_type[$i]=='seq_nom'||$txt_type[$i]=='contexte'||$txt_type[$i]=='niveau'||$txt_type[$i]=='progression'||$txt_type[$i]=='date_crea'||$txt_type[$i]=='date_maj'||$txt_type[$i]=='intervs')&&$iden_h3 == 0) {
				$edit_fiche = '<br><hr class="hr1n"> <h3>'.IDENT4.'</h3>';
				$iden_h3 = 1;
			}
			else if(($txt_type[$i]=='enjeux'||$txt_type[$i]=='requis'||$txt_type[$i]=='notions'||$txt_type[$i]=='SAVOIRS'||$txt_type[$i]=='autres')&&$prea_h3 == 0) {
				$edit_fiche .= '<br><hr class="hr1n"> <h3>'.EDIT_TIT1.'</h3>';
				$prea_h3 = 1;
			}
			else if(($txt_type[$i]=='production'||$txt_type[$i]=='calendrier'||$txt_type[$i]=='ressources'||$txt_type[$i]=='synopsis'||$txt_type[$i]=='outils1'||$txt_type[$i]=='outils2')&&$deta_h3 == 0) {
				$edit_fiche .= '<br><hr class="hr1n"> <h3>'.EDIT_TIT2.'</h3>';
				$deta_h3 = 1;
			}
			else if(($txt_type[$i]=='eval'||$txt_type[$i]=='reinves'||$txt_type[$i]=='bilan'||$txt_type[$i]=='conseils')&&$eval_h3 == 0) {
				$edit_fiche .= '<br><hr class="hr1n"> <h3>'.EDIT_TIT3.'</h3>';
				$eval_h3 = 1;
			}
			else if(($txt_type[$i]=='auteur'||$txt_type[$i]=='licence'||$txt_type[$i]=='url')&&$ment_h3 == 0) {
				$edit_fiche .= '<br><hr class="hr1n"> <h3>'.EDIT_TIT4.'</h3>';
				$ment_h3 = 1;
			}
			$edit_fiche .= '<div id="'.$txt_type[$i].'">';
				if($txt_type[$i]!='seq_nom'&&$txt_type[$i]!='niveau'&&$txt_type[$i]!='progression'&&$txt_type[$i]!='date_crea'&&$txt_type[$i]!='date_maj'&&$txt_type[$i]!='SAVOIRS'&&$txt_type[$i]!='notions'&&$txt_type[$i]!='documents'&&$txt_type[$i]!='auteur'&&$txt_type[$i]!='licence'&&$txt_type[$i]!='url') {
					$edit_fiche .= '<br> <hr class="hr2n"> <h4>'.$txt_typen[$i].' :</h4>';
				}
				else { $edit_fiche .= '<br>'; }
				include('edit_consult_case.php');
			$edit_fiche .= '</div>';
		}
	}

	echo $edit_fiche;
	echo '<hr class="hr2n"><br>';
	if($seq_info['seq_complete']=="1") { echo EDIT_COMP1; } else { echo EDIT_COMP2; }
	echo '</div>';

	echo '<div class="fiche_boutons">';
	$seq_id = $_GET['seq_id'];
	echo '<input type="button" class="bouton boutseq" onclick="window.location.href=\'edit.php?page=consult&seq_id='.$seq_id.'&modif\';" value="'.EDIT_BUTTON1.'" />';
	echo '<form method="POST" action="" name="duplicate">
	<input class="bouton boutseq" type="submit" value="'.EDIT_BUTTON2.'" name="duplicate" />
	</form>';
	$filename = 'xml/'.$prefix.'/idlog-'.$seq_info['niveau'].'e-'.$seq_info['seq_nom'].'.xml';
	if(file_exists($filename)) {
		echo '<br><form method="POST" name="xmlrecrea" action=""><input type="hidden" name="supprxml" value="xml/'.$prefix.'/idlog-'.$seq_info['niveau'].'e-'.$seq_info['seq_nom'].'.xml"><input class="bouton boutseq" type="submit" value="XML '.FROM2.' '.date('d-m-Y',filemtime($filename)).EDIT_BUTTON3.'" name="reexport" /></form>';
		echo '<a class="none" href="xml/'.$prefix.'/idlog-'.$seq_info['niveau'].'e-'.$seq_info['seq_nom'].'.xml" download="'.$seq_info['niveau'].'e-'.$seq_info['seq_nom'].'.xml"><input class="bouton boutseq" type="submit" value="'.EDIT_BUTTON4.'" name="export" /></a>';
	}
	else {
		echo '<br><form method="POST" name="xmlcrea" action=""><input class="bouton boutseq" type="submit" value="'.EDIT_BUTTON5.'" name="xmlcrea" /></form>';
	}
	echo '<br><a onclick="submitpdf();" id="preview-pdf"><img src="img/pdf-icon.png" title="'.PDF_EXTRACT.'"/></a>';
	// Envoi par mail
	if(@$_SESSION['auth_mail']==1) {
		if(isset($_POST['mail_send'])) {
			include('./db/proc/mailing/mailing_seq.php');
		}
		echo '&nbsp; &nbsp; <a href="javascript:;clearTimeout(timer);" id="mail" onmousedown="toggleDiv(\'divmail\');"><img src="img/div/mail.png" title="'.EDIT_SEND1.'" name="noprint" class="printer" /></a>';
		echo '<div name="noprint" id="divmail" style="display:none;">
		<br>'.EDIT_SEND2.'
		<form method="POST" action="" name="mailing">';
		$mails = mysqli_query($bdd,"SELECT * FROM ".$prefix."_user WHERE mail <> ''");
		if(mysqli_num_rows($mails)>0) {
			while($mail=mysqli_fetch_array($mails)) {
				echo '<input type="checkbox" name="mail[]" id="'.$mail['mail'].'" value="'.$mail['mail'].'" checked /><label for="'.$mail['mail'].'"> '.$mail['login'].' : '.$mail['mail'].'</label><br>';
			}
		}
		echo MAILOTH.' (1) :<br><input type="email" name="mail1" /><br>
		'.MAILOTH.' (2) :<br><input type="email" name="mail2" /><br>
		<input type="hidden" name="seq_id" value="'.$seq_id.'" />
		<input type="hidden" name="seq_niv" value="'.$seq_info['niveau'].'" />
		<input type="hidden" name="seq_nom" value="'.$seq_info['seq_nom'].'" />
		<input class="bouton boutseq" type="submit" name="mail_send" value="'.SEND.'" /></form>';
		echo '</div>';
	}
	if(isset($_POST['havepdf'])) {
		include('./include/pdf_loaders/edit_fiche.php');
	}
    echo '<form id="loadpdf" action="./include/pdf_loaders/edit_fiche.php" method="post"><input type="hidden" name="havepdf" value="1" /></form>';
    echo '<script type="text/javascript">
    function submitpdf(){
    	$("#loadpdf").submit()
    }
    </script>';
    echo '<form method="POST" action="edit.php?page=consult&seq_id='.$seq_id.'">
	<input class="bouton boutseq" type="submit" value="'.EDIT_BUTTON6.'" name="suppr" /></td></tr>
	</form>';
	echo '</div';
	$edit_fiche_pdf = '<h1>'.EDIT_SEQ1.' '.$seq_info['seq_nom'].' ('.$niv.')</h1>';
	$edit_fiche_pdf .= $edit_fiche;
	$_SESSION['edit_fiche'] = $edit_fiche_pdf;
}
?>
