Repositories | Polyback | Log in to see private repositories
5,648 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);
if ($Place->creator != SESSION["user"]["id"] && !Users::IsAdmin([Users::STAFF_CATALOG, Users::STAFF_ADMINISTRATOR])) 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));
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
$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, 3));
$Error = Products::Save($PlaceID, $part[0], $part[2] ?? "", $part[1] ?? "");
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();
$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"></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 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="5" class="text-muted">Nothing yet.</td></tr>
<?php } ?>
</tbody>
</table>
</div>
<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</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">In scripts: <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(); ?>