packing.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. align-items: center;
  205. gap: 1.5rem;
  206. }
  207. .progress-track {
  208. flex: 1;
  209. height: 6px;
  210. background: var(--border);
  211. border-radius: 999px;
  212. overflow: hidden;
  213. }
  214. .progress-fill {
  215. height: 100%;
  216. background: linear-gradient(90deg, var(--accent2), var(--accent));
  217. border-radius: 999px;
  218. width: 0%;
  219. transition: width .3s ease;
  220. }
  221. .progress-label {
  222. font-size: .82rem;
  223. color: var(--muted);
  224. white-space: nowrap;
  225. }
  226. .progress-label strong { color: var(--accent); font-variant-numeric: tabular-nums; }
  227. .section-title {
  228. font-family: 'Bebas Neue', sans-serif;
  229. font-size: 1.1rem;
  230. letter-spacing: .12em;
  231. color: var(--accent2);
  232. text-transform: uppercase;
  233. margin-bottom: .75rem;
  234. display: flex;
  235. align-items: center;
  236. gap: .6rem;
  237. }
  238. .section-title::after {
  239. content: '';
  240. flex: 1;
  241. height: 1px;
  242. background: var(--border);
  243. }
  244. .item-group {
  245. margin-bottom: 2rem;
  246. }
  247. .item-list {
  248. display: flex;
  249. flex-direction: column;
  250. gap: .4rem;
  251. }
  252. .item-row {
  253. display: flex;
  254. align-items: center;
  255. gap: .75rem;
  256. padding: .6rem 1rem;
  257. background: var(--surface);
  258. border: 1.5px solid var(--border);
  259. border-radius: var(--radius);
  260. cursor: pointer;
  261. transition: border-color .15s, background .15s;
  262. user-select: none;
  263. }
  264. .item-row:hover { background: #1e2333; }
  265. .item-row.checked {
  266. border-color: rgba(92,184,92,.5);
  267. background: rgba(92,184,92,.05);
  268. }
  269. .item-row.checked .item-name {
  270. text-decoration: line-through;
  271. color: var(--muted);
  272. }
  273. .item-check {
  274. width: 20px; height: 20px;
  275. border: 1.5px solid var(--border);
  276. border-radius: 5px;
  277. flex-shrink: 0;
  278. display: flex; align-items: center; justify-content: center;
  279. background: var(--bg);
  280. color: #fff;
  281. transition: all .15s;
  282. }
  283. .item-row.checked .item-check {
  284. background: var(--green);
  285. border-color: var(--green);
  286. }
  287. .item-check svg { display: none; }
  288. .item-row.checked .item-check svg { display: block; }
  289. .item-name { flex: 1; font-size: .95rem; }
  290. /* Share box */
  291. .share-box {
  292. background: var(--surface);
  293. border: 1.5px solid var(--border);
  294. border-radius: var(--radius);
  295. padding: 1.25rem 1.5rem;
  296. margin-bottom: 2.5rem;
  297. display: flex;
  298. gap: .75rem;
  299. align-items: center;
  300. flex-wrap: wrap;
  301. }
  302. .share-url {
  303. flex: 1;
  304. font-size: .8rem;
  305. color: var(--muted);
  306. font-family: monospace;
  307. white-space: nowrap;
  308. overflow: hidden;
  309. text-overflow: ellipsis;
  310. min-width: 0;
  311. }
  312. .copy-feedback {
  313. font-size: .8rem;
  314. color: var(--green);
  315. opacity: 0;
  316. transition: opacity .3s;
  317. }
  318. .copy-feedback.show { opacity: 1; }
  319. @media print {
  320. header .btn-row, .share-box, .progress-bar-wrap { display: none !important; }
  321. body { background: #fff; color: #000; }
  322. .item-row { border-color: #ccc; background: none; break-inside: avoid; }
  323. .section-title { color: #333; }
  324. .pill { background: #f5f5f5; color: #333; border-color: #ccc; }
  325. /* The tick boxes carry the dark screen background. Browsers that print
  326. background graphics (Safari, and any dialog with "Background graphics"
  327. ticked – common when saving as PDF) then render them as solid black
  328. rectangles, leaving nothing to cross by hand. Keep them blank on paper. */
  329. .item-check,
  330. .item-row.checked .item-check {
  331. background: #fff;
  332. border-color: #555;
  333. color: #000;
  334. }
  335. /* Checked items also need to survive without background colours. */
  336. .item-row.checked { background: none; border-color: #ccc; }
  337. .item-row.checked .item-name { color: #000; }
  338. }
  339. </style>
  340. </head>
  341. <body>
  342. <header>
  343. <div class="header-left">
  344. <div class="logo">PackIt</div>
  345. <div class="header-meta">
  346. <strong><?= $totalItems ?></strong> items &nbsp;·&nbsp;
  347. <strong><?= count($activityNames) ?></strong> activities
  348. </div>
  349. </div>
  350. <div class="btn-row">
  351. <a href="index.php" class="btn btn-outline">← New list</a>
  352. <button class="btn btn-outline" id="resetBtn" onclick="resetList()" hidden>↺ Reset</button>
  353. <button class="btn btn-teal" onclick="window.print()">🖨 Print</button>
  354. <button class="btn btn-accent" onclick="copyLink()">🔗 Copy link</button>
  355. </div>
  356. </header>
  357. <main>
  358. <!-- Activity pills -->
  359. <div class="activity-pills">
  360. <?php foreach ($activityNames as $n): ?>
  361. <span class="pill"><?= htmlspecialchars($n) ?></span>
  362. <?php endforeach; ?>
  363. </div>
  364. <!-- Share box -->
  365. <div class="share-box">
  366. <span class="share-url" id="shareUrl"><?= htmlspecialchars($pageUrl) ?></span>
  367. <span class="copy-feedback" id="copyFb">Copied!</span>
  368. <button class="btn btn-teal" onclick="copyLink()">Copy link</button>
  369. </div>
  370. <!-- Progress -->
  371. <div class="progress-bar-wrap">
  372. <div class="progress-track"><div class="progress-fill" id="progressFill"></div></div>
  373. <div class="progress-label"><strong id="checkedCnt">0</strong> / <?= $totalItems ?> packed</div>
  374. </div>
  375. <!-- Grouped item list -->
  376. <?php foreach ($grouped as $gid => $group): ?>
  377. <div class="item-group">
  378. <div class="section-title"><?= htmlspecialchars($group['name']) ?></div>
  379. <div class="item-list">
  380. <?php foreach ($group['items'] as $item): ?>
  381. <div class="item-row" data-item-id="<?= (int)$item['id'] ?>" onclick="toggleItem(this)">
  382. <div class="item-check">
  383. <svg width="10" height="8" viewBox="0 0 10 8" fill="none">
  384. <path d="M1 4l3 3 5-6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  385. </svg>
  386. </div>
  387. <span class="item-name"><?php if ($item['quantity'] > 1): ?><?= (int)$item['quantity'] ?>&times; <?php endif; ?><?= htmlspecialchars($item['name']) ?></span>
  388. </div>
  389. <?php endforeach; ?>
  390. </div>
  391. </div>
  392. <?php endforeach; ?>
  393. </main>
  394. <script>
  395. const total = <?= $totalItems ?>;
  396. // Packing happens over hours, not in one sitting: a reload, a rotated phone or
  397. // an evicted tab must not lose the ticks. They live per list hash, in this
  398. // browser only – no account, no server round-trip.
  399. const STORE_KEY = 'packit:checked:<?= htmlspecialchars($hash, ENT_QUOTES) ?>';
  400. // Site data can be blocked (private windows, strict settings). Every access is
  401. // guarded so the list still works, just without remembering anything.
  402. function loadChecked() {
  403. try {
  404. const raw = localStorage.getItem(STORE_KEY);
  405. return new Set(raw ? JSON.parse(raw) : []);
  406. } catch (e) {
  407. return new Set();
  408. }
  409. }
  410. function saveChecked(set) {
  411. try {
  412. localStorage.setItem(STORE_KEY, JSON.stringify(Array.from(set)));
  413. } catch (e) {
  414. /* full, blocked, or private mode – ticks just won't survive the reload */
  415. }
  416. }
  417. function toggleItem(el) {
  418. const nowChecked = el.classList.toggle('checked');
  419. const set = loadChecked();
  420. if (nowChecked) {
  421. set.add(el.dataset.itemId);
  422. } else {
  423. set.delete(el.dataset.itemId);
  424. }
  425. saveChecked(set);
  426. updateProgress();
  427. }
  428. function resetList() {
  429. if (!confirm('Uncheck every item on this list?')) return;
  430. document.querySelectorAll('.item-row.checked').forEach(row => row.classList.remove('checked'));
  431. try { localStorage.removeItem(STORE_KEY); } catch (e) {}
  432. updateProgress();
  433. }
  434. function updateProgress() {
  435. const checked = document.querySelectorAll('.item-row.checked').length;
  436. document.getElementById('checkedCnt').textContent = checked;
  437. document.getElementById('progressFill').style.width = (total ? checked / total * 100 : 0) + '%';
  438. document.getElementById('resetBtn').hidden = checked === 0;
  439. }
  440. function restoreChecked() {
  441. const stored = loadChecked();
  442. const alive = new Set();
  443. document.querySelectorAll('.item-row').forEach(row => {
  444. if (stored.has(row.dataset.itemId)) {
  445. row.classList.add('checked');
  446. alive.add(row.dataset.itemId);
  447. }
  448. });
  449. // Items dropped from the list in the meantime would otherwise linger forever.
  450. if (alive.size !== stored.size) saveChecked(alive);
  451. updateProgress();
  452. }
  453. restoreChecked();
  454. function copyLink() {
  455. const url = document.getElementById('shareUrl').textContent;
  456. navigator.clipboard.writeText(url).then(() => {
  457. const fb = document.getElementById('copyFb');
  458. fb.classList.add('show');
  459. setTimeout(() => fb.classList.remove('show'), 2000);
  460. }).catch(() => {
  461. // Fallback
  462. const ta = document.createElement('textarea');
  463. ta.value = url;
  464. document.body.appendChild(ta);
  465. ta.select();
  466. document.execCommand('copy');
  467. document.body.removeChild(ta);
  468. });
  469. }
  470. // Offline support: a list you have opened stays readable without signal.
  471. if ('serviceWorker' in navigator) {
  472. window.addEventListener('load', () => navigator.serviceWorker.register('/sw.js').catch(() => {}));
  473. }
  474. </script>
  475. </body>
  476. </html>