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/anvil.komma.pro/app/KommaApp/Forms/ContactController.php
<?php
/**
 * Created by PhpStorm.
 * User: mike
 * Date: 26/09/17
 * Time: 15:24
 */

namespace App\KommaApp\Forms;


use App\Http\Controllers\Controller;
use App\Http\Requests\ContactRequest;
use App\KommaApp\Pages\PageController;
use App\Mail\ContactMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redirect;

class ContactController extends Controller
{
    /**
     * @var FormService
     */
    private $formService;

    /**
     * ContactController constructor.
     * @param FormService $formService
     */
    public function __construct(FormService $formService)
    {
        parent::__construct();

        $this->formService = $formService;
        $this->formService->setOrigin('contact');
    }

    /**
     * Store the request and send it by e-mail
     * Note: Validation is done in the request itself
     *
     * @param ContactRequest $request
     */
    public function process(ContactRequest $request)
    {
        // Store request in the database
        $this->formService->storeRequest($request);

        // Remove token from request (this creates and array)
        Mail::send(new ContactMail($request->except('_token')));

        // Send to the success route
        return Redirect::route('contact.success');
    }

    public function ajaxProcess(Request $request)
    {

        $validator = \Validator::make($request->all(), [
            'name'        => 'required',
            'email'       => 'required|email|different:name',
            'formMessage' => 'required|different:email',
            'honey'       => 'present|nullable|size:0'
        ]);

        if ($validator->passes() && $request->input('willie') == 'wonka') {

            $storeRequest = $request->duplicate();
            $storeRequest = $storeRequest->except('_token', 'willie');

            // Store request in the database
            $this->formService->storeRequest($storeRequest);

            // Remove token from request (this creates and array)
            Mail::send(new ContactMail($request->except('_token', 'willie')));

            return response()->json([
                'status' => 200,
                'data' => [
                    'redirectUrl' => route('contact.success')
                ],
            ]);
        }

        // Get the normal errors
        $errors = $validator->errors();

        // Add custom secret code validation error
        if($request->input('willie') != 'wonka'){
            $errors->add('secretCode', __('validation.secretCode.error'));
        }

        // Check if validation passes and the secret code isset
        return response()->json([
            'status' => 422,
            'errors' => $validator->errors()
        ]);
    }

    /**
     * Show page where we thank the user
     *
     * @return mixed
     */
    public function success()
    {
        $pageController = new PageController();
        return $pageController->show($this->pageService->getPageByCodeName('contact'))->with('send', true);
    }
}