File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Forms/ContactController.php
<?php
/**
* Created by PhpStorm.
* User: mike
* Date: 26/09/17
* Time: 15:24
*/
namespace App\Komma\Forms;
use App\Http\Requests\ContactRequest;
use App\Komma\Base\Controller;
use App\Komma\Mailchimp\Mailchimp;
use App\Komma\Mailchimp\Types\ListMember;
use App\Komma\Pages\PageController;
use App\Mail\ContactMail;
use Illuminate\Support\Facades\Mail;
final 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
* @return \Illuminate\Http\JsonResponse
*/
public function process(ContactRequest $request)
{
// Check for secret code
if ($request->input('_willie') !== 'wonka') {
return response()->json([
'errors' => [
'_secretCode' => [
__('validation.secretCode'),
],
],
], 422);
}
// Store request in the database
$this->formService->storeRequest($request);
// Append to news letter if checked ('on')
if ($request->has('subscribe_newsletter_2') && $request->input('subscribe_newsletter_2', false) == 'on') {
/** @var Mailchimp $mailchimp */
$mailchimp = app()->make(Mailchimp::class);
$listMember = new ListMember($request->input('email'));
$listMember->addTag('Contactformulier');
$mailchimp->addNewListMember($listMember);
}
$redirectUrl = localized_route('contact.thanks');
if (isset($request['thanks']) && ! empty($request['thanks'])) $redirectUrl = $request['thanks'];
// Remove token from request (this creates and array)
Mail::send(new ContactMail($request->except('_token', '_willie', '_honey', 'thanks')));
// Return json response with
return response()->json([
'redirectUrl' => $redirectUrl,
]);
}
/**
* Show page where we thank the user
*
* @return mixed
*/
public function success()
{
$pageController = new PageController();
return $pageController->show($this->links->contact->node)->with('send', true);
}
}