--- /dev/null
+<?php
+const BASE_URL = "http://localhost:5000/collabs.php";
+$GLOBALS['db'] = new PDO("sqlite:database.db");
+$event = $_GET['event'];
+$name = $_GET['name'];
+
+function event_url($e) {
+ return BASE_URL."?event=$e";
+}
+
+function query_db($query, $params=null) {
+ $statement = $GLOBALS['db']->prepare($query);
+ $statement->setFetchMode(PDO::FETCH_ASSOC);
+ $result = $statement->execute($params);
+ #$statement->debugDumpParams();
+ if (!$result) {
+ error(500, "Uh-oh, something went wrong. Sorry!");
+ }
+ return $statement;
+}
+
+function peep_exists($e, $n) {
+ $q = query_db(
+ "SELECT * FROM peeps WHERE eventid = :eventid AND name = :name",
+ [ 'eventid' => $e, 'name' => $n ]
+ );
+ return (bool) $q->fetch();
+}
+
+function add_peep($e, $n) {
+ query_db(
+ "INSERT INTO peeps (eventid, name, number)
+ VALUES (:eventid, :name, :number)",
+ [ 'eventid' => $e, 'name' => $n, 'number' => rand() ]
+ );
+}
+
+function get_peeps($e) {
+ if (!$e) return [];
+
+ return query_db(
+ "SELECT * FROM peeps WHERE eventid = :eventid ORDER BY COALESCE(number, name)",
+ [ 'eventid' => $e ])
+ ->fetchAll();
+}
+
+if ($event == "new") {
+ // Create new event and redirect
+ $q = query_db("INSERT INTO events DEFAULT VALUES RETURNING eventid");
+ $event = $q->fetch()['eventid'];
+ header('Location: '.event_url($event));
+ exit;
+}
+
+if (isset($event) && isset($name)) {
+ // Add user to db if they don't exist already
+ if (!peep_exists($event, $name)) {
+ add_peep($event, $name);
+ }
+}
+
+$peeps = get_peeps($event);
+$shareable_link = event_url($event);
+?>
+<!DOCTYPE html>
+<html>
+<head>
+ <title>The Swap Shop</title>
+ <style>
+ body {
+ background-color: #ffd;
+ font-family: sans-serif;
+ max-width: 30em;
+ margin: 10px auto;
+ padding: 20px;
+ border: 1px solid #eeb;
+ color: #434;
+ }
+ table {
+ margin: 10px;
+ text-align: center;
+ }
+ input[type=submit] {
+ background: #fdf;
+ border: 0;
+ cursor: pointer;
+ border-radius: 5px;
+ padding: 5px;
+ margin: 5px 0px;
+ font-weight: bold;
+ }
+ hr {
+ border: none;
+ border-top: 1px solid #eeb;
+ }
+ .me {
+ font-weight: bold;
+ }
+ </style>
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+</head>
+
+<?php if (isset($name)): # Periodically reload after join ?>
+<body onload="setTimeout('location.reload()', 10000)">
+<?php else: ?>
+<body>
+<?php endif ?>
+ <h1>The Swap Shop</h1>
+
+<?php if (isset($event)): ?>
+ <?php if (isset($name)): ?>
+ <h2>Hello, <?= $name ?>!</h2>
+ <?php else: ?>
+ <h2>Hello!</h2>
+ <form>
+ <input type="hidden" name="event" value="<?= $event ?>">
+ <label>Your name: <input type="text" name="name"></label>
+ <input type="submit" value="Join!">
+ </form>
+ <?php endif ?>
+<?php endif ?>
+<?php if ($peeps): ?>
+ <hr>
+ <h3>Today's Shoppers</h3>
+ <table>
+ <?php
+ foreach ($peeps as &$p) {
+ $cls = $p['name'] == $name ? 'me' : '';
+ echo "<tr><td class='$cls'>$p[name]</td></tr>\n";
+ echo "<tr><td>⬇</td></tr>\n";
+ }
+ $first = reset($peeps);
+ echo "<tr><td>$first[name]</td></tr>\n";
+ ?>
+ </table>
+<?php elseif (isset($event)): ?>
+ <p>Waiting for peeps to join...</p>
+<?php endif ?>
+<?php if (isset($event)): ?>
+ <hr>
+ <small>
+ Shareable link for this swap list:
+ <a href="<?= $shareable_link ?>"><?= $shareable_link ?></a><br>
+ </small>
+<?php endif ?>
+ <hr>
+ <form>
+ <input type="hidden" name="event" value="new">
+ <input type="submit" value="Create New Swap List">
+ </form>
+ <hr>
+ <h3>How it works</h3>
+ <ol>
+ <li>Start a song, work for 30 minutes
+ </li><li>Send it to the person <b>after you</b> on the list
+ </li><li>Finish the song you receive from the person before you
+ </li>
+ </ol>
+</body>
+</html>
+