Files
alrahma_sunday_school_api/app/Http/Controllers/Api/PageController.php
T
2026-03-05 12:29:37 -05:00

157 lines
5.4 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Api;
use App\Models\ContactUs;
use App\Services\EmailService;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response;
class PageController extends BaseApiController
{
protected ContactUs $contactUs;
protected EmailService $emailService;
public function __construct()
{
parent::__construct();
$this->contactUs = model(ContactUs::class);
$this->emailService = app(EmailService::class);
}
/**
* POST /api/v1/pages/contact
* Submit contact form
*/
public function submitContact()
{
$payload = $this->payloadData();
if (empty($payload)) {
return $this->respondError('Invalid request data', Response::HTTP_BAD_REQUEST);
}
$rules = [
'email' => 'required|email',
'message' => 'required|min_length[10]',
];
$errors = $this->validateRequest($payload, $rules);
if (!empty($errors)) {
return $this->respondValidationError($errors);
}
$email = strtolower(trim((string) ($payload['email'] ?? '')));
$message = trim((string) ($payload['message'] ?? ''));
try {
// Save the message to the database using ContactUs
// Note: ContactUs expects different fields, so we'll use DB directly
// or adapt to the model structure if needed
$contactData = [
'email' => $email,
'message' => $message,
'created_at' => utc_now(),
'updated_at' => utc_now(),
];
// Try using model first, fallback to DB if model structure differs
$saved = false;
try {
// Check if ContactUs supports email/message fields directly
// ContactUs has different fields (sender_id, receiver_id, etc.), so use DB directly
$saved = DB::table('contactus')->insert($contactData);
} catch (\Throwable $e) {
log_message('error', 'Failed to save contact data: ' . $e->getMessage());
return $this->respondError('Failed to save message. Please try again.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
if (!$saved) {
log_message('error', 'Failed to save contact data');
return $this->respondError('Failed to save message. Please try again.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
// Prepare the email content
$recipient = 'alrahma.isgl@gmail.com';
$subject = 'Contact Us Form Submission';
$emailMessage = "You have received a new message from {$email}:\n\n{$message}";
// Send email using EmailService
$emailSent = false;
try {
$this->emailService->setTo($recipient);
$this->emailService->setFrom(
config('mail.from.address', 'no-reply@alrahmaisgl.org'),
config('mail.from.name', 'Alrahma Team')
);
$this->emailService->setSubject($subject);
$this->emailService->setMessage($emailMessage);
$emailSent = $this->emailService->send();
} catch (\Throwable $e) {
log_message('error', 'Failed to send email: ' . $e->getMessage());
}
if (!$emailSent) {
log_message('error', 'Failed to send email.');
// Don't fail the request if email fails, just log it
}
return $this->success([
'email' => $email,
'email_sent' => $emailSent,
], 'Message sent successfully');
} catch (\Throwable $e) {
log_message('error', 'Contact form error: ' . $e->getMessage());
return $this->respondError('An unexpected error occurred. Please try again.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
/**
* GET /api/v1/pages/privacy
* Get privacy policy HTML content
*/
public function privacyPolicy()
{
return $this->renderStaticFile('privacy_policy.html', 'privacy_policy');
}
/**
* GET /api/v1/pages/terms
* Get terms of service HTML content
*/
public function termsOfService()
{
return $this->renderStaticFile('terms_of_service.html', 'terms_of_service');
}
/**
* GET /api/v1/pages/help
* Get help center HTML content
*/
public function helpCenter()
{
return $this->renderStaticFile('help_center.html', 'help_center');
}
protected function renderStaticFile(string $filename, string $type)
{
$path = base_path('public/html/' . $filename);
if (!is_readable($path)) {
return $this->respondError('Failed to retrieve ' . str_replace('_', ' ', $type), Response::HTTP_INTERNAL_SERVER_ERROR);
}
try {
$content = file_get_contents($path);
} catch (\Throwable $e) {
log_message('error', ucfirst($type) . ' load error: ' . $e->getMessage());
return $this->respondError('Failed to retrieve ' . str_replace('_', ' ', $type), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->success([
'content' => $content,
'type' => $type,
], ucfirst(str_replace('_', ' ', $type)) . ' retrieved successfully');
}
}