packing.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. // packing.php – displays the packing list for a given hash
  3. require_once __DIR__ . '/db.php';
  4. $pdo = get_pdo();
  5. // 5 characters normally; list.php lengthens a link only when a shorter one
  6. // would collide with a different selection.
  7. $hash = isset($_GET['h']) ? trim($_GET['h']) : '';
  8. if (!preg_match('/^[a-f0-9]{5,16}$/', $hash)) {
  9. http_response_code(400);
  10. die('<p>Invalid or missing list hash.</p>');
  11. }
  12. // Resolve hash → activity IDs
  13. $stmt = $pdo->prepare("SELECT activity_ids FROM selection_hashes WHERE hash = ?");
  14. $stmt->execute([$hash]);
  15. $row = $stmt->fetch();
  16. if (!$row) {
  17. http_response_code(404);
  18. die('<p>List not found. It may have expired or the link is wrong.</p>');
  19. }
  20. $activityIds = array_map('intval', explode(',', $row['activity_ids']));
  21. if (empty($activityIds)) {
  22. die('<p>No activities stored for this hash.</p>');
  23. }
  24. // Fetch the selected activity names for display
  25. $ph = implode(',', array_fill(0, count($activityIds), '?'));
  26. $stmt = $pdo->prepare("
  27. SELECT a.activityName
  28. FROM activities a
  29. WHERE a.activityID IN ($ph)
  30. ORDER BY a.activityName
  31. ");
  32. $stmt->execute($activityIds);
  33. $activityNames = $stmt->fetchAll(PDO::FETCH_COLUMN);
  34. // Get all items for these activities, deduplicated, grouped by item_group.
  35. // When the same item is mapped to multiple selected activities with different
  36. // quantities, take the MAX so we reflect the highest need across activities.
  37. $stmt = $pdo->prepare("
  38. SELECT
  39. ig.groupID,
  40. ig.groupName,
  41. ig.sortOrder AS groupSort,
  42. i.itemID,
  43. i.itemName,
  44. i.sortOrder AS itemSort,
  45. MAX(aim.quantity) AS quantity
  46. FROM activity_item_map aim
  47. JOIN items i ON i.itemID = aim.itemID
  48. JOIN item_groups ig ON ig.groupID = i.groupID
  49. WHERE aim.activityID IN ($ph)
  50. GROUP BY ig.groupID, ig.groupName, ig.sortOrder,
  51. i.itemID, i.itemName, i.sortOrder
  52. ORDER BY ig.sortOrder, ig.groupID, i.sortOrder, i.itemID
  53. ");
  54. $stmt->execute($activityIds);
  55. $allItems = $stmt->fetchAll();
  56. // Group items by item_group
  57. $grouped = [];
  58. foreach ($allItems as $item) {
  59. $gid = $item['groupID'];
  60. if (!isset($grouped[$gid])) {
  61. $grouped[$gid] = ['name' => $item['groupName'], 'items' => []];
  62. }
  63. $grouped[$gid]['items'][] = [
  64. 'id' => $item['itemID'],
  65. 'name' => $item['itemName'],
  66. 'quantity' => (int)$item['quantity'],
  67. ];
  68. }
  69. $pageUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
  70. . '://' . $_SERVER['HTTP_HOST']
  71. . $_SERVER['REQUEST_URI'];
  72. $totalItems = count($allItems);
  73. ?>
  74. <!DOCTYPE html>
  75. <html lang="en">
  76. <head>
  77. <meta charset="UTF-8">
  78. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  79. <title>PackIt – Your Packing List</title>
  80. <link rel="manifest" href="/manifest.webmanifest">
  81. <meta name="theme-color" content="#e8c547">
  82. <link rel="icon" type="image/png" sizes="192x192" href="/img/icon-192.png">
  83. <link rel="preconnect" href="https://fonts.googleapis.com">
  84. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  85. <link href="https://fonts.googleapis.com/css2?family=Bebas+Neue&family=DM+Sans:wght@300;400;500;600&display=swap" rel="stylesheet">
  86. <style>
  87. :root {
  88. --bg: #0f1117;
  89. --surface: #181c27;
  90. --border: #2a2f3f;
  91. --accent: #e8c547;
  92. --accent2: #4fd1c5;
  93. --text: #e8eaf0;
  94. --muted: #7a8099;
  95. --green: #5cb85c;
  96. --radius: 10px;
  97. --gap: 1.5rem;
  98. }
  99. *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  100. body {
  101. background: var(--bg);
  102. color: var(--text);
  103. font-family: 'DM Sans', sans-serif;
  104. font-size: 15px;
  105. min-height: 100vh;
  106. display: flex;
  107. flex-direction: column;
  108. }
  109. header {
  110. padding: 2rem var(--gap) 1.5rem;
  111. border-bottom: 1px solid var(--border);
  112. display: flex;
  113. align-items: flex-end;
  114. justify-content: space-between;
  115. gap: 1rem;
  116. flex-wrap: wrap;
  117. position: relative;
  118. overflow: hidden;
  119. }
  120. header::before {
  121. content: '';
  122. position: absolute;
  123. inset: 0;
  124. background: radial-gradient(ellipse 60% 80% at 90% 50%, rgba(79,209,197,.06) 0%, transparent 70%);
  125. pointer-events: none;
  126. }
  127. .header-left { display: flex; align-items: flex-end; gap: 1rem; position: relative; }
  128. .logo {
  129. font-family: 'Bebas Neue', sans-serif;
  130. font-size: clamp(2.2rem, 5vw, 3.5rem);
  131. letter-spacing: .04em;
  132. color: var(--accent);
  133. line-height: 1;
  134. }
  135. .header-meta {
  136. font-size: .8rem;
  137. color: var(--muted);
  138. padding-bottom: .2rem;
  139. }
  140. .header-meta strong { color: var(--accent2); }
  141. .btn-row {
  142. display: flex;
  143. gap: .6rem;
  144. flex-wrap: wrap;
  145. position: relative;
  146. }
  147. .btn {
  148. display: inline-flex; align-items: center; gap: .5rem;
  149. padding: .55rem 1.1rem;
  150. border-radius: var(--radius);
  151. font-family: 'DM Sans', sans-serif;
  152. font-size: .82rem;
  153. font-weight: 500;
  154. letter-spacing: .03em;
  155. cursor: pointer;
  156. border: 1.5px solid transparent;
  157. transition: all .15s;
  158. text-decoration: none;
  159. }
  160. .btn-accent {
  161. background: var(--accent); color: #0f1117; border-color: var(--accent);
  162. }
  163. .btn-accent:hover { background: #f0d260; }
  164. .btn-outline {
  165. background: transparent; color: var(--text); border-color: var(--border);
  166. }
  167. .btn-outline:hover { border-color: var(--muted); }
  168. .btn-teal {
  169. background: transparent; color: var(--accent2); border-color: var(--accent2);
  170. }
  171. .btn-teal:hover { background: rgba(79,209,197,.08); }
  172. main {
  173. flex: 1;
  174. max-width: 760px;
  175. width: 100%;
  176. margin: 0 auto;
  177. padding: 2.5rem var(--gap) 3rem;
  178. }
  179. /* Activities pills */
  180. .activity-pills {
  181. display: flex;
  182. flex-wrap: wrap;
  183. gap: .4rem;
  184. margin-bottom: 2.5rem;
  185. }
  186. .pill {
  187. background: rgba(232,197,71,.1);
  188. border: 1px solid rgba(232,197,71,.3);
  189. color: var(--accent);
  190. font-size: .75rem;
  191. font-weight: 500;
  192. padding: .3rem .75rem;
  193. border-radius: 999px;
  194. letter-spacing: .03em;
  195. }
  196. /* Progress bar */
  197. .progress-bar-wrap {
  198. background: var(--surface);
  199. border: 1px solid var(--border);
  200. border-radius: var(--radius);
  201. padding: 1rem 1.25rem;
  202. margin-bottom: 2.5rem;
  203. display: flex;
  204. flex-wrap: wrap;
  205. align-items: center;
  206. gap: .6rem 1.5rem;
  207. }
  208. .progress-nag {
  209. flex-basis: 100%;
  210. font-size: .82rem;
  211. font-style: italic;
  212. color: var(--muted);
  213. }
  214. .progress-nag:empty { display: none; }
  215. .progress-track {
  216. flex: 1;
  217. height: 6px;
  218. background: var(--border);
  219. border-radius: 999px;
  220. overflow: hidden;
  221. }
  222. .progress-fill {
  223. height: 100%;
  224. background: linear-gradient(90deg, var(--accent2), var(--accent));
  225. border-radius: 999px;
  226. width: 0%;
  227. transition: width .3s ease;
  228. }
  229. .progress-label {
  230. font-size: .82rem;
  231. color: var(--muted);
  232. white-space: nowrap;
  233. }
  234. .progress-label strong { color: var(--accent); font-variant-numeric: tabular-nums; }
  235. .section-title {
  236. font-family: 'Bebas Neue', sans-serif;
  237. font-size: 1.1rem;
  238. letter-spacing: .12em;
  239. color: var(--accent2);
  240. text-transform: uppercase;
  241. margin-bottom: .75rem;
  242. display: flex;
  243. align-items: center;
  244. gap: .6rem;
  245. }
  246. .section-title::after {
  247. content: '';
  248. flex: 1;
  249. height: 1px;
  250. background: var(--border);
  251. }
  252. .item-group {
  253. margin-bottom: 2rem;
  254. }
  255. .item-list {
  256. display: flex;
  257. flex-direction: column;
  258. gap: .4rem;
  259. }
  260. .item-row {
  261. display: flex;
  262. align-items: center;
  263. gap: .75rem;
  264. padding: .6rem 1rem;
  265. background: var(--surface);
  266. border: 1.5px solid var(--border);
  267. border-radius: var(--radius);
  268. cursor: pointer;
  269. transition: border-color .15s, background .15s;
  270. user-select: none;
  271. }
  272. .item-row:hover { background: #1e2333; }
  273. .item-row.checked {
  274. border-color: rgba(92,184,92,.5);
  275. background: rgba(92,184,92,.05);
  276. }
  277. .item-row.checked .item-name {
  278. text-decoration: line-through;
  279. color: var(--muted);
  280. }
  281. .item-check {
  282. width: 20px; height: 20px;
  283. border: 1.5px solid var(--border);
  284. border-radius: 5px;
  285. flex-shrink: 0;
  286. display: flex; align-items: center; justify-content: center;
  287. background: var(--bg);
  288. color: #fff;
  289. transition: all .15s;
  290. }
  291. .item-row.checked .item-check {
  292. background: var(--green);
  293. border-color: var(--green);
  294. }
  295. .item-check svg { display: none; }
  296. .item-row.checked .item-check svg { display: block; }
  297. .item-name { flex: 1; font-size: .95rem; }
  298. /* Share box */
  299. .share-box {
  300. background: var(--surface);
  301. border: 1.5px solid var(--border);
  302. border-radius: var(--radius);
  303. padding: 1.25rem 1.5rem;
  304. margin-bottom: 2.5rem;
  305. display: flex;
  306. gap: .75rem;
  307. align-items: center;
  308. flex-wrap: wrap;
  309. }
  310. .share-url {
  311. flex: 1;
  312. font-size: .8rem;
  313. color: var(--muted);
  314. font-family: monospace;
  315. white-space: nowrap;
  316. overflow: hidden;
  317. text-overflow: ellipsis;
  318. min-width: 0;
  319. }
  320. .copy-feedback {
  321. font-size: .8rem;
  322. color: var(--green);
  323. opacity: 0;
  324. transition: opacity .3s;
  325. }
  326. .copy-feedback.show { opacity: 1; }
  327. @media print {
  328. header .btn-row, .share-box, .progress-bar-wrap { display: none !important; }
  329. body { background: #fff; color: #000; }
  330. .item-row { border-color: #ccc; background: none; break-inside: avoid; }
  331. .section-title { color: #333; }
  332. .pill { background: #f5f5f5; color: #333; border-color: #ccc; }
  333. /* The tick boxes carry the dark screen background. Browsers that print
  334. background graphics (Safari, and any dialog with "Background graphics"
  335. ticked – common when saving as PDF) then render them as solid black
  336. rectangles, leaving nothing to cross by hand. Keep them blank on paper. */
  337. .item-check,
  338. .item-row.checked .item-check {
  339. background: #fff;
  340. border-color: #555;
  341. color: #000;
  342. }
  343. /* Checked items also need to survive without background colours. */
  344. .item-row.checked { background: none; border-color: #ccc; }
  345. .item-row.checked .item-name { color: #000; }
  346. }
  347. </style>
  348. </head>
  349. <body>
  350. <header>
  351. <div class="header-left">
  352. <div class="logo">PackIt</div>
  353. <div class="header-meta">
  354. <strong><?= $totalItems ?></strong> items &nbsp;·&nbsp;
  355. <strong><?= count($activityNames) ?></strong> activities
  356. </div>
  357. </div>
  358. <div class="btn-row">
  359. <a href="index.php" class="btn btn-outline">← New list</a>
  360. <button class="btn btn-outline" id="resetBtn" onclick="resetList()" hidden>↺ Reset</button>
  361. <button class="btn btn-teal" onclick="window.print()">🖨 Print</button>
  362. <button class="btn btn-accent" onclick="copyLink()">🔗 Copy link</button>
  363. </div>
  364. </header>
  365. <main>
  366. <!-- Activity pills -->
  367. <div class="activity-pills">
  368. <?php foreach ($activityNames as $n): ?>
  369. <span class="pill"><?= htmlspecialchars($n) ?></span>
  370. <?php endforeach; ?>
  371. </div>
  372. <!-- Share box -->
  373. <div class="share-box">
  374. <span class="share-url" id="shareUrl"><?= htmlspecialchars($pageUrl) ?></span>
  375. <span class="copy-feedback" id="copyFb">Copied!</span>
  376. <button class="btn btn-teal" onclick="copyLink()">Copy link</button>
  377. </div>
  378. <!-- Progress -->
  379. <div class="progress-bar-wrap">
  380. <div class="progress-track"><div class="progress-fill" id="progressFill"></div></div>
  381. <div class="progress-label"><strong id="checkedCnt">0</strong> / <?= $totalItems ?> packed</div>
  382. <div class="progress-nag" id="nag" aria-live="polite"></div>
  383. </div>
  384. <!-- Grouped item list -->
  385. <?php foreach ($grouped as $gid => $group): ?>
  386. <div class="item-group">
  387. <div class="section-title"><?= htmlspecialchars($group['name']) ?></div>
  388. <div class="item-list">
  389. <?php foreach ($group['items'] as $item): ?>
  390. <div class="item-row" data-item-id="<?= (int)$item['id'] ?>" data-name="<?= htmlspecialchars($item['name']) ?>" onclick="toggleItem(this)">
  391. <div class="item-check">
  392. <svg width="10" height="8" viewBox="0 0 10 8" fill="none">
  393. <path d="M1 4l3 3 5-6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  394. </svg>
  395. </div>
  396. <span class="item-name"><?php if ($item['quantity'] > 1): ?><?= (int)$item['quantity'] ?>&times; <?php endif; ?><?= htmlspecialchars($item['name']) ?></span>
  397. </div>
  398. <?php endforeach; ?>
  399. </div>
  400. </div>
  401. <?php endforeach; ?>
  402. </main>
  403. <script>
  404. const total = <?= $totalItems ?>;
  405. // Packing happens over hours, not in one sitting: a reload, a rotated phone or
  406. // an evicted tab must not lose the ticks. They live per list hash, in this
  407. // browser only – no account, no server round-trip.
  408. const STORE_KEY = 'packit:checked:<?= htmlspecialchars($hash, ENT_QUOTES) ?>';
  409. // Site data can be blocked (private windows, strict settings). Every access is
  410. // guarded so the list still works, just without remembering anything.
  411. function loadChecked() {
  412. try {
  413. const raw = localStorage.getItem(STORE_KEY);
  414. return new Set(raw ? JSON.parse(raw) : []);
  415. } catch (e) {
  416. return new Set();
  417. }
  418. }
  419. function saveChecked(set) {
  420. try {
  421. localStorage.setItem(STORE_KEY, JSON.stringify(Array.from(set)));
  422. } catch (e) {
  423. /* full, blocked, or private mode – ticks just won't survive the reload */
  424. }
  425. }
  426. function toggleItem(el) {
  427. const nowChecked = el.classList.toggle('checked');
  428. const set = loadChecked();
  429. if (nowChecked) {
  430. set.add(el.dataset.itemId);
  431. } else {
  432. set.delete(el.dataset.itemId);
  433. }
  434. saveChecked(set);
  435. updateProgress();
  436. }
  437. function resetList() {
  438. if (!confirm('Uncheck every item on this list?')) return;
  439. document.querySelectorAll('.item-row.checked').forEach(row => row.classList.remove('checked'));
  440. try { localStorage.removeItem(STORE_KEY); } catch (e) {}
  441. updateProgress();
  442. }
  443. function updateProgress() {
  444. const checked = document.querySelectorAll('.item-row.checked').length;
  445. document.getElementById('checkedCnt').textContent = checked;
  446. document.getElementById('progressFill').style.width = (total ? checked / total * 100 : 0) + '%';
  447. document.getElementById('resetBtn').hidden = checked === 0;
  448. updateNag(checked);
  449. }
  450. // A little encouragement (read: nagging) under the progress bar. {left} is the
  451. // number of items still to pack, {item} a random one of them.
  452. const NAGS = {
  453. none: ["The tent won't pack itself.",
  454. "Staring at the list doesn't count as packing.",
  455. "Step one: pick up an item. Any item."],
  456. start: ["A start! A small, fragile start.",
  457. "Nice. Now do that {left} more times.",
  458. "Only {left} to go. Easy. Probably."],
  459. low: ["Still more list than luggage.",
  460. "Keep going. The snacks are watching.",
  461. "Your bag is still suspiciously light."],
  462. half: ["Halfway there. No stopping for snacks.",
  463. "Over half. Your backpack is starting to believe in you.",
  464. "Past the point of no return. Well, you could still unpack."],
  465. close: ["So close. It's the {item}, isn't it.",
  466. "{left} to go. Don't get sloppy now.",
  467. "Almost there. Is the {item} hiding somewhere?"],
  468. last: ["Just the {item} left. You know what to do.",
  469. "One item. The {item}. It's waiting for you."],
  470. done: ["All packed. Now go outside.",
  471. "Done! Go on, touch some grass.",
  472. "Packed and ready. Remember where you put the keys?"],
  473. };
  474. const nag = { tier: null, text: '', item: null };
  475. function nagTier(checked) {
  476. const left = total - checked;
  477. if (left === 0) return 'done';
  478. if (left === 1) return 'last';
  479. if (checked === 0) return 'none';
  480. if (checked / total < .25) return 'start';
  481. if (checked / total < .5) return 'low';
  482. if (checked / total < .75) return 'half';
  483. return 'close';
  484. }
  485. function updateNag(checked) {
  486. const el = document.getElementById('nag');
  487. if (total === 0) { el.textContent = ''; return; }
  488. // Keep the same line while the tier holds, so it doesn't flicker on every tick.
  489. const tier = nagTier(checked);
  490. if (tier !== nag.tier) {
  491. const pool = NAGS[tier];
  492. nag.tier = tier;
  493. nag.text = pool[Math.floor(Math.random() * pool.length)];
  494. }
  495. let item = '';
  496. if (nag.text.includes('{item}')) {
  497. const open = Array.from(document.querySelectorAll('.item-row:not(.checked)'), r => r.dataset.name);
  498. if (!open.includes(nag.item)) nag.item = open[Math.floor(Math.random() * open.length)];
  499. item = nag.item; // as written: lowercasing would mangle German nouns
  500. }
  501. el.textContent = nag.text.replace('{left}', total - checked).replace('{item}', item);
  502. }
  503. function restoreChecked() {
  504. const stored = loadChecked();
  505. const alive = new Set();
  506. document.querySelectorAll('.item-row').forEach(row => {
  507. if (stored.has(row.dataset.itemId)) {
  508. row.classList.add('checked');
  509. alive.add(row.dataset.itemId);
  510. }
  511. });
  512. // Items dropped from the list in the meantime would otherwise linger forever.
  513. if (alive.size !== stored.size) saveChecked(alive);
  514. updateProgress();
  515. }
  516. restoreChecked();
  517. function copyLink() {
  518. const url = document.getElementById('shareUrl').textContent;
  519. navigator.clipboard.writeText(url).then(() => {
  520. const fb = document.getElementById('copyFb');
  521. fb.classList.add('show');
  522. setTimeout(() => fb.classList.remove('show'), 2000);
  523. }).catch(() => {
  524. // Fallback
  525. const ta = document.createElement('textarea');
  526. ta.value = url;
  527. document.body.appendChild(ta);
  528. ta.select();
  529. document.execCommand('copy');
  530. document.body.removeChild(ta);
  531. });
  532. }
  533. // Offline support: a list you have opened stays readable without signal.
  534. if ('serviceWorker' in navigator) {
  535. window.addEventListener('load', () => navigator.serviceWorker.register('/sw.js').catch(() => {}));
  536. }
  537. </script>
  538. </body>
  539. </html>