}
    }
}
//HTML Template for the user-specific part on the website
$userTemplate = new Template("www/templates/loggedin_template.html");
//Give the template the possibility to use the UserAuthentification class.
$userTemplate->setVar("UserAuthentification", $userAuth);
//Array mapping a champion name to its icon. Array ( ["ChampionName"] => "Icon", ["ChampionName2"] => "Icon2" )
$champJson = cURL("https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion?champData=image&api_key=" . $apiKey);
$championData = parseChampJSON($champJson, array("Champion" => new ArrayObject(array("img" => "www/any.png", "name" => "Any Champion", "key" => "Champion"), ArrayObject::ARRAY_AS_PROPS)));
//$spellData = parseSpellJSON(cURL("https://global.api.pvp.net/api/lol/static-data/euw/v1.2/summoner-spell?spellData=image,key&api_key=" . $apiKey));
$res = $database->query("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 `OwnerID` = " . $userAuth->id . " ORDER BY `Sortrank` DESC;", false);
$rawSets = collapseResultArray($res);
$sets = array();
foreach ($rawSets as $rawSet) {
    $set = ItemSet::newSet($rawSet);
    $set->setEditable(true);
    $set->canRate(false);
    $set->canCopy(true);
    array_push($sets, $set);
}
$setListTemplate = new Template("www/templates/home_setList_template.html");
$setListTemplate->setVar("ChampionData", $championData);
//$setListTemplate->setVar("SpellData", $spellData);
$setListTemplate->setVar("ItemSets", $sets);
$headerTemplate = new Template("www/templates/header_template.html");
$headerTemplate->setVar("UserTemplate", $userTemplate);
$headerTemplate->setVar("buttons", array(new ArrayObject(array("name" => "Home", "href" => "index.php"), ArrayObject::ARRAY_AS_PROPS), new ArrayObject(array("name" => "Create Set", "href" => "createset.php"), ArrayObject::ARRAY_AS_PROPS)));
$mysets_template = new Template("www/templates/mysets_template.html");
$mysets_template->setVar("ItemSetTemplate", $setListTemplate);
$footerTemplate = new Template("www/templates/footer_template.html");
    $stmt3->free_result();
    $stmt3->close();
    $itemset = ItemSet::newSet(collapseResultArray($ret)[$_GET["edit"]]);
} elseif (isset($_GET["upload"])) {
} elseif (isset($_GET["copy"])) {
    $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["copy"]);
    $stmt3->execute();
    $res = $stmt3->get_result();
    $ret = array();
    while ($row = $res->fetch_assoc()) {
        array_push($ret, $row);
    }
    $stmt3->free_result();
    $stmt3->close();
    $itemset = ItemSet::newSet(collapseResultArray($ret)[$_GET["copy"]]);
}
//HTML Template for the user-specific part on the website
$userTemplate = new Template("www/templates/loggedin_template.html");
//Give the template the possibility to use the UserAuthentification class.
$userTemplate->setVar("UserAuthentification", $userAuth);
//Array mapping a champion name to its icon. Array ( ["ChampionName"] => "Icon", ["ChampionName2"] => "Icon2" )
$champJson = cURL("https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion?champData=image&api_key=" . $apiKey);
$championData = parseChampJSON($champJson, array("Champion" => new ArrayObject(array("img" => "www/any.png", "name" => "Any Champion", "key" => "Champion"), ArrayObject::ARRAY_AS_PROPS)));
//$spellData = parseSpellJSON(cURL("https://global.api.pvp.net/api/lol/static-data/euw/v1.2/summoner-spell?spellData=image,key&api_key=" . $apiKey));
//Get all those items...
$itemdata = recursiveArrayObject(collapseResultArray2($database->query("SELECT i.ID AS ItemID, i.Gold, i.Name AS ItemName, i.Description, ibi.BuildsIntoID, ibf.BuildsFromID, it.Tag FROM `Items` i LEFT JOIN `ItemBuildsFrom` ibf ON i.ID = ibf.ItemID LEFT JOIN `ItemBuildsInto` ibi ON i.ID = ibi.ItemID LEFT JOIN `ItemTags` it ON i.ID = it.ItemID"), true, true, true), ArrayObject::ARRAY_AS_PROPS);
$headerTemplate = new Template("www/templates/header_template.html");
$headerTemplate->setVar("UserTemplate", $userTemplate);
$headerTemplate->setVar("buttons", array(new ArrayObject(array("name" => "Home", "href" => "index.php"), ArrayObject::ARRAY_AS_PROPS), new ArrayObject(array("name" => "My Sets", "href" => "mysets.php"), ArrayObject::ARRAY_AS_PROPS)));
//And then, printerino!
/**
 * Creates an ItemSet and adjusts it depending on the user.
 * @param $database
 * @param $userAuth
 * @param $val
 * @return ItemSet
 */
function newItemSet($database, $userAuth, $val)
{
    $set = ItemSet::newSet($val);
    $set->setEditable($set->ownerID == $userAuth->id || $userAuth->userGroup == UserAuthentification::$ADMIN);
    $res = $database->query("SELECT `Rating` FROM `Votes` WHERE `UserID`=" . $userAuth->id . " AND `ItemsetID` = " . $set->ID . ";");
    $set->canRate($userAuth->userGroup != UserAuthentification::$GUEST && $set->ownerID != $userAuth->id && empty($res));
    $set->canCopy($userAuth->id != UserAuthentification::$GUEST);
    return $set;
}