Repositories | Polyback | Log in to see private repositories


polyback-website / b0d92d543d33db09acbae728aa0eb9b4a0a682ce / my/products.php

9,865 bytes | parent directory

<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';

// What a place sells to its players while they play. /my/products?place=9

use pizzaboxer\ProjectPolygon\Catalog;
use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\PageBuilder;
use pizzaboxer\ProjectPolygon\Polygon;
use pizzaboxer\ProjectPolygon\Products;
use pizzaboxer\ProjectPolygon\Users;

Users::RequireLogin();

$PlaceID = (int) ($_GET["place"] ?? 0);
$Place = Catalog::GetAssetInfo($PlaceID);
if (!$Place || $Place->type != 9) PageBuilder::instance()->errorCode(404);
$IsStaff = Users::IsAdmin([Users::STAFF_CATALOG, Users::STAFF_ADMINISTRATOR]);
if ($Place->creator != SESSION["user"]["id"] && !$IsStaff) PageBuilder::instance()->errorCode(404);

$Error = $Success = false;
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	if (!is_string($_POST["polygon_csrf"] ?? null) || !hash_equals((string) SESSION["csrfToken"], $_POST["polygon_csrf"])) die(http_response_code(403));
	// games check purchases when the player joins: what's refunded or taken back stops counting
	if (isset($_POST["refund"]))
	{
		$Error = Products::Refund($PlaceID, $_POST["refund"]);
		if (!$Error) $Success = "Refunded";
	}
	else if ((isset($_POST["takeback"]) || isset($_POST["giveback"])) && $IsStaff)
	{
		Products::TakeBack($PlaceID, $_POST["takeback"] ?? $_POST["giveback"], isset($_POST["takeback"]));
		$Success = "Saved";
	}
	else if (isset($_POST["pass"]))
	{
		$p = Database::singleton()->run("SELECT IsPass FROM PlaceProducts WHERE ID = :id AND PlaceID = :p", [":id" => (int) $_POST["pass"], ":p" => $PlaceID])->fetchColumn();
		$Error = Products::SetPass($PlaceID, $_POST["pass"], !$p);
		if (!$Error) $Success = "Saved";
	}
	else if (isset($_POST["toggle"]))
	{
		Database::singleton()->run("UPDATE PlaceProducts SET Active = 1 - Active WHERE ID = :id AND PlaceID = :p", [":id" => (int) $_POST["toggle"], ":p" => $PlaceID]);
		$Success = "Saved";
	}
	else if (isset($_POST["list"]))
	{
		// one per line: name | price | description (| pass: kept and tradable, not used up)
		$n = 0;
		foreach (preg_split('/\r\n|\r|\n/', (string) $_POST["list"]) as $i => $line)
		{
			if (trim($line) === "") continue;
			$part = array_map("trim", explode("|", $line, 4));
			$Error = Products::Save($PlaceID, $part[0], $part[2] ?? "", $part[1] ?? "", strtolower($part[3] ?? "") === "pass" ? true : null);
			if ($Error) { $Error = "Line " . ($i + 1) . ": " . $Error; break; }
			$n++;
		}
		if (!$Error) $Success = $n == 1 ? "Saved 1 product" : "Saved $n products";
	}
	else
	{
		$Error = Products::Save($PlaceID, $_POST["name"] ?? "", $_POST["description"] ?? "", $_POST["price"] ?? "");
		if (!$Error) $Success = "Saved";
	}
}

$List = Products::ForPlace($PlaceID, true);
$Earned = (int) Database::singleton()->run("SELECT COALESCE(SUM(Amount), 0) FROM GamePurchases WHERE PlaceID = :p AND ProductID IS NOT NULL AND UserID != SellerID", [":p" => $PlaceID])->fetchColumn();
$Purchases = Database::singleton()->run(
	"SELECT g.ID, g.UserID, g.Amount, g.Granted, g.Revoked, g.Created, u.username, p.Name FROM GamePurchases g
	 JOIN PlaceProducts p ON p.ID = g.ProductID LEFT JOIN users u ON u.id = g.UserID
	 WHERE g.PlaceID = :p ORDER BY g.ID DESC LIMIT 100", [":p" => $PlaceID]
)->fetchAll(PDO::FETCH_OBJ);
$csrf = '<input type="hidden" name="polygon_csrf" value="' . htmlspecialchars(SESSION["csrfToken"]) . '">';

$pageBuilder = new PageBuilder(["title" => "Products"]);
$pageBuilder->buildHeader();
?>
<h2 class="font-weight-normal">Products</h2>
<p>Sold in <a href="/item?ID=<?=$PlaceID?>"><?=Polygon::FilterText($Place->name)?></a> while people play. You get everything they pay: there are no fees. Earned so far: <span class="text-success"><i class="fal fa-pizza-slice"></i> <?=number_format($Earned)?></span></p>
<?php if ($Error) { ?><div class="alert alert-danger px-2 py-1" role="alert"><?=htmlspecialchars($Error)?></div><?php } ?>
<?php if ($Success) { ?><div class="alert alert-success px-2 py-1" role="alert"><?=htmlspecialchars($Success)?></div><?php } ?>
<div class="table-responsive-sm">
	<table class="table table-hover">
		<thead class="table-bordered bg-light">
			<tr>
				<th class="font-weight-normal py-2">Id</th>
				<th class="font-weight-normal py-2">Name</th>
				<th class="font-weight-normal py-2">Price</th>
				<th class="font-weight-normal py-2">Sold</th>
				<th class="font-weight-normal py-2">Kind</th>
				<th class="font-weight-normal py-2"></th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ($List as $p) { ?>
			<tr<?=$p->Active ? "" : " class=\"text-muted\""?>>
				<td><?=(int) $p->ID?></td>
				<td><?=htmlspecialchars($p->Name)?><?php if ($p->Description !== "") { ?><br><small class="text-muted"><?=htmlspecialchars($p->Description)?></small><?php } ?></td>
				<td><i class="fal fa-pizza-slice"></i> <?=number_format($p->Price)?></td>
				<td><?=number_format($p->Sales)?></td>
				<td>
					<form method="post" class="d-inline"><?=$csrf?><input type="hidden" name="pass" value="<?=(int) $p->ID?>">
						<button class="btn btn-sm btn-light" title="<?=$p->IsPass ? "Kept by who buys it, can be traded" : "Used up when bought (like coins)"?>"><?=$p->IsPass ? "Pass" : "Used up"?></button>
					</form>
				</td>
				<td class="text-right">
					<form method="post" class="d-inline"><?=$csrf?><input type="hidden" name="toggle" value="<?=(int) $p->ID?>">
						<button class="btn btn-sm btn-light"><?=$p->Active ? "Stop selling" : "Sell again"?></button>
					</form>
				</td>
			</tr>
			<?php } if (!$List) { ?>
			<tr><td colspan="6" class="text-muted">Nothing yet.</td></tr>
			<?php } ?>
		</tbody>
	</table>
</div>
<h4 class="font-weight-normal">Recent purchases</h4>
<div class="table-responsive-sm">
	<table class="table table-hover table-sm">
		<thead class="table-bordered bg-light">
			<tr>
				<th class="font-weight-normal py-2">When</th>
				<th class="font-weight-normal py-2">Who</th>
				<th class="font-weight-normal py-2">What</th>
				<th class="font-weight-normal py-2">Paid</th>
				<th class="font-weight-normal py-2"></th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ($Purchases as $b) { ?>
			<tr<?=$b->Revoked ? " class=\"text-muted\"" : ""?>>
				<td><?=date("j M Y H:i", (int) $b->Created)?></td>
				<td><a href="/user?ID=<?=(int) $b->UserID?>"><?=htmlspecialchars((string) $b->username)?></a></td>
				<td><?=htmlspecialchars($b->Name)?><?=[0 => ($b->Granted ? "" : " <small class=\"text-muted\">(not handed out yet)</small>"), Products::REFUNDED => " <small>(refunded)</small>", Products::TAKEN_BACK => " <small>(taken back, no refund)</small>"][(int) $b->Revoked] ?? ""?></td>
				<td><i class="fal fa-pizza-slice"></i> <?=number_format($b->Amount)?></td>
				<td class="text-right">
					<?php if ((int) $b->Revoked === 0) { ?>
					<form method="post" class="d-inline" onsubmit="return confirm('Give <?=htmlspecialchars(addslashes((string) $b->username))?> back the <?=number_format($b->Amount)?> Pizzas they paid? They lose what they bought.')"><?=$csrf?><input type="hidden" name="refund" value="<?=(int) $b->ID?>">
						<button class="btn btn-sm btn-light">Refund</button>
					</form>
					<?php if ($IsStaff) { ?>
					<form method="post" class="d-inline" onsubmit="return confirm('Take it back without a refund?')"><?=$csrf?><input type="hidden" name="takeback" value="<?=(int) $b->ID?>">
						<button class="btn btn-sm btn-light">Take back, no refund</button>
					</form>
					<?php } } else if ((int) $b->Revoked === Products::TAKEN_BACK && $IsStaff) { ?>
					<form method="post" class="d-inline"><?=$csrf?><input type="hidden" name="giveback" value="<?=(int) $b->ID?>">
						<button class="btn btn-sm btn-light">Give back</button>
					</form>
					<?php } ?>
				</td>
			</tr>
			<?php } if (!$Purchases) { ?>
			<tr><td colspan="5" class="text-muted">None yet.</td></tr>
			<?php } ?>
		</tbody>
	</table>
</div>
<p class="text-muted small">A refund gives the buyer back what they paid at the time, from your Pizzas, and they lose what they bought the next time they join a game.</p>
<div class="row">
	<div class="col-md-6">
		<h4 class="font-weight-normal">Add or change one</h4>
		<form method="post">
			<?=$csrf?>
			<div class="form-group"><label for="name">Name:</label><input class="form-control form-control-sm" id="name" name="name" maxlength="50"></div>
			<div class="form-group"><label for="description">Description:</label><textarea class="form-control form-control-sm" id="description" name="description" maxlength="500" rows="2"></textarea></div>
			<div class="form-group"><label for="price">Price in Pizzas:</label><input class="form-control form-control-sm" id="price" name="price" type="number" min="1" max="<?=Products::MAX_PRICE?>" style="max-width:10rem"></div>
			<button class="btn btn-primary">Save</button>
			<small class="text-muted ml-2">The same name again changes that product.</small>
		</form>
	</div>
	<div class="col-md-6">
		<h4 class="font-weight-normal">Several at once</h4>
		<form method="post">
			<?=$csrf?>
			<div class="form-group"><label for="list">One per line: name | price | description, and "| pass" for passes</label>
				<textarea class="form-control form-control-sm" id="list" name="list" rows="7" placeholder="100 Coins | 20 | A handful of coins"></textarea></div>
			<button class="btn btn-primary">Save all</button>
		</form>
	</div>
</div>
<p class="text-muted mt-4 mb-0">A <b>pass</b> is kept by whoever buys it and can be traded; you can trade away any number of your own. In scripts: <code>GamePassService:PlayerHasPass(player, id)</code> tells whether somebody holds a pass. Also: <code>MarketplaceService:GetDeveloperProductsAsync()</code> lists these, <code>MarketplaceService:PromptProductPurchase(player, id)</code> offers one, and <code>MarketplaceService.ProcessReceipt</code> hands it out.</p>
<?php $pageBuilder->buildFooter(); ?>