<?php
$check_imgmode = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_imgmode'");
$check_imgmode_info = mysqli_fetch_assoc($check_imgmode);
if ($check_imgmode_info['valeur']=="gen") {
	if($specific_extract==0) {
		$img_name = "idlog_edt";
	}
	else {
		if($espid!=0) {
			$img_name = str_replace(' ','_',$espaces_info['espace_nom'])."_edt";
		}
		else if($userid!=0) {
			$img_name = str_replace(' ','_',$affname)."_edt";
		}
	}
}
else if ($check_imgmode_info['valeur']=="spe") {
	if($specific_extract==0) {
		$img_name = "cdi_edt_".$week;
	}
	else {
		if($espid!=0) {
			$img_name = str_replace(' ','_',$espaces_info['espace_nom'])."_edt_".$week;
		}
		else if($userid!=0) {
			$img_name = str_replace(' ','_',$affname)."_edt_".$week;
		}
	}
}

// pour l'image, on va chercher les éléments html-image dans edt_table et edt_cellule, avec possibilité d'extraire des éléments avec data-html2canvas-ignore
echo '<script type="text/javascript">
	$(document).ready(function(){
		var divw = $("#html-imageu'.$userid.'e'.$espid.'").width();
		var imgageData;
		$("#upload-Html2Imageu'.$userid.'e'.$espid.'").on("click", function() {
			html2canvas($("#html-imageu'.$userid.'e'.$espid.'")[0], {
	    		width:divw,
	    		height:'.$total_height.',
	    		scrollX:0,
	    		scrollY:0,
	    		scale:1
	   		 }).then(canvas => {
			    imgageData = canvas.toDataURL("image/png");
			    // pour uploader :
				var newData = imgageData.replace(/^data:image\/png/, "data:application/octet-stream");
				document.getElementById("fileimgu'.$userid.'e'.$espid.'").value = imgageData;
				document.getElementById("upload_imgu'.$userid.'e'.$espid.'").submit();
			});			
		});
	});
</script>';

if($_SESSION['user_droits'][2]==2) { 
	echo '<form method="POST" action="" style="display:inline-block;"><input type="hidden" name="havepdfu'.$userid.'e'.$espid.'" value="1" /><input type="submit" class="boutonpdf" name="subpdf'.$userid.'e'.$espid.'" value="" /></form>';
	// Envoi par mail
	if(@$_SESSION['auth_mail']==1) {
		if(isset($_POST['mail_sendu'.$userid.'e'.$espid])) {
			$_SESSION['edt_tablepdf'] = $_SESSION['edt_tablepdfu'.$userid.'e'.$espid];
			if($espid!=0) {
				$_SESSION['edt_tit'] = $espaces_info['espace_nom'].' '.$entete_titw;
				$_SESSION['edt_msg']='';
				$_SESSION['lieu']=$espaces_info['espace_nom'];
			}
			else if($userid!=0) {
				$_SESSION['edt_tit'] = $affname.' '.$entete_titw;
				$_SESSION['edt_msg']='';
				$_SESSION['lieu']=$affname;
			}
			else {
				$_SESSION['lieu']='CDI';
			}
			include('./db/proc/mailing/mailing_edt.php');
		}
		echo '&nbsp; &nbsp; <a href="javascript:;clearTimeout(timer);" id="mailu'.$userid.'e'.$espid.'" onmousedown="toggleDiv(\'divmailu'.$userid.'e'.$espid.'\');"><img src="img/div/mail.png" title="'.EDT_SEND1.'" name="noprint" class="printer"/></a>';
		echo '<div name="noprint" id="divmailu'.$userid.'e'.$espid.'" style="display:none;">
		<p>'.EDT_SEND2.'</p>
		<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'].'u'.$userid.'e'.$espid.'" value="'.$mail['mail'].'" checked /><label for="'.$mail['mail'].'u'.$userid.'e'.$espid.'"> '.$mail['login'].' : '.$mail['mail'].'</label><br>';
			}
		}
		echo MAILOTH.' (1) : <input type="email" name="mail1" /><br>
		'.MAILOTH.' (2) : <input type="email" name="mail2" /><br>
		<input type="hidden" name="week" value="'.$week.'" />
		<input class="bouton boutedt paddvalid1" type="submit" name="mail_sendu'.$userid.'e'.$espid.'" value="'.SEND.'" /></form>';
		echo '</div>';
	}
	if(isset($_POST['subpdf'.$userid.'e'.$espid])) {
		$_SESSION['edt_tablepdf'] = $_SESSION['edt_tablepdfu'.$userid.'e'.$espid];
		if($espid!=0) {
			$_SESSION['edt_tit'] = $espaces_info['espace_nom'].' '.$entete_titw;
			$_SESSION['edt_msg']='';
		}
		else if($userid!=0) {
			$_SESSION['edt_tit'] = $affname.' '.$entete_titw;
			$_SESSION['edt_msg']='';
		}
		$_SESSION['pdf'] = 1;
	}    
}

if(isset($_POST["fileimgu".$userid."e".$espid])) { 
	$dossier = './img/edt/'.$prefix.'/';
    $nom = $img_name.'.png';
 	$current = file_get_contents($_POST["fileimgu".$userid."e".$espid]);
	file_put_contents($dossier.$nom, $current);
	//suppression des images en trop :
	if ($check_imgmode_info['valeur']=="spe") {
		$imgnb = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_imgnb'");
		$imgnb_info = mysqli_fetch_assoc($imgnb);
		$nbmax = $imgnb_info['valeur'];
		$dossier = opendir('./img/edt/'.$prefix.'/'); 
		$fichiers = array();
		while(false !== ($fichier = readdir($dossier))) {
			if($fichier != '.' && $fichier != '..') {
				array_push ($fichiers,$fichier);
			}
		}
		$count = count($fichiers);
		if($count >= $nbmax) {
			// Inverser l'ordre des fichiers png pour ne pas supprimer les derniers fichiers
			rsort($fichiers);
			// Suppression...
			for($i = $nbmax; $i < $count; $i++) {
				unlink('./img/edt/'.$prefix.'/'.$fichiers[$i]);
			}
		}
		closedir($dossier);
	}
	$_SESSION['reload']=1;
}
echo '<div class="previewImage" id="previewImage">
	<table class="noborder"><tr><td><img src="img/toimage.png" title="'.EDT_GEN1.'"/></td><td>
	<form name="upload_img" id="upload_imgu'.$userid.'e'.$espid.'" method="POST" enctype="multipart/form-data" action=""><input name="fileimgu'.$userid.'e'.$espid.'" type="hidden" id="fileimgu'.$userid.'e'.$espid.'" value="" /><a id="upload-Html2Imageu'.$userid.'e'.$espid.'" href="javascript:;"><i class="fas fa-angle-double-right"></i>'.EDT_GEN2.'</a><br><i>'.EDT_GEN3.'</i></form></td></tr></table>
</div></div>';
?>