
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Posté Fiche</title>
<link rel="stylesheet" href="w3.css">

</head>
<body>
<form method="post" enctype="multipart/form-data" action="_uploadfichedir.php">
<?php
include 'header.php';
$classeid=0;
$date=$agenda="";
$ctrlErr="";

echo '<table width="90%" border="0" cellpadding="1" cellspacing="1" class="box">';
include '_serveur.php';
$sql = 'SELECT * from classes order by id ASC';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
//echo'<select name="cla" class="w3-select w3-border w3-light-grey">';
$i=0;
echo '<tr><td colspan="2">';
echo '<p><label class="w3-text-teal" >Classe:</label>';

 while($data = mysqli_fetch_array($req)) {
$classe=$data['name'];
$classeid=$data['id'];
$classe=strtoupper($classe);
// echo' <option value="'.$classeid.'">'.$classe.'</option>';
echo ' <input class="w3-check" type="checkbox" name="cla[]"  value="'.$classeid.";".$classe.'">
  <label>'.$classe.'</label>';
  $i=$i+1;
  if($i==4) {echo "<br>"; $i=0;}
        };

echo '</td></tr>';
$sql = 'SELECT * from subjects order by ordering ASC';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
echo '<tr><td colspan="2">';
echo '<p><label class="w3-text-teal" >Matières:</label>';

$i=0;
 while($data = mysqli_fetch_array($req)) {
$subject=$data['name'];
$subjectID=$data['id'];
echo ' <input class="w3-check" type="radio" name="mat"  value="'.$subjectID.";".$subject.'">
  <label>'.$subject.'</label>';
    $i=$i+1;
  if($i==4) {echo "<br>"; $i=0;}

        };


?>

</td></tr>
<tr height="50"><td height="57"><label class="w3-text-black" >Titre:</label><input name="title" type="text" width="200" id="titre" /></td>
<td height="50" >&nbsp;&nbsp;&nbsp;&nbsp;<label class="w3-text-black" >Date:</label><input name="date" type="date" width="200" id="date" value="<?php echo date("Y-m-d") ?>"/></td>
</tr>

<tr> 

<td ><label class="w3-text-black" >Fiche (file)</label><input name="userfile" type="file" class="style6" id="userfile" size="50"> 
</td>
&nbsp;&nbsp;&nbsp;&nbsp;
<td ><input name="upload" type="submit" class="style6" id="upload" value=" Publier "></td>
</tr>
</table>
</form>

<?php
include("sjmSendPush.php");
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$name = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$size = $_FILES['userfile']['size'];
$type = $_FILES['userfile']['type'];
$date=$_POST['date'];
$title=$_POST['title'];
$genre=substr($name, -3);
$genre=strtolower($genre);
 $name = preg_replace('/[^A-Za-z0-9-_ \.\[\]]/', '', $name);
$subj1=array();
$mat1=$_POST['mat'];
   $subj1=explode(";",$mat1);
 $subjectID=$subj1[0];
  $subject=$subj1[1];
$cellm=array();
$cla1=$_POST['cla'];
if(($genre=="jpg")||($genre=="peg")||($genre=="gif")||($genre=="png")||($genre=="pdf")||($genre=="ocx")||($genre=="doc")||($genre=="mp4")||($genre=="mov")||($genre=="ppt")||($genre=="pps")||($genre=="ptx")||($genre=="psx")
||($genre=="ptx")||($genre=="mp3"))
{
for($i=0;$i<sizeof($cla1);$i++) {
   $cellm=explode(";",$cla1[$i]);
  $classeid=$cellm[0];
  $classe=$cellm[1];
  $uploadDir[$i]="FICHE/".$subjectID."/".$classeid."/";	

	$filepath[$i]=$uploadDir[$i].$name;
	if (!file_exists($uploadDir[$i])) {
    mkdir($uploadDir[$i], 0777, true);
                        }
echo '<div class="w3-container">';
$sql = 'select * from fiches  where classID="'.$classeid.'" and date="'.$date.'" and type="'.$type.'" and url="'.$filepath[$i].'"';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=mysqli_query($link, $sql);
  while($data = mysqli_fetch_array($req)){
                  $mat=$data['url'];
				
}
if($mat==""){
if($i==0){
$result = move_uploaded_file($tmpName, $filepath[$i]);
if (!$result) {
echo "Error uploading file";
exit;
	}
}
else
{
	copy($filepath[0],$filepath[$i]);
}
	$sql1='INSERT INTO fiches  VALUES(DEFAULT,"'.$classeid.'","'.$subjectID.'","'.$title.'","'.$date.'","'.$type.'","'.$filepath[$i].'")';
 if(!(mysqli_query($link, $sql1))){
    echo "ERROR: Could not able to execute $sql1. " . mysqli_error($link);
                          }
echo '<div class="w3-panel w3-blue w3-round-xlarge">';
echo "<p align='center'>Fiche Téléchargé</p></div>";
$sql = 'SELECT * from students join users on students.userID=users.id 
								join classes on students.classID=classes.id  where classes.id="'.$classeid.'" order by classes.id ASC';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
$j=0;
 while($data = mysqli_fetch_array($req)) {
$classe=$data[13];
$classeid=$data[4];
$userID[$j]=$data[1];
$userType=$data[10];
$title1=" Voir la fiche de ".$subject;
$message=" Voir la fiche ".$title. " pour la classe ".$classe." envoyée le ".$date;
$sql1= 'INSERT INTO notifications VALUES(DEFAULT,"'.$title1.'","'.$message.'","'.$date.'",NULL,"'.$userID[$j].'")';
   if(!(mysqli_query($link, $sql1))){
    echo "ERROR: Could not able to execute $sql1. " . mysqli_error($link);
        }
		$j++;
		
 }
for($p=0;$p<=$j;$p++){
$sql = 'SELECT * from devices where userID="'.$userID[$p].'"';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
                          }
$req=  mysqli_query($link, $sql);

echo "<table width='95%' border=1 bordercolor='#D1FAFC'>"; 
while ($data = mysqli_fetch_array($req)) {
	                 $deviceType=$data[2];
					 $pushid=$data[0];
					 $pushid=(string)$pushid;

					 if($deviceType=="ios") $deviceType="iOS";
					      $payload = array('type' =>$deviceType,
					           'data'=>["title" => $title, "body" => $message],
							   'devices' =>[$pushid]);
					
                     echo sendPush(json_encode($payload));
                          }
					
                  }
$res_cycle = mysqli_query($link, "SELECT cycleID FROM classes WHERE id='$classeid'");
$row_cycle = mysqli_fetch_assoc($res_cycle);
$cycleid = $row_cycle['cycleID'] ?? null;

if(($cycleid==1)or($cycleid==2)){
				  $sql = 'SELECT * FROM devices 
            WHERE userID="' . $userID[$p] . '" 
            OR userID IN (240200,100102, 10023)';
			                       }
		elseif($cycleid==5){
				  $sql = 'SELECT * FROM devices 
            WHERE userID="' . $userID[$p] . '" 
            OR userID IN (100103,100102)';
		}
      else
								   {
				$sql = 'SELECT * FROM devices 
            WHERE userID="' . $userID[$p] . '" 
            OR userID IN (253062,100102, 10023,10010)';
									   }
    if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
                          }
$req=  mysqli_query($link, $sql);

echo "<table width='95%' border=1 bordercolor='#D1FAFC'>"; 
while ($data = mysqli_fetch_array($req)) {
	                 $deviceType=$data[2];
					 $pushid=$data[0];
					 $pushid=(string)$pushid;

					 if($deviceType=="ios") $deviceType="iOS";
					      $payload = array('type' =>$deviceType,
					           'data'=>["title" => $title, "body" => $message],
							   'devices' =>[$pushid]);
					
                     echo sendPush(json_encode($payload));
                          }


}
else
{
echo '<div class="w3-panel w3-blue w3-round-xlarge">';
echo "<p align='center'>Fiche Non Téléchargé</p></div>";
}


}
}
else
{echo '<div class="w3-panel w3-blue w3-round-xlarge">';
echo "<p align='center'>Genre de Fiche interdit</p></div>";

}
}




?>

</body>
</html>