hypothetical/app/Http/Controllers/ApiController.php

77 lines
2.1 KiB
PHP
Raw Normal View History

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