|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +require_once __DIR__ . '/src/csrf.php'; |
| 5 | +require_once __DIR__ . '/src/assets.php'; |
| 6 | +require_once __DIR__ . '/src/migrate.php'; |
| 7 | +require_once __DIR__ . '/src/places.php'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Places worth driving to. |
| 11 | + * |
| 12 | + * A page rather than a tab on the dashboard, for the same reason Website |
| 13 | + * content is one: it is edited occasionally and at length, not glanced at |
| 14 | + * while running the day. The dashboard is for today's bookings. |
| 15 | + */ |
| 16 | + |
| 17 | +$me = require_login(); |
| 18 | + |
| 19 | +$migrationError = migrate_if_needed(); |
| 20 | + |
| 21 | +header('X-Frame-Options: DENY'); |
| 22 | +header('X-Content-Type-Options: nosniff'); |
| 23 | +header('Referrer-Policy: same-origin'); |
| 24 | + |
| 25 | +$notice = null; |
| 26 | +$error = $migrationError === null ? null |
| 27 | + : 'The database could not be brought up to date: ' . $migrationError; |
| 28 | + |
| 29 | +if ($_SERVER['REQUEST_METHOD'] === 'POST') { |
| 30 | + require_csrf(); |
| 31 | + |
| 32 | + $action = (string) ($_POST['action'] ?? ''); |
| 33 | + |
| 34 | + if ($action === 'delete') { |
| 35 | + place_delete((int) ($_POST['id'] ?? 0), (int) $me['id']); |
| 36 | + $notice = 'Removed.'; |
| 37 | + } elseif ($action === 'save') { |
| 38 | + $problem = place_save($_POST, $_FILES['photo'] ?? null, (int) $me['id']); |
| 39 | + if ($problem !== null) { |
| 40 | + $error = $problem; |
| 41 | + } else { |
| 42 | + $notice = 'Saved. It appears on the site within a minute.'; |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +$places = places_all(true); |
| 48 | +$editing = null; |
| 49 | +if (isset($_GET['edit'])) { |
| 50 | + foreach ($places as $row) { |
| 51 | + if ((int) $row['id'] === (int) $_GET['edit']) { |
| 52 | + $editing = $row; |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +?> |
| 57 | +<!DOCTYPE html> |
| 58 | +<html lang="en"> |
| 59 | +<head> |
| 60 | + <meta charset="UTF-8"> |
| 61 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 62 | + <title>Places to visit — NiteSha Cars Admin</title> |
| 63 | + <meta name="robots" content="noindex, nofollow"> |
| 64 | + <link rel="stylesheet" href="<?= asset('admin.css') ?>"> |
| 65 | +</head> |
| 66 | +<body> |
| 67 | +<div class="c-wrap"> |
| 68 | + |
| 69 | + <div class="c-top"> |
| 70 | + <div> |
| 71 | + <h1 style="margin:0;font-size:1.3rem">Places to visit</h1> |
| 72 | + <p class="c-note" style="margin:.3rem 0 0"> |
| 73 | + Shown on the home page and on <strong>niteshacars.in/places</strong>. These are |
| 74 | + what people search for before they book a car, so it is worth keeping current. |
| 75 | + </p> |
| 76 | + </div> |
| 77 | + <a class="btn btn-ghost btn-sm" href="dashboard.php">Back to dashboard</a> |
| 78 | + </div> |
| 79 | + |
| 80 | + <?php if ($notice !== null): ?><div class="c-msg c-ok"><?= e($notice) ?></div><?php endif; ?> |
| 81 | + <?php if ($error !== null): ?><div class="c-msg c-bad"><?= e($error) ?></div><?php endif; ?> |
| 82 | + |
| 83 | + <form class="c-sec" method="post" enctype="multipart/form-data"> |
| 84 | + <?= csrf_field() ?> |
| 85 | + <input type="hidden" name="action" value="save"> |
| 86 | + <input type="hidden" name="id" value="<?= e((string) ($editing['id'] ?? '')) ?>"> |
| 87 | + |
| 88 | + <h2><?= $editing === null ? 'Add a place' : 'Edit ' . e((string) $editing['name']) ?></h2> |
| 89 | + |
| 90 | + <div class="modal-row"> |
| 91 | + <div class="field-group"> |
| 92 | + <label for="placeName">Name</label> |
| 93 | + <input type="text" id="placeName" name="name" required maxlength="120" |
| 94 | + value="<?= e((string) ($editing['name'] ?? '')) ?>" |
| 95 | + placeholder="e.g. Vattakottai Fort"> |
| 96 | + </div> |
| 97 | + <div class="field-group"> |
| 98 | + <label for="placeCategory">Group</label> |
| 99 | + <input type="text" id="placeCategory" name="category" maxlength="60" |
| 100 | + value="<?= e((string) ($editing['category'] ?? '')) ?>" |
| 101 | + placeholder="e.g. Temples & heritage"> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + |
| 105 | + <div class="field-group"> |
| 106 | + <label for="placeBlurb">One or two sentences</label> |
| 107 | + <textarea id="placeBlurb" name="blurb" rows="2" maxlength="400" |
| 108 | + placeholder="What someone would want to know before driving there."><?= e((string) ($editing['blurb'] ?? '')) ?></textarea> |
| 109 | + </div> |
| 110 | + |
| 111 | + <div class="field-group"> |
| 112 | + <label for="placeMap">Map link</label> |
| 113 | + <input type="url" id="placeMap" name="map_url" maxlength="500" |
| 114 | + value="<?= e((string) ($editing['map_url'] ?? '')) ?>" |
| 115 | + placeholder="https://www.google.com/maps/..."> |
| 116 | + <p class="field-note">Open the place in Google Maps, press Share, and paste the link. |
| 117 | + Visitors get a Directions button that opens it on their phone.</p> |
| 118 | + </div> |
| 119 | + |
| 120 | + <div class="modal-row"> |
| 121 | + <div class="field-group"> |
| 122 | + <label for="placeOrder">Order</label> |
| 123 | + <input type="number" id="placeOrder" name="sort_order" min="0" max="9999" |
| 124 | + value="<?= e((string) ($editing['sort_order'] ?? 0)) ?>"> |
| 125 | + <p class="field-note">Lowest first.</p> |
| 126 | + </div> |
| 127 | + <div class="field-group"> |
| 128 | + <label for="placePublished">Shown on the site</label> |
| 129 | + <label class="finance-toggle"> |
| 130 | + <input type="checkbox" id="placePublished" name="published" |
| 131 | + <?= ($editing === null || (int) $editing['published'] === 1) ? 'checked' : '' ?>> |
| 132 | + Yes |
| 133 | + </label> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + |
| 137 | + <div class="field-group"> |
| 138 | + <label for="placePhoto">Photograph</label> |
| 139 | + <?php if (!empty($editing['photo_file'])): ?> |
| 140 | + <div class="photo-preview"> |
| 141 | + <img src="<?= e((string) place_photo_url((string) $editing['photo_file'])) ?>" alt=""> |
| 142 | + </div> |
| 143 | + <?php endif; ?> |
| 144 | + <input type="file" id="placePhoto" name="photo" |
| 145 | + accept="image/jpeg,image/png,image/webp,image/avif"> |
| 146 | + <p class="field-note">Landscape works best — it is shown in a wide card. Leave empty |
| 147 | + to keep the picture already there. Up to 6 MB.</p> |
| 148 | + </div> |
| 149 | + |
| 150 | + <div class="modal-actions"> |
| 151 | + <?php if ($editing !== null): ?> |
| 152 | + <a class="btn btn-ghost" href="places.php">Cancel</a> |
| 153 | + <?php endif; ?> |
| 154 | + <button class="btn btn-primary" type="submit"> |
| 155 | + <?= $editing === null ? 'Add place' : 'Save changes' ?> |
| 156 | + </button> |
| 157 | + </div> |
| 158 | + </form> |
| 159 | + |
| 160 | + <div class="c-sec"> |
| 161 | + <h2><?= count($places) ?> place<?= count($places) === 1 ? '' : 's' ?></h2> |
| 162 | + |
| 163 | + <?php if ($places === []): ?> |
| 164 | + <div class="empty-state">Nothing yet. Add the first one above.</div> |
| 165 | + <?php else: ?> |
| 166 | + <div class="place-list"> |
| 167 | + <?php foreach ($places as $row): ?> |
| 168 | + <div class="place-row"> |
| 169 | + <div class="place-thumb"> |
| 170 | + <?php if (!empty($row['photo_file'])): ?> |
| 171 | + <img src="<?= e((string) place_photo_url((string) $row['photo_file'])) ?>" alt="" loading="lazy"> |
| 172 | + <?php else: ?> |
| 173 | + <span class="brand-slot-empty">No photo</span> |
| 174 | + <?php endif; ?> |
| 175 | + </div> |
| 176 | + <div class="place-body"> |
| 177 | + <strong><?= e((string) $row['name']) ?></strong> |
| 178 | + <?php if ((int) $row['published'] !== 1): ?> |
| 179 | + <span class="c-flag">hidden</span> |
| 180 | + <?php endif; ?> |
| 181 | + <p class="c-note" style="margin:.2rem 0 0"><?= e((string) $row['blurb']) ?></p> |
| 182 | + <p class="c-note" style="margin:.2rem 0 0"> |
| 183 | + <?= e((string) $row['category']) ?> · order <?= (int) $row['sort_order'] ?> |
| 184 | + <?= $row['map_url'] !== '' ? ' · has a map link' : ' · no map link' ?> |
| 185 | + </p> |
| 186 | + </div> |
| 187 | + <div class="place-actions"> |
| 188 | + <a class="btn btn-outline btn-sm" href="places.php?edit=<?= (int) $row['id'] ?>">Edit</a> |
| 189 | + <form method="post" onsubmit="return confirm('Remove this place?')"> |
| 190 | + <?= csrf_field() ?> |
| 191 | + <input type="hidden" name="action" value="delete"> |
| 192 | + <input type="hidden" name="id" value="<?= (int) $row['id'] ?>"> |
| 193 | + <button class="btn btn-danger btn-sm" type="submit">Delete</button> |
| 194 | + </form> |
| 195 | + </div> |
| 196 | + </div> |
| 197 | + <?php endforeach; ?> |
| 198 | + </div> |
| 199 | + <?php endif; ?> |
| 200 | + </div> |
| 201 | + |
| 202 | +</div> |
| 203 | +</body> |
| 204 | +</html> |
0 commit comments