showreel.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Showreel manager: upload, reorder and remove the portfolio images.
  4. * These are stored locally in public/media/ in full resolution.
  5. */
  6. require dirname(__DIR__, 2) . '/app/bootstrap.php';
  7. auth_require();
  8. $site = site_get();
  9. $reel = $site['showreel'] ?? [];
  10. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  11. csrf_verify();
  12. $action = $_POST['action'] ?? '';
  13. if ($action === 'upload') {
  14. $count = 0;
  15. foreach ($_FILES['images']['name'] ?? [] as $i => $name) {
  16. $file = [
  17. 'name' => $name,
  18. 'tmp_name' => $_FILES['images']['tmp_name'][$i],
  19. 'error' => $_FILES['images']['error'][$i],
  20. ];
  21. $stored = media_store_upload($file);
  22. if ($stored !== null) {
  23. $reel[] = $stored;
  24. $count++;
  25. }
  26. }
  27. flash_set($count > 0 ? "$count image(s) added to the showreel." : 'No valid images uploaded.', $count > 0 ? 'ok' : 'error');
  28. }
  29. if ($action === 'delete') {
  30. $name = (string)($_POST['file'] ?? '');
  31. $reel = array_values(array_filter($reel, fn($f) => $f !== $name));
  32. media_delete($name);
  33. flash_set('Image removed.');
  34. }
  35. if ($action === 'move') {
  36. $i = (int)($_POST['index'] ?? -1);
  37. $dir = $_POST['dir'] === 'up' ? -1 : 1;
  38. $j = $i + $dir;
  39. if (isset($reel[$i], $reel[$j])) {
  40. [$reel[$i], $reel[$j]] = [$reel[$j], $reel[$i]];
  41. }
  42. }
  43. $site['showreel'] = array_values($reel);
  44. site_save($site);
  45. redirect('showreel.php');
  46. }
  47. admin_header('Showreel', 'showreel');
  48. flash_render();
  49. ?>
  50. <h1>Showreel</h1>
  51. <form method="post" enctype="multipart/form-data" class="card">
  52. <?= csrf_field() ?>
  53. <input type="hidden" name="action" value="upload">
  54. <label for="imgs">Add images</label>
  55. <input type="file" id="imgs" name="images[]" accept="image/*" multiple>
  56. <p class="help">
  57. Full resolution, unmodified. These upload through the webhost, so add a few
  58. at a time if your hosting has upload size limits.
  59. </p>
  60. <button type="submit">Upload</button>
  61. </form>
  62. <h2>Current order (<?= count($reel) ?>)</h2>
  63. <?php if (!$reel): ?>
  64. <p class="help">The showreel is empty.</p>
  65. <?php else: ?>
  66. <div class="card"><table>
  67. <tr><th></th><th>Image</th><th>File</th><th style="width:170px">Actions</th></tr>
  68. <?php foreach ($reel as $i => $file): ?>
  69. <tr>
  70. <td><?= $i + 1 ?></td>
  71. <td><img src="../media/<?= e(rawurlencode($file)) ?>" alt="" style="width:110px;height:74px;object-fit:cover;border-radius:4px"></td>
  72. <td><?= e($file) ?></td>
  73. <td>
  74. <form method="post" style="display:inline"><?= csrf_field() ?>
  75. <input type="hidden" name="action" value="move">
  76. <input type="hidden" name="index" value="<?= $i ?>">
  77. <input type="hidden" name="dir" value="up">
  78. <button class="btn-ghost" style="margin:0;padding:.3rem .7rem" <?= $i === 0 ? 'disabled' : '' ?>>↑</button>
  79. </form>
  80. <form method="post" style="display:inline"><?= csrf_field() ?>
  81. <input type="hidden" name="action" value="move">
  82. <input type="hidden" name="index" value="<?= $i ?>">
  83. <input type="hidden" name="dir" value="down">
  84. <button class="btn-ghost" style="margin:0;padding:.3rem .7rem" <?= $i === count($reel) - 1 ? 'disabled' : '' ?>>↓</button>
  85. </form>
  86. <form method="post" style="display:inline"
  87. onsubmit="return confirm('Remove this image from the showreel?')"><?= csrf_field() ?>
  88. <input type="hidden" name="action" value="delete">
  89. <input type="hidden" name="file" value="<?= e($file) ?>">
  90. <button class="btn-danger" style="margin:0;padding:.3rem .7rem">✕</button>
  91. </form>
  92. </td>
  93. </tr>
  94. <?php endforeach; ?>
  95. </table></div>
  96. <?php endif; ?>
  97. <?php admin_footer(); ?>