firebase integration

This commit is contained in:
2026-05-22 16:16:34 +02:00
parent bf36c8561c
commit dacc9ce2bc
6 changed files with 204 additions and 3 deletions
+76
View File
@@ -367,6 +367,18 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'delet
.list-item { padding: 12px 0; border-bottom: 1px solid rgba(255,255,255,0.08); display: flex; justify-content: space-between; align-items: center; }
.pill { background: rgba(255,255,255,0.06); color: var(--ink); }
</style>
<!-- Firebase v10 compat SDKs -->
<script src="https://www.gstatic.com/firebasejs/10.8.0/firebase-app-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/10.8.0/firebase-auth-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/10.8.0/firebase-firestore-compat.js"></script>
<script>
(function() {
var config = <?php echo json_encode(get_firebase_config()); ?>;
firebase.initializeApp(config);
window.auth = firebase.auth();
window.db = firebase.firestore();
})();
</script>
</head>
<body>
<div class="admin-header">
@@ -376,6 +388,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'delet
<a href="/admin.php" class="<?php echo $view === 'recipes' ? 'active' : ''; ?>">Recipes</a>
<a href="/admin.php?view=settings" class="<?php echo $view === 'settings' ? 'active' : ''; ?>">Site settings</a>
</div>
<?php if ($view === 'recipes' && !$editing): ?>
<button id="btn-seed-firestore" class="button" style="background: var(--accent-gold, #c5a880); color: #000; font-weight: 700; border: none; padding: 10px 14px; border-radius: 10px; cursor: pointer;">Seed Firestore</button>
<?php endif; ?>
<a class="button" href="/index.php" style="padding:10px 14px;">View site</a>
<?php if ($editing): ?>
<a class="button" href="/admin.php" style="padding:10px 14px;">Back</a>
@@ -638,5 +653,66 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'delet
<button type="submit" class="button" style="width: fit-content;"><?php echo $editing ? 'Save changes' : 'Save recipe'; ?></button>
</form>
<?php endif; ?>
<script>
// Firestore Seeder Logic
document.addEventListener('DOMContentLoaded', () => {
const btnSeed = document.getElementById('btn-seed-firestore');
if (btnSeed) {
btnSeed.addEventListener('click', async () => {
if (!confirm('Bist du sicher, dass du alle Rezepte nach Firestore importieren möchtest? Bestehende Dokumente mit identischem Slug werden überschrieben.')) return;
btnSeed.disabled = true;
btnSeed.innerText = 'Seeding...';
try {
const recipes = <?php echo json_encode($allRecipes); ?>;
let count = 0;
for (const recipe of recipes) {
await window.db.collection('recipes').doc(recipe.slug).set(recipe);
console.log('Seeded to Firestore:', recipe.slug);
count++;
}
alert(`Erfolgreich ${count} Rezepte nach Firestore migriert!`);
} catch (err) {
console.error('Seeding Fehler:', err);
alert('Fehler beim Seeding: ' + err.message);
} finally {
btnSeed.disabled = false;
btnSeed.innerText = 'Seed Firestore';
}
});
}
});
</script>
<!-- Auto-Sync after PHP Save -->
<?php if (isset($record) && str_contains($message ?? '', 'Saved')): ?>
<script>
document.addEventListener('DOMContentLoaded', async () => {
const recipe = <?php echo json_encode($record); ?>;
try {
await window.db.collection('recipes').doc(recipe.slug).set(recipe);
console.log('Successfully synced saved recipe to Firestore:', recipe.slug);
} catch (err) {
console.error('Error syncing saved recipe to Firestore:', err);
}
});
</script>
<?php endif; ?>
<!-- Auto-Sync after PHP Delete -->
<?php if (isset($slugDel) && ($message ?? '') === 'Recipe deleted.'): ?>
<script>
document.addEventListener('DOMContentLoaded', async () => {
const slugDel = <?php echo json_encode($slugDel); ?>;
try {
await window.db.collection('recipes').doc(slugDel).delete();
console.log('Successfully deleted recipe from Firestore:', slugDel);
} catch (err) {
console.error('Error deleting recipe from Firestore:', err);
}
});
</script>
<?php endif; ?>
</body>
</html>