["Type" => "Integer"], "UserID" => ["Type" => "Integer"] ]; function SetError($text) { global $Alert; $Alert = ["text" => $text, "color" => "danger"]; } if($_SERVER["REQUEST_METHOD"] == "POST") { $AssetID = $_POST["AssetID"] ?? ""; $Condition = $_POST["Condition"] ?? ""; $ConditionData = $_POST["ConditionData"] ?? ""; if (empty($AssetID)) SetError("Asset ID cannot be empty"); else if (!isset($Conditions[$Condition])) SetError("Condition is not valid"); else if (empty($ConditionData)) SetError("Condition data must be set"); else if ($Conditions[$Condition]["Type"] == "Integer" && !is_numeric($ConditionData)) SetError("Condition data must be a number"); else if (!Catalog::GetAssetInfo($AssetID)) SetError("The asset you're trying to give does not exist"); if ($Alert === false) { $ItemName = Catalog::GetAssetInfo($AssetID)->name; $ConditionString = ""; $UserIDs = []; $TagID = generateUUID(); if ($Condition == "UserID") { $ConditionString = "had the user ID $ConditionData"; $UserIDs = Database::singleton()->run( "SELECT id FROM users WHERE id = :ConditionData AND NOT (SELECT COUNT(*) FROM ownedAssets WHERE userId = users.id AND assetId = :AssetID)", [":AssetID" => $AssetID, ":ConditionData" => $ConditionData] )->fetchAll(\PDO::FETCH_COLUMN); } else if ($Condition == "AssetID") { $ConditionString = "purchased an asset with ID $ConditionData"; $UserIDs = Database::singleton()->run( "SELECT id FROM users WHERE id IN (SELECT userId FROM ownedAssets WHERE assetId = :ConditionData) AND NOT (SELECT COUNT(*) FROM ownedAssets WHERE userId = users.id AND assetId = :AssetID)", [":AssetID" => $AssetID, ":ConditionData" => $ConditionData] )->fetchAll(\PDO::FETCH_COLUMN); } foreach ($UserIDs as $UserID) { Database::singleton()->run( "INSERT INTO ownedAssets (assetId, userId, TagID, timestamp) VALUES (:AssetID, :UserID, :TagID, UNIX_TIMESTAMP())", [":UserID" => $UserID, ":AssetID" => $AssetID, ":TagID" => $TagID] ); } Database::singleton()->run( "UPDATE assets SET Sales = Sales + :UserCount WHERE id = :AssetID", [":AssetID" => $AssetID, ":UserCount" => count($UserIDs)] ); $Alert = ["text" => sprintf("\"%s\" has been given to %d user(s) (Tag ID %s)", $ItemName, count($UserIDs), $TagID), "color" => "primary"]; Users::LogStaffAction(sprintf( "[ Give Asset ] %s gave \"%s\" (ID %s) to %d user(s) who %s (Tag ID %s)", SESSION["user"]["username"], $ItemName, $AssetID, count($UserIDs), $ConditionString, $TagID )); } } $pageBuilder = new PageBuilder(["title" => "Give Asset"]); $pageBuilder->buildHeader(); ?>

Give Asset

px-2 py-1" role="alert">

Be careful about how you use this. Actions done here can be reverted, but may take a while to roll back.

Give to anyone who has
buildFooter(); ?>