<?php

//Give access to some utility functions/variables.
include_once "GlobalUtils.php";
//Database wrapper for MySQL Calls. Not really simpler than using MySQLi directly, but who cares.
$database = new DBWrapper();
//The user authentification class handles registration and log-in. It has access to the database, and should get its data from $_POST. Constructor handles registration requests.
$userAuth = new UserAuthentification($database);
//Try to log-in with the current $_POST. Sets cookies if needed (Supports sessions)
$userAuth->tryLogin();
//Only viewable by members.
if ($userAuth->userGroup == UserAuthentification::$GUEST) {
    setcookie("mustlogin");
    header('Location: index.php');
    exit;
}
//Itemset displayed in the creator. Empty by default.
$itemset = new ItemSet();
//How will the ItemSet be saved? On a new ID, or based on an old one? create = new, some id = old one
$type = "create";
if (isset($_GET["edit"])) {
    $type = $_GET["edit"];
    $stmt3 = $database->mysqli->prepare("SELECT m.ItemSetID, m.ItemID, m.ItemCount, s.OwnerID, s.Title, s.Type, s.Map, s.Mode, s.Sortrank, s.Champion, IFNULL(v.Rating, 0) AS Rating, s.Date, b.Name, b.recMath, b.minSumLvl, b.maxSumLvl, b.showIfSumSpell, b.hideIfSumSpell, i.Gold, i.Name AS ItemName, i.Description, ibi.BuildsIntoID, ibf.BuildsFromID FROM `ItemSetMap` m JOIN `ItemSets` s ON m.ItemSetID = s.ID JOIN `ItemBlocks` b ON m.BlockID = b.ID JOIN `Items` i ON m.ItemID = i.ID LEFT JOIN `ItemBuildsFrom` ibf ON m.ItemID = ibf.ItemID LEFT JOIN `ItemBuildsInto` ibi ON m.ItemID = ibi.ItemID LEFT JOIN (SELECT ItemsetID, SUM(Rating) AS Rating FROM `Votes` GROUP BY ItemsetID) v ON v.ItemsetID = m.ItemSetID WHERE m.ItemSetID = ?;");
    $stmt3->bind_param("i", $_GET["edit"]);
    $stmt3->execute();
    $res = $stmt3->get_result();
    $ret = array();
    while ($row = $res->fetch_assoc()) {
        array_push($ret, $row);
    }
    $stmt3->free_result();
<?php

//Give access to some utility functions/variables.
include_once "GlobalUtils.php";
//Database wrapper for MySQL Calls. Not really simpler than using MySQLi directly, but who cares.
$database = new DBWrapper();
//The user authentification class handles registration and log-in. It has access to the database, and should get its data from $_POST. Constructor handles registration requests.
$userAuth = new UserAuthentification($database);
//Try to log-in with the current $_POST. Sets cookies if needed (Supports sessions)
$userAuth->tryLogin();
//Only for members.
if ($userAuth->userGroup == UserAuthentification::$GUEST) {
    setcookie("mustlogin");
    header('Location: index.php');
    exit;
}
//Save set if there is a request. This code is in mysets.php because after creating a set you will be redirected to your sets.
if (isset($_POST["saveset"])) {
    $set = json_decode($_POST["setdata"], true);
    if ($_POST["saveset"] != "create") {
        delSet($database, $userAuth, $_POST["saveset"]);
    }
    $time = time();
    $stmt = $database->mysqli->prepare("INSERT INTO `ItemSets` (`OwnerID`, `Title`, `Map`, `Mode`, `Champion`, `Date`) VALUES(?, ?, ?, ?, ?, ?);");
    $stmt->bind_param("issssi", $userAuth->id, $set['title'], $set['map'], $set['mode'], $set['champion'], $time);
    $stmt->execute();
    $stmt->close();
    $stmt = $database->mysqli->prepare("SELECT `ID` FROM `ItemSets` WHERE `Date` = ? AND `OwnerID` = ?");
    $stmt->bind_param("ii", $time, $userAuth->id);
    $stmt->execute();
    $sid = $stmt->get_result()->fetch_assoc()["ID"];