HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/timentessagaantrouwen.nl/app/Http/Controllers/QuestionsController.php
<?php


namespace App\Http\Controllers;


use App\Http\Requests\StoreQuestionsRequest;
use App\Mail\sendUserResponseMail;
use App\UserResponse;
use App\UserResponseExport;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Mail;
use Maatwebsite\Excel\Facades\Excel;

class QuestionsController extends Controller
{

    public function index()
    {

        // If the user has already filled in a response
        if ($userResponse = auth()->user()->response) {

            // And no input old is found, populate the input with found response
            if(sizeof(old()) == 0) {
                session()->flashInput([
                    'present'         => $userResponse->present,
                    'present_persons' => $userResponse->present_persons,
                    'at_ceremony'   => $userResponse->at_ceremony,
                    'stay_sleeping'   => $userResponse->stay_sleeping,
                    'allergies'       => $userResponse->allergies,
                    'has_allergies'   => $userResponse->has_allergies,
                    'street'         => $userResponse->street,
                    'postal'         => $userResponse->postal,
                    'city'         => $userResponse->city,
                    'email'         => $userResponse->email,
                    'form_message'    => $userResponse->form_message,
                ]);

                return view('templates.questions')->with(['filled' => true]);
            }
        }

        return view('templates.questions');
    }

    public function store(StoreQuestionsRequest $request)
    {

        $responseData = $request->except('_token');
        if (isset($responseData['allergies']) && isset($responseData['has_allergies']) && $responseData['allergies'] == 0) Arr::forget($responseData, 'has_allergies');
        if (isset($responseData['present']) && isset($responseData['present_persons']) && $responseData['present'] == 0) Arr::forget($responseData, 'present_persons');

        // If has response update it
        if (auth()->user()->response) {
            auth()->user()->response->update($responseData);
        } else {
            // else create response
            $response = new UserResponse($responseData);
            auth()->user()->response()->save($response);
        }

        return redirect()->route('questions.success');
    }

    public function success()
    {
        return view('templates.success');
    }

    public function sendResponses()
    {

        $excelString = Carbon::now()->format('Y-m-d_h-i');
        $excelString .= '_user-response.xlsx';

        Excel::store(new UserResponseExport, $excelString);

        Mail::send(new sendUserResponseMail($excelString));
    }

}