2024-07-15 06:24:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
2024-07-24 19:41:19 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2024-07-15 06:24:05 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\Form;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Question;
|
|
|
|
use App\Models\Response;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-07-16 11:44:26 +00:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2024-07-19 11:08:05 +00:00
|
|
|
use Illuminate\Validation\Rules\Unique;
|
2024-07-18 11:43:20 +00:00
|
|
|
|
2024-07-15 06:24:05 +00:00
|
|
|
class FormController extends Controller
|
|
|
|
{
|
|
|
|
public function index()
|
|
|
|
{
|
2024-07-19 11:08:05 +00:00
|
|
|
$totalForms = Form::count();
|
|
|
|
$publishedForms = Form::where('is_published', true)->count();
|
|
|
|
$totalResponses = Response::count();
|
|
|
|
|
2024-07-23 19:59:23 +00:00
|
|
|
$forms = Form::where('user_id', Auth::id())->orderBy('created_at', 'desc')->get();
|
2024-07-19 11:08:05 +00:00
|
|
|
return view('forms.index', [
|
|
|
|
'forms' => $forms,
|
|
|
|
'totalForms' => $totalForms,
|
|
|
|
'publishedForms' => $publishedForms,
|
|
|
|
'totalResponses' => $totalResponses,
|
|
|
|
]);
|
2024-07-15 06:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return view('forms.create');
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:12:07 +00:00
|
|
|
public function togglePublish(Form $form)
|
|
|
|
{
|
|
|
|
$form->is_published = !$form->is_published;
|
|
|
|
$form->save();
|
|
|
|
|
|
|
|
return redirect()->route('forms.show', $form->id)->with('success', 'Form publish status updated.');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-15 06:24:05 +00:00
|
|
|
public function edit(Form $form)
|
2024-07-18 11:43:20 +00:00
|
|
|
{
|
|
|
|
$questions = $form->questions;
|
|
|
|
foreach ($questions as $question) {
|
|
|
|
$question->options = json_decode($question->options, true);
|
|
|
|
}
|
2024-07-15 06:24:05 +00:00
|
|
|
|
2024-07-18 11:43:20 +00:00
|
|
|
return view('forms.edit', compact('form', 'questions'));
|
|
|
|
}
|
2024-07-15 06:24:05 +00:00
|
|
|
|
|
|
|
|
2024-07-23 19:59:23 +00:00
|
|
|
public function createWithTemplate($template)
|
2024-07-24 05:41:55 +00:00
|
|
|
{
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
switch ($template) {
|
|
|
|
case 'contact':
|
|
|
|
$data = [
|
|
|
|
'title' => 'Contact Information',
|
|
|
|
'description' => 'Template for collecting contact information.',
|
|
|
|
'questions' => [
|
|
|
|
['type' => 'text', 'question_text' => 'Name'],
|
|
|
|
['type' => 'text', 'question_text' => 'Email'],
|
|
|
|
// Add more questions as needed
|
|
|
|
],
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'rsvp':
|
|
|
|
$data = [
|
|
|
|
'title' => 'RSVP',
|
|
|
|
'description' => 'Event Address: 123 Your Street Your City, ST 12345
|
2024-07-23 19:59:23 +00:00
|
|
|
Contact us at (123) 456-7890 or no_reply@example.com
|
|
|
|
',
|
2024-07-24 05:41:55 +00:00
|
|
|
'questions' => [
|
|
|
|
['type' => 'text', 'question_text' => 'Can you attend?'],
|
|
|
|
['type' => 'text', 'question_text' => 'Number of Guests'],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'party':
|
|
|
|
$data = [
|
|
|
|
'title' => 'Party Invite',
|
|
|
|
'description' => 'Template for party invitations.',
|
|
|
|
'questions' => [
|
|
|
|
['type' => 'text', 'question_text' => 'Name'],
|
|
|
|
['type' => 'text', 'question_text' => 'RSVP Status'],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
2024-07-23 19:59:23 +00:00
|
|
|
|
2024-07-24 05:41:55 +00:00
|
|
|
return view('forms.create', ['data' => $data]);
|
|
|
|
}
|
2024-07-23 19:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-15 06:24:05 +00:00
|
|
|
|
|
|
|
public function store(Request $request)
|
2024-07-24 05:41:55 +00:00
|
|
|
{
|
2024-07-24 21:16:46 +00:00
|
|
|
$validatedData = $request->validate([
|
|
|
|
'title' => 'required|string|max:255',
|
|
|
|
'description' => 'nullable|string',
|
|
|
|
'questions' => 'required|array',
|
|
|
|
'questions.*.type' => 'required|string|in:multiple_choice,checkbox,dropdown,text',
|
|
|
|
'questions.*.text' => 'required|string',
|
|
|
|
'questions.*.options' => 'nullable|array',
|
|
|
|
'questions.*.required' => 'boolean',
|
|
|
|
]);
|
2024-07-24 05:41:55 +00:00
|
|
|
|
2024-07-24 21:16:46 +00:00
|
|
|
DB::beginTransaction();
|
2024-07-24 05:41:55 +00:00
|
|
|
|
2024-07-24 21:16:46 +00:00
|
|
|
try {
|
|
|
|
$form = Form::create([
|
|
|
|
'title' => $validatedData['title'],
|
|
|
|
'description' => $validatedData['description'],
|
|
|
|
'user_id' => Auth::id(),
|
|
|
|
]);
|
2024-07-15 06:24:05 +00:00
|
|
|
|
2024-07-24 05:41:55 +00:00
|
|
|
foreach ($validatedData['questions'] as $questionData) {
|
2024-07-24 21:16:46 +00:00
|
|
|
$question = new Question([
|
|
|
|
'form_id' => $form->id,
|
|
|
|
'type' => $questionData['type'],
|
|
|
|
'question_text' => $questionData['text'],
|
|
|
|
'options' => json_encode($questionData['options'] ?? []),
|
|
|
|
'required' => $questionData['required'],
|
|
|
|
]);
|
2024-07-24 05:41:55 +00:00
|
|
|
$question->save();
|
|
|
|
}
|
2024-07-18 21:12:07 +00:00
|
|
|
|
2024-07-24 21:16:46 +00:00
|
|
|
DB::commit();
|
|
|
|
return response()->json(['success' => true, 'message' => 'Form saved successfully.']);
|
2024-07-24 05:41:55 +00:00
|
|
|
} catch (\Exception $e) {
|
2024-07-24 21:16:46 +00:00
|
|
|
DB::rollBack();
|
|
|
|
Log::error('Error saving form: ' . $e->getMessage());
|
|
|
|
return response()->json(['success' => false, 'message' => 'Failed to save form.']);
|
2024-07-24 05:41:55 +00:00
|
|
|
}
|
2024-07-15 06:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function show(Form $form)
|
|
|
|
{
|
|
|
|
$form->load('questions.responses');
|
|
|
|
return view('forms.show', compact('form'));
|
|
|
|
}
|
|
|
|
|
2024-07-16 11:44:26 +00:00
|
|
|
public function preview($id)
|
2024-07-18 11:43:20 +00:00
|
|
|
{
|
|
|
|
$form = Form::findOrFail($id);
|
|
|
|
return view('forms.previewForm', compact('form'));
|
|
|
|
}
|
2024-07-16 11:44:26 +00:00
|
|
|
|
2024-07-15 06:24:05 +00:00
|
|
|
|
|
|
|
public function update(Request $request, Form $form)
|
2024-07-24 19:41:19 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
// Normalize the 'required' field to boolean
|
|
|
|
if ($request->has('questions')) {
|
|
|
|
$questions = $request->input('questions');
|
|
|
|
foreach ($questions as $index => $question) {
|
|
|
|
if (isset($question['required']) && $question['required'] === 'on') {
|
|
|
|
$questions[$index]['required'] = true;
|
|
|
|
} else {
|
|
|
|
$questions[$index]['required'] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$request->merge(['questions' => $questions]);
|
2024-07-15 06:24:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
// Validate the request
|
2024-07-15 06:24:05 +00:00
|
|
|
$validatedData = $request->validate([
|
|
|
|
'title' => 'required|string|max:255',
|
|
|
|
'description' => 'nullable|string|max:255',
|
|
|
|
'questions' => 'required|array',
|
|
|
|
'questions.*.id' => 'nullable|exists:questions,id',
|
2024-07-18 21:12:07 +00:00
|
|
|
'questions.*.type' => 'required|string|in:multiple_choice,checkbox,dropdown,text',
|
2024-07-15 06:24:05 +00:00
|
|
|
'questions.*.text' => 'required|string|max:255',
|
|
|
|
'questions.*.options' => 'nullable|array',
|
|
|
|
'questions.*.options.*' => 'nullable|string|max:255',
|
2024-07-24 19:41:19 +00:00
|
|
|
'questions.*.required' => 'boolean',
|
2024-07-15 06:24:05 +00:00
|
|
|
]);
|
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
// Update form
|
2024-07-15 06:24:05 +00:00
|
|
|
$form->update([
|
|
|
|
'title' => $validatedData['title'],
|
|
|
|
'description' => $validatedData['description'],
|
|
|
|
]);
|
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
// Clear existing questions
|
|
|
|
$form->questions()->delete();
|
2024-07-15 06:24:05 +00:00
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
// Create or update questions
|
|
|
|
foreach ($validatedData['questions'] as $questionData) {
|
|
|
|
$question = new Question([
|
|
|
|
'form_id' => $form->id,
|
|
|
|
'type' => $questionData['type'],
|
|
|
|
'question_text' => $questionData['text'],
|
|
|
|
'options' => json_encode($questionData['options'] ?? []),
|
|
|
|
'required' => $questionData['required'],
|
|
|
|
]);
|
2024-07-15 06:24:05 +00:00
|
|
|
$question->save();
|
|
|
|
}
|
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
DB::commit();
|
2024-07-15 06:24:05 +00:00
|
|
|
return redirect()->route('forms.show', $form)->with('success', 'Form updated successfully.');
|
2024-07-24 19:41:19 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
Log::error('Error updating form: ' . $e->getMessage());
|
|
|
|
return back()->withErrors(['error' => 'An error occurred while updating the form. Please try again.'])->withInput();
|
2024-07-15 06:24:05 +00:00
|
|
|
}
|
2024-07-24 19:41:19 +00:00
|
|
|
}
|
2024-07-15 06:24:05 +00:00
|
|
|
|
2024-07-18 21:12:07 +00:00
|
|
|
|
|
|
|
|
2024-07-15 06:24:05 +00:00
|
|
|
public function destroy(Form $form)
|
|
|
|
{
|
|
|
|
// This will also delete all related questions and responses due to foreign key constraints
|
|
|
|
$form->delete();
|
|
|
|
|
2024-07-16 11:44:26 +00:00
|
|
|
return redirect()->route('forms.index')->with('delete', 'Form deleted successfully.');
|
2024-07-15 06:24:05 +00:00
|
|
|
}
|
|
|
|
}
|