<!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 name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="w3.css">
<title>Ecrire Circulaire</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="_uploadcirculaireclass.php">
<?php
include "header.php";
include '_serveur.php';
echo '<table width="90%" border="0" cellpadding="1" cellspacing="1" class="box">';

$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 '<tr><td><label class="w3-text-black" >Classe:</label>';
//echo'<select name="cla" class="w3-select w3-border w3-light-grey">';
echo '<select name="classe" class="w3-select w3-border w3-light-grey">';

 while($data = mysqli_fetch_array($req)) {
$classe=$data['name'];
$classID=$data['id'];
$cycleID=$data['cycleID'];
$classe=strtoupper($classe);
echo '</p><label class="w3-text-teal">classe:</label>';
 echo' <option value="'.$classID.";".$cycleID.'">'.$classe.'</option>';

        };

echo '</select></td>';

?>
 <td> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label class="w3-text-black" >Genre: </label>  
    <Input class="w3-radio" type="radio" Name ="person" value=1>&nbsp;&nbsp;&nbsp;&nbsp;Profs. Seulement&nbsp;&nbsp;&nbsp;&nbsp;<img src="profs.png" width="52" height="33" align="absmiddle"/>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Input  class="w3-radio" type = "radio" Name ="person" value=0>&nbsp;&nbsp;&nbsp;&nbsp;Profs. Et Elèves&nbsp;&nbsp;&nbsp;&nbsp;<img src="proel.png" width="45" height="33" align="absmiddle" /></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" /></td>
</tr>

<tr> 

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

<?php
$date="";
$cycle="";
$circulaire="";
$trig=1;
include("_serveur.php");

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$uploadDir = 'circulaire/';
//$uploadDir = 'home/domains/collegendp.com/httpdocs/download/';

$classe=$_POST['classe'];
$cellm=explode(";",$classe);
 $classe1=$cellm[0];
 $cycle=$cellm[1];

$person=$_POST['person'];
$title=$_POST['title'];
$date=$_POST['date'];
$name = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$size = $_FILES['userfile']['size'];
$type = $_FILES['userfile']['type'];
$filepath = $uploadDir.$name;
$genre=substr($name, -3);
$genre=strtolower($genre);

if(($genre=="jpg")||($genre=="peg")||($genre=="gif")||($genre=="png")||($genre=="pdf")||($genre=="ocx")||($genre=="doc")||($genre=="ppt")||($genre=="pps")||($genre=="ptx")||($genre=="psx")||($genre=="ptx"))
{

$result = move_uploaded_file($tmpName, $filepath);
if (!$result) {
echo "Error uploading file";
exit;
}
/*if(!get_magic_quotes_gpc())
{
$name = addslashes($name);
$filepath = addslashes($filepath);
}*/
echo '<div class="w3-container">';
$sql = 'INSERT INTO circulaire  VALUES(DEFAULT,"'.$cycle.'","'.$person.'","'.$title.'","'.$date.'","'.$type.'","'.$filepath.'")';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
                                    }
									
echo '<div class="w3-panel w3-blue w3-round-xlarge">';
echo "<p align='center'>Circulaire Téléchargé</p></div>";
}
else
{
echo '<div class="w3-panel w3-blue w3-round-xlarge">';	
echo "<p align='center'>Genre Interdit</p></div>";
	
}
include("sjmSendPush.php");
    $message="Voir le circulaire nommé ".$title;
	$sql = 'SELECT * from students join users on students.userID=users.id 
								join classes on students.classID=classes.id  where classes.id="'.$classe1.'"  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];
$sql1= 'INSERT INTO notifications VALUES(DEFAULT,"Circulaire","'.$message.'","'.$date1.'",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++){
if(($cycle==1)or($cycle==2)){
$sql = 'SELECT * from devices where userID="'.$userID[$p].'" or userID=240200 or userID=10023 or userID=100102';
	                            }
	else
	{
		$sql = 'SELECT * from devices where userID="'.$userID[$p].'" or userID=10023 or userID=100102';

	}
	   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));
                          }
                    }
 



}
?>

</body>
</html>