ldc-boutique-cnc/app/Http/Controllers/ContactController.php

70 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2021-04-06 12:56:54 +02:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ContactFormRequest;
use App\Mail\SendContactMail;
2021-04-21 17:50:57 +02:00
use App\Models\Magasin;
2021-04-06 12:56:54 +02:00
use App\Models\Setting;
2021-04-22 15:21:09 +02:00
use App\Models\Content;
2021-04-06 12:56:54 +02:00
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
class ContactController extends Controller
{
2022-09-03 16:56:16 +02:00
/**
* legal notice page
*
* @param Request $request
* @return \Illuminate\Contracts\View\View
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
*/
2021-04-06 12:56:54 +02:00
public function contact(Request $request)
{
2022-09-03 16:56:16 +02:00
// @phpcs:enable
2021-04-06 12:56:54 +02:00
$status = $request->session()->get('status');
$magasin = Magasin::first();
$magasins = Magasin::all();
2021-04-22 15:21:09 +02:00
$content = Content::where('key', 'contact')->first();
2021-04-21 17:50:57 +02:00
return view('contact', compact('status', 'magasins', 'magasin', 'content'));
2021-04-06 12:56:54 +02:00
}
2022-09-03 16:56:16 +02:00
/**
* submit contact form
*
* @param ContactFormRequest $request
* @return \Illuminate\Http\Response
2022-11-28 12:08:55 +01:00
* @throws \RuntimeException If email missing.
2022-09-03 16:56:16 +02:00
*/
2021-04-06 12:56:54 +02:00
public function contactSubmit(ContactFormRequest $request)
{
2022-11-28 12:08:55 +01:00
/** @var \App\Model\Magasin|null */
$shop = Magasin::where('name', $request->shop)->first();
2022-11-28 12:08:55 +01:00
if ($shop and $shop->email_contact) {
/** @var string */
$toMail = $shop->email_contact;
2021-04-06 12:56:54 +02:00
} else {
2022-11-28 12:08:55 +01:00
/** @var string|null */
$toMail = \optional(Setting::where('key', 'send_contact_mail_to')->first())->value;
if (!$toMail) {
/** @var string|null */
$toMail = config('mail.from.address');
}
2021-04-06 12:56:54 +02:00
}
2022-11-28 12:08:55 +01:00
if (!$toMail) {
throw new \RuntimeException('Impossible de trouver un mail');
}
2022-09-03 16:56:16 +02:00
// Envoi du mail d'alerte.
Mail::to($toMail)->send(new SendContactMail($request));
2021-04-06 12:56:54 +02:00
return back()->with('status', true);
}
}