New contact form with friendlycaptcha
This commit is contained in:
74
app/Http/Controllers/ContactController.php
Normal file
74
app/Http/Controllers/ContactController.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\Rule;
|
||||
use App\Mail\ContactFormSubmitted;
|
||||
|
||||
class ContactController extends Controller
|
||||
{
|
||||
public function show()
|
||||
{
|
||||
return view('contact');
|
||||
}
|
||||
|
||||
public function submit(Request $request)
|
||||
{
|
||||
$rules = [
|
||||
'name' => 'required|string|max:255',
|
||||
'email' => 'required|email|max:255',
|
||||
'message' => 'required|string',
|
||||
'image' => 'nullable|image|max:10240',
|
||||
'video' => 'nullable|mimetypes:video/avi,video/mpeg,video/quicktime,video/mp4|max:512000',
|
||||
];
|
||||
|
||||
$debugCaptcha = env('FRIENDLY_CAPTCHA_DEBUG', false)
|
||||
|| (app()->environment('local')
|
||||
&& (!env('FRIENDLY_CAPTCHA_SITEKEY') || !env('FRIENDLY_CAPTCHA_SECRET')));
|
||||
|
||||
if ($debugCaptcha) {
|
||||
$rules['frc-captcha-solution'] = 'nullable';
|
||||
} else {
|
||||
$rules['frc-captcha-solution'] = ['required', Rule::friendlycaptcha()];
|
||||
}
|
||||
|
||||
$request->validate($rules);
|
||||
|
||||
$data = $request->only(['name', 'email', 'message']);
|
||||
$attachments = [];
|
||||
|
||||
// Handle Image
|
||||
if ($request->hasFile('image')) {
|
||||
// Store temporarily to attach
|
||||
$imagePath = $request->file('image')->store('contact_images');
|
||||
$attachments['image'] = storage_path('app/' . $imagePath);
|
||||
}
|
||||
|
||||
// Handle Video
|
||||
if ($request->hasFile('video')) {
|
||||
$videoPath = $request->file('video')->store('contact_videos', 'local');
|
||||
$data['video_link'] = route('contact.video', ['filename' => basename($videoPath)]);
|
||||
}
|
||||
|
||||
// Send Email
|
||||
Mail::to('info@nhgooi.nl')->send(new ContactFormSubmitted($data, $attachments));
|
||||
|
||||
return redirect()->route('contact')->with('success', 'Bedankt voor uw bericht. We nemen zo snel mogelijk contact met u op.');
|
||||
}
|
||||
|
||||
public function video(string $filename)
|
||||
{
|
||||
$path = 'contact_videos/' . basename($filename);
|
||||
|
||||
if (!Storage::disk('local')->exists($path)) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$fullPath = Storage::disk('local')->path($path);
|
||||
|
||||
return response()->file($fullPath);
|
||||
}
|
||||
}
|
||||
@@ -91,7 +91,7 @@ class Controller extends BaseController
|
||||
$this->getDataFromFileAndConvert('laatste_podcasts.json', ['podcasts'], '\Model\Podcast')
|
||||
);
|
||||
});
|
||||
View::composer('widgets.menu', function($view) {
|
||||
View::composer('widgets.menu', function($view) {
|
||||
$view->with('items', json_decode(Storage::get('static/menu.json')));
|
||||
});
|
||||
|
||||
@@ -198,7 +198,7 @@ class Controller extends BaseController
|
||||
if ($page->edited) {
|
||||
$page->edited = Controller::JsonToDateTime($page->edited);
|
||||
}
|
||||
|
||||
|
||||
return view('static', compact('page'));
|
||||
}
|
||||
}
|
||||
|
||||
45
app/Mail/ContactFormSubmitted.php
Normal file
45
app/Mail/ContactFormSubmitted.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class ContactFormSubmitted extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public $data;
|
||||
public $attachments_files;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($data, $attachments_files = [])
|
||||
{
|
||||
$this->data = $data;
|
||||
$this->attachments_files = $attachments_files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the message.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
$email = $this->view('emails.contact_submitted')
|
||||
->subject('Nieuw contactformulier bericht van NH Gooi')
|
||||
->replyTo($this->data['email'], $this->data['name']);
|
||||
|
||||
if (isset($this->attachments_files['image'])) {
|
||||
$email->attach($this->attachments_files['image']);
|
||||
}
|
||||
|
||||
return $email;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Ossycodes\FriendlyCaptcha\Rules\FriendlyCaptcha as FriendlyCaptchaRule;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -14,6 +16,10 @@ class AppServiceProvider extends ServiceProvider
|
||||
public function boot()
|
||||
{
|
||||
\Illuminate\Support\Facades\URL::forceScheme('https');
|
||||
|
||||
Rule::macro('friendlycaptcha', function () {
|
||||
return app(FriendlyCaptchaRule::class);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user