require_once("inc/func.php");
$reserved = [];
start();
if (isset($_COOKIE['fid']))
{
$fid = intval($_COOKIE['fid']);
$flea_q = q("SELECT * FROM flea_fleas WHERE id = $fid LIMIT 1");
if (!rows())
redirect("begin.php");
else
{
$flea = assoc($flea_q);
if (($flea['booking_open_time'] > time() || $flea['booking_close_time'] < time()) && !$LOGGED_IN)
redirect("begin.php");
}
}
else
redirect("begin.php");
$get_free_tables = q("SELECT SUM(tables) FROM flea_orders WHERE flea_id = $fid");
$free_tables = $flea['tables'] - mysqli_result($get_free_tables, 0, 0);
//$free_tables = $flea['tables'] - mysql_result(q("SELECT SUM(tables) FROM flea_orders WHERE flea_id = $fid"), 0, 0);
if ($_POST['p_s'] ?? null) // Insert a new booking into the database
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$phone = str_replace(" ", "", $_POST['phone']);
$tables = intval($_POST['tables']);
$square_count = intval($_POST['squares_count']);
$squares = explode(",", $_POST['squares_list']);
foreach ($squares as $value)
$square_qs[] = "square = ".intval($value);
$square_qstr = join(" OR ", $square_qs);
q("SELECT * FROM flea_bookings WHERE ($square_qstr) AND flea_id = $fid LIMIT 1");
if (rows())
redirect("index.php?succeed=".urlencode("Tilaus ei onnistunut.\n\nJoku muu kerkesi varaamaan haluamasi ruudun. Joudut tekemään tilauksen uudestaan."));
else if (min(strlen($first_name), strlen($last_name)) < 2 || strlen($phone) < 7)
redirect("index.php?succeed=".urlencode("Tilaus ei onnistunut. Joudut tekemään sen uudelleen."));
else if ($tables > 2)
redirect("index.php?succeed=".urlencode("Tilaus ei onnistunut. Pöytiä voi tilata nollasta kahteen, ei enempää. Joudut tekemään tilauksen uudestaan."));
else if ($tables > $free_tables)
redirect("index.php?succeed=".urlencode("Tilaus ei onnistunut.\n\nTilasit pöytiä enemmän kuin on vapaana. Tämä johtuu mahdollisesti siitä, että joku muu varasi pöydän/pöytiä, kun vasta kirjoitit tilauksesi tietoja."));
else if (($flea['booking_open_time'] > time() || $flea['booking_close_time'] < time()) && !$LOGGED_IN)
redirect("index.php?succeed=".urlencode("Tilaus ei onnistunut.\n\nKirpputorin varaus ei ole auki."));
else
{
q("SELECT id FROM flea_orders WHERE first_name = ".esc($first_name)." AND last_name = ".esc($last_name)." AND flea_id = $fid LIMIT 1");
if (rows())
redirect("index.php?succeed=".urlencode("Tilaus ei onnistunut.\n\nSamalla nimellä on jo tilaus.\nJos haluat tehdä muutoksia varaukseesi, se tapahtuu linkistä sivun oikeasta alareunasta."));
$price = $square_count*$flea['price_square'] + $tables*$flea['price_table'];
$code = rand(1000, 9999);
q("INSERT INTO flea_orders (flea_id, first_name, last_name, phone_nr, tables, price, code) VALUES ($fid, ".esc(ucci($first_name)).", ".esc(ucci($last_name)).", ".esc($phone).", $tables, $price, $code)");
$order_id = mysqli_insert_id($C1);
foreach ($squares as $square)
q("INSERT INTO flea_bookings (flea_id, square, order_id) VALUES ($fid, ".intval($square).", $order_id)");
redirect("index.php?succeed=".urlencode("Paikkavaraus luotu onnistuneesti! Kiitos tilauksesta!\n\nHUOM! Tässä koodi, jolla voit muuttaa tilauksesi tietoja, kirjaa se ylös:\n$code\n\nMuutokset ja peruutukset sivun oikeassa alareunassa olevasta linkistä."));
}
}
$book_q = q("SELECT * FROM flea_bookings WHERE flea_id = $fid");
while ($book = assoc($book_q))
{
$all = "";
$reserved[] = $book['square'];
$squares_q = q("SELECT square FROM flea_bookings WHERE order_id = $book[order_id] AND flea_id = $fid ORDER BY square ASC");
while ($square = assoc($squares_q))
$all .= $square['square'].", ";
$all = substr($all, 0, strlen($all)-2);
$booker_info = assoc(q("SELECT * FROM flea_orders WHERE id = $book[order_id] AND flea_id = $fid LIMIT 1"));
$booker_info['squares'] = $all;
$booker[] = $booker_info;
}
if (!$reserved)
$reserved = array(0);
db_close();
tprint_begin("Kirpputorivaraus");
tprint("grid_out_begin");
$p_id = 0;
for ($x=6; $x>=1; $x--)
{
// Define which squares will be printed
$not = array(0);
$block = array(0);
if ($x == 1)
{
$not = array(16,17);
$yes = array(0);
}
else if ($x == 2)
{
$yes = array(4,5,7,8,10,11,13,14,16,17,19,20,22,23,31,32,34,35);
$block = array(2 => "Ilmast.", 27 => "Ilmast.");
}
else if ($x == 3)
$yes = array(4,5,7,8,10,11,13,14,16,17,19,20,22,23,31,32,34,35);
else if ($x == 4)
$yes = array(4,5,7,8,10,11,13,14,16,17,19,20,22,23,25,26,28,29,31,32,34,35);
else if ($x == 5)
$yes = array(4,5,7,8,10,11,13,14,16,17,19,20,22,23,25,26,28,29,31,32,34,35);
else if ($x == 6)
$yes = array(0);
// Loop that prints squares
$row_id = 0;
for ($y=0; $y<38; $y++)
{
if ($x>=2 && $x<=5 && ($y<1 || $y>=37))
continue;
$row_id++; // Increases when a square is printed
if (array_search($row_id, $not) === false)
$class = "square left$x border$x";
else
$class = "square left$x";
if (array_search($row_id, $yes) !== false)
{
$p_id++;
$id = $p_id;
$id_str = " id='$p_id'";
$reserved_id = array_search($p_id, $reserved);
if ($reserved_id !== false)
{
$class .= " reserved";
$orderer = $booker[$reserved_id];
if ($LOGGED_IN)
$id_str .= " reserved='true' onmouseover='hover(this, \"$orderer[first_name] $orderer[last_name]\", \"$orderer[phone_nr]\", \"$orderer[squares]\", $orderer[tables], $orderer[price], $orderer[code], $orderer[id], ".((strpos($orderer['squares'], ",") !== false)?'true':'false').", $orderer[tables])' onmouseout='unhover()'";
}
else
{
$class .= " active";
$id_str .= " onmouseover='this.style.backgroundColor=\"#00a31d\"' onmouseout='this.style.backgroundColor=\"#007c16\"' onclick='grid_select($p_id)'";
}
}
else
{
$id_str = "";
$id = "";
$block_res = array_key_exists($row_id, $block);
if ($block_res != false)
{
$id = $block[$row_id];
$class .= " blocked";
$id_str = " block='$block_res'";
}
}
tprint("grid_div", array($class, $id_str, "top:".(800 - $y*21)."px;", $id));
}
}
tprint("grid_div", array("entrance", "", "", "Parkkihallin
Sisäänajo"));
tprint("grid_div", array("ingong", "", "", "Ovet
Sisään"));
tprint("grid_div", array("claimer", "", "", "Tuulosen Parkkihalli"));
tprint("grid_out_end");
$flea_information = "Kirpputori: ".date("j.n.Y", $flea['time'])." ".date("H:i", $flea['flea_open_time'])."-".date("H.i", $flea['flea_close_time'])."
Varaaminen avoinna: ".date("j.n.Y", $flea['booking_open_time'])."-".date("j.n.Y", $flea['booking_close_time']);
$dis_1 = ($free_tables < 1)? ' disabled':'';
$dis_2 = ($free_tables < 2)? ' disabled':'';
tprint("grid_tools", array($free_tables, $flea['price_square'], $flea['price_table'], $fid, ($flea['flea_close_time'] < time())?'true':'false', $flea_information, $dis_1, $dis_2));
$succeed = $_GET['succeed'] ?? null;
if ($succeed)
tprint("alert", array(prepareAlert($succeed), "index.php"));
tprint_end();
?>