aboutsummaryrefslogtreecommitdiff
path: root/public/admin-promo.php
blob: 83103e3fe7ed93025564597fa1a5160298995466 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<?php require "../lib/login.php"; ?>
<?php if_privileged(PRIVILEGE_ADMIN, "/") ?>
<?php require "../lib/promo.php"; ?>
<?php do {
	if ($_SERVER['REQUEST_METHOD'] !== 'POST') break;
	if (!$_POST['type']) break;

	switch($_POST['type']) {
		case "delete": {
			$statement = $cursor->prepare("delete from promotion where id = ?");
			$statement->bind_param("i", $_POST['id']);
			$statement->execute();
			break;
		}
		case "add": {
			$statement = $cursor->prepare("insert into webs.promotion (`product`, `price_buff`, `count_buff`) values (?, ?, ?)");
			$statement->bind_param("idi", $_POST['product_id'], $_POST['price_buff'], $_POST['count_buff']);
			$statement->execute();
			break;
		}
  }

	$promo_id = $_POST['id'];
	$new_status = $_POST['status'];
	if (!$promo_id) break;
	if (!$new_status) break;

	$statement = $cursor->prepare("update `promo` set status = ? where id = ?");
	$statement->bind_param("ii", $new_status, $promo_id);
	$statement->execute();
} while (false); ?>
<?php
function promo_template($promo) {
  $promo_str = promobuff2str($promo->price_buff, $promo->count_buff);
	echo <<<"EOF"
		<form method="post">
			<input type="hidden" name="type" value="delete">
			<input type="hidden" name="id" value="$promo->id">
			<tr>
				<td>$promo->product_id</td>
				<td>$promo->name</td>
				<td>$promo->count_buff</td>
				<td>$promo->price_buff</td>
				<td>$promo_str</td>
				<td>
					<input type="submit" value="verwijderen">
				</td>
			</tr>
		</form>
	EOF;
}
?>

<html>
<head>
	<?php include 'head.php' ?>
	<title>aanbiedingen</title>
	<link rel="stylesheet" href="admin.css">
</head>
<body>
	<?php include 'navbar.php' ?>
	<div class="main limwidth">
		<h2>aanbieding toevoegen</h2>
		<form action="/admin-promo.php" method="post">
			<input type="hidden" name="type" value="add">
			<label for="product_id">Product</label>
			<select id="product_id" name="product_id">
				<?php
					$res = $cursor->query("select id, name from webs.product");
					while ($c = $res->fetch_object()) echo "<option value='$c->id'>$c->name</option>";
				?>
			</select>
			<label for="count_buff">Per aantal</label>
			<input id="count_buff" name="count_buff" type="number" min=1 step=1>
			<label for="price_buff">Vermenigvuldigingsfactor</label>
			<input id="price_buff" name="price_buff" type="number" step="any">
			<input id="submit" type="submit" value="Toevoegen">
		</form>
		<h2>huidige aanbiedingen</h2>
		<table class="promo-table">
			<tr>
				<th>product id</th>
				<th>product naam</th>
				<th>per aantal</th>
				<th>ver&shy;menig&shy;vuldigings&shy;factor</th>
				<th>zichtbaar als</th>
				<th>verwijderen</th>
			</tr>
			<?php
				$res = $cursor->query("select promotion.id, product.id as product_id, product.name, count_buff, price_buff from promotion join product on product.id = promotion.product");
				while ($promo = $res->fetch_object()) promo_template($promo);
			?>
		</table>
	</div>
	<?php include 'footer.php' ?>
</body>
</html>