WebOrbiton
v3.0.0.2

FlatlyPage

174 lines · 7.1 KB
  1. <?php
  2. ​
  3. $publish_enabled = $edit_product['publish_enabled'] ?? false;
  4. $publish_date = $edit_product['publish_date'] ?? '';
  5. $publish_time_format = $edit_product['publish_time_format'] ?? '24h';
  6. $publish_timezone = $edit_product['publish_timezone'] ?? 'Europe/Warsaw';
  7. ​
  8. $publish_time_24h = $edit_product['publish_time'] ?? '';
  9. ​
  10. $publish_hour_12 = '';
  11. $publish_ampm = $edit_product['publish_ampm'] ?? '';
  12. if ($publish_time_24h !== '') {
  13. [$h24, $min] = array_pad(explode(':', $publish_time_24h), 2, '00');
  14. $h24 = (int)$h24;
  15. if ($publish_ampm === '') {
  16. $publish_ampm = $h24 >= 12 ? 'PM' : 'AM';
  17. }
  18. $h12 = $h24 % 12;
  19. if ($h12 === 0) {
  20. $h12 = 12;
  21. }
  22. $publish_hour_12 = sprintf('%02d:%s', $h12, $min);
  23. }
  24. ​
  25. $timezone_list = get_publish_timezone_list();
  26. ?>
  27. <div class="form-group publish-schedule-block" style="border-top: 1px solid var(--border, #e5e7eb); padding-top: 16px; margin-top: 8px;">
  28. <label class="checkbox-label">
  29. <input
  30. type="checkbox"
  31. name="publish_enabled"
  32. id="publish_enabled"
  33. value="1"
  34. onchange="document.getElementById('publishScheduleFields').style.display = this.checked ? 'block' : 'none';"
  35. <?= $publish_enabled ? 'checked' : '' ?>>
  36. <span>Publish later (optional)</span>
  37. </label>
  38. <p class="form-hint">Keep this page hidden from visitors until the date and time you pick. You can still see it here in the dashboard.</p>
  39. ​
  40. <div id="publishScheduleFields" class="publish-schedule-fields-wrapper" style="display: <?= $publish_enabled ? 'block' : 'none' ?>; margin-top: 12px;">
  41. <div class="form-row-3">
  42. <div class="form-group">
  43. <label class="form-label">Publish Date</label>
  44. <input type="date" name="publish_date" class="form-input" value="<?= e($publish_date) ?>">
  45. </div>
  46. ​
  47. <div class="form-group">
  48. <label class="form-label">Publish Time</label>
  49. ​
  50. <input
  51. type="time"
  52. name="publish_time_24"
  53. class="form-input publish-time-24"
  54. value="<?= e($publish_time_24h) ?>"
  55. style="<?= $publish_time_format === '12h' ? 'display:none;' : '' ?>">
  56. ​
  57. <div class="publish-time-12-wrapper" style="display: <?= $publish_time_format === '12h' ? 'flex' : 'none' ?>; gap: 8px;">
  58. <input
  59. type="time"
  60. name="publish_time_12"
  61. class="form-input publish-time-12"
  62. value="<?= e($publish_hour_12) ?>"
  63. style="flex: 1;">
  64. <select name="publish_ampm" class="form-input publish-ampm" style="flex: 0 0 90px;">
  65. <option value="AM" <?= $publish_ampm === 'AM' ? 'selected' : '' ?>>AM</option>
  66. <option value="PM" <?= $publish_ampm === 'PM' ? 'selected' : '' ?>>PM</option>
  67. </select>
  68. </div>
  69. ​
  70. <input type="hidden" name="publish_time" id="publish_time_hidden" value="<?= e($publish_time_24h) ?>">
  71. </div>
  72. ​
  73. <div class="form-group">
  74. <label class="form-label">Time Format</label>
  75. <select name="publish_time_format" class="form-input publish-time-format-select">
  76. <option value="24h" <?= $publish_time_format === '24h' ? 'selected' : '' ?>>24-hour</option>
  77. <option value="12h" <?= $publish_time_format === '12h' ? 'selected' : '' ?>>12-hour (AM/PM)</option>
  78. </select>
  79. </div>
  80. </div>
  81. ​
  82. <div class="form-group">
  83. <label class="form-label">Timezone</label>
  84. <select name="publish_timezone" class="form-input">
  85. <?php foreach ($timezone_list as $tz): ?>
  86. <option value="<?= e($tz) ?>" <?= $publish_timezone === $tz ? 'selected' : '' ?>><?= e($tz) ?></option>
  87. <?php endforeach; ?>
  88. </select>
  89. <p class="form-hint">The publish time follows this timezone, not your visitors' local time.</p>
  90. </div>
  91. </div>
  92. </div>
  93. <script>
  94. (function() {
  95. const scriptEl = document.currentScript;
  96. let container = scriptEl.previousElementSibling;
  97. while (container && !container.classList.contains('publish-schedule-block')) {
  98. container = container.previousElementSibling;
  99. }
  100. if (!container) return;
  101. ​
  102. const wrapper = container.querySelector('.publish-schedule-fields-wrapper');
  103. if (!wrapper) return;
  104. ​
  105. const time24Input = wrapper.querySelector('.publish-time-24');
  106. const time12Wrapper = wrapper.querySelector('.publish-time-12-wrapper');
  107. const time12Input = wrapper.querySelector('.publish-time-12');
  108. const ampmSelect = wrapper.querySelector('.publish-ampm');
  109. const hiddenTime = wrapper.querySelector('#publish_time_hidden');
  110. const formatSelect = wrapper.querySelector('.publish-time-format-select');
  111. ​
  112. function to12h(time24) {
  113. if (!time24) return {
  114. time: '',
  115. ampm: 'AM'
  116. };
  117. const [h, m] = time24.split(':').map(Number);
  118. const ampm = h >= 12 ? 'PM' : 'AM';
  119. let h12 = h % 12;
  120. if (h12 === 0) h12 = 12;
  121. return {
  122. time: String(h12).padStart(2, '0') + ':' + String(m).padStart(2, '0'),
  123. ampm
  124. };
  125. }
  126. ​
  127. function to24h(time12, ampm) {
  128. if (!time12) return '';
  129. const [h, m] = time12.split(':').map(Number);
  130. let h24 = h % 12;
  131. if (ampm === 'PM') h24 += 12;
  132. return String(h24).padStart(2, '0') + ':' + String(m).padStart(2, '0');
  133. }
  134. ​
  135. function syncHidden() {
  136. if (formatSelect.value === '12h') {
  137. hiddenTime.value = to24h(time12Input.value, ampmSelect.value);
  138. } else {
  139. hiddenTime.value = time24Input.value;
  140. }
  141. }
  142. ​
  143. function switchFormat() {
  144. if (formatSelect.value === '12h') {
  145. const converted = to12h(time24Input.value);
  146. if (converted.time) {
  147. time12Input.value = converted.time;
  148. ampmSelect.value = converted.ampm;
  149. }
  150. time24Input.style.display = 'none';
  151. time12Wrapper.style.display = 'flex';
  152. } else {
  153. const time24 = to24h(time12Input.value, ampmSelect.value);
  154. if (time24) {
  155. time24Input.value = time24;
  156. }
  157. time24Input.style.display = '';
  158. time12Wrapper.style.display = 'none';
  159. }
  160. syncHidden();
  161. }
  162. ​
  163. formatSelect.addEventListener('change', switchFormat);
  164. time24Input.addEventListener('input', syncHidden);
  165. time12Input.addEventListener('input', syncHidden);
  166. ampmSelect.addEventListener('change', syncHidden);
  167. ​
  168. syncHidden();
  169. const form = wrapper.closest('form');
  170. if (form) {
  171. form.addEventListener('submit', syncHidden);
  172. }
  173. })();
  174. </script>