["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 = db::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 = db::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) { db::run( "INSERT INTO ownedAssets (assetId, userId, TagID, timestamp) VALUES (:AssetID, :UserID, :TagID, UNIX_TIMESTAMP())", [":UserID" => $UserID, ":AssetID" => $AssetID, ":TagID" => $TagID] ); } $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["userName"], $ItemName, $AssetID, count($UserIDs), $ConditionString, $TagID )); } } pageBuilder::$pageConfig["title"] = "Give Asset"; pageBuilder::buildHeader(); ?>
Be careful about how you use this. Actions done here can be reverted, but may take a while to roll back.