2016-01-26 23:20:08 -05:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
2017-09-26 16:05:01 -04:00
|
|
|
use Log;
|
2018-04-26 20:26:18 -04:00
|
|
|
use Mail;
|
|
|
|
use Newsletter;
|
|
|
|
use App\Models\Blog;
|
|
|
|
use App\Models\Contact;
|
2016-01-26 23:20:08 -05:00
|
|
|
use App\Models\Subscriptions;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2018-04-26 20:26:18 -04:00
|
|
|
class ApiController extends Controller {
|
|
|
|
|
|
|
|
public function getBlogEntries()
|
|
|
|
{
|
|
|
|
return Blog::getBlogEntries();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postContactSubmit(Request $request)
|
|
|
|
{
|
2024-03-19 17:11:58 -04:00
|
|
|
$request->validate([
|
2018-04-26 20:26:18 -04:00
|
|
|
'name' => 'required',
|
|
|
|
'email' => 'required|email',
|
|
|
|
'message' => 'required'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$contact = new Contact;
|
|
|
|
$contact->name = $request['name'];
|
|
|
|
$contact->email = $request['email'];
|
|
|
|
$contact->message = $request['message'];
|
|
|
|
$contact->save();
|
|
|
|
|
2020-02-25 15:41:15 -05:00
|
|
|
// Send the email if the MAIL_TO_ADDRESS variable is set
|
|
|
|
if (env('MAIL_TO_ADDRESS') != null) {
|
2018-04-26 20:26:18 -04:00
|
|
|
Mail::send('email.contact', [ 'contact' => $contact ], function($mail) use ($contact) {
|
2020-02-25 15:41:15 -05:00
|
|
|
$mail->to(env('MAIL_TO_ADDRESS'))
|
2018-04-26 20:26:18 -04:00
|
|
|
->subject('Contact form submission');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'success';
|
|
|
|
}
|
2016-08-02 22:58:40 -04:00
|
|
|
|
2016-01-26 23:20:08 -05:00
|
|
|
public function postSubscriptionSubmit(Request $request)
|
|
|
|
{
|
2024-03-19 17:11:58 -04:00
|
|
|
$request->validate([
|
2016-01-26 23:20:08 -05:00
|
|
|
'name' => 'required',
|
2017-09-26 15:35:30 -04:00
|
|
|
'email' => 'required|email'
|
2016-01-26 23:20:08 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
$name = $request['name'];
|
|
|
|
$fname = preg_replace('/ .*$/', '', $name);
|
2018-04-17 19:26:15 -04:00
|
|
|
$lname = preg_match('/. ./', $name) == 1 ? preg_replace('/^[^ ][^ ]* /', '', $name) : '';
|
2016-01-26 23:20:08 -05:00
|
|
|
$email = $request['email'];
|
|
|
|
|
2017-09-26 16:02:53 -04:00
|
|
|
if (env('MAILCHIMP_APIKEY') != null && env('MAILCHIMP_LISTID') != null) {
|
2017-09-26 15:03:23 -04:00
|
|
|
// Submit the subscription request
|
2017-09-26 15:59:57 -04:00
|
|
|
Newsletter::subscribeOrUpdate($email, [
|
|
|
|
'FNAME' => $fname,
|
|
|
|
'LNAME' => $lname
|
2017-09-26 15:03:23 -04:00
|
|
|
]);
|
2017-09-26 15:59:57 -04:00
|
|
|
|
|
|
|
if (!Newsletter::lastActionSucceeded()) {
|
|
|
|
Log::info('Mail Chimp Error: ' . Newsletter::getLastError());
|
|
|
|
}
|
2017-09-26 15:03:23 -04:00
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
|
|
|
|
// Save to the database on success
|
|
|
|
$subscriptions = new Subscriptions;
|
|
|
|
$subscriptions->name = $name;
|
|
|
|
$subscriptions->email = $email;
|
|
|
|
$subscriptions->save();
|
|
|
|
|
|
|
|
return 'success';
|
|
|
|
}
|
2016-08-02 22:58:40 -04:00
|
|
|
|
2016-01-26 23:20:08 -05:00
|
|
|
}
|