| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- // create.php - Form creation handler
- $message = '';
- $form_created = false;
- $output_links = [];
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $title = trim($_POST['title'] ?? 'Untitled Form');
- $description = trim($_POST['description'] ?? '');
- $admin_email = trim($_POST['admin_email'] ?? '');
- $questions_json = $_POST['questions'] ?? '[]';
-
- $questions = json_decode($questions_json, true);
-
- if (empty($questions) || !is_array($questions)) {
- $message = '<div class="alert alert-error">Please add at least one question.</div>';
- } else {
- $form_id = uniqid('form_');
- $admin_token = bin2hex(random_bytes(16));
-
- $form_data = [
- 'id' => $form_id,
- 'title' => $title,
- 'description' => $description,
- 'admin_email' => $admin_email,
- 'admin_token' => $admin_token,
- 'created_at' => date('c'),
- 'questions' => $questions
- ];
-
- $forms_dir = __DIR__ . '/data/forms';
- if (!is_dir($forms_dir)) {
- mkdir($forms_dir, 0755, true);
- }
-
- file_put_contents("$forms_dir/$form_id.json", json_encode($form_data, JSON_PRETTY_PRINT));
-
- $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
- $host = $_SERVER['HTTP_HOST'];
- $base_url = $protocol . $host . dirname($_SERVER['REQUEST_URI']);
- if (substr($base_url, -1) !== '/') {
- $base_url .= '/';
- }
-
- $answer_link = $base_url . "answer.php?id=" . $form_id;
- $admin_link = $base_url . "admin.php?id=" . $form_id . "&token=" . $admin_token;
-
- $output_links = [
- 'answer' => $answer_link,
- 'admin' => $admin_link
- ];
- // Send email if provided
- if (!empty($admin_email) && filter_var($admin_email, FILTER_VALIDATE_EMAIL)) {
- $subject = "Your Intranet Form is Ready: $title";
- $email_body = "Hello,\n\nYour form '$title' has been created.\n\n";
- $email_body .= "Public Link for respondents:\n$answer_link\n\n";
- $email_body .= "Secret Admin Link for you to view responses:\n$admin_link\n\n";
- $email_body .= "Do not share the admin link.\n\nThank you.";
- $headers = "From: no-reply@" . $host . "\r\n";
- @mail($admin_email, $subject, $email_body, $headers);
- }
-
- $form_created = true;
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Create Form</title>
- <link rel="stylesheet" href="assets/css/style.css">
- </head>
- <body>
- <header class="site-header">
- <div class="container header-inner">
- <div class="brand">
- <a href="index.php" class="brand-title" style="color:white;">Intranet Forms</a>
- </div>
- </div>
- </header>
- <main class="container">
- <?php if ($form_created): ?>
- <div class="panel">
- <h2 class="card-title" style="color: var(--brand-accent)">Form Created Successfully!</h2>
- <div class="alert alert-success mt-2">
- <strong>Public Answering Link (Share this):</strong><br>
- <a href="<?= htmlspecialchars($output_links['answer']) ?>" target="_blank"><?= htmlspecialchars($output_links['answer']) ?></a>
- </div>
-
- <div class="alert alert-info mt-2">
- <strong>Secret Admin Link (Keep this safe):</strong><br>
- <a href="<?= htmlspecialchars($output_links['admin']) ?>" target="_blank"><?= htmlspecialchars($output_links['admin']) ?></a>
- </div>
-
- <?php if (!empty($admin_email)): ?>
- <p class="mt-2 text-center text-muted">A notification with these links has been sent to <?= htmlspecialchars($admin_email) ?></p>
- <?php endif; ?>
-
- <div class="mt-3 text-center">
- <a href="index.php" class="btn btn-secondary">Return Home</a>
- </div>
- </div>
- <?php else: ?>
-
- <?= $message ?>
-
- <form method="POST" id="form-builder-form">
- <div class="panel">
- <h2 class="card-title">Form Settings</h2>
- <div class="form-group">
- <label for="title">Form Title *</label>
- <input type="text" id="title" name="title" required placeholder="e.g. Employee Satisfaction Survey">
- </div>
- <div class="form-group">
- <label for="description">Description (Optional)</label>
- <textarea id="description" name="description" rows="3" placeholder="Explain the purpose of this form"></textarea>
- </div>
- <div class="form-group">
- <label for="admin_email">Admin Email (Optional)</label>
- <input type="email" id="admin_email" name="admin_email" placeholder="We'll send the admin link here">
- </div>
- </div>
- <div class="panel">
- <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom: 1rem; flex-wrap:wrap; gap: 0.5rem">
- <h2 class="card-title">Questions</h2>
- <div>
- <button type="button" class="btn btn-small btn-secondary text-center mb-1" onclick="addQuestion('text')">+ Text</button>
- <button type="button" class="btn btn-small btn-secondary text-center mb-1" onclick="addQuestion('textarea')">+ Text Area</button>
- <button type="button" class="btn btn-small btn-secondary text-center mb-1" onclick="addQuestion('single_choice')">+ Single Choice</button>
- <button type="button" class="btn btn-small btn-secondary text-center mb-1" onclick="addQuestion('multiple_choice')">+ Multi Choice</button>
- <button type="button" class="btn btn-small btn-secondary text-center mb-1" onclick="addQuestion('dropdown')">+ Dropdown</button>
- </div>
- </div>
-
- <div id="builder-canvas">
- <div class="alert alert-info" id="empty-state">No questions added yet. Use the buttons above to add one.</div>
- </div>
-
- <input type="hidden" name="questions" id="questions_input" value="[]">
- </div>
-
- <div class="panel" style="text-align: right;">
- <a href="index.php" class="btn btn-secondary">Cancel</a>
- <button type="submit" class="btn" style="margin-left: 10px;" onclick="prepareSubmission(event)">Save Form</button>
- </div>
- </form>
- <?php endif; ?>
- </main>
- <script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
- <script src="assets/js/create.js"></script>
- </body>
- </html>
|