2016-01-26 23:20:08 -05:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Newsletter;
|
|
|
|
use App\Models\Subscriptions;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2016-08-02 22:58:40 -04:00
|
|
|
class SubscriptionController extends Controller {
|
|
|
|
|
2016-01-26 23:20:08 -05:00
|
|
|
public function postSubscriptionSubmit(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'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);
|
|
|
|
$lname = preg_match('/. ./', $name) === 1 ? preg_replace('/^[^ ][^ ]* /', '', $name) : '';
|
|
|
|
$email = $request['email'];
|
|
|
|
|
2017-09-26 15:03:23 -04:00
|
|
|
if (env('MAILCHIMP_APIKEY', '') != '' && env('MAILCHIMP_LISTID', '') != '') {
|
|
|
|
// Submit the subscription request
|
|
|
|
Newsletter::subscribe($email, [
|
|
|
|
'FNAME' => $fname,
|
2017-09-26 15:35:30 -04:00
|
|
|
'LNAME' => $lname
|
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
|
|
|
}
|