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);
}
}