117 lines
4.6 KiB
PHP
117 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Enums\ContactFormType;
|
|
use App\Enums\StaticPageType;
|
|
use App\Exceptions\AbortContactMailer;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\ContactFormRequest;
|
|
use App\Jobs\Mails\SendClaimMail;
|
|
use App\Jobs\Mails\SendNLMail;
|
|
use App\Jobs\Mails\SendPasserelleMail;
|
|
use App\Models\MailContactData;
|
|
use App\Models\StaticPage;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ContactController extends Controller
|
|
{
|
|
/**
|
|
* Contact page.
|
|
*
|
|
* @param \App\Models\StaticPage $staticPageModel
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function contact(StaticPage $staticPageModel): \Illuminate\Contracts\View\View
|
|
{
|
|
$staticPageModel = StaticPage::getCachedStaticPage(StaticPageType::contact);
|
|
return view('fo.pages.contact', ['staticPageModel' => $staticPageModel]);
|
|
}
|
|
|
|
/**
|
|
* Send contact form data.
|
|
*
|
|
* @param \App\Http\Requests\Fo\ContactFormRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function contactSubmit(ContactFormRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string, mixed> $validatedData */
|
|
$validatedData = $request->validated();
|
|
return DB::transaction(function () use ($validatedData) {
|
|
try {
|
|
$joinFilePath = ContactFormType::make($validatedData['type'])->hasFileInput() && !empty($validatedData['joinFile'])
|
|
? $this->storeFile($validatedData['joinFile'])
|
|
: null;
|
|
$data = new MailContactData(
|
|
Arr::except($validatedData, ['g-recaptcha-response', 'joinFile', 'locale', 'crmsubject']),
|
|
$joinFilePath
|
|
);
|
|
|
|
if (ContactFormType::make($validatedData['type'])->equals(ContactFormType::complain)) {
|
|
// Send to Passerelle if it is a complain about a product.
|
|
\dispatch(new SendPasserelleMail($data, $joinFilePath));
|
|
} else if (isset($validatedData['locale']) && ContactFormType::make($validatedData['locale'])->equals(ContactFormType::nl)) {
|
|
// Send to Export Mailing List if it is a complain from NL page.
|
|
app()->setLocale('nl');
|
|
\dispatch(new SendNLMail($data, $joinFilePath));
|
|
} else {
|
|
// Send to the CRM.
|
|
if ($joinFilePath) {
|
|
$relativePath = str_replace(storage_path('app/public') . '/', '', $joinFilePath);
|
|
$binary = Storage::disk('public')->get($relativePath);
|
|
$base64 = base64_encode($binary);
|
|
}
|
|
|
|
/** @var \LDC\LDCCRM\CrmApiV4 */
|
|
$crm = app('crmAPI');
|
|
$crm->setClaim(
|
|
$validatedData['lastname'],
|
|
$validatedData['firstname'],
|
|
$validatedData['email'],
|
|
$validatedData['message'],
|
|
config('crm.brand'),
|
|
$validatedData['crmsubject'],
|
|
$validatedData['address'],
|
|
$validatedData['town'],
|
|
$validatedData['postal'],
|
|
$base64 ?? null,
|
|
);
|
|
|
|
// Send mail.
|
|
\dispatch(new SendClaimMail($data, $joinFilePath));
|
|
}
|
|
|
|
return back()->with('success', trans('contact.message_contact_send'));
|
|
} catch (AbortContactMailer $e) {
|
|
Log::error('Fail to build mail from complain page ', ['exception' => $e]);
|
|
} //end try
|
|
return back()->withInput()->with('error', trans('contact.message_contact_failed'));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Store the file sent in the request if it exists.
|
|
*
|
|
* @param \Illuminate\Http\UploadedFile|null $file
|
|
*
|
|
* @return string|null
|
|
*/
|
|
private function storeFile(\Illuminate\Http\UploadedFile|null $file): string|null
|
|
{
|
|
$joinFilePath = null;
|
|
$joinFile = '';
|
|
if ($file) {
|
|
$uniqIdDirectory = \uniqid();
|
|
$joinFile = Storage::disk('local')->put(
|
|
sprintf('public/tmp/%s', $uniqIdDirectory),
|
|
$file
|
|
);
|
|
$joinFilePath = storage_path('app/' . $joinFile);
|
|
}
|
|
return $joinFilePath;
|
|
}
|
|
}
|