packing.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
  3. // packing.php – displays the packing list for a given hash
  4. require_once __DIR__ . '/db.php';
  5. $pdo = get_pdo();
  6. $hash = isset($_GET['h']) ? trim($_GET['h']) : '';
  7. if (!preg_match('/^[a-f0-9]{5}$/', $hash)) {
  8. http_response_code(400);
  9. die('<p>Invalid or missing list hash.</p>');
  10. }
  11. // Resolve hash → activity IDs
  12. $stmt = $pdo->prepare("SELECT activity_ids FROM selection_hashes WHERE hash = ?");
  13. $stmt->execute([$hash]);
  14. $row = $stmt->fetch();
  15. if (!$row) {
  16. http_response_code(404);
  17. die('<p>List not found. It may have expired or the link is wrong.</p>');
  18. }
  19. $activityIds = array_map('intval', explode(',', $row['activity_ids']));
  20. if (empty($activityIds)) {
  21. die('<p>No activities stored for this hash.</p>');
  22. }
  23. // Fetch the selected activity names for display
  24. $ph = implode(',', array_fill(0, count($activityIds), '?'));
  25. $stmt = $pdo->prepare("
  26. SELECT a.activityName
  27. FROM activities a
  28. WHERE a.activityID IN ($ph)
  29. ORDER BY a.activityName
  30. ");
  31. $stmt->execute($activityIds);
  32. $activityNames = $stmt->fetchAll(PDO::FETCH_COLUMN);
  33. // Get all items for these activities, deduplicated, grouped by item_group.
  34. // When the same item is mapped to multiple selected activities with different
  35. // quantities, take the MAX so we reflect the highest need across activities.
  36. $stmt = $pdo->prepare("
  37. SELECT
  38. ig.groupID,
  39. ig.groupName,
  40. ig.sortOrder AS groupSort,
  41. i.itemID,
  42. i.itemName,
  43. i.sortOrder AS itemSort,
  44. MAX(aim.quantity) AS quantity
  45. FROM activity_item_map aim
  46. JOIN items i ON i.itemID = aim.itemID
  47. JOIN item_groups ig ON ig.groupID = i.groupID
  48. WHERE aim.activityID IN ($ph)
  49. GROUP BY ig.groupID, ig.groupName, ig.sortOrder,
  50. i.itemID, i.itemName, i.sortOrder
  51. ORDER BY ig.sortOrder, ig.groupID, i.sortOrder, i.itemID
  52. ");
  53. $stmt->execute($activityIds);
  54. $allItems = $stmt->fetchAll();
  55. // Group items by item_group
  56. $grouped = [];
  57. foreach ($allItems as $item) {
  58. $gid = $item['groupID'];
  59. if (!isset($grouped[$gid])) {
  60. $grouped[$gid] = ['name' => $item['groupName'], 'items' => []];
  61. }
  62. $grouped[$gid]['items'][] = [
  63. 'id' => $item['itemID'],
  64. 'name' => $item['itemName'],
  65. 'quantity' => (int)$item['quantity'],
  66. ];
  67. }
  68. $pageUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
  69. . '://' . $_SERVER['HTTP_HOST']
  70. . $_SERVER['REQUEST_URI'];
  71. $totalItems = count($allItems);
  72. ?>
  73. <!DOCTYPE html>
  74. <html lang="en">
  75. <head>
  76. <meta charset="UTF-8">
  77. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  78. <title>PackIt – Your Packing List</title>
  79. <link rel="manifest" href="/manifest.webmanifest">
  80. <meta name="theme-color" content="#e8c547">
  81. <link rel="icon" type="image/png" sizes="192x192" href="/img/icon-192.png">
  82. <link rel="preconnect" href="https://fonts.googleapis.com">
  83. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  84. <link href="https://fonts.googleapis.com/css2?family=Bebas+Neue&family=DM+Sans:wght@300;400;500;600&display=swap" rel="stylesheet">
  85. <style>
  86. :root {
  87. --bg: #0f1117;
  88. --surface: #181c27;
  89. --border: #2a2f3f;
  90. --accent: #e8c547;
  91. --accent2: #4fd1c5;
  92. --text: #e8eaf0;
  93. --muted: #7a8099;
  94. --green: #5cb85c;
  95. --radius: 10px;
  96. --gap: 1.5rem;
  97. }
  98. *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  99. body {
  100. background: var(--bg);
  101. color: var(--text);
  102. font-family: 'DM Sans', sans-serif;
  103. font-size: 15px;
  104. min-height: 100vh;
  105. display: flex;
  106. flex-direction: column;
  107. }
  108. header {
  109. padding: 2rem var(--gap) 1.5rem;
  110. border-bottom: 1px solid var(--border);
  111. display: flex;
  112. align-items: flex-end;
  113. justify-content: space-between;
  114. gap: 1rem;
  115. flex-wrap: wrap;
  116. position: relative;
  117. overflow: hidden;
  118. }
  119. header::before {
  120. content: '';
  121. position: absolute;
  122. inset: 0;
  123. background: radial-gradient(ellipse 60% 80% at 90% 50%, rgba(79,209,197,.06) 0%, transparent 70%);
  124. pointer-events: none;
  125. }
  126. .header-left { display: flex; align-items: flex-end; gap: 1rem; position: relative; }
  127. .logo {
  128. font-family: 'Bebas Neue', sans-serif;
  129. font-size: clamp(2.2rem, 5vw, 3.5rem);
  130. letter-spacing: .04em;
  131. color: var(--accent);
  132. line-height: 1;
  133. }
  134. .header-meta {
  135. font-size: .8rem;
  136. color: var(--muted);
  137. padding-bottom: .2rem;
  138. }
  139. .header-meta strong { color: var(--accent2); }
  140. .btn-row {
  141. display: flex;
  142. gap: .6rem;
  143. flex-wrap: wrap;
  144. position: relative;
  145. }
  146. .btn {
  147. display: inline-flex; align-items: center; gap: .5rem;
  148. padding: .55rem 1.1rem;
  149. border-radius: var(--radius);
  150. font-family: 'DM Sans', sans-serif;
  151. font-size: .82rem;
  152. font-weight: 500;
  153. letter-spacing: .03em;
  154. cursor: pointer;
  155. border: 1.5px solid transparent;
  156. transition: all .15s;
  157. text-decoration: none;
  158. }
  159. .btn-accent {
  160. background: var(--accent); color: #0f1117; border-color: var(--accent);
  161. }
  162. .btn-accent:hover { background: #f0d260; }
  163. .btn-outline {
  164. background: transparent; color: var(--text); border-color: var(--border);
  165. }
  166. .btn-outline:hover { border-color: var(--muted); }
  167. .btn-teal {
  168. background: transparent; color: var(--accent2); border-color: var(--accent2);
  169. }
  170. .btn-teal:hover { background: rgba(79,209,197,.08); }
  171. main {
  172. flex: 1;
  173. max-width: 760px;
  174. width: 100%;
  175. margin: 0 auto;
  176. padding: 2.5rem var(--gap) 3rem;
  177. }
  178. /* Activities pills */
  179. .activity-pills {
  180. display: flex;
  181. flex-wrap: wrap;
  182. gap: .4rem;
  183. margin-bottom: 2.5rem;
  184. }
  185. .pill {
  186. background: rgba(232,197,71,.1);
  187. border: 1px solid rgba(232,197,71,.3);
  188. color: var(--accent);
  189. font-size: .75rem;
  190. font-weight: 500;
  191. padding: .3rem .75rem;
  192. border-radius: 999px;
  193. letter-spacing: .03em;
  194. }
  195. /* Progress bar */
  196. .progress-bar-wrap {
  197. background: var(--surface);
  198. border: 1px solid var(--border);
  199. border-radius: var(--radius);
  200. padding: 1rem 1.25rem;
  201. margin-bottom: 2.5rem;
  202. display: flex;
  203. align-items: center;
  204. gap: 1.5rem;
  205. }
  206. .progress-track {
  207. flex: 1;
  208. height: 6px;
  209. background: var(--border);
  210. border-radius: 999px;
  211. overflow: hidden;
  212. }
  213. .progress-fill {
  214. height: 100%;
  215. background: linear-gradient(90deg, var(--accent2), var(--accent));
  216. border-radius: 999px;
  217. width: 0%;
  218. transition: width .3s ease;
  219. }
  220. .progress-label {
  221. font-size: .82rem;
  222. color: var(--muted);
  223. white-space: nowrap;
  224. }
  225. .progress-label strong { color: var(--accent); font-variant-numeric: tabular-nums; }
  226. .section-title {
  227. font-family: 'Bebas Neue', sans-serif;
  228. font-size: 1.1rem;
  229. letter-spacing: .12em;
  230. color: var(--accent2);
  231. text-transform: uppercase;
  232. margin-bottom: .75rem;
  233. display: flex;
  234. align-items: center;
  235. gap: .6rem;
  236. }
  237. .section-title::after {
  238. content: '';
  239. flex: 1;
  240. height: 1px;
  241. background: var(--border);
  242. }
  243. .item-group {
  244. margin-bottom: 2rem;
  245. }
  246. .item-list {
  247. display: flex;
  248. flex-direction: column;
  249. gap: .4rem;
  250. }
  251. .item-row {
  252. display: flex;
  253. align-items: center;
  254. gap: .75rem;
  255. padding: .6rem 1rem;
  256. background: var(--surface);
  257. border: 1.5px solid var(--border);
  258. border-radius: var(--radius);
  259. cursor: pointer;
  260. transition: border-color .15s, background .15s;
  261. user-select: none;
  262. }
  263. .item-row:hover { background: #1e2333; }
  264. .item-row.checked {
  265. border-color: rgba(92,184,92,.5);
  266. background: rgba(92,184,92,.05);
  267. }
  268. .item-row.checked .item-name {
  269. text-decoration: line-through;
  270. color: var(--muted);
  271. }
  272. .item-check {
  273. width: 20px; height: 20px;
  274. border: 1.5px solid var(--border);
  275. border-radius: 5px;
  276. flex-shrink: 0;
  277. display: flex; align-items: center; justify-content: center;
  278. background: var(--bg);
  279. transition: all .15s;
  280. }
  281. .item-row.checked .item-check {
  282. background: var(--green);
  283. border-color: var(--green);
  284. }
  285. .item-check svg { display: none; }
  286. .item-row.checked .item-check svg { display: block; }
  287. .item-name { flex: 1; font-size: .95rem; }
  288. /* Share box */
  289. .share-box {
  290. background: var(--surface);
  291. border: 1.5px solid var(--border);
  292. border-radius: var(--radius);
  293. padding: 1.25rem 1.5rem;
  294. margin-bottom: 2.5rem;
  295. display: flex;
  296. gap: .75rem;
  297. align-items: center;
  298. flex-wrap: wrap;
  299. }
  300. .share-url {
  301. flex: 1;
  302. font-size: .8rem;
  303. color: var(--muted);
  304. font-family: monospace;
  305. white-space: nowrap;
  306. overflow: hidden;
  307. text-overflow: ellipsis;
  308. min-width: 0;
  309. }
  310. .copy-feedback {
  311. font-size: .8rem;
  312. color: var(--green);
  313. opacity: 0;
  314. transition: opacity .3s;
  315. }
  316. .copy-feedback.show { opacity: 1; }
  317. @media print {
  318. header .btn-row, .share-box, .progress-bar-wrap { display: none !important; }
  319. body { background: #fff; color: #000; }
  320. .item-row { border-color: #ccc; background: none; }
  321. .section-title { color: #333; }
  322. .pill { background: #f5f5f5; color: #333; border-color: #ccc; }
  323. }
  324. </style>
  325. </head>
  326. <body>
  327. <header>
  328. <div class="header-left">
  329. <div class="logo">PackIt</div>
  330. <div class="header-meta">
  331. <strong><?= $totalItems ?></strong> items &nbsp;·&nbsp;
  332. <strong><?= count($activityNames) ?></strong> activities
  333. </div>
  334. </div>
  335. <div class="btn-row">
  336. <a href="index.php" class="btn btn-outline">← New list</a>
  337. <button class="btn btn-teal" onclick="window.print()">🖨 Print</button>
  338. <button class="btn btn-accent" onclick="copyLink()">🔗 Copy link</button>
  339. </div>
  340. </header>
  341. <main>
  342. <!-- Activity pills -->
  343. <div class="activity-pills">
  344. <?php foreach ($activityNames as $n): ?>
  345. <span class="pill"><?= htmlspecialchars($n) ?></span>
  346. <?php endforeach; ?>
  347. </div>
  348. <!-- Share box -->
  349. <div class="share-box">
  350. <span class="share-url" id="shareUrl"><?= htmlspecialchars($pageUrl) ?></span>
  351. <span class="copy-feedback" id="copyFb">Copied!</span>
  352. <button class="btn btn-teal" onclick="copyLink()">Copy link</button>
  353. </div>
  354. <!-- Progress -->
  355. <div class="progress-bar-wrap">
  356. <div class="progress-track"><div class="progress-fill" id="progressFill"></div></div>
  357. <div class="progress-label"><strong id="checkedCnt">0</strong> / <?= $totalItems ?> packed</div>
  358. </div>
  359. <!-- Grouped item list -->
  360. <?php foreach ($grouped as $gid => $group): ?>
  361. <div class="item-group">
  362. <div class="section-title"><?= htmlspecialchars($group['name']) ?></div>
  363. <div class="item-list">
  364. <?php foreach ($group['items'] as $item): ?>
  365. <div class="item-row" onclick="toggleItem(this)">
  366. <div class="item-check">
  367. <svg width="10" height="8" viewBox="0 0 10 8" fill="none">
  368. <path d="M1 4l3 3 5-6" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  369. </svg>
  370. </div>
  371. <span class="item-name"><?php if ($item['quantity'] > 1): ?><?= (int)$item['quantity'] ?>&times; <?php endif; ?><?= htmlspecialchars($item['name']) ?></span>
  372. </div>
  373. <?php endforeach; ?>
  374. </div>
  375. </div>
  376. <?php endforeach; ?>
  377. </main>
  378. <script>
  379. const total = <?= $totalItems ?>;
  380. function toggleItem(el) {
  381. el.classList.toggle('checked');
  382. updateProgress();
  383. }
  384. function updateProgress() {
  385. const checked = document.querySelectorAll('.item-row.checked').length;
  386. document.getElementById('checkedCnt').textContent = checked;
  387. document.getElementById('progressFill').style.width = (checked / total * 100) + '%';
  388. }
  389. function copyLink() {
  390. const url = document.getElementById('shareUrl').textContent;
  391. navigator.clipboard.writeText(url).then(() => {
  392. const fb = document.getElementById('copyFb');
  393. fb.classList.add('show');
  394. setTimeout(() => fb.classList.remove('show'), 2000);
  395. }).catch(() => {
  396. // Fallback
  397. const ta = document.createElement('textarea');
  398. ta.value = url;
  399. document.body.appendChild(ta);
  400. ta.select();
  401. document.execCommand('copy');
  402. document.body.removeChild(ta);
  403. });
  404. }
  405. </script>
  406. </body>
  407. </html>