partials.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Shared HTML fragments for the public site and the admin backoffice.
  4. * Admin pages all sit in admin/ and hardcode '../assets/...'. Public pages sit
  5. * in the document root except the gallery viewer in gallery/, so they prefix
  6. * their links with base() — '' or '../' depending on the page (see bootstrap).
  7. */
  8. declare(strict_types=1);
  9. function public_header(string $title, string $active = '', string $bodyClass = ''): void
  10. {
  11. $site = site_get();
  12. $name = $site['intro_title'] ?: config('site.name', 'Portfolio');
  13. $base = base();
  14. ?><!DOCTYPE html>
  15. <html lang="en">
  16. <head>
  17. <meta charset="utf-8">
  18. <meta name="viewport" content="width=device-width, initial-scale=1">
  19. <title><?= e($title) ?></title>
  20. <link rel="stylesheet" href="<?= e($base) ?>assets/site.css">
  21. </head>
  22. <body class="<?= e($bodyClass) ?>">
  23. <header class="nav" id="nav">
  24. <a class="nav-brand" href="<?= e($base ?: './') ?>"><?= e($name) ?></a>
  25. <nav class="nav-links">
  26. <a href="<?= e($base ?: './') ?>" class="<?= $active === 'home' ? 'active' : '' ?>">Home</a>
  27. <a href="<?= e($base) ?>showreel.php" class="<?= $active === 'showreel' ? 'active' : '' ?>">Showreel</a>
  28. <a href="<?= e($base) ?>contact.php" class="<?= $active === 'contact' ? 'active' : '' ?>">Contact</a>
  29. </nav>
  30. </header>
  31. <?php
  32. }
  33. function public_footer(): void
  34. {
  35. $base = base();
  36. ?><footer class="footer">
  37. <nav class="footer-links">
  38. <a href="<?= e($base) ?>impressum.php">Impressum</a>
  39. <a href="<?= e($base) ?>datenschutz.php">Datenschutz</a>
  40. </nav>
  41. <span>&copy; <?= date('Y') ?> <?= e(site_get()['intro_title'] ?: config('site.name', '')) ?></span>
  42. </footer>
  43. <script src="<?= e($base) ?>assets/site.js"></script>
  44. </body>
  45. </html>
  46. <?php
  47. // Last thing on every public page: nudge the gallery-archive worker along if
  48. // anything is queued. Cheap when there is nothing to do, and it flushes the
  49. // page to the visitor before doing anything slow. See app/archive.php.
  50. archive_kick();
  51. }
  52. function admin_header(string $title, string $active = ''): void
  53. {
  54. ?><!DOCTYPE html>
  55. <html lang="en">
  56. <head>
  57. <meta charset="utf-8">
  58. <meta name="viewport" content="width=device-width, initial-scale=1">
  59. <title><?= e($title) ?> · Admin</title>
  60. <link rel="stylesheet" href="../assets/site.css">
  61. </head>
  62. <body class="admin">
  63. <header class="admin-nav">
  64. <a class="nav-brand" href="index.php">Backoffice</a>
  65. <nav class="nav-links">
  66. <a href="frontpage.php" class="<?= $active === 'frontpage' ? 'active' : '' ?>">Front page</a>
  67. <a href="showreel.php" class="<?= $active === 'showreel' ? 'active' : '' ?>">Showreel</a>
  68. <a href="galleries.php" class="<?= $active === 'galleries' ? 'active' : '' ?>">Galleries</a>
  69. <a href="contact.php" class="<?= $active === 'contact' ? 'active' : '' ?>">Contact</a>
  70. <a href="settings.php" class="<?= $active === 'settings' ? 'active' : '' ?>">Settings</a>
  71. <a href="../" target="_blank" rel="noopener">View site ↗</a>
  72. <a href="logout.php">Log out</a>
  73. </nav>
  74. </header>
  75. <main class="admin-main">
  76. <?php
  77. }
  78. function admin_footer(): void
  79. {
  80. ?></main>
  81. </body>
  82. </html>
  83. <?php
  84. }
  85. /**
  86. * The "resize uploads to" control, shared by the gallery create and settings
  87. * forms. $current is the gallery's stored cap in pixels, or null for Original.
  88. *
  89. * The select carries pixel values, never names, so the handler
  90. * (parse_max_resolution) never has to map a label back to a number. The custom
  91. * input is revealed by a tiny delegated script emitted once per page —
  92. * assets/admin.js is the uploader and is not loaded on galleries.php.
  93. */
  94. function resolution_field(?int $current = null): void
  95. {
  96. $isPreset = $current !== null && in_array($current, RESOLUTION_PRESETS, true);
  97. $isCustom = $current !== null && !$isPreset;
  98. ?>
  99. <label for="res">Resize uploads to <span style="text-transform:none;letter-spacing:0">(longest edge)</span></label>
  100. <div class="res-field">
  101. <select id="res" name="max_resolution">
  102. <option value="" <?= $current === null ? 'selected' : '' ?>>Original — no resize</option>
  103. <?php foreach (RESOLUTION_PRESETS as $px): ?>
  104. <option value="<?= $px ?>" <?= $current === $px ? 'selected' : '' ?>><?= e(resolution_label($px)) ?></option>
  105. <?php endforeach; ?>
  106. <option value="custom" <?= $isCustom ? 'selected' : '' ?>>Custom…</option>
  107. </select>
  108. <input type="number" name="max_resolution_custom" placeholder="e.g. 3000"
  109. min="<?= RESOLUTION_MIN ?>" max="<?= RESOLUTION_MAX ?>"
  110. value="<?= $isCustom ? (int)$current : '' ?>" <?= $isCustom ? '' : 'hidden' ?>>
  111. </div>
  112. <p class="help">
  113. Images larger than this are downscaled in the browser before uploading,
  114. which also keeps them under the server's upload limit. The re-encode
  115. drops EXIF data (camera, lens, date, location) — choose Original to keep
  116. it. Files the browser cannot read, such as RAW, are always uploaded
  117. untouched.
  118. </p>
  119. <?php
  120. static $scriptDone = false;
  121. if ($scriptDone) {
  122. return;
  123. }
  124. $scriptDone = true;
  125. ?>
  126. <script>
  127. document.addEventListener('change', function (e) {
  128. var select = e.target.closest('.res-field select');
  129. if (!select) return;
  130. var custom = select.parentNode.querySelector('input[name="max_resolution_custom"]');
  131. custom.hidden = select.value !== 'custom';
  132. if (!custom.hidden) custom.focus();
  133. });
  134. </script>
  135. <?php
  136. }
  137. /** One-shot status message helpers (flash messages via session). */
  138. function flash_set(string $msg, string $kind = 'ok'): void
  139. {
  140. session_boot();
  141. $_SESSION['flash'] = ['msg' => $msg, 'kind' => $kind];
  142. }
  143. function flash_render(): void
  144. {
  145. session_boot();
  146. if (!empty($_SESSION['flash'])) {
  147. $f = $_SESSION['flash'];
  148. unset($_SESSION['flash']);
  149. echo '<div class="flash flash-' . e($f['kind']) . '">' . e($f['msg']) . '</div>';
  150. }
  151. }