create.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // create.php - Form creation handler
  3. $message = '';
  4. $form_created = false;
  5. $output_links = [];
  6. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  7. $title = trim($_POST['title'] ?? 'Unbenanntes Formular');
  8. $description = trim($_POST['description'] ?? '');
  9. $admin_email = trim($_POST['admin_email'] ?? '');
  10. $questions_json = $_POST['questions'] ?? '[]';
  11. $questions = json_decode($questions_json, true);
  12. if (empty($questions) || !is_array($questions)) {
  13. $message = '<div class="alert alert-error">Bitte fügen Sie mindestens eine Frage hinzu.</div>';
  14. } else {
  15. $form_id = uniqid('form_');
  16. $admin_token = bin2hex(random_bytes(16));
  17. $form_data = [
  18. 'id' => $form_id,
  19. 'title' => $title,
  20. 'description' => $description,
  21. 'admin_email' => $admin_email,
  22. 'admin_token' => $admin_token,
  23. 'created_at' => date('c'),
  24. 'questions' => $questions
  25. ];
  26. $forms_dir = __DIR__ . '/data/forms';
  27. if (!is_dir($forms_dir)) {
  28. mkdir($forms_dir, 0755, true);
  29. }
  30. file_put_contents("$forms_dir/$form_id.json", json_encode($form_data, JSON_PRETTY_PRINT));
  31. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  32. $host = $_SERVER['HTTP_HOST'];
  33. $base_url = $protocol . $host . dirname($_SERVER['REQUEST_URI']);
  34. if (substr($base_url, -1) !== '/') {
  35. $base_url .= '/';
  36. }
  37. $answer_link = $base_url . "answer.php?id=" . $form_id;
  38. $admin_link = $base_url . "admin.php?id=" . $form_id . "&token=" . $admin_token;
  39. $output_links = [
  40. 'answer' => $answer_link,
  41. 'admin' => $admin_link
  42. ];
  43. // Send email if provided
  44. if (!empty($admin_email) && filter_var($admin_email, FILTER_VALIDATE_EMAIL)) {
  45. $subject = "Ihr Intranet Formular ist bereit: $title";
  46. $email_body = "Guten Tag,\n\nIhr Formular '$title' wurde erfolgreich erstellt.\n\n";
  47. $email_body .= "Öffentlicher Link (zum Teilen):\n$answer_link\n\n";
  48. $email_body .= "Geheimer Admin-Link (für Antworten):\n$admin_link\n\n";
  49. $email_body .= "Bitte geben Sie den Admin-Link nicht weiter.\n\nVielen Dank.";
  50. $headers = "From: no-reply@" . $host . "\r\n";
  51. @mail($admin_email, $subject, $email_body, $headers);
  52. }
  53. $form_created = true;
  54. }
  55. }
  56. ?>
  57. <!DOCTYPE html>
  58. <html lang="de">
  59. <head>
  60. <meta charset="UTF-8">
  61. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  62. <title>Formular erstellen</title>
  63. <link rel="stylesheet" href="assets/css/style.css">
  64. </head>
  65. <body>
  66. <header class="site-header">
  67. <div class="container header-inner">
  68. <div class="brand">
  69. <a href="index.php" class="brand-title" style="color:white;">Intranet Formulare</a>
  70. </div>
  71. </div>
  72. </header>
  73. <main class="container">
  74. <?php if ($form_created): ?>
  75. <div class="panel">
  76. <h2 class="card-title" style="color: var(--brand-accent)">Formular erfolgreich erstellt!</h2>
  77. <div class="alert alert-success mt-2">
  78. <strong>Öffentlicher Link (um Antworten zu sammeln):</strong><br>
  79. <a href="<?= htmlspecialchars($output_links['answer']) ?>" target="_blank"><?= htmlspecialchars($output_links['answer']) ?></a>
  80. </div>
  81. <div class="alert alert-info mt-2">
  82. <strong>Geheimer Admin-Link (Muss sicher aufbewahrt werden):</strong><br>
  83. <a href="<?= htmlspecialchars($output_links['admin']) ?>" target="_blank"><?= htmlspecialchars($output_links['admin']) ?></a>
  84. </div>
  85. <?php if (!empty($admin_email)): ?>
  86. <p class="mt-2 text-center text-muted">Eine Benachrichtigung mit diesen Links wurde an <?= htmlspecialchars($admin_email) ?> gesendet.</p>
  87. <?php endif; ?>
  88. <div class="mt-3 text-center">
  89. <a href="index.php" class="btn btn-secondary">Zurück zur Startseite</a>
  90. </div>
  91. </div>
  92. <?php else: ?>
  93. <?= $message ?>
  94. <form method="POST" id="form-builder-form">
  95. <div class="panel">
  96. <h2 class="card-title">Formulareinstellungen</h2>
  97. <div class="form-group">
  98. <label for="title">Formulartitel *</label>
  99. <input type="text" id="title" name="title" required placeholder="z.B. Mitarbeiterbefragung">
  100. </div>
  101. <div class="form-group">
  102. <label for="description">Beschreibung (Optional)</label>
  103. <textarea id="description" name="description" rows="3" placeholder="Erklären Sie den Zweck dieses Formulars"></textarea>
  104. </div>
  105. <div class="form-group">
  106. <label for="admin_email">Admin-E-Mail (Optional)</label>
  107. <input type="email" id="admin_email" name="admin_email" placeholder="Wir senden den Admin-Link hierher">
  108. </div>
  109. </div>
  110. <div class="panel">
  111. <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom: 1rem; flex-wrap:wrap; gap: 0.5rem">
  112. <h2 class="card-title">Fragen</h2>
  113. <div>
  114. <button type="button" class="btn btn-small btn-secondary text-center mb-1" onclick="addQuestion('text')">+ Text</button>
  115. <button type="button" class="btn btn-small btn-secondary text-center mb-1" onclick="addQuestion('textarea')">+ Textbereich</button>
  116. <button type="button" class="btn btn-small btn-secondary text-center mb-1" onclick="addQuestion('single_choice')">+ Einzelauswahl</button>
  117. <button type="button" class="btn btn-small btn-secondary text-center mb-1" onclick="addQuestion('multiple_choice')">+ Mehrfachauswahl</button>
  118. <button type="button" class="btn btn-small btn-secondary text-center mb-1" onclick="addQuestion('dropdown')">+ Dropdown</button>
  119. </div>
  120. </div>
  121. <div id="builder-canvas">
  122. <div class="alert alert-info" id="empty-state">Noch keine Fragen hinzugefügt. Verwenden Sie die Schaltflächen oben, um eine Frage hinzuzufügen.</div>
  123. </div>
  124. <input type="hidden" name="questions" id="questions_input" value="[]">
  125. </div>
  126. <div class="panel" style="text-align: right;">
  127. <a href="index.php" class="btn btn-secondary">Abbrechen</a>
  128. <button type="submit" class="btn" style="margin-left: 10px;" onclick="prepareSubmission(event)">Speichern</button>
  129. </div>
  130. </form>
  131. <?php endif; ?>
  132. </main>
  133. <script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
  134. <script src="assets/js/create.js"></script>
  135. </body>
  136. </html>