Repositories | Polyback | Log in to see private repositories


polyback-website / 8b48bedad56346c44734ee80e6f153429435481c / directory_admin/sell-archive.php

11,090 bytes | parent directory

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

// Administrators put items of the Roblox archive in the catalog, for a price. /admin/sell-archive

use pizzaboxer\ProjectPolygon\Archive;
use pizzaboxer\ProjectPolygon\Catalog;
use pizzaboxer\ProjectPolygon\PageBuilder;
use pizzaboxer\ProjectPolygon\Users;

Users::RequireAdmin(Users::STAFF_ADMINISTRATOR);

$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["request"]))
	{
		// ids that aren't archived yet: a file for Studio's importer
		preg_match_all('/[0-9]{1,19}/', (string) $_POST["request"], $m);
		$ids = array_slice(array_values(array_unique($m[0])), 0, 200);
		if ($ids)
		{
			header("Content-Type: application/xml");
			header('Content-Disposition: attachment; filename="archive-request.rbxmx"');
			die(Archive::ImportFile($ids));
		}
		$Error = "Enter one or more ids";
	}
	else
	{
		$creator = Users::GetIDFromName(trim((string) ($_POST["creator"] ?? "")));
		if (!$creator) $Error = "There's no user with that name to credit the sales to";
		else
		{
			$result = Archive::Sell($_POST["robloxid"] ?? 0, $_POST["name"] ?? "", $_POST["description"] ?? "", $_POST["price"] ?? "", $creator);
			if (is_int($result)) $Success = $result;
			else $Error = $result;
		}
	}
}

$Sold = Archive::Sold();
$BrowseType = isset(Archive::BROWSE_TYPES[(int) ($_GET["type"] ?? 0)]) ? (int) $_GET["type"] : 8;
$Search = (string) ($_GET["q"] ?? "");
$Page = max(1, (int) ($_GET["page"] ?? 1));
[$Items, $Total] = Archive::Browse($BrowseType, $Search, $Page);
$Pages = max(1, (int) ceil($Total / Archive::PER_PAGE));
function browseLink($type, $search, $page) { return "?" . http_build_query(array_filter(["type" => $type, "q" => $search, "page" => $page > 1 ? $page : null])) . "#browse"; }
$csrf = '<input type="hidden" name="polygon_csrf" value="' . htmlspecialchars(SESSION["csrfToken"]) . '">';

$pageBuilder = new PageBuilder(["title" => "Sell from the Roblox archive"]);
$pageBuilder->buildHeader();
?>
<h2 class="font-weight-normal">Sell from the Roblox archive</h2>
<p>Puts an item of the archive in the catalog: hats, gear, shirts, pants, T-shirts and heads. Its file isn't copied: the catalog item is served from the archive. Only administrators can do this; players upload their own items on the Develop page.</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">It's in the catalog: <a href="/item?ID=<?=(int) $Success?>">item <?=(int) $Success?></a>. Its picture is being rendered.</div><?php } ?>
<div class="row">
	<div class="col-md-7">
		<h4 class="font-weight-normal">Put one in the catalog</h4>
		<form method="post">
			<?=$csrf?>
			<div class="form-group"><label for="robloxid">Roblox id (in the archive):</label>
				<div class="form-inline"><input class="form-control form-control-sm mr-2" id="robloxid" name="robloxid" inputmode="numeric" maxlength="19" style="max-width:14rem" value="<?=htmlspecialchars((string) ($Error ? ($_POST["robloxid"] ?? "") : ""))?>">
				<button type="button" class="btn btn-sm btn-light" id="fetch-details">Fetch from ROBLOX</button>
				<small class="text-muted ml-2" id="fetch-status"></small></div></div>
			<div class="form-group"><label for="name">Name:</label><input class="form-control form-control-sm" id="name" name="name" maxlength="50" value="<?=htmlspecialchars((string) ($Error ? ($_POST["name"] ?? "") : ""))?>"></div>
			<div class="form-group"><label for="description">Description:</label><textarea class="form-control form-control-sm" id="description" name="description" maxlength="1000" rows="2"><?=htmlspecialchars((string) ($Error ? ($_POST["description"] ?? "") : ""))?></textarea></div>
			<div class="form-group"><label for="price">Price in Pizzas (0: free):</label><input class="form-control form-control-sm" id="price" name="price" type="number" min="0" max="<?=Archive::MAX_PRICE?>" style="max-width:10rem" value="<?=htmlspecialchars((string) ($Error ? ($_POST["price"] ?? "") : ""))?>"></div>
			<div class="form-group"><label for="creator">Sales go to (user name):</label><input class="form-control form-control-sm" id="creator" name="creator" maxlength="20" style="max-width:14rem" value="<?=htmlspecialchars((string) ($_POST["creator"] ?? SESSION["user"]["username"]))?>"></div>
			<button class="btn btn-primary">Put in the catalog</button>
		</form>
	</div>
	<div class="col-md-5">
		<h4 class="font-weight-normal">Not in the archive yet?</h4>
		<p class="small">Things from live ROBLOX go into the archive first, never straight into the catalog: on <a href="/admin/fetch-archive">Fetch Archive Assets</a> (with the stored ROBLOX token), or from Studio. Enter their ids for a file to open in Studio with <b>File &gt; Import ROBLOX file</b>, choosing <b>Add to the ROBLOX archive (administrators)</b> and your .ROBLOSECURITY: it fetches each one and what it uses (meshes, textures) into the archive. Then put them in the catalog here.</p>
		<form method="post">
			<?=$csrf?>
			<div class="form-group"><textarea class="form-control form-control-sm" name="request" rows="3" placeholder="Roblox ids, separated by spaces or commas"></textarea></div>
			<button class="btn btn-light">Get the import file</button>
		</form>
	</div>
</div>
<h4 class="font-weight-normal mt-4" id="browse">Browse the archive</h4>
<ul class="nav nav-tabs mb-2">
	<?php foreach (Archive::BROWSE_TYPES as $t => $label) { ?>
	<li class="nav-item"><a class="nav-link<?=$t == $BrowseType ? " active" : ""?>" href="<?=htmlspecialchars(browseLink($t, "", 1))?>"><?=$label?></a></li>
	<?php } ?>
</ul>
<form method="get" class="form-inline mb-2" action="#browse">
	<input type="hidden" name="type" value="<?=$BrowseType?>">
	<input class="form-control form-control-sm mr-2" name="q" value="<?=htmlspecialchars($Search)?>" placeholder="Roblox id, or part of a name" style="width:16rem">
	<button class="btn btn-sm btn-light">Search</button>
	<span class="text-muted small ml-3"><?=number_format($Total)?> <?=strtolower(Archive::BROWSE_TYPES[$BrowseType])?><?=$Total ? ", page $Page of $Pages" : ""?></span>
</form>
<div class="row">
	<?php foreach ($Items as $it) { ?>
	<div class="col-6 col-sm-4 col-md-3 col-lg-2 mb-3">
		<div class="card h-100">
			<div class="bg-light text-center" style="height:150px">
				<img src="/Thumbs/Asset.ashx?roblox=1&amp;id=<?=(int) $it->id?>&amp;x=150&amp;y=150" alt="" loading="lazy" style="max-width:150px;max-height:150px"
					onerror="this.replaceWith(Object.assign(document.createElement('div'), {className: 'text-muted small pt-5', textContent: 'No picture yet'}))">
			</div>
			<div class="card-body p-2 small">
				<div class="text-truncate" title="<?=htmlspecialchars((string) $it->name)?>"><?=$it->name !== null ? htmlspecialchars($it->name) : '<span class="text-muted">no name</span>'?></div>
				<div class="text-muted"><?=(int) $it->id?></div>
				<?php if ($it->CatalogID) { ?>
				<a href="/item?ID=<?=(int) $it->CatalogID?>">In the catalog</a> <span class="text-muted">(<i class="fal fa-pizza-slice"></i> <?=number_format($it->price)?>)</span>
				<?php } else { ?>
				<button type="button" class="btn btn-sm btn-outline-primary btn-block mt-1 sell-this" data-id="<?=(int) $it->id?>" data-name="<?=htmlspecialchars((string) $it->name)?>">Sell this</button>
				<?php } ?>
			</div>
		</div>
	</div>
	<?php } if (!$Items) { ?>
	<div class="col text-muted">Nothing here.</div>
	<?php } ?>
</div>
<?php if ($Pages > 1) { ?>
<nav><ul class="pagination pagination-sm">
	<li class="page-item<?=$Page <= 1 ? " disabled" : ""?>"><a class="page-link" href="<?=htmlspecialchars(browseLink($BrowseType, $Search, $Page - 1))?>">Previous</a></li>
	<?php foreach (array_unique([1, max(1, $Page - 2), max(1, $Page - 1), $Page, min($Pages, $Page + 1), min($Pages, $Page + 2), $Pages]) as $n) { ?>
	<li class="page-item<?=$n == $Page ? " active" : ""?>"><a class="page-link" href="<?=htmlspecialchars(browseLink($BrowseType, $Search, $n))?>"><?=$n?></a></li>
	<?php } ?>
	<li class="page-item<?=$Page >= $Pages ? " disabled" : ""?>"><a class="page-link" href="<?=htmlspecialchars(browseLink($BrowseType, $Search, $Page + 1))?>">Next</a></li>
</ul></nav>
<?php } ?>
<script>
	// "Fetch from ROBLOX": the item's name and description, to change before selling if wanted
	document.getElementById("fetch-details").addEventListener("click", function () {
		var id = document.getElementById("robloxid").value.trim(), status = document.getElementById("fetch-status");
		if (!/^[0-9]+$/.test(id)) { status.textContent = "Enter a ROBLOX id first"; return; }
		status.textContent = "Asking ROBLOX...";
		fetch("/api/admin/roblox-details?id=" + id, {credentials: "same-origin"}).then(function (r) { return r.json(); }).then(function (d) {
			if (!d.success) { status.textContent = d.message || "That didn't work"; return; }
			document.getElementById("name").value = d.name;
			document.getElementById("description").value = d.description;
			status.textContent = "Filled in" + (d.creator ? " (made by " + d.creator + ")" : "");
		}).catch(function () { status.textContent = "That didn't work"; });
	});
	// "Sell this" fills in the form at the top
	document.querySelectorAll(".sell-this").forEach(function (b) {
		b.addEventListener("click", function () {
			document.getElementById("robloxid").value = b.dataset.id;
			if (b.dataset.name) document.getElementById("name").value = b.dataset.name;
			document.getElementById("robloxid").scrollIntoView({behavior: "smooth", block: "center"});
			document.getElementById("name").focus();
		});
	});
</script>
<h4 class="font-weight-normal mt-4">From the archive so far</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">Item</th>
				<th class="font-weight-normal py-2">Kind</th>
				<th class="font-weight-normal py-2">Roblox id</th>
				<th class="font-weight-normal py-2">Price</th>
				<th class="font-weight-normal py-2">Sales go to</th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ($Sold as $s) { ?>
			<tr<?=$s->sale ? "" : " class=\"text-muted\""?>>
				<td><a href="/item?ID=<?=(int) $s->id?>"><?=htmlspecialchars($s->name)?></a></td>
				<td><?=htmlspecialchars((string) Catalog::GetTypeByNum($s->type))?></td>
				<td><?=(int) $s->RobloxID?></td>
				<td><i class="fal fa-pizza-slice"></i> <?=number_format($s->price)?><?=$s->sale ? "" : " (off sale)"?></td>
				<td><?=htmlspecialchars((string) $s->username)?></td>
			</tr>
			<?php } if (!$Sold) { ?>
			<tr><td colspan="5" class="text-muted">Nothing yet.</td></tr>
			<?php } ?>
		</tbody>
	</table>
</div>
<p class="text-muted small">Prices and descriptions can be changed later on each item's own page, like any catalog item.</p>
<?php $pageBuilder->buildFooter(); ?>