packing.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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="preconnect" href="https://fonts.googleapis.com">
  80. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  81. <link href="https://fonts.googleapis.com/css2?family=Bebas+Neue&family=DM+Sans:wght@300;400;500;600&display=swap" rel="stylesheet">
  82. <style>
  83. :root {
  84. --bg: #0f1117;
  85. --surface: #181c27;
  86. --border: #2a2f3f;
  87. --accent: #e8c547;
  88. --accent2: #4fd1c5;
  89. --text: #e8eaf0;
  90. --muted: #7a8099;
  91. --green: #5cb85c;
  92. --radius: 10px;
  93. --gap: 1.5rem;
  94. }
  95. *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  96. body {
  97. background: var(--bg);
  98. color: var(--text);
  99. font-family: 'DM Sans', sans-serif;
  100. font-size: 15px;
  101. min-height: 100vh;
  102. display: flex;
  103. flex-direction: column;
  104. }
  105. header {
  106. padding: 2rem var(--gap) 1.5rem;
  107. border-bottom: 1px solid var(--border);
  108. display: flex;
  109. align-items: flex-end;
  110. justify-content: space-between;
  111. gap: 1rem;
  112. flex-wrap: wrap;
  113. position: relative;
  114. overflow: hidden;
  115. }
  116. header::before {
  117. content: '';
  118. position: absolute;
  119. inset: 0;
  120. background: radial-gradient(ellipse 60% 80% at 90% 50%, rgba(79,209,197,.06) 0%, transparent 70%);
  121. pointer-events: none;
  122. }
  123. .header-left { display: flex; align-items: flex-end; gap: 1rem; position: relative; }
  124. .logo {
  125. font-family: 'Bebas Neue', sans-serif;
  126. font-size: clamp(2.2rem, 5vw, 3.5rem);
  127. letter-spacing: .04em;
  128. color: var(--accent);
  129. line-height: 1;
  130. }
  131. .header-meta {
  132. font-size: .8rem;
  133. color: var(--muted);
  134. padding-bottom: .2rem;
  135. }
  136. .header-meta strong { color: var(--accent2); }
  137. .btn-row {
  138. display: flex;
  139. gap: .6rem;
  140. flex-wrap: wrap;
  141. position: relative;
  142. }
  143. .btn {
  144. display: inline-flex; align-items: center; gap: .5rem;
  145. padding: .55rem 1.1rem;
  146. border-radius: var(--radius);
  147. font-family: 'DM Sans', sans-serif;
  148. font-size: .82rem;
  149. font-weight: 500;
  150. letter-spacing: .03em;
  151. cursor: pointer;
  152. border: 1.5px solid transparent;
  153. transition: all .15s;
  154. text-decoration: none;
  155. }
  156. .btn-accent {
  157. background: var(--accent); color: #0f1117; border-color: var(--accent);
  158. }
  159. .btn-accent:hover { background: #f0d260; }
  160. .btn-outline {
  161. background: transparent; color: var(--text); border-color: var(--border);
  162. }
  163. .btn-outline:hover { border-color: var(--muted); }
  164. .btn-teal {
  165. background: transparent; color: var(--accent2); border-color: var(--accent2);
  166. }
  167. .btn-teal:hover { background: rgba(79,209,197,.08); }
  168. main {
  169. flex: 1;
  170. max-width: 760px;
  171. width: 100%;
  172. margin: 0 auto;
  173. padding: 2.5rem var(--gap) 3rem;
  174. }
  175. /* Activities pills */
  176. .activity-pills {
  177. display: flex;
  178. flex-wrap: wrap;
  179. gap: .4rem;
  180. margin-bottom: 2.5rem;
  181. }
  182. .pill {
  183. background: rgba(232,197,71,.1);
  184. border: 1px solid rgba(232,197,71,.3);
  185. color: var(--accent);
  186. font-size: .75rem;
  187. font-weight: 500;
  188. padding: .3rem .75rem;
  189. border-radius: 999px;
  190. letter-spacing: .03em;
  191. }
  192. /* Progress bar */
  193. .progress-bar-wrap {
  194. background: var(--surface);
  195. border: 1px solid var(--border);
  196. border-radius: var(--radius);
  197. padding: 1rem 1.25rem;
  198. margin-bottom: 2.5rem;
  199. display: flex;
  200. align-items: center;
  201. gap: 1.5rem;
  202. }
  203. .progress-track {
  204. flex: 1;
  205. height: 6px;
  206. background: var(--border);
  207. border-radius: 999px;
  208. overflow: hidden;
  209. }
  210. .progress-fill {
  211. height: 100%;
  212. background: linear-gradient(90deg, var(--accent2), var(--accent));
  213. border-radius: 999px;
  214. width: 0%;
  215. transition: width .3s ease;
  216. }
  217. .progress-label {
  218. font-size: .82rem;
  219. color: var(--muted);
  220. white-space: nowrap;
  221. }
  222. .progress-label strong { color: var(--accent); font-variant-numeric: tabular-nums; }
  223. .section-title {
  224. font-family: 'Bebas Neue', sans-serif;
  225. font-size: 1.1rem;
  226. letter-spacing: .12em;
  227. color: var(--accent2);
  228. text-transform: uppercase;
  229. margin-bottom: .75rem;
  230. display: flex;
  231. align-items: center;
  232. gap: .6rem;
  233. }
  234. .section-title::after {
  235. content: '';
  236. flex: 1;
  237. height: 1px;
  238. background: var(--border);
  239. }
  240. .item-group {
  241. margin-bottom: 2rem;
  242. }
  243. .item-list {
  244. display: flex;
  245. flex-direction: column;
  246. gap: .4rem;
  247. }
  248. .item-row {
  249. display: flex;
  250. align-items: center;
  251. gap: .75rem;
  252. padding: .6rem 1rem;
  253. background: var(--surface);
  254. border: 1.5px solid var(--border);
  255. border-radius: var(--radius);
  256. cursor: pointer;
  257. transition: border-color .15s, background .15s;
  258. user-select: none;
  259. }
  260. .item-row:hover { background: #1e2333; }
  261. .item-row.checked {
  262. border-color: rgba(92,184,92,.5);
  263. background: rgba(92,184,92,.05);
  264. }
  265. .item-row.checked .item-name {
  266. text-decoration: line-through;
  267. color: var(--muted);
  268. }
  269. .item-check {
  270. width: 20px; height: 20px;
  271. border: 1.5px solid var(--border);
  272. border-radius: 5px;
  273. flex-shrink: 0;
  274. display: flex; align-items: center; justify-content: center;
  275. background: var(--bg);
  276. transition: all .15s;
  277. }
  278. .item-row.checked .item-check {
  279. background: var(--green);
  280. border-color: var(--green);
  281. }
  282. .item-check svg { display: none; }
  283. .item-row.checked .item-check svg { display: block; }
  284. .item-name { flex: 1; font-size: .95rem; }
  285. /* Share box */
  286. .share-box {
  287. background: var(--surface);
  288. border: 1.5px solid var(--border);
  289. border-radius: var(--radius);
  290. padding: 1.25rem 1.5rem;
  291. margin-bottom: 2.5rem;
  292. display: flex;
  293. gap: .75rem;
  294. align-items: center;
  295. flex-wrap: wrap;
  296. }
  297. .share-url {
  298. flex: 1;
  299. font-size: .8rem;
  300. color: var(--muted);
  301. font-family: monospace;
  302. white-space: nowrap;
  303. overflow: hidden;
  304. text-overflow: ellipsis;
  305. min-width: 0;
  306. }
  307. .copy-feedback {
  308. font-size: .8rem;
  309. color: var(--green);
  310. opacity: 0;
  311. transition: opacity .3s;
  312. }
  313. .copy-feedback.show { opacity: 1; }
  314. @media print {
  315. header .btn-row, .share-box, .progress-bar-wrap { display: none !important; }
  316. body { background: #fff; color: #000; }
  317. .item-row { border-color: #ccc; background: none; }
  318. .section-title { color: #333; }
  319. .pill { background: #f5f5f5; color: #333; border-color: #ccc; }
  320. }
  321. </style>
  322. </head>
  323. <body>
  324. <header>
  325. <div class="header-left">
  326. <div class="logo">PackIt</div>
  327. <div class="header-meta">
  328. <strong><?= $totalItems ?></strong> items &nbsp;·&nbsp;
  329. <strong><?= count($activityNames) ?></strong> activities
  330. </div>
  331. </div>
  332. <div class="btn-row">
  333. <a href="index.php" class="btn btn-outline">← New list</a>
  334. <button class="btn btn-teal" onclick="window.print()">🖨 Print</button>
  335. <button class="btn btn-accent" onclick="copyLink()">🔗 Copy link</button>
  336. </div>
  337. </header>
  338. <main>
  339. <!-- Activity pills -->
  340. <div class="activity-pills">
  341. <?php foreach ($activityNames as $n): ?>
  342. <span class="pill"><?= htmlspecialchars($n) ?></span>
  343. <?php endforeach; ?>
  344. </div>
  345. <!-- Share box -->
  346. <div class="share-box">
  347. <span class="share-url" id="shareUrl"><?= htmlspecialchars($pageUrl) ?></span>
  348. <span class="copy-feedback" id="copyFb">Copied!</span>
  349. <button class="btn btn-teal" onclick="copyLink()">Copy link</button>
  350. </div>
  351. <!-- Progress -->
  352. <div class="progress-bar-wrap">
  353. <div class="progress-track"><div class="progress-fill" id="progressFill"></div></div>
  354. <div class="progress-label"><strong id="checkedCnt">0</strong> / <?= $totalItems ?> packed</div>
  355. </div>
  356. <!-- Grouped item list -->
  357. <?php foreach ($grouped as $gid => $group): ?>
  358. <div class="item-group">
  359. <div class="section-title"><?= htmlspecialchars($group['name']) ?></div>
  360. <div class="item-list">
  361. <?php foreach ($group['items'] as $item): ?>
  362. <div class="item-row" onclick="toggleItem(this)">
  363. <div class="item-check">
  364. <svg width="10" height="8" viewBox="0 0 10 8" fill="none">
  365. <path d="M1 4l3 3 5-6" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  366. </svg>
  367. </div>
  368. <span class="item-name"><?php if ($item['quantity'] > 1): ?><?= (int)$item['quantity'] ?>&times; <?php endif; ?><?= htmlspecialchars($item['name']) ?></span>
  369. </div>
  370. <?php endforeach; ?>
  371. </div>
  372. </div>
  373. <?php endforeach; ?>
  374. </main>
  375. <script>
  376. const total = <?= $totalItems ?>;
  377. function toggleItem(el) {
  378. el.classList.toggle('checked');
  379. updateProgress();
  380. }
  381. function updateProgress() {
  382. const checked = document.querySelectorAll('.item-row.checked').length;
  383. document.getElementById('checkedCnt').textContent = checked;
  384. document.getElementById('progressFill').style.width = (checked / total * 100) + '%';
  385. }
  386. function copyLink() {
  387. const url = document.getElementById('shareUrl').textContent;
  388. navigator.clipboard.writeText(url).then(() => {
  389. const fb = document.getElementById('copyFb');
  390. fb.classList.add('show');
  391. setTimeout(() => fb.classList.remove('show'), 2000);
  392. }).catch(() => {
  393. // Fallback
  394. const ta = document.createElement('textarea');
  395. ta.value = url;
  396. document.body.appendChild(ta);
  397. ta.select();
  398. document.execCommand('copy');
  399. document.body.removeChild(ta);
  400. });
  401. }
  402. </script>
  403. </body>
  404. </html>