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


$txt="0";
$id = 0;
$idtype			= 0;
$timeedition	= 0;
$maxtimeup		= 0;
$day_0 			= 0;
$day_1 			= 0;
$day_2 			= 0;
$day_3 			= 0;
$day_4 			= 0;
$day_5 			= 0;
$day_6 			= 0;
$days			= "";
$path			= "";
$name			= "";

if (isset($_POST['id']) && $_POST['id']!='') 				$id = $_POST['id'];
if (isset($_POST['idtype']) && $_POST['idtype']!='')		$idtype = $_POST['idtype'];
if (isset($_POST['timeedition']) && $_POST['timeedition']!='') 	$timeedition = formatFromHHMM($_POST['timeedition']);
if (isset($_POST['maxtimeup']) && $_POST['maxtimeup']!='') 		$maxtimeup = formatFromHHMM($_POST['maxtimeup']);
if (isset($_POST['day_0']) && $_POST['day_0']!='') 				$day_0 = $_POST['day_0'];
if (isset($_POST['day_1']) && $_POST['day_1']!='') 				$day_1 = $_POST['day_1'];
if (isset($_POST['day_2']) && $_POST['day_2']!='') 				$day_2 = $_POST['day_2'];
if (isset($_POST['day_3']) && $_POST['day_3']!='') 				$day_3 = $_POST['day_3'];
if (isset($_POST['day_4']) && $_POST['day_4']!='') 				$day_4 = $_POST['day_4'];
if (isset($_POST['day_5']) && $_POST['day_5']!='') 				$day_5 = $_POST['day_5'];
if (isset($_POST['day_6']) && $_POST['day_6']!='') 				$day_6 = $_POST['day_6'];
if (isset($_POST['path']) && $_POST['path']!='') 				$path = $_POST['path'];
if (isset($_POST['name']) && $_POST['name']!='') 				$name = $_POST['name'];

$days=$day_0.$day_1.$day_2.$day_3.$day_4.$day_5.$day_6;

if(substr($path,(strlen($path)-1))=='/'){
	$path=substr($path,0,(strlen($path)-1));
}

if($id==0) {
	$id=1;
	$sql="SELECT MAX(id) AS maxid FROM ".$s_prefix."_editions";
	$val = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
	if($res = mysqli_fetch_array($val, MYSQLI_ASSOC)) {
		$id=($res['maxid']+1);
	}
	
	$sql="INSERT INTO ".$s_prefix."_editions (id, idtype, timeedition, maxtimeup, days, path, name) VALUE (".$id.", ".$idtype.", ".$timeedition.", ".$maxtimeup.", '".$days."', '".$path."', '".$name."')";
//echo $sql.'<br>';
	$res = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
} else {
	$sql="UPDATE ".$s_prefix."_editions SET idtype=".$idtype.", timeedition='".$timeedition."', maxtimeup='".$maxtimeup."', days='".$days."', path='".$path."', name='".$name."' WHERE id=".$id;
	$res = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
}


echo $_SESSION['urlbase'].'/'.$path . "<br/>";
// verifico path e la creo se non c'�
if (!file_exists($_SESSION['urlbase'].'/'.$path)) {

    mkdir($_SESSION['urlbase'].'/'.$path, 0777, true);
} 
header("Location: index.php?sz=0&go=showEditions");
?>