| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
- // list.php – receives selected activity IDs, creates/reuses a hash, redirects to packing.php
- require_once __DIR__ . '/db.php';
- $pdo = get_pdo();
- // Sanitize input
- $raw = isset($_GET['aID']) && is_array($_GET['aID']) ? $_GET['aID'] : [];
- $ids = [];
- foreach ($raw as $v) {
- $v = (int)$v;
- if ($v > 0) $ids[] = $v;
- }
- $ids = array_unique($ids);
- sort($ids);
- if (empty($ids)) {
- header('Location: index.php?error=no_selection');
- exit;
- }
- // Validate IDs exist in DB
- $placeholders = implode(',', array_fill(0, count($ids), '?'));
- $stmt = $pdo->prepare("SELECT activityID FROM activities WHERE activityID IN ($placeholders)");
- $stmt->execute($ids);
- $validIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
- if (empty($validIds)) {
- header('Location: index.php?error=invalid_selection');
- exit;
- }
- sort($validIds);
- $idsStr = implode(',', $validIds);
- // Generate deterministic hash from the sorted activity IDs
- $hash = hash('sha256', $idsStr);
- // Check if this exact selection already has a hash stored
- $stmt = $pdo->prepare("SELECT hash FROM selection_hashes WHERE activity_ids = ?");
- $stmt->execute([$idsStr]);
- $existing = $stmt->fetchColumn();
- if ($existing) {
- $hash = $existing;
- } else {
- // Store new hash
- $stmt = $pdo->prepare("INSERT INTO selection_hashes (hash, activity_ids) VALUES (?, ?)");
- $stmt->execute([$hash, $idsStr]);
- }
- // Redirect to the shareable packing list URL
- header("Location: packing.php?h=" . urlencode($hash));
- exit;
|