File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Forms/QuotationController.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\Http\Requests\QuotationRequest;
use App\Komma\Base\Controller;
use App\Komma\Mailchimp\Mailchimp;
use App\Komma\Mailchimp\Types\ListMember;
use App\Komma\Pages\PageController;
use App\Komma\Teamleader\TeamleaderService;
use App\Mail\ClientQuotationRequestMail;
use App\Mail\QuotationRequestMail;
use Illuminate\Support\Facades\Mail;
final class QuotationController extends Controller
{
/**
* @var FormService
*/
private $formService;
/**
* QuotationController constructor.
* @param FormService $formService
*/
public function __construct(FormService $formService)
{
parent::__construct();
$this->formService = $formService;
$this->formService->setOrigin('quotation');
}
/**
* Store the request and send it by e-mail
* Note: Validation is done in the request itself
*
* @param ContactRequest $request
* @return \Illuminate\Http\JsonResponse
*/
/**
* Store the request and send it by e-mail
* Note: Validation is done in the request itself
*
* @param QuotationRequest $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function process(QuotationRequest $request)
{
// Store request in the database
$this->formService->storeRequest($request);
// Append to news letter if checked ('on')
if ($request->has('subscribe_newsletter') && $request->input('subscribe_newsletter') == 'on') {
/** @var Mailchimp $mailchimp */
$mailchimp = app()->make(Mailchimp::class);
$listMember = new ListMember($request->input('email'));
$listMember->addTag('Offerteformulier');
$listMember->addFirstName($request->input('first_name'));
// Check if we need to merge the name preposition and last name
if (! empty($request->input('name_preposition'))) {
$lastName = $request->input('name_preposition').' '.$request->input('last_name');
} else {
$lastName = $request->input('last_name');
}
$listMember->addLastName($lastName);
$mailchimp->addNewListMember($listMember);
}
// Send data to Teamleader
$teamleaderService = new TeamleaderService();
[$savedToTeamleader, $messageLines] = $teamleaderService->insertQuotationIntoTeamLeader($request);
// Remove token from request (this creates and array)
Mail::send(new QuotationRequestMail($request->except('_token'), $savedToTeamleader, $messageLines));
Mail::send(new ClientQuotationRequestMail($request->except('_token')));
return redirect(localized_route('quotation.success'));
}
/**
* Show page where we thank the user
*
* @return mixed
*/
public function success()
{
$pageController = new PageController();
return $pageController->show($this->links->quotation->node)->with('send', true);
}
}