<?php
session_start();
ini_set('max_execution_time', 300000);
include("include/setsession.php");
require_once("include/connect.inc");
require_once("include/functions.php");
mysqli_query($link, "SET character_set_results=utf8");

$i=0;
$txt="";
$d='';
//Recupera la data passata da GET
if (isset($_GET['d']) && $_GET['d']!='') $d = $_GET['d'];

$parts = explode('-', $d);
$dw = date("w", mktime(0, 0, 0, $parts[1], $parts[2], $parts[0]));
$now = strtotime("now");
$arrFileAudio=array();

/*
Riceve una data (d=YYYY-MM-DD)
Recupera tutte le edizioni disponibili per quel giorno
Mostra le edizioni in una <select>
Segna quelle scadute in rosso e disabilitate
Prepara hidden input con:
percorso del file audio
ultima data di upload (o “Non esiste”)
*/
$txt.='<label for="edition" class="form-label">Edizione <span class="text-danger">*</span></label> <select name="edition" id="edition" style="width:400px;" required><option value="0"></option>';

if($_SESSION['userlevel']==1){ // AMMINISTRATORE
	$sql="SELECT a.*, b.description FROM ".$s_prefix."_editions a LEFT JOIN ".$s_prefix."_editionstype b ON a.idtype=b.id ORDER BY b.description, a.timeedition";	
} else { // UTENTE
	$sql="SELECT a.*, b.description FROM ".$s_prefix."_editions a LEFT JOIN ".$s_prefix."_editionstype b ON a.idtype=b.id 
	LEFT JOIN ".$s_prefix."_editionsuser c ON c.idedition=a.idtype 
	WHERE c.iduser = ".$s_iduser." ORDER BY b.description, a.timeedition";
}

//echo $sql;
$val = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
while ($row = mysqli_fetch_array($val, MYSQLI_ASSOC)) {
	if(substr($row['days'],$dw,1)==1) { // il giorno � valido
		$maxtime = strtotime($d.' '.formatHHMM($row['maxtimeup']/1000));
		//echo formatHHMM($row['maxtimeup']/1000);
		//echo $maxtime;
		if($now<$maxtime) { // l'ora di maxupload � valida
			$txt.='<option value="'.$row['id'].'">'.$row['description'].' '.formatHHMM($row['timeedition']/1000).' - ('.formatHHMM($row['maxtimeup']/1000).')</option>';
		} else {
			$txt.='<option value="'.$row['id'].'" style="color:red;" disabled>'.$row['description'].' '.formatHHMM($row['timeedition']/1000).' - TEMPO MASSIMO SUPERATO ('.formatHHMM($row['maxtimeup']/1000).')</option>';
		}
		$path = $row['path'];
		//verificare che la variabile $path termini con uno slash (/) e, se non lo fa, aggiungerlo
		if (substr($path, (strlen($path) - 1)) != '/') $path = $path . '/';

		$timeed = formatHHMM($row['timeedition']/1000);
		$filename=$row['name'].'_'.$parts[0].'-'.$parts[1].'-'.$parts[2].'_'.str_replace(":",".",$timeed).'.mp3';
		$arrFileAudio[$i][0]=$row['id'];
		$arrFileAudio[$i][1]=$path.$filename;

		//echo $_SESSION['urlbase'].'/'.$arrFileAudio[$i][1]; 
		if (file_exists($_SESSION['urlbase'].'/'.$arrFileAudio[$i][1])) {
			$arrFileAudio[$i][2]=date ("Y-m-d H:i:s", filemtime($_SESSION['urlbase'].'/'.$arrFileAudio[$i][1]));
		} else {
			$arrFileAudio[$i][2]='Non esiste';
		}
		$i++;
	}
}
$txt.='</select>';

for($i=0;$i<count($arrFileAudio);$i++){
	$txt.='<input type="hidden" id="'.$arrFileAudio[$i][0].'" name="'.$arrFileAudio[$i][0].'" value="'.$arrFileAudio[$i][1].'" style="width: 400px" />';
	$txt.='<input type="hidden" id="'.$arrFileAudio[$i][0].'_prop" name="'.$arrFileAudio[$i][0].'_prop" value="'.$arrFileAudio[$i][2].'"  style="width: 400px"/>';
}

echo $txt;
?>
