beginTransaction(); // Check if user already exists $sql = "SELECT id FROM users WHERE email = ?"; $stmt = $pdo->prepare($sql); $stmt->execute([$email]); if ($stmt->fetch()) { throw new Exception('Email already exists.'); } // Insert into users table $password_hash = password_hash($password, PASSWORD_BCRYPT); $sql = "INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, 'driver')"; $stmt = $pdo->prepare($sql); $stmt->execute([$full_name, $email, $password_hash]); $user_id = $pdo->lastInsertId(); // Insert into drivers table $sql = "INSERT INTO drivers (user_id, full_name, phone_number, vehicle_details, approval_status) VALUES (?, ?, ?, ?, 'pending')"; $stmt = $pdo->prepare($sql); $stmt->execute([$user_id, $full_name, $phone_number, $vehicle_details]); // Handle file uploads if (isset($_FILES['documents'])) { foreach ($_FILES['documents']['tmp_name'] as $doc_id => $tmp_path) { if (!empty($tmp_path) && is_uploaded_file($tmp_path)) { $file_name = $_FILES['documents']['name'][$doc_id]; $file_error = $_FILES['documents']['error'][$doc_id]; if ($file_error !== UPLOAD_ERR_OK) { throw new Exception("Failed to upload file: " . $file_name); } $extension = pathinfo($file_name, PATHINFO_EXTENSION); $key = "documents/drivers/{$user_id}/{$doc_id}_" . time() . "." . $extension; $s3_url = S3Service::uploadFile($tmp_path, $key); if ($s3_url) { $sql = "INSERT INTO user_documents (user_id, document_id, file_path) VALUES (?, ?, ?)"; $stmt = $pdo->prepare($sql); $stmt->execute([$user_id, $doc_id, $s3_url]); } else { throw new Exception("Failed to upload document to S3."); } } } } // Send email notification to admins require_once __DIR__ . '/mail/MailService.php'; $stmt_emails = $pdo->prepare("SELECT email FROM email_recipients WHERE form_type = ?"); $stmt_emails->execute(['driver_signup']); $recipients = $stmt_emails->fetchAll(PDO::FETCH_COLUMN); if (!empty($recipients)) { $subject = 'New Driver Signup: ' . $full_name; $html_content = "

A new driver has signed up and is awaiting approval.

Name: {$full_name}

Email: {$email}

Please visit the admin panel to review and approve the application.

"; $text_content = "A new driver has signed up and is awaiting approval.\nName: {$full_name}\nEmail: {$email}\nPlease visit the admin panel to review and approve the application."; MailService::sendMail($recipients, $subject, $html_content, $text_content); } $pdo->commit(); header("Location: driver_pending_approval.php"); exit; } catch (Exception $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } die("Error: " . $e->getMessage()); } } ?>