Repositories | Polyback | Log in to see private repositories


polyback-website / 4e3cec34a8b37065044edb337c27d5951760e480 / my/trades.php

4,381 bytes | parent directory

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

// Trades waiting for you, the ones you've sent, and what happened to earlier ones. /my/trades

use pizzaboxer\ProjectPolygon\PageBuilder;
use pizzaboxer\ProjectPolygon\Trades;
use pizzaboxer\ProjectPolygon\Users;

Users::RequireLogin();
$Me = (int) SESSION["user"]["id"];

$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));
	$id = (int) ($_POST["trade"] ?? 0);
	switch ($_POST["action"] ?? "")
	{
		case "accept":
			$Error = Trades::Accept($id, $Me);
			if (!$Error) $Success = "Traded!";
			break;
		case "decline": Trades::Decline($id, $Me); $Success = "Declined"; break;
		case "cancel": Trades::Cancel($id, $Me); $Success = "Cancelled"; break;
	}
}

$List = Trades::ForUser($Me);
$csrf = '<input type="hidden" name="polygon_csrf" value="' . htmlspecialchars(SESSION["csrfToken"]) . '">';
$StatusText = [Trades::WAITING => "Waiting", Trades::DONE => "Done", Trades::DECLINED => "Declined", Trades::CANCELLED => "Cancelled", Trades::FAILED => "Couldn't be done"];

function side($things, $pizzas)
{
	$parts = array_map("htmlspecialchars", $things);
	if ($pizzas > 0) $parts[] = '<i class="fal fa-pizza-slice"></i> ' . number_format($pizzas);
	return $parts ? implode("<br>", $parts) : '<span class="text-muted">nothing</span>';
}

$pageBuilder = new PageBuilder(["title" => "Trades"]);
$pageBuilder->buildHeader();
?>
<h2 class="font-weight-normal">Trades</h2>
<p>Trade items, passes of games and Pizzas with other players: open somebody's profile and press <b>Trade</b>. Nothing is taken as a fee.</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">With</th>
				<th class="font-weight-normal py-2">You give</th>
				<th class="font-weight-normal py-2">You get</th>
				<th class="font-weight-normal py-2">Status</th>
				<th class="font-weight-normal py-2"></th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ($List as $t) { $sent = (int) $t->SenderID === $Me; ?>
			<tr<?=$t->Status == Trades::WAITING ? "" : " class=\"text-muted\""?>>
				<td>
					<?php $otherId = $sent ? $t->ReceiverID : $t->SenderID; $otherName = $sent ? $t->ReceiverName : $t->SenderName; ?>
					<a href="/user?ID=<?=(int) $otherId?>"><?=htmlspecialchars((string) $otherName)?></a><br>
					<small class="text-muted"><?=$sent ? "you sent it" : "sent to you"?>, <?=date("j M Y H:i", (int) $t->Created)?></small>
				</td>
				<td><?=$sent ? side($t->SenderGives, $t->SenderPizzas) : side($t->ReceiverGives, $t->ReceiverPizzas)?></td>
				<td><?=$sent ? side($t->ReceiverGives, $t->ReceiverPizzas) : side($t->SenderGives, $t->SenderPizzas)?></td>
				<td><?=$StatusText[(int) $t->Status] ?? ""?><?php if ($t->Note !== "") { ?><br><small><?=htmlspecialchars($t->Note)?></small><?php } ?></td>
				<td class="text-right">
					<?php if ($t->Status == Trades::WAITING && !$sent) { ?>
					<form method="post" class="d-inline" onsubmit="return confirm('Do this trade?')"><?=$csrf?><input type="hidden" name="trade" value="<?=(int) $t->ID?>"><input type="hidden" name="action" value="accept">
						<button class="btn btn-sm btn-success">Accept</button>
					</form>
					<form method="post" class="d-inline"><?=$csrf?><input type="hidden" name="trade" value="<?=(int) $t->ID?>"><input type="hidden" name="action" value="decline">
						<button class="btn btn-sm btn-light">Decline</button>
					</form>
					<?php } else if ($t->Status == Trades::WAITING) { ?>
					<form method="post" class="d-inline"><?=$csrf?><input type="hidden" name="trade" value="<?=(int) $t->ID?>"><input type="hidden" name="action" value="cancel">
						<button class="btn btn-sm btn-light">Cancel</button>
					</form>
					<?php } ?>
				</td>
			</tr>
			<?php } if (!$List) { ?>
			<tr><td colspan="5" class="text-muted">No trades yet.</td></tr>
			<?php } ?>
		</tbody>
	</table>
</div>
<?php $pageBuilder->buildFooter(); ?>